Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Support/AddTicketCommentCommandHandlerTests.cs
T
mrleo1nidandClaude Opus 5.5 1bc86dc426
CI / Backend (build + test) (push) Failing after 6m7s
CI / Frontend (lint + typecheck + build) (push) Successful in 39s
Notify via Telegram about new support ticket comments
Owner's reply goes to all admins (text preview + link to the ticket),
admin's reply goes to the ticket owner as a DM. The comment author is
never notified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 17:57:08 +03:00

208 lines
6.8 KiB
C#

using NSubstitute;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Models;
using PnvPanel.Application.Support;
using PnvPanel.Application.Support.AddComment;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Support;
using Xunit;
namespace PnvPanel.Application.Tests.Support;
public class AddTicketCommentCommandHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
private readonly IFileStorage _fileStorage = Substitute.For<IFileStorage>();
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
private static CurrentUserProfile Profile(Guid userId, string role) =>
new(
userId,
"user",
Guid.NewGuid(),
role,
IsActivated: true,
IsBlocked: false,
ConfigQuota: 3,
PlanId: null,
MaxIpLimit: RoleQuota.Unlimited,
SubscriptionToken: "token",
BillingEnabled: false,
BillingPaidUntil: null,
BillingSuspended: false
);
[Fact]
public async Task Handle_WhenOwnerComments_AddsCommentAndDoesNotNotifySelf()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var ticket = SupportTicket.CreateBugReport(userId);
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, "user"));
var currentUser = FakeCurrentUser.Authenticated(userId, "alice");
var handler = new AddTicketCommentCommandHandler(
dbContext,
_identityService,
_fileStorage,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(
new AddTicketCommentCommand(ticket.Id, "апдейт", []),
CancellationToken.None
);
Assert.True(result.IsSuccess);
Assert.Equal("апдейт", result.Value.Body);
await _notifier
.DidNotReceive()
.NotifyTicketUpdatedAsync(
Arg.Any<Guid>(),
Arg.Any<Guid>(),
Arg.Any<CancellationToken>()
);
await _telegramNotifier
.DidNotReceive()
.NotifyUserAsync(
Arg.Any<Guid>(),
Arg.Any<string>(),
Arg.Any<string?>(),
Arg.Any<CancellationToken>()
);
await _telegramNotifier
.Received(1)
.NotifyAdminsTicketCommentAddedAsync(
ticket.Id,
"alice",
"апдейт",
Arg.Any<CancellationToken>()
);
}
[Fact]
public async Task Handle_WhenAdminComments_NotifiesOwner()
{
using var dbContext = InMemoryDbContextFactory.Create();
var ownerId = Guid.NewGuid();
var adminId = Guid.NewGuid();
var ticket = SupportTicket.CreateBugReport(ownerId);
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(adminId, Arg.Any<CancellationToken>())
.Returns(Profile(adminId, "admin"));
var currentUser = FakeCurrentUser.Authenticated(adminId, "admin");
var handler = new AddTicketCommentCommandHandler(
dbContext,
_identityService,
_fileStorage,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(
new AddTicketCommentCommand(ticket.Id, "ответ админа", []),
CancellationToken.None
);
Assert.True(result.IsSuccess);
await _notifier
.Received(1)
.NotifyTicketUpdatedAsync(ticket.Id, ownerId, Arg.Any<CancellationToken>());
await _telegramNotifier
.Received(1)
.NotifyUserAsync(
ownerId,
Arg.Any<string>(),
$"/support?ticket={ticket.Id}",
Arg.Any<CancellationToken>()
);
await _telegramNotifier
.DidNotReceive()
.NotifyAdminsTicketCommentAddedAsync(
Arg.Any<Guid>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
public async Task Handle_WhenNotOwnerAndNotAdmin_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var ownerId = Guid.NewGuid();
var strangerId = Guid.NewGuid();
var ticket = SupportTicket.CreateBugReport(ownerId);
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(strangerId, Arg.Any<CancellationToken>())
.Returns(Profile(strangerId, "user"));
var currentUser = FakeCurrentUser.Authenticated(strangerId);
var handler = new AddTicketCommentCommandHandler(
dbContext,
_identityService,
_fileStorage,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(
new AddTicketCommentCommand(ticket.Id, "текст", []),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(SupportErrors.NotFound, result.Error);
}
[Fact]
public async Task Handle_WhenTicketClosed_ReturnsTicketClosedError()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var ticket = SupportTicket.CreateBugReport(userId);
ticket.Close();
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetProfileAsync(userId, Arg.Any<CancellationToken>())
.Returns(Profile(userId, "user"));
var currentUser = FakeCurrentUser.Authenticated(userId);
var handler = new AddTicketCommentCommandHandler(
dbContext,
_identityService,
_fileStorage,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(
new AddTicketCommentCommand(ticket.Id, "текст", []),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(SupportErrors.TicketClosed, result.Error);
}
}