- Added `NotifyBillingStatusChangedAsync` method to `IRealtimeNotifier` for notifying clients about changes in billing status. - Updated `BillingConfigResumer` to call the new notification method after modifying billing configurations, ensuring users receive real-time updates. - Enhanced `ListUsersQueryHandler` to include a `BillingPendingReview` property in `UserSummaryDto`, indicating if a user has a pending payment request awaiting confirmation. - Refactored various command handlers to utilize `AdvisoryLock` for managing concurrent requests, preventing race conditions in billing operations. - Updated tests to cover new notification behaviors and ensure proper functionality in billing status management.
97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using NSubstitute;
|
|
using PnvPanel.Application.Admin.Support;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Support;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Support;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Admin.Support;
|
|
|
|
public class CloseTicketCommandHandlerTests
|
|
{
|
|
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
|
|
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
|
|
|
[Fact]
|
|
public async Task Handle_ClosesOpenTicket()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
var ticket = SupportTicket.CreateBugReport(userId);
|
|
dbContext.SupportTickets.Add(ticket);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
|
|
var handler = new CloseTicketCommandHandler(
|
|
dbContext,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
currentUser
|
|
);
|
|
|
|
var result = await handler.Handle(
|
|
new CloseTicketCommand(ticket.Id),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(TicketStatus.Closed, ticket.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenTicketIsOwnedByAdmin_StillCloses()
|
|
{
|
|
// Закрыть свой же тикет можно — Close не меняет роль, риска нет (иначе одинокий админ не
|
|
// смог бы почистить случайно созданный тикет никогда).
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var adminId = Guid.NewGuid();
|
|
var ticket = SupportTicket.CreateBugReport(adminId);
|
|
dbContext.SupportTickets.Add(ticket);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(adminId, "admin");
|
|
var handler = new CloseTicketCommandHandler(
|
|
dbContext,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
currentUser
|
|
);
|
|
|
|
var result = await handler.Handle(
|
|
new CloseTicketCommand(ticket.Id),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(TicketStatus.Closed, ticket.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenExtensionRequest_ReturnsOnlyBugReportCanBeClosedDirectly()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
var ticket = SupportTicket.CreateExtensionRequest(userId, 30);
|
|
dbContext.SupportTickets.Add(ticket);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
|
|
var handler = new CloseTicketCommandHandler(
|
|
dbContext,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
currentUser
|
|
);
|
|
|
|
var result = await handler.Handle(
|
|
new CloseTicketCommand(ticket.Id),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(SupportErrors.OnlyBugReportCanBeClosedDirectly, result.Error);
|
|
Assert.Equal(TicketStatus.Open, ticket.Status);
|
|
}
|
|
}
|