|
| 1 | +namespace SkillMatrixLlm.Api.Tests; |
| 2 | + |
| 3 | +using System.Net; |
| 4 | +using Constants; |
| 5 | +using Data; |
| 6 | +using Enums; |
| 7 | +using Fixtures; |
| 8 | +using Messaging; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Models.Recommendations; |
| 11 | +using Moq; |
| 12 | +using Xunit; |
| 13 | +using ProjectEntity = SkillMatrixLlm.Api.Data.Entities.Project; |
| 14 | +using UserEntity = SkillMatrixLlm.Api.Data.Entities.User; |
| 15 | + |
| 16 | +public class RecommendationsControllerTests(ApiFactory factory) : IClassFixture<ApiFactory>, IAsyncLifetime |
| 17 | +{ |
| 18 | + public Task InitializeAsync() |
| 19 | + { |
| 20 | + using var scope = factory.Services.CreateScope(); |
| 21 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 22 | + db.Recommendations.RemoveRange(db.Recommendations); |
| 23 | + db.TeamMemberships.RemoveRange(db.TeamMemberships); |
| 24 | + db.Teams.RemoveRange(db.Teams); |
| 25 | + db.Projects.RemoveRange(db.Projects); |
| 26 | + db.Users.RemoveRange(db.Users); |
| 27 | + db.SaveChanges(); |
| 28 | + |
| 29 | + Mock.Get(factory.Services.GetRequiredService<IMessageChannel<ProjectDescriptionPayload>>()).Reset(); |
| 30 | + |
| 31 | + return Task.CompletedTask; |
| 32 | + } |
| 33 | + |
| 34 | + public Task DisposeAsync() => Task.CompletedTask; |
| 35 | + |
| 36 | + private static UserEntity SeedUser(AppDbContext db) |
| 37 | + { |
| 38 | + var user = new UserEntity { KeycloakId = "test-keycloak-id", DisplayName = "PM", Email = "pm@example.com" }; |
| 39 | + db.Users.Add(user); |
| 40 | + db.SaveChanges(); |
| 41 | + return user; |
| 42 | + } |
| 43 | + |
| 44 | + private static ProjectEntity SeedProject(AppDbContext db, UserEntity user, ProjectStatus status) |
| 45 | + { |
| 46 | + var project = new ProjectEntity |
| 47 | + { |
| 48 | + Title = "Test Project", |
| 49 | + Description = "A project description", |
| 50 | + DesiredTeamSize = 3, |
| 51 | + Timeline = "3 months", |
| 52 | + Status = status, |
| 53 | + CreatedByUserId = user.Id, |
| 54 | + CreatedAt = DateTime.UtcNow, |
| 55 | + }; |
| 56 | + db.Projects.Add(project); |
| 57 | + db.SaveChanges(); |
| 58 | + return project; |
| 59 | + } |
| 60 | + |
| 61 | + // ------------------------------------------------------------------------- |
| 62 | + // POST /api/projects/{projectId}/recommendations |
| 63 | + // ------------------------------------------------------------------------- |
| 64 | + |
| 65 | + [Theory] |
| 66 | + [InlineData(ProjectStatus.Draft)] |
| 67 | + [InlineData(ProjectStatus.Open)] |
| 68 | + public async Task Trigger_ReturnsAccepted_WhenProjectIsInAllowedStatus(ProjectStatus status) |
| 69 | + { |
| 70 | + using var scope = factory.Services.CreateScope(); |
| 71 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 72 | + var user = SeedUser(db); |
| 73 | + var project = SeedProject(db, user, status); |
| 74 | + |
| 75 | + var client = factory.CreateAuthenticatedClient([TestClaims.Sub, TestClaims.ManageProjectsRole]); |
| 76 | + |
| 77 | + var response = await client.PostAsync($"/api/projects/{project.Id}/recommendations", null); |
| 78 | + |
| 79 | + Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); |
| 80 | + } |
| 81 | + |
| 82 | + [Theory] |
| 83 | + [InlineData(ProjectStatus.Draft)] |
| 84 | + [InlineData(ProjectStatus.Open)] |
| 85 | + public async Task Trigger_PublishesPayload_WhenProjectIsInAllowedStatus(ProjectStatus status) |
| 86 | + { |
| 87 | + using var scope = factory.Services.CreateScope(); |
| 88 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 89 | + var user = SeedUser(db); |
| 90 | + var project = SeedProject(db, user, status); |
| 91 | + |
| 92 | + var client = factory.CreateAuthenticatedClient([TestClaims.Sub, TestClaims.ManageProjectsRole]); |
| 93 | + |
| 94 | + await client.PostAsync($"/api/projects/{project.Id}/recommendations", null); |
| 95 | + |
| 96 | + var queue = factory.Services.GetRequiredService<IMessageChannel<ProjectDescriptionPayload>>(); |
| 97 | + Mock.Get(queue).Verify( |
| 98 | + q => q.PublishAsync( |
| 99 | + It.Is<ProjectDescriptionPayload>(p => p.ProjectId == project.Id), |
| 100 | + It.IsAny<CancellationToken>()), |
| 101 | + Times.Once); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async Task Trigger_ReturnsNotFound_WhenProjectDoesNotExist() |
| 106 | + { |
| 107 | + var client = factory.CreateAuthenticatedClient([TestClaims.Sub, TestClaims.ManageProjectsRole]); |
| 108 | + |
| 109 | + var response = await client.PostAsync($"/api/projects/{Guid.NewGuid()}/recommendations", null); |
| 110 | + |
| 111 | + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [InlineData(ProjectStatus.TeamConfirmed)] |
| 116 | + [InlineData(ProjectStatus.Closed)] |
| 117 | + public async Task Trigger_ReturnsConflict_WhenProjectStatusDisallowsRecommendations(ProjectStatus status) |
| 118 | + { |
| 119 | + using var scope = factory.Services.CreateScope(); |
| 120 | + var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 121 | + var user = SeedUser(db); |
| 122 | + var project = SeedProject(db, user, status); |
| 123 | + |
| 124 | + var client = factory.CreateAuthenticatedClient([TestClaims.Sub, TestClaims.ManageProjectsRole]); |
| 125 | + |
| 126 | + var response = await client.PostAsync($"/api/projects/{project.Id}/recommendations", null); |
| 127 | + |
| 128 | + Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public async Task Trigger_ReturnsForbidden_WithoutManageProjectsRole() |
| 133 | + { |
| 134 | + var client = factory.CreateAuthenticatedClient([TestClaims.Sub]); |
| 135 | + |
| 136 | + var response = await client.PostAsync($"/api/projects/{Guid.NewGuid()}/recommendations", null); |
| 137 | + |
| 138 | + Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode); |
| 139 | + } |
| 140 | +} |
0 commit comments