using System.Net; using System.Net.Http.Json; using Helpdesk.Application.Catalogs; using Helpdesk.Application.Common; using Helpdesk.Application.Notifications; using Helpdesk.Application.Proposals; using Helpdesk.Application.Statistics; using static Helpdesk.IntegrationTests.BackendFixture; namespace Helpdesk.IntegrationTests; public sealed class ProposalWorkflowTests(BackendFixture fixture) : IClassFixture { private const string Proposals = "/api/v1/proposals"; [Fact] public async Task VotesAndDecisionsAreReflectedInTheProposalAndItsHistory() { using var anonymous = fixture.CreateClient(); using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken); var authorUser = await fixture.RegisterAsync(anonymous); using var author = fixture.CreateClient((await LoginAsync(anonymous, authorUser.Email)).AccessToken); var managerSession = await fixture.RegisterStaffAsync(anonymous, admin, "MANAGEMENT"); using var manager = fixture.CreateClient(managerSession.AccessToken); var categories = await ReadAsync(author.GetAsync("/api/v1/proposal-categories", Cancellation)); var request = new SaveProposalRequest("Prodloužení studovny", "Rozšířit večerní otevírací dobu.", categories[0].Id); await ExpectAsync(author.PostAsJsonAsync(Proposals, request with { Title = " " }, Cancellation), HttpStatusCode.BadRequest); await ExpectAsync(author.PostAsJsonAsync(Proposals, request with { CategoryId = int.MaxValue }, Cancellation), HttpStatusCode.BadRequest); var proposal = await ReadAsync(author.PostAsJsonAsync(Proposals, request, Cancellation), HttpStatusCode.Created); Assert.Equal("SUBMITTED", proposal.Status); var path = $"{Proposals}/{proposal.Id}"; var edit = request with { CategoryId = categories[1].Id, Description = "Otevřít do 22 hodin." }; await ExpectAsync(manager.PutAsJsonAsync(path, edit, Cancellation), HttpStatusCode.Forbidden); proposal = await ReadAsync(author.PutAsJsonAsync(path, edit, Cancellation)); Assert.Equal((edit.CategoryId, edit.Description, "UPDATED"), (proposal.CategoryId, proposal.Description, proposal.Events[^1].EventType)); var other = await ReadAsync(author.PostAsJsonAsync(Proposals, edit with { Title = "Vybavení studovny", }, Cancellation), HttpStatusCode.Created); await VerifyVotesAsync(author, manager, path); var from = Uri.EscapeDataString(proposal.CreatedAt.AddMinutes(-1).ToString("O")); var to = Uri.EscapeDataString(other.CreatedAt.AddMinutes(1).ToString("O")); var filter = $"{Proposals}?status=SUBMITTED&categoryId={edit.CategoryId}&authorId={authorUser.Id}&q=studovny&from={from}&to={to}"; var first = await ReadAsync>(manager.GetAsync(filter + "&sort=score&pageSize=1", Cancellation)); var second = await ReadAsync>(manager.GetAsync(filter + "&sort=score&pageSize=1&page=2", Cancellation)); Assert.Equal(2, first.TotalCount); Assert.Equal(proposal.Id, Assert.Single(first.Items).Id); Assert.Equal(other.Id, Assert.Single(second.Items).Id); var newest = await ReadAsync>(manager.GetAsync(filter, Cancellation)); Assert.Equal(other.Id, newest.Items[0].Id); var outside = await ReadAsync>(manager.GetAsync($"{Proposals}?from={to}", Cancellation)); Assert.Empty(outside.Items); await ExpectAsync(author.PostAsJsonAsync(path + "/status", new ChangeProposalStatusRequest("ACCEPTED", null), Cancellation), HttpStatusCode.Forbidden); await ExpectAsync(manager.PostAsJsonAsync(path + "/status", new ChangeProposalStatusRequest("REJECTED", " "), Cancellation), HttpStatusCode.BadRequest); await ExpectAsync(manager.PostAsJsonAsync(path + "/status", new ChangeProposalStatusRequest("IMPLEMENTED", null), Cancellation), HttpStatusCode.Conflict); var review = await ReadAsync(manager.PostAsJsonAsync(path + "/status", new ChangeProposalStatusRequest("UNDER_REVIEW", null), Cancellation)); Assert.Equal("UNDER_REVIEW", review.Status); var accepted = await ReadAsync(manager.PostAsJsonAsync(path + "/status", new ChangeProposalStatusRequest("ACCEPTED", " Schváleno. "), Cancellation)); Assert.Equal(("ACCEPTED", "Schváleno."), (accepted.Status, accepted.Events[^1].Note)); await ExpectAsync(author.PutAsJsonAsync(path, edit, Cancellation), HttpStatusCode.Conflict); await ExpectAsync(manager.DeleteAsync(path + "/vote", Cancellation), HttpStatusCode.Conflict); var inbox = await ReadAsync>(author.GetAsync("/api/v1/notifications", Cancellation)); Assert.Equal(2, inbox.Items.Count(item => item.ProposalId == proposal.Id && item.Type == "PROPOSAL_STATUS_CHANGED")); var top = Assert.Single(await ReadAsync( manager.GetAsync($"/api/v1/statistics/proposals/top?limit=1&from={from}&to={to}", Cancellation))); Assert.Equal((proposal.Id, 1L), (top.Id, top.Score)); } private static async Task VerifyVotesAsync(HttpClient author, HttpClient voter, string path) { var vote = path + "/vote"; await ExpectAsync(author.PutAsJsonAsync(vote, new VoteRequest(1), Cancellation), HttpStatusCode.Forbidden); await ExpectAsync(voter.PutAsJsonAsync(vote, new VoteRequest(0), Cancellation), HttpStatusCode.BadRequest); await ExpectAsync(voter.PutAsJsonAsync(vote, new VoteRequest(1), Cancellation), HttpStatusCode.NoContent); var detail = await ReadAsync(voter.GetAsync(path, Cancellation)); Assert.Equal((1L, 0L, 1L, (int?)1), (detail.VotesFor, detail.VotesAgainst, detail.Score, detail.MyVote)); await ExpectAsync(voter.PutAsJsonAsync(vote, new VoteRequest(-1), Cancellation), HttpStatusCode.NoContent); detail = await ReadAsync(voter.GetAsync(path, Cancellation)); Assert.Equal((0L, 1L, -1L, (int?)-1), (detail.VotesFor, detail.VotesAgainst, detail.Score, detail.MyVote)); await ExpectAsync(voter.PutAsJsonAsync(vote, new VoteRequest(-1), Cancellation), HttpStatusCode.NoContent); await ExpectAsync(voter.DeleteAsync(vote, Cancellation), HttpStatusCode.NoContent); detail = await ReadAsync(voter.GetAsync(path, Cancellation)); Assert.Equal(0L, detail.Score); Assert.Null(detail.MyVote); await ExpectAsync(voter.PutAsJsonAsync(vote, new VoteRequest(1), Cancellation), HttpStatusCode.NoContent); } }