|
| 1 | +using System.Net; |
| 2 | +using Microsoft.AspNetCore.Authentication; |
| 3 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 4 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 5 | +using Microsoft.AspNetCore.TestHost; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.IdentityModel.Protocols.OpenIdConnect; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace LoginAuthCode.Tests; |
| 11 | + |
| 12 | +public class AuthIntegrationTests : IClassFixture<WebApplicationFactory<Program>> |
| 13 | +{ |
| 14 | + private readonly WebApplicationFactory<Program> _factory; |
| 15 | + |
| 16 | + public AuthIntegrationTests(WebApplicationFactory<Program> factory) |
| 17 | + { |
| 18 | + // Env vars needed so the OIDC middleware initializes at app boot time. |
| 19 | + Environment.SetEnvironmentVariable("ISSUER_URL", "https://idp.example"); |
| 20 | + Environment.SetEnvironmentVariable("CLIENT_ID", "test-client"); |
| 21 | + Environment.SetEnvironmentVariable("CLIENT_SECRET", "test-secret"); |
| 22 | + Environment.SetEnvironmentVariable("SCOPES", "openid profile email"); |
| 23 | + Environment.SetEnvironmentVariable("REDIRECT_URI", "https://localhost:4260/callback"); |
| 24 | + Environment.SetEnvironmentVariable("POST_LOGOUT_URI", "https://localhost:4260/"); |
| 25 | + |
| 26 | + _factory = factory.WithWebHostBuilder(builder => |
| 27 | + { |
| 28 | + builder.ConfigureTestServices(services => |
| 29 | + { |
| 30 | + // Pre-populate OIDC Configuration so the middleware doesn't call the |
| 31 | + // discovery endpoint (which would fail — there's no real IdP here). |
| 32 | + services.Configure<OpenIdConnectOptions>( |
| 33 | + OpenIdConnectDefaults.AuthenticationScheme, |
| 34 | + options => |
| 35 | + { |
| 36 | + options.Configuration = new OpenIdConnectConfiguration |
| 37 | + { |
| 38 | + Issuer = "https://idp.example", |
| 39 | + AuthorizationEndpoint = "https://idp.example/authorize", |
| 40 | + TokenEndpoint = "https://idp.example/token", |
| 41 | + EndSessionEndpoint = "https://idp.example/end_session", |
| 42 | + JwksUri = "https://idp.example/jwks", |
| 43 | + }; |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task Root_Unauthenticated_ShowsSignIn() |
| 51 | + { |
| 52 | + var client = _factory.CreateClient(new WebApplicationFactoryClientOptions |
| 53 | + { |
| 54 | + AllowAutoRedirect = false, |
| 55 | + }); |
| 56 | + var res = await client.GetAsync("/"); |
| 57 | + Assert.Equal(HttpStatusCode.OK, res.StatusCode); |
| 58 | + var body = await res.Content.ReadAsStringAsync(); |
| 59 | + Assert.Contains("Sign in", body); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task Root_Authenticated_ShowsWelcome() |
| 64 | + { |
| 65 | + var factory = _factory.WithWebHostBuilder(builder => |
| 66 | + { |
| 67 | + builder.ConfigureTestServices(services => |
| 68 | + { |
| 69 | + services.AddAuthentication("Test") |
| 70 | + .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", _ => { }); |
| 71 | + services.PostConfigure<AuthenticationOptions>(options => |
| 72 | + { |
| 73 | + options.DefaultScheme = "Test"; |
| 74 | + options.DefaultAuthenticateScheme = "Test"; |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + var client = factory.CreateClient(); |
| 79 | + var res = await client.GetAsync("/"); |
| 80 | + Assert.Equal(HttpStatusCode.OK, res.StatusCode); |
| 81 | + var body = await res.Content.ReadAsStringAsync(); |
| 82 | + Assert.Contains("Welcome, Test User (test@example.com)", body); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task Login_ReturnsChallengeRedirectToAuthorizeEndpoint() |
| 87 | + { |
| 88 | + var client = _factory.CreateClient(new WebApplicationFactoryClientOptions |
| 89 | + { |
| 90 | + AllowAutoRedirect = false, |
| 91 | + }); |
| 92 | + var res = await client.GetAsync("/login"); |
| 93 | + Assert.True( |
| 94 | + res.StatusCode == HttpStatusCode.Redirect || res.StatusCode == HttpStatusCode.Found, |
| 95 | + $"Expected redirect, got {res.StatusCode}"); |
| 96 | + var location = res.Headers.Location!.ToString(); |
| 97 | + Assert.Contains("idp.example/authorize", location); |
| 98 | + Assert.Contains("client_id=test-client", location); |
| 99 | + Assert.Contains("response_type=code", location); |
| 100 | + Assert.Contains("code_challenge=", location); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public async Task Logout_RedirectsToEndSessionEndpoint() |
| 105 | + { |
| 106 | + var client = _factory.CreateClient(new WebApplicationFactoryClientOptions |
| 107 | + { |
| 108 | + AllowAutoRedirect = false, |
| 109 | + }); |
| 110 | + var res = await client.GetAsync("/logout"); |
| 111 | + Assert.True( |
| 112 | + res.StatusCode == HttpStatusCode.Redirect || res.StatusCode == HttpStatusCode.Found, |
| 113 | + $"Expected redirect, got {res.StatusCode}"); |
| 114 | + var location = res.Headers.Location!.ToString(); |
| 115 | + Assert.Contains("idp.example/end_session", location); |
| 116 | + } |
| 117 | +} |
0 commit comments