|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Json; |
| 3 | +using Microsoft.AspNetCore.Identity; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using SentenceStudio.Api.Auth; |
| 6 | +using SentenceStudio.Api.Tests.Infrastructure; |
| 7 | +using SentenceStudio.Shared.Models; |
| 8 | + |
| 9 | +namespace SentenceStudio.Api.Tests; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Integration tests for the Identity auth endpoints (register, login, confirm-email, refresh). |
| 13 | +/// Uses JwtBearerApiFactory so Identity services and JWT signing are configured. |
| 14 | +/// </summary> |
| 15 | +public class IdentityAuthTests : IClassFixture<JwtBearerApiFactory> |
| 16 | +{ |
| 17 | + private readonly JwtBearerApiFactory _factory; |
| 18 | + private readonly HttpClient _client; |
| 19 | + |
| 20 | + public IdentityAuthTests(JwtBearerApiFactory factory) |
| 21 | + { |
| 22 | + _factory = factory; |
| 23 | + _client = factory.CreateClient(); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task Register_ReturnsOk() |
| 28 | + { |
| 29 | + var email = $"register-ok-{Guid.NewGuid():N}@test.local"; |
| 30 | + |
| 31 | + var response = await _client.PostAsJsonAsync("/api/auth/register", new |
| 32 | + { |
| 33 | + Email = email, |
| 34 | + Password = "Test1234!", |
| 35 | + DisplayName = "Tester" |
| 36 | + }); |
| 37 | + |
| 38 | + response.StatusCode.Should().Be(HttpStatusCode.OK, |
| 39 | + "register should succeed for a new user"); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task Login_UnconfirmedEmail_Returns401() |
| 44 | + { |
| 45 | + var email = $"unconfirmed-{Guid.NewGuid():N}@test.local"; |
| 46 | + |
| 47 | + // Register but do NOT confirm email |
| 48 | + var reg = await _client.PostAsJsonAsync("/api/auth/register", new |
| 49 | + { |
| 50 | + Email = email, |
| 51 | + Password = "Test1234!", |
| 52 | + }); |
| 53 | + reg.EnsureSuccessStatusCode(); |
| 54 | + |
| 55 | + var response = await _client.PostAsJsonAsync("/api/auth/login", new |
| 56 | + { |
| 57 | + Email = email, |
| 58 | + Password = "Test1234!" |
| 59 | + }); |
| 60 | + |
| 61 | + response.StatusCode.Should().Be(HttpStatusCode.Unauthorized, |
| 62 | + "login should be rejected when email is not confirmed"); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task Login_WrongPassword_Returns401() |
| 67 | + { |
| 68 | + var email = $"wrongpw-{Guid.NewGuid():N}@test.local"; |
| 69 | + |
| 70 | + // Register and confirm |
| 71 | + await RegisterAndConfirmAsync(email, "Test1234!"); |
| 72 | + |
| 73 | + var response = await _client.PostAsJsonAsync("/api/auth/login", new |
| 74 | + { |
| 75 | + Email = email, |
| 76 | + Password = "WrongPassword99!" |
| 77 | + }); |
| 78 | + |
| 79 | + response.StatusCode.Should().Be(HttpStatusCode.Unauthorized, |
| 80 | + "login should be rejected with wrong password"); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task ConfirmEmail_ThenLogin_Succeeds() |
| 85 | + { |
| 86 | + var email = $"confirm-{Guid.NewGuid():N}@test.local"; |
| 87 | + const string password = "Test1234!"; |
| 88 | + |
| 89 | + // Register |
| 90 | + var reg = await _client.PostAsJsonAsync("/api/auth/register", new |
| 91 | + { |
| 92 | + Email = email, |
| 93 | + Password = password, |
| 94 | + }); |
| 95 | + reg.EnsureSuccessStatusCode(); |
| 96 | + |
| 97 | + // Confirm email via UserManager |
| 98 | + await ConfirmEmailDirectlyAsync(email); |
| 99 | + |
| 100 | + // Login should now succeed |
| 101 | + var response = await _client.PostAsJsonAsync("/api/auth/login", new |
| 102 | + { |
| 103 | + Email = email, |
| 104 | + Password = password |
| 105 | + }); |
| 106 | + |
| 107 | + response.StatusCode.Should().Be(HttpStatusCode.OK, |
| 108 | + "login should succeed after email confirmation"); |
| 109 | + |
| 110 | + var auth = await response.Content.ReadFromJsonAsync<AuthResponse>(); |
| 111 | + auth.Should().NotBeNull(); |
| 112 | + auth!.Token.Should().NotBeNullOrWhiteSpace("JWT should be returned"); |
| 113 | + auth.RefreshToken.Should().NotBeNullOrWhiteSpace("refresh token should be returned"); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public async Task RefreshToken_ReturnsNewTokens() |
| 118 | + { |
| 119 | + var email = $"refresh-{Guid.NewGuid():N}@test.local"; |
| 120 | + const string password = "Test1234!"; |
| 121 | + |
| 122 | + await RegisterAndConfirmAsync(email, password); |
| 123 | + |
| 124 | + // Login to get initial tokens |
| 125 | + var loginResponse = await _client.PostAsJsonAsync("/api/auth/login", new |
| 126 | + { |
| 127 | + Email = email, |
| 128 | + Password = password |
| 129 | + }); |
| 130 | + loginResponse.EnsureSuccessStatusCode(); |
| 131 | + |
| 132 | + var initial = await loginResponse.Content.ReadFromJsonAsync<AuthResponse>(); |
| 133 | + initial.Should().NotBeNull(); |
| 134 | + |
| 135 | + // Use refresh token to get new tokens |
| 136 | + var refreshResponse = await _client.PostAsJsonAsync("/api/auth/refresh", new |
| 137 | + { |
| 138 | + RefreshToken = initial!.RefreshToken |
| 139 | + }); |
| 140 | + |
| 141 | + refreshResponse.StatusCode.Should().Be(HttpStatusCode.OK, |
| 142 | + "refresh should return new tokens"); |
| 143 | + |
| 144 | + var refreshed = await refreshResponse.Content.ReadFromJsonAsync<AuthResponse>(); |
| 145 | + refreshed.Should().NotBeNull(); |
| 146 | + refreshed!.Token.Should().NotBeNullOrWhiteSpace("new JWT should be returned"); |
| 147 | + refreshed.RefreshToken.Should().NotBeNullOrWhiteSpace("new refresh token should be returned"); |
| 148 | + refreshed.RefreshToken.Should().NotBe(initial.RefreshToken, |
| 149 | + "refresh token should be rotated"); |
| 150 | + } |
| 151 | + |
| 152 | + // -- helpers -- |
| 153 | + |
| 154 | + private async Task RegisterAndConfirmAsync(string email, string password) |
| 155 | + { |
| 156 | + var reg = await _client.PostAsJsonAsync("/api/auth/register", new |
| 157 | + { |
| 158 | + Email = email, |
| 159 | + Password = password, |
| 160 | + }); |
| 161 | + reg.EnsureSuccessStatusCode(); |
| 162 | + await ConfirmEmailDirectlyAsync(email); |
| 163 | + } |
| 164 | + |
| 165 | + private async Task ConfirmEmailDirectlyAsync(string email) |
| 166 | + { |
| 167 | + using var scope = _factory.Services.CreateScope(); |
| 168 | + var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>(); |
| 169 | + var user = await userManager.FindByEmailAsync(email); |
| 170 | + user.Should().NotBeNull($"user {email} should exist after registration"); |
| 171 | + |
| 172 | + var token = await userManager.GenerateEmailConfirmationTokenAsync(user!); |
| 173 | + var result = await userManager.ConfirmEmailAsync(user!, token); |
| 174 | + result.Succeeded.Should().BeTrue("email confirmation should succeed"); |
| 175 | + } |
| 176 | +} |
0 commit comments