- Updated various package versions in `Directory.Packages.props` to the latest compatible releases, enhancing overall stability and security. - Modified test assertions in `GrantBillingGiftCommandHandlerTests`, `RejectPaymentRequestCommandHandlerTests`, `BlockUserCommandHandlerTests`, and `DeleteUserCommandHandlerTests` to use null-safe checks, ensuring robustness against potential null reference exceptions. - Updated PostgreSqlContainer initialization in `PnvPanelWebApplicationFactory` for improved clarity and maintainability.
177 lines
6.0 KiB
C#
177 lines
6.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using PnvPanel.Application.Admin.Users;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Common.Models;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Configs;
|
|
using PnvPanel.Domain.Inbounds;
|
|
using PnvPanel.Domain.Nodes;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Admin.Users;
|
|
|
|
public class DeleteUserCommandHandlerTests
|
|
{
|
|
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
|
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
|
|
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
|
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
|
|
private readonly ILogger<DeleteUserCommandHandler> _logger = Substitute.For<
|
|
ILogger<DeleteUserCommandHandler>
|
|
>();
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenAdminTargetsSelf_ReturnsCannotDeleteSelfWithoutTouchingConfigs()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var adminId = Guid.NewGuid();
|
|
_currentUser.UserId.Returns(adminId);
|
|
|
|
var handler = new DeleteUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_telegramNotifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new DeleteUserCommand(adminId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(UserErrors.CannotDeleteSelf, result.Error);
|
|
await _identityService
|
|
.DidNotReceive()
|
|
.DeleteUserAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_Success_RevokesConfigsWritesAuditNotifiesThenDeletesUser()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
var adminId = Guid.NewGuid();
|
|
|
|
var node = Node.Register(
|
|
"node-1",
|
|
new Uri("https://node1.example.com"),
|
|
new NodeCredentials("admin", "protected"),
|
|
null
|
|
);
|
|
var inbound = Inbound.FromRemote(node.Id, "1", VpnProtocol.Vless, "remark", 443);
|
|
var config = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "my-config");
|
|
config.AssignRemoteClient("external-id");
|
|
|
|
dbContext.Nodes.Add(node);
|
|
dbContext.Inbounds.Add(inbound);
|
|
dbContext.VpnConfigs.Add(config);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_currentUser.UserId.Returns(adminId);
|
|
_gateway
|
|
.RemoveClientAsync(
|
|
Arg.Any<Node>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<VpnProtocol>(),
|
|
Arg.Any<CancellationToken>()
|
|
)
|
|
.Returns(Result.Success());
|
|
_identityService
|
|
.DeleteUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success());
|
|
|
|
var handler = new DeleteUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_telegramNotifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new DeleteUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(ConfigStatus.Revoked, config.Status);
|
|
|
|
await _gateway
|
|
.Received(1)
|
|
.RemoveClientAsync(
|
|
Arg.Is<Node>(n => n!.Id == node.Id),
|
|
inbound.RemoteInboundId,
|
|
"external-id",
|
|
config.Protocol,
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
await _telegramNotifier
|
|
.Received(1)
|
|
.NotifyUserAsync(userId, Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<CancellationToken>());
|
|
await _identityService.Received(1).DeleteUserAsync(userId, Arg.Any<CancellationToken>());
|
|
|
|
var audit = Assert.Single(dbContext.AuditLogs.Local);
|
|
Assert.Equal("UserDeleted", audit.Action);
|
|
Assert.Equal(adminId, audit.ActorId);
|
|
Assert.Equal(userId.ToString(), audit.TargetId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_NoConfigs_SkipsGatewayButStillDeletesUser()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
_currentUser.UserId.Returns(Guid.NewGuid());
|
|
_identityService
|
|
.DeleteUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success());
|
|
|
|
var handler = new DeleteUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_telegramNotifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new DeleteUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
await _gateway
|
|
.DidNotReceive()
|
|
.RemoveClientAsync(
|
|
Arg.Any<Node>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<VpnProtocol>(),
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenIdentityServiceFails_ReturnsFailure()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
_currentUser.UserId.Returns(Guid.NewGuid());
|
|
_identityService
|
|
.DeleteUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Failure(UserErrors.NotFound));
|
|
|
|
var handler = new DeleteUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_telegramNotifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new DeleteUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(UserErrors.NotFound, result.Error);
|
|
}
|
|
}
|