- Introduced a new error, `CannotRemoveLastAdmin`, to handle attempts to downgrade the last admin user in the system. - Updated `RoleService` to check the number of admin users before allowing a role change that would remove the last admin. - Enhanced unit tests to verify the new behavior, ensuring that attempts to downgrade the last admin correctly propagate the failure. - Updated API documentation to reflect the new validation logic and its implications for role management.
68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
using NSubstitute;
|
|
using PnvPanel.Application.Admin.Support;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Support;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Admin.Support;
|
|
|
|
public class RejectRoleRequestCommandHandlerTests
|
|
{
|
|
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
|
|
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
|
|
|
[Fact]
|
|
public async Task Handle_RejectsRoleRequest_ClosesTicket()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
var ticket = SupportTicket.CreateRoleRequestForExistingRole(userId, Guid.NewGuid());
|
|
dbContext.SupportTickets.Add(ticket);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
|
|
var handler = new RejectRoleRequestCommandHandler(
|
|
dbContext,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
currentUser
|
|
);
|
|
|
|
var result = await handler.Handle(
|
|
new RejectRoleRequestCommand(ticket.Id, "не подходит"),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(TicketStatus.Closed, ticket.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenTicketIsOwnedByAdmin_StillRejects()
|
|
{
|
|
// Отклонить свою же заявку можно — Reject не меняет роль, риска нет.
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var adminId = Guid.NewGuid();
|
|
var ticket = SupportTicket.CreateRoleRequestForExistingRole(adminId, Guid.NewGuid());
|
|
dbContext.SupportTickets.Add(ticket);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(adminId, "admin");
|
|
var handler = new RejectRoleRequestCommandHandler(
|
|
dbContext,
|
|
_notifier,
|
|
_telegramNotifier,
|
|
currentUser
|
|
);
|
|
|
|
var result = await handler.Handle(
|
|
new RejectRoleRequestCommand(ticket.Id, null),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(TicketStatus.Closed, ticket.Status);
|
|
}
|
|
}
|