backend: Enforce notification access and clear push devices

This commit is contained in:
GonnaHetzMe committed 2026-12-04 21:42:00 +01:00
1 parent ee5f1d3d10
commit 4f95c8a379
12 files changed
+145 -46

No files matched your search

@@ -5,6 +5,7 @@ using Helpdesk.Application.Catalogs;
using Helpdesk.Application.Common;
using Helpdesk.Application.Issues;
using Helpdesk.Application.Notifications;
using Helpdesk.Application.Users;
using Helpdesk.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -66,6 +67,50 @@ public sealed class NotificationTests(BackendFixture fixture) : IClassFixture<Ba
Assert.Empty((await ReadAsync<PagedResult<NotificationDto>>(manager.GetAsync(inbox + "?unreadOnly=true", Cancellation))).Items);
}
[Fact]
public async Task RoleChangeHidesOtherIssuesAndSkipsQueuedEmail()
{
using var anonymous = fixture.CreateClient();
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
var reporterAccount = await fixture.RegisterAsync(anonymous);
using var reporter = fixture.CreateClient((await LoginAsync(anonymous, reporterAccount.Email)).AccessToken);
var managerSession = await fixture.RegisterStaffAsync(anonymous, admin, "MANAGEMENT");
using var manager = fixture.CreateClient(managerSession.AccessToken);
const string issues = "/api/v1/issues";
const string inbox = "/api/v1/notifications";
var categories = await ReadAsync<CategoryDto[]>(reporter.GetAsync("/api/v1/issue-categories", Cancellation));
var request = new CreateIssueRequest("Závada v učebně", "Závadu je potřeba prověřit.", categories[0].Id, null, null);
fixture.FailNextEmail(managerSession.User.Email, "TEST_RECIPIENT_REJECTED", retryable: false);
var issue = await ReadAsync<IssueDetail>(reporter.PostAsJsonAsync(issues, request, Cancellation), HttpStatusCode.Created);
var before = await ReadAsync<PagedResult<NotificationDto>>(manager.GetAsync(inbox, Cancellation));
var hidden = Assert.Single(before.Items);
Assert.Equal(issue.Id, hidden.IssueId);
var failed = await WaitForDeliveryAsync(admin, hidden.Id, "FAILED");
await ExpectAsync(admin.PutAsJsonAsync($"/api/v1/admin/users/{managerSession.User.Id}/role",
new ChangeRoleRequest("STUDENT"), Cancellation), HttpStatusCode.NoContent);
using var student = fixture.CreateClient((await LoginAsync(anonymous, managerSession.User.Email)).AccessToken);
var ownIssue = await ReadAsync<IssueDetail>(student.PostAsJsonAsync(issues, request, Cancellation), HttpStatusCode.Created);
await ReadAsync<IssueEventDto>(admin.PostAsJsonAsync($"{issues}/{ownIssue.Id}/comments",
new CommentRequest("Komentář k vlastní závadě uživatele."), Cancellation));
var after = await ReadAsync<PagedResult<NotificationDto>>(student.GetAsync(inbox, Cancellation));
Assert.Equal(1, after.TotalCount);
Assert.Equal(ownIssue.Id, Assert.Single(after.Items).IssueId);
Assert.Equal(1, (await ReadAsync<UnreadCountDto>(student.GetAsync(inbox + "/unread-count", Cancellation))).Count);
await ExpectAsync(student.PostAsync($"{inbox}/{hidden.Id}/read", null, Cancellation), HttpStatusCode.NotFound);
await ExpectAsync(student.PostAsync(inbox + "/read-all", null, Cancellation), HttpStatusCode.NoContent);
Assert.Equal(0, (await ReadAsync<UnreadCountDto>(student.GetAsync(inbox + "/unread-count", Cancellation))).Count);
await ExpectAsync(admin.PostAsync($"/api/v1/admin/notification-deliveries/{failed.Id}/retry", null, Cancellation),
HttpStatusCode.NoContent);
await WaitForDeliveryAsync(admin, hidden.Id, "SKIPPED", attempts: 0);
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
Assert.Null(await db.Notifications.Where(n => n.Id == hidden.Id).Select(n => n.ReadAt).SingleAsync(Cancellation));
}
[Fact]
public async Task DeviceRegistrationPreservesCaseAndTransfersOwnership()
{