diff --git a/server.tests/integrationTests/EpisodesControllerIntegrationTests.cs b/server.tests/integrationTests/EpisodesControllerIntegrationTests.cs index 3a2d5e7..71794d4 100644 --- a/server.tests/integrationTests/EpisodesControllerIntegrationTests.cs +++ b/server.tests/integrationTests/EpisodesControllerIntegrationTests.cs @@ -1,4 +1,7 @@ -using Microsoft.AspNetCore.Mvc.Testing; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc.Testing; +using server.Models; +using System.Net; using System.Text.Json; namespace server.tests.integrationTests @@ -24,5 +27,24 @@ namespace server.tests.integrationTests _endpoint = "/api/episodes"; } + + [Fact] + public async Task GetEpisodesAsync_AllEpisodes_Returns200StatusCodeWithEpisodes() + { + var response = await _client.GetAsync(_endpoint); + + var data = await response.Content.ReadAsStreamAsync(); + + var episodes = JsonSerializer.Deserialize>(data, _serializerOptions); + + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.Headers.Should().ContainKey("X-Rate-Limit-Limit"); + response.Headers.Should().ContainKey("X-Rate-Limit-Remaining"); + response.Headers.Should().ContainKey("X-Rate-Limit-Reset"); + + episodes.Should().NotBeNull(); + episodes.Should().BeOfType>(); + episodes.Should().HaveCountGreaterThan(0); + } } } diff --git a/server.tests/unitTests/EpisodesControllerUnitTests.cs b/server.tests/unitTests/EpisodesControllerUnitTests.cs index 097e104..1a8341d 100644 --- a/server.tests/unitTests/EpisodesControllerUnitTests.cs +++ b/server.tests/unitTests/EpisodesControllerUnitTests.cs @@ -1,6 +1,10 @@ -using Moq; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Moq; using server.Controllers.v1; +using server.Models; using server.Persistence.Repositories; +using System.Net; namespace server.tests.unitTests { @@ -14,5 +18,54 @@ namespace server.tests.unitTests _mockRepo = new Mock(); _controller = new EpisodesController(_mockRepo.Object); } + + [Fact] + public async Task GetEpisodesAsync_AllEpisodes_Returns200StatusCodeWithEpisodesCollection() + { + var filter = new EpisodeFilter(); + + var episodes = new List { new Episode(), new Episode() }; + + _mockRepo + .Setup(repo => repo.GetEpisodesAsync(filter)) + .ReturnsAsync(episodes); + + var response = await _controller.GetEpisodesAsync(filter) as ObjectResult; + + var data = response.Value as List; + + _mockRepo.Verify(repo => repo.GetEpisodesAsync(It.IsAny()), Times.Once()); + + response.Should().NotBeNull(); + response.Should().BeOfType(); + response.StatusCode.Should().Be((int)HttpStatusCode.OK); + + data.Should().NotBeNull(); + data.Should().BeOfType>(); + data.Should().HaveCount(2); + } + + [Fact] + public async Task GetEpisodesAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() + { + var filter = new EpisodeFilter(); + + _mockRepo + .Setup(repo => repo.GetEpisodesAsync(filter)) + .Throws(new Exception()); + + var response = await _controller.GetEpisodesAsync(filter) as ObjectResult; + + var details = response.Value; + + _mockRepo.Verify(repo => repo.GetEpisodesAsync(It.IsAny()), Times.Once()); + + response.Should().NotBeNull(); + response.Should().BeOfType(); + response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); + + details.Should().NotBeNull(); + details.Should().BeOfType(); + } } }