|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using sparkly_server.DTO; |
| 3 | +using sparkly_server.Infrastructure; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace sparkly_server.test |
| 8 | +{ |
| 9 | + public class UserTest : IClassFixture<TestWebApplicationFactory>, IAsyncLifetime |
| 10 | + { |
| 11 | + private readonly HttpClient _client; |
| 12 | + private readonly TestWebApplicationFactory _factory; |
| 13 | + |
| 14 | + public UserTest(TestWebApplicationFactory factory) |
| 15 | + { |
| 16 | + _factory = factory; |
| 17 | + _client = factory.CreateClient(); |
| 18 | + } |
| 19 | + |
| 20 | + public async Task InitializeAsync() |
| 21 | + { |
| 22 | + using var scope = _factory.Services.CreateScope(); |
| 23 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 24 | + |
| 25 | + db.Users.RemoveRange(db.Users); |
| 26 | + db.Projects.RemoveRange(db.Projects); |
| 27 | + await db.SaveChangesAsync(); |
| 28 | + } |
| 29 | + |
| 30 | + public Task DisposeAsync() => Task.CompletedTask; |
| 31 | + |
| 32 | + private async Task RegisterTestUserAsync(string userName, string email, string password) |
| 33 | + { |
| 34 | + var payload = new RegisterRequest(Username: userName, Email: email, Password: password); |
| 35 | + |
| 36 | + var content = new StringContent( |
| 37 | + JsonSerializer.Serialize(payload), |
| 38 | + Encoding.UTF8, |
| 39 | + "application/json" |
| 40 | + ); |
| 41 | + |
| 42 | + var response = await _client.PostAsync("/api/v1/auth/register", content); |
| 43 | + response.EnsureSuccessStatusCode(); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task User_CanBeCreated() |
| 48 | + { |
| 49 | + var userName = $"user_{Guid.NewGuid():N}"; |
| 50 | + var email = $"{Guid.NewGuid():N}@example.com"; |
| 51 | + var password = "UserPassword"; |
| 52 | + |
| 53 | + await RegisterTestUserAsync(userName, email, password); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task User_CanLoginByEmail() |
| 58 | + { |
| 59 | + var userName = $"user_{Guid.NewGuid():N}"; |
| 60 | + var email = $"{Guid.NewGuid():N}@example.com"; |
| 61 | + var password = "UserPassword"; |
| 62 | + |
| 63 | + await RegisterTestUserAsync(userName, email, password); |
| 64 | + |
| 65 | + var payload = new LoginRequest(Identifier: email, Password: password); |
| 66 | + |
| 67 | + var content = new StringContent( |
| 68 | + JsonSerializer.Serialize(payload), |
| 69 | + Encoding.UTF8, |
| 70 | + "application/json" |
| 71 | + ); |
| 72 | + |
| 73 | + var response = await _client.PostAsync("/api/v1/auth/login", content); |
| 74 | + |
| 75 | + response.EnsureSuccessStatusCode(); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task User_CanLoginByUsername() |
| 80 | + { |
| 81 | + var userName = $"user_{Guid.NewGuid():N}"; |
| 82 | + var email = $"{Guid.NewGuid():N}@example.com"; |
| 83 | + var password = "UserPassword"; |
| 84 | + |
| 85 | + await RegisterTestUserAsync(userName, email, password); |
| 86 | + |
| 87 | + var payload = new LoginRequest(Identifier: userName, Password: password); |
| 88 | + |
| 89 | + var content = new StringContent( |
| 90 | + JsonSerializer.Serialize(payload), |
| 91 | + Encoding.UTF8, |
| 92 | + "application/json" |
| 93 | + ); |
| 94 | + |
| 95 | + var response = await _client.PostAsync("/api/v1/auth/login", content); |
| 96 | + |
| 97 | + response.EnsureSuccessStatusCode(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments