|
| 1 | +using System.Net; |
| 2 | +using AngleSharp; |
| 3 | +using AngleSharp.Dom; |
| 4 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 5 | +using Microsoft.AspNetCore.TestHost; |
| 6 | +using NSubstitute; |
| 7 | +using NSubstitute.ExceptionExtensions; |
| 8 | +using SchoolAccount.Application.Abstractions.Messaging; |
| 9 | +using SchoolAccount.Application.Greetings.GetTimeSpecificHello; |
| 10 | +using SchoolAccount.SharedKernel; |
| 11 | +using Shouldly; |
| 12 | + |
| 13 | +namespace SchoolAccount.IntegrationTests.Controllers.Home; |
| 14 | + |
| 15 | +public class HomeControllerTests : IClassFixture<WebApplicationFactory<Program>> |
| 16 | +{ |
| 17 | + private readonly HttpClient _client; |
| 18 | + private readonly IQueryHandler< |
| 19 | + GetTimeSpecificHelloQuery, |
| 20 | + GetTimeSpecificHelloResponse |
| 21 | + > _getTimeSpecificHelloHandler = Substitute.For< |
| 22 | + IQueryHandler<GetTimeSpecificHelloQuery, GetTimeSpecificHelloResponse> |
| 23 | + >(); |
| 24 | + |
| 25 | + public HomeControllerTests(WebApplicationFactory<Program> factory) |
| 26 | + { |
| 27 | + _client = factory |
| 28 | + .WithWebHostBuilder(builder => |
| 29 | + builder.ConfigureTestServices(services => |
| 30 | + services.AddScoped< |
| 31 | + IQueryHandler<GetTimeSpecificHelloQuery, GetTimeSpecificHelloResponse> |
| 32 | + >(_ => _getTimeSpecificHelloHandler) |
| 33 | + ) |
| 34 | + ) |
| 35 | + .CreateClient(); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task Ensure_that_the_home_controller_returns_a_successful_result() |
| 40 | + { |
| 41 | + // Arrange |
| 42 | + var stubbedGetSpecificHelloResponse = new GetTimeSpecificHelloResponse( |
| 43 | + GetTimeSpecificHelloHandler.Messages.Morning |
| 44 | + ); |
| 45 | + _getTimeSpecificHelloHandler |
| 46 | + .Handle(Arg.Any<GetTimeSpecificHelloQuery>(), Arg.Any<CancellationToken>()) |
| 47 | + .Returns(Result.Success(stubbedGetSpecificHelloResponse)); |
| 48 | + |
| 49 | + // Act |
| 50 | + HttpResponseMessage response = await _client.GetAsync( |
| 51 | + "/", |
| 52 | + TestContext.Current.CancellationToken |
| 53 | + ); |
| 54 | + |
| 55 | + // Assert |
| 56 | + response.IsSuccessStatusCode.ShouldBeTrue(); |
| 57 | + |
| 58 | + string html = await response.Content.ReadAsStringAsync( |
| 59 | + TestContext.Current.CancellationToken |
| 60 | + ); |
| 61 | + IBrowsingContext context = BrowsingContext.New(Configuration.Default); |
| 62 | + IDocument page = await context.OpenAsync( |
| 63 | + req => req.Content(html), |
| 64 | + TestContext.Current.CancellationToken |
| 65 | + ); |
| 66 | + |
| 67 | + page.ShouldNotBeNull(); |
| 68 | + |
| 69 | + IElement? pageTitle = page.QuerySelector("title"); |
| 70 | + pageTitle.ShouldNotBeNull(); |
| 71 | + pageTitle.TextContent.ShouldBeEquivalentTo("Home Page"); |
| 72 | + |
| 73 | + IElement? headingElement = page.QuerySelector("h1.govuk-heading-l"); |
| 74 | + headingElement.ShouldNotBeNull(); |
| 75 | + headingElement.TextContent.ShouldBeEquivalentTo(stubbedGetSpecificHelloResponse.Message); |
| 76 | + |
| 77 | + IElement? bodyElement = page.QuerySelector("p.govuk-body"); |
| 78 | + bodyElement.ShouldNotBeNull(); |
| 79 | + bodyElement.TextContent.ShouldContainWithoutWhitespace("In case I don't see ya"); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task Ensure_that_the_home_controller_returns_a_404_result_on_unknown_page() |
| 84 | + { |
| 85 | + // Act |
| 86 | + HttpResponseMessage response = await _client.GetAsync( |
| 87 | + "/orangesandapples", |
| 88 | + TestContext.Current.CancellationToken |
| 89 | + ); |
| 90 | + |
| 91 | + // Assert |
| 92 | + response.IsSuccessStatusCode.ShouldBeFalse(); |
| 93 | + response.StatusCode.ShouldBe(HttpStatusCode.NotFound); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public async Task Ensure_that_when_the_home_controller_fails_it_returns_a_500_result() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + _getTimeSpecificHelloHandler |
| 101 | + .Handle(Arg.Any<GetTimeSpecificHelloQuery>(), Arg.Any<CancellationToken>()) |
| 102 | + .Throws(new ApplicationException("Bang!")); |
| 103 | + |
| 104 | + // Act |
| 105 | + HttpResponseMessage response = await _client.GetAsync( |
| 106 | + "/", |
| 107 | + TestContext.Current.CancellationToken |
| 108 | + ); |
| 109 | + |
| 110 | + // Assert |
| 111 | + response.IsSuccessStatusCode.ShouldBeFalse(); |
| 112 | + response.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); |
| 113 | + } |
| 114 | +} |
0 commit comments