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 BlockUserCommandHandlerTests { private readonly IIdentityService _identityService = Substitute.For(); private readonly IXuiPanelGateway _gateway = Substitute.For(); private readonly IRealtimeNotifier _notifier = Substitute.For(); private readonly ITelegramNotifier _telegramNotifier = Substitute.For(); private readonly ICurrentUser _currentUser = Substitute.For(); private readonly ILogger _logger = Substitute.For>(); [Fact] public async Task Handle_IdentityServiceFails_ReturnsFailureWithoutTouchingConfigs() { using var dbContext = InMemoryDbContextFactory.Create(); var userId = Guid.NewGuid(); var failure = UserErrors.NotFound; _identityService.BlockUserAsync(userId, Arg.Any()).Returns(Result.Failure(failure)); var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger); var result = await handler.Handle(new BlockUserCommand(userId), CancellationToken.None); Assert.False(result.IsSuccess); Assert.Equal(failure, result.Error); await _gateway.DidNotReceive().UpdateClientAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Handle_Success_DisablesActiveConfigsAndWritesAudit() { 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", 0); dbContext.Nodes.Add(node); dbContext.Inbounds.Add(inbound); dbContext.VpnConfigs.Add(config); await dbContext.SaveChangesAsync(CancellationToken.None); _identityService.BlockUserAsync(userId, Arg.Any()).Returns(Result.Success()); _currentUser.UserId.Returns(adminId); _gateway.UpdateClientAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(Result.Success()); var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger); var result = await handler.Handle(new BlockUserCommand(userId), CancellationToken.None); Assert.True(result.IsSuccess); Assert.Equal(ConfigStatus.Disabled, config.Status); await _gateway.Received(1).UpdateClientAsync( Arg.Is(n => n.Id == node.Id), inbound.RemoteInboundId, config.ClientExternalId, config.Protocol, "my-config", config.DeviceLimit, enable: false, Arg.Any()); await _notifier.Received(1).NotifyConfigStatusChangedAsync(userId, config.Id, ConfigStatus.Disabled, Arg.Any()); var audit = Assert.Single(dbContext.AuditLogs.Local); Assert.Equal("UserBlocked", audit.Action); Assert.Equal(adminId, audit.ActorId); } [Fact] public async Task Handle_NoActiveConfigs_SkipsGatewayCalls() { using var dbContext = InMemoryDbContextFactory.Create(); var userId = Guid.NewGuid(); _identityService.BlockUserAsync(userId, Arg.Any()).Returns(Result.Success()); _currentUser.UserId.Returns(Guid.NewGuid()); var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger); var result = await handler.Handle(new BlockUserCommand(userId), CancellationToken.None); Assert.True(result.IsSuccess); await _gateway.DidNotReceive().UpdateClientAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task Handle_GatewayFails_KeepsConfigActiveForRetryAndDoesNotNotify() { 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", 0); dbContext.Nodes.Add(node); dbContext.Inbounds.Add(inbound); dbContext.VpnConfigs.Add(config); await dbContext.SaveChangesAsync(CancellationToken.None); _identityService.BlockUserAsync(userId, Arg.Any()).Returns(Result.Success()); _currentUser.UserId.Returns(adminId); _gateway.UpdateClientAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(Result.Failure(Error.Failure("Xui.UpdateClientFailed", "Нода недоступна."))); var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger); var result = await handler.Handle(new BlockUserCommand(userId), CancellationToken.None); Assert.True(result.IsSuccess); Assert.Equal(ConfigStatus.Active, config.Status); await _notifier.DidNotReceive().NotifyConfigStatusChangedAsync( Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } }