MvcPagesE2ETests.cs
1,909 bytes
| 1 | using System.Net; |
|---|---|
| 2 | using App.Tests.Integration; |
| 3 | using FluentAssertions; |
| 4 | using Microsoft.AspNetCore.Mvc.Testing; |
| 5 | |
| 6 | namespace App.Tests.E2E; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// End-to-end tests of the MVC client surface — full pipeline (routing → controller → |
| 10 | /// service → repo → SQLite → repo → service → controller → Razor → HTML). |
| 11 | /// Verifies real user-facing pages render and unauthenticated access redirects to login. |
| 12 | /// </summary> |
| 13 | public class MvcPagesE2ETests : IClassFixture<WebApiTestFactory> |
| 14 | { |
| 15 | private readonly HttpClient _client; |
| 16 | |
| 17 | public MvcPagesE2ETests(WebApiTestFactory factory) |
| 18 | { |
| 19 | // Disable auto-redirect — we want to *observe* the 302 to /Identity/Account/Login |
| 20 | _client = factory.CreateClient(new WebApplicationFactoryClientOptions |
| 21 | { |
| 22 | AllowAutoRedirect = false |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | [Fact] |
| 27 | public async Task HomePage_AnonymousUser_RendersHtmlSuccessfully() |
| 28 | { |
| 29 | // Act — anonymous user hits root |
| 30 | var response = await _client.GetAsync("/"); |
| 31 | |
| 32 | // Assert — full Razor pipeline produced an HTML response |
| 33 | response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 34 | response.Content.Headers.ContentType?.MediaType.Should().Be("text/html"); |
| 35 | |
| 36 | var html = await response.Content.ReadAsStringAsync(); |
| 37 | html.Should().Contain("<html"); |
| 38 | html.Should().Contain("</html>"); |
| 39 | } |
| 40 | |
| 41 | [Fact] |
| 42 | public async Task ProtectedMvcRoute_AnonymousUser_RedirectsToLogin() |
| 43 | { |
| 44 | // Act — protected MVC route without auth cookie |
| 45 | var response = await _client.GetAsync("/Trips"); |
| 46 | |
| 47 | // Assert — Identity middleware issues a 302 redirect to the login page |
| 48 | response.StatusCode.Should().Be(HttpStatusCode.Redirect); |
| 49 | response.Headers.Location.Should().NotBeNull(); |
| 50 | response.Headers.Location!.OriginalString.Should().Contain("/Identity/Account/Login"); |
| 51 | } |
| 52 | } |
| 53 | |