Files
NTBug/backend/tests/Helpdesk.IntegrationTests/IssueWorkflowTests.cs
T

138 lines
9.6 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Helpdesk.Application.Catalogs;
using Helpdesk.Application.Common;
using Helpdesk.Application.Issues;
using Helpdesk.Application.Notifications;
using Helpdesk.Application.Statistics;
using static Helpdesk.IntegrationTests.BackendFixture;
namespace Helpdesk.IntegrationTests;
public sealed class IssueWorkflowTests(BackendFixture fixture) : IClassFixture<BackendFixture>
{
[Fact]
public async Task ReporterAndAssigneesCanResolveAnIssueAndManagementCanReportOnIt()
{
using var anonymous = fixture.CreateClient();
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
var author = await fixture.RegisterAsync(anonymous);
using var reporter = fixture.CreateClient((await LoginAsync(anonymous, author.Email)).AccessToken);
var management = await fixture.RegisterStaffAsync(anonymous, admin, "MANAGEMENT");
var firstAssignee = await fixture.RegisterStaffAsync(anonymous, admin, "FACILITY_MANAGER");
var secondAssignee = await fixture.RegisterStaffAsync(anonymous, admin, "FACILITY_MANAGER");
using var manager = fixture.CreateClient(management.AccessToken);
using var firstSolver = fixture.CreateClient(firstAssignee.AccessToken);
using var secondSolver = fixture.CreateClient(secondAssignee.AccessToken);
var categories = await ReadAsync<CategoryDto[]>(reporter.GetAsync("/api/v1/issue-categories", Cancellation));
var buildings = await ReadAsync<BuildingDto[]>(reporter.GetAsync("/api/v1/buildings", Cancellation));
var request = new CreateIssueRequest("Nefunkční projektor", "Projektor se nezapne.", categories[0].Id, buildings[0].Id, "A101");
var issue = await PostIssueAsync(reporter, "/api/v1/issues", request, HttpStatusCode.Created);
var path = $"/api/v1/issues/{issue.Id}";
Assert.Equal("RECEIVED", issue.Status);
await ExpectAsync(anonymous.GetAsync(path, Cancellation), HttpStatusCode.Unauthorized);
issue = await ReadAsync<IssueDetail>(reporter.PutAsJsonAsync(path,
new EditIssueRequest(issue.Title, "Projektor nereaguje ani po restartu.", issue.CategoryId,
issue.BuildingId, issue.LocationDetail, issue.RowVersion), Cancellation));
Assert.Equal("Projektor nereaguje ani po restartu.", issue.Description);
var assign = new AssignIssueRequest(firstAssignee.User.Id, null, issue.RowVersion);
await ExpectAsync(reporter.PostAsJsonAsync(path + "/assign", assign, Cancellation), HttpStatusCode.Forbidden);
issue = await PostIssueAsync(manager, path + "/assign", assign);
await ExpectAsync(firstSolver.PostAsJsonAsync(path + "/start",
new IssueVersionRequest(assign.RowVersion), Cancellation), HttpStatusCode.Conflict);
var reporterNotifications = await InboxAsync(reporter, issue.Id);
Assert.Equal("ISSUE_STATUS_CHANGED", Assert.Single(reporterNotifications).Type);
issue = await PostIssueAsync(manager, path + "/assign", new AssignIssueRequest(secondAssignee.User.Id, null, issue.RowVersion));
Assert.Equal("ISSUE_ASSIGNED", Assert.Single(await InboxAsync(secondSolver, issue.Id)).Type);
Assert.Equal(reporterNotifications, await InboxAsync(reporter, issue.Id));
issue = await PostIssueAsync(secondSolver, path + "/start", new IssueVersionRequest(issue.RowVersion));
Assert.Equal("IN_PROGRESS", issue.Status);
reporterNotifications = await InboxAsync(reporter, issue.Id);
issue = await PostIssueAsync(manager, path + "/assign", new AssignIssueRequest(firstAssignee.User.Id, null, issue.RowVersion));
Assert.Equal("ASSIGNED", issue.Status);
var reassignment = Assert.Single((await InboxAsync(reporter, issue.Id)).Except(reporterNotifications));
Assert.Equal("ISSUE_STATUS_CHANGED", reassignment.Type);
Assert.Equal(2, (await InboxAsync(firstSolver, issue.Id)).Count(notification => notification.Type == "ISSUE_ASSIGNED"));
issue = await PostIssueAsync(firstSolver, path + "/start", new IssueVersionRequest(issue.RowVersion));
const string commentText = "Porucha stále trvá.";
var comment = await ReadAsync<IssueEventDto>(reporter.PostAsJsonAsync(path + "/comments",
new CommentRequest(commentText), Cancellation));
issue = await ReadAsync<IssueDetail>(firstSolver.GetAsync(path, Cancellation));
issue = await PostIssueAsync(firstSolver, path + "/submit-for-verification", new VerifyIssueRequest(issue.RowVersion, null));
Assert.Equal("PENDING_VERIFICATION", issue.Status);
issue = await PostIssueAsync(reporter, path + "/reopen", new IssueReasonRequest(issue.RowVersion, "Porucha se vrátila."));
Assert.Equal("IN_PROGRESS", issue.Status);
Assert.Contains(await InboxAsync(firstSolver, issue.Id), notification => notification.Type == "ISSUE_REOPENED");
issue = await PostIssueAsync(firstSolver, path + "/submit-for-verification", new VerifyIssueRequest(issue.RowVersion, "Ověřeno."));
issue = await PostIssueAsync(reporter, path + "/confirm", new IssueVersionRequest(issue.RowVersion));
Assert.Equal("RESOLVED", issue.Status);
var events = await ReadAsync<IssueEventDto[]>(reporter.GetAsync(path + "/events", Cancellation));
Assert.Equal(commentText, Assert.Single(events, entry => entry.Id == comment.Id).Note);
Assert.Equal("RESOLVED", events[^1].ToStatus);
var openIssue = await PostIssueAsync(reporter, "/api/v1/issues",
request with { Title = "Porucha osvětlení" }, HttpStatusCode.Created);
await VerifyReadsAsync(reporter, manager, issue, openIssue);
var rejected = await PostIssueAsync(manager, $"/api/v1/issues/{openIssue.Id}/reject",
new IssueReasonRequest(openIssue.RowVersion, "Duplicitní hlášení."));
Assert.Equal("REJECTED", rejected.Status);
}
private static async Task VerifyReadsAsync(HttpClient reporter, HttpClient manager, IssueDetail resolved, IssueDetail open)
{
var query = $"/api/v1/issues?categoryId={resolved.CategoryId}&buildingId={resolved.BuildingId}&pageSize=1";
var first = await ReadAsync<PagedResult<IssueSummary>>(manager.GetAsync(query + "&page=1", Cancellation));
var second = await ReadAsync<PagedResult<IssueSummary>>(manager.GetAsync(query + "&page=2", Cancellation));
var filtered = await ReadAsync<PagedResult<IssueSummary>>(manager.GetAsync(query + "&status=RESOLVED", Cancellation));
Assert.Equal(2, first.TotalCount);
Assert.Equal(open.Id, Assert.Single(first.Items).Id);
Assert.Equal(resolved.Id, Assert.Single(second.Items).Id);
Assert.Equal(1, filtered.TotalCount);
Assert.Equal(resolved.Id, Assert.Single(filtered.Items).Id);
const string overview = "/api/v1/statistics/overview";
await ExpectAsync(reporter.GetAsync(overview, Cancellation), HttpStatusCode.Forbidden);
var from = Uri.EscapeDataString(resolved.CreatedAt.AddMinutes(-1).ToString("O"));
var to = Uri.EscapeDataString(open.CreatedAt.AddMinutes(1).ToString("O"));
var period = $"?from={from}&to={to}";
var statistics = await ReadAsync<StatisticsOverview>(manager.GetAsync(overview + period, Cancellation));
Assert.Equal(2, statistics.TotalIssues);
Assert.Equal(1, statistics.IssuesByStatus["RESOLVED"]);
Assert.Equal(1, statistics.IssuesByStatus["RECEIVED"]);
Assert.NotNull(statistics.AverageHoursToAssignment);
Assert.NotNull(statistics.AverageHoursToClosure);
var byCategory = Assert.Single(await ReadAsync<IssueCategoryStatistics[]>(
manager.GetAsync("/api/v1/statistics/issues/by-category" + period, Cancellation)));
Assert.Equal((resolved.CategoryId, 2), (byCategory.CategoryId, byCategory.IssueCount));
Assert.NotNull(byCategory.AverageHoursToResolution);
var byBuilding = Assert.Single(await ReadAsync<IssueBuildingStatistics[]>(
manager.GetAsync("/api/v1/statistics/issues/by-building" + period, Cancellation)));
Assert.Equal((resolved.BuildingId, 2), (byBuilding.BuildingId, byBuilding.IssueCount));
var byAssignee = Assert.Single(await ReadAsync<IssueAssigneeStatistics[]>(
manager.GetAsync("/api/v1/statistics/issues/by-assignee" + period, Cancellation)));
Assert.Equal((resolved.AssigneeId, 1, 0), ((int?)byAssignee.AssigneeId, byAssignee.ResolvedIssues, byAssignee.OpenIssues));
var available = await ReadAsync<AssignableUserDto[]>(manager.GetAsync("/api/v1/users/assignable", Cancellation));
Assert.Contains(available, user => user.Id == resolved.AssigneeId && user.OpenIssues == 0);
var trend = await ReadAsync<IssueTrendPoint[]>(manager.GetAsync("/api/v1/statistics/issues/trend" + period, Cancellation));
Assert.Equal(2, trend.Sum(point => point.CreatedIssues));
Assert.Equal(1, trend.Sum(point => point.ClosedIssues));
var after = await ReadAsync<StatisticsOverview>(manager.GetAsync(overview + $"?from={to}", Cancellation));
Assert.Equal(0, after.TotalIssues);
}
private static Task<IssueDetail> PostIssueAsync<T>(
HttpClient client, string path, T request, HttpStatusCode expected = HttpStatusCode.OK) =>
ReadAsync<IssueDetail>(client.PostAsJsonAsync(path, request, Cancellation), expected);
private static async Task<NotificationDto[]> InboxAsync(HttpClient client, int issueId)
{
var notifications = await ReadAsync<PagedResult<NotificationDto>>(client.GetAsync("/api/v1/notifications", Cancellation));
return notifications.Items.Where(notification => notification.IssueId == issueId).ToArray();
}
}