|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using sparkly_server.DTO.Auth; |
| 3 | +using sparkly_server.DTO.Projects; |
| 4 | +using sparkly_server.Enum; |
| 5 | +using sparkly_server.Infrastructure; |
| 6 | +using System.Net.Http.Headers; |
| 7 | +using System.Net.Http.Json; |
| 8 | + |
| 9 | +namespace sparkly_server.test |
| 10 | +{ |
| 11 | + public class ProjectTest : IClassFixture<TestWebApplicationFactory>, IAsyncLifetime |
| 12 | + { |
| 13 | + private readonly HttpClient _client; |
| 14 | + private readonly TestWebApplicationFactory _factory; |
| 15 | + |
| 16 | + public ProjectTest(TestWebApplicationFactory factory) |
| 17 | + { |
| 18 | + _factory = factory; |
| 19 | + _client = factory.CreateClient(); |
| 20 | + } |
| 21 | + |
| 22 | + public async Task InitializeAsync() |
| 23 | + { |
| 24 | + using var scope = _factory.Services.CreateScope(); |
| 25 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 26 | + |
| 27 | + db.Users.RemoveRange(db.Users); |
| 28 | + db.Projects.RemoveRange(db.Projects); |
| 29 | + await db.SaveChangesAsync(); |
| 30 | + } |
| 31 | + |
| 32 | + public Task DisposeAsync() => Task.CompletedTask; |
| 33 | + |
| 34 | + // Helpers |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates a new project asynchronously with the specified project name and returns the created project's details. |
| 38 | + /// </summary> |
| 39 | + /// <param name="projectName">The name of the project to be created.</param> |
| 40 | + /// <returns>A task representing the asynchronous operation. The result contains the details of the created project, or null if deserialization fails.</returns> |
| 41 | + private async Task<ProjectResponse?> CreateProjectAsync(string projectName) |
| 42 | + { |
| 43 | + var payload = new CreateProjectRequest( |
| 44 | + ProjectName: projectName, |
| 45 | + Description: "Test project", |
| 46 | + Visibility: ProjectVisibility.Public |
| 47 | + ); |
| 48 | + |
| 49 | + var response = await _client.PostAsJsonAsync("/api/v1/projects/create", payload); |
| 50 | + response.EnsureSuccessStatusCode(); |
| 51 | + |
| 52 | + var created = await response.Content.ReadFromJsonAsync<ProjectResponse>(); |
| 53 | + return created; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Registers a new user and logs them in, setting the authentication token in the HTTP client header for subsequent requests. |
| 58 | + /// </summary> |
| 59 | + /// <exception cref="InvalidOperationException">Thrown when the authentication response does not contain an access token.</exception> |
| 60 | + /// <returns>A task that represents the asynchronous operation of registering and logging in a user.</returns> |
| 61 | + private async Task RegisterAndLoginUser() |
| 62 | + { |
| 63 | + |
| 64 | + var password = "Test1234!"; |
| 65 | + var userName = "testuser"; |
| 66 | + |
| 67 | + var registerPayload = new RegisterRequest(Username:userName, Email: email, Password: password); |
| 68 | + var registerResponse = await _client.PostAsJsonAsync("/api/v1/auth/register", registerPayload); |
| 69 | + registerResponse.EnsureSuccessStatusCode(); |
| 70 | + |
| 71 | + var loginPayload = new LoginRequest(Identifier: email, Password: password); |
| 72 | + var loginResponse = await _client.PostAsJsonAsync("/api/v1/auth/login", loginPayload); |
| 73 | + |
| 74 | + loginResponse.EnsureSuccessStatusCode(); |
| 75 | + |
| 76 | + var loginContent = await loginResponse.Content.ReadFromJsonAsync<AuthResponse>(); |
| 77 | + |
| 78 | + if (loginContent?.AccessToken is null) |
| 79 | + { |
| 80 | + throw new InvalidOperationException("Auth response did not contain access token"); |
| 81 | + } |
| 82 | + |
| 83 | + _client.DefaultRequestHeaders.Authorization = |
| 84 | + new AuthenticationHeaderValue("Bearer", loginContent.AccessToken); |
| 85 | + } |
| 86 | + |
| 87 | + // Tests |
| 88 | + [Fact] |
| 89 | + public async Task CreateProject_Should_Create_Project_For_Authenticated_User() |
| 90 | + { |
| 91 | + await RegisterAndLoginUser(); |
| 92 | + var projectName = "MyTestProject"; |
| 93 | + |
| 94 | + var created = await CreateProjectAsync(projectName); |
| 95 | + |
| 96 | + Assert.NotNull(created); |
| 97 | + Assert.Equal(projectName, created.ProjectName); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments