- Cleaned up whitespace in Directory.Build.props and Directory.Packages.props for consistency. - Reformatted project file references in PnvPanel.Api.csproj for better clarity. - Enhanced code readability in various endpoint files by adjusting line breaks and indentation. - Standardized method signatures and improved formatting in ResultExtensions and multiple endpoint classes for better maintainability.
186 lines
6.2 KiB
C#
186 lines
6.2 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 UnblockUserCommandHandlerTests
|
|
{
|
|
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
|
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
|
|
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
|
|
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
|
|
private readonly ILogger<UnblockUserCommandHandler> _logger = Substitute.For<
|
|
ILogger<UnblockUserCommandHandler>
|
|
>();
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenUnblockSucceeds_ReEnablesDisabledConfigsAndWritesAudit()
|
|
{
|
|
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.Disable();
|
|
|
|
dbContext.Nodes.Add(node);
|
|
dbContext.Inbounds.Add(inbound);
|
|
dbContext.VpnConfigs.Add(config);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_currentUser.UserId.Returns(adminId);
|
|
_identityService
|
|
.UnblockUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success());
|
|
_gateway
|
|
.UpdateClientAsync(
|
|
Arg.Any<Node>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<VpnProtocol>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<bool>(),
|
|
Arg.Any<CancellationToken>()
|
|
)
|
|
.Returns(Result.Success());
|
|
|
|
var handler = new UnblockUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new UnblockUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(ConfigStatus.Active, config.Status);
|
|
await _gateway
|
|
.Received(1)
|
|
.UpdateClientAsync(
|
|
node,
|
|
inbound.RemoteInboundId,
|
|
config.ClientExternalId,
|
|
config.Protocol,
|
|
Arg.Any<string>(),
|
|
true,
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
await _notifier
|
|
.Received(1)
|
|
.NotifyConfigStatusChangedAsync(
|
|
userId,
|
|
config.Id,
|
|
ConfigStatus.Active,
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
Assert.Single(dbContext.AuditLogs.Local);
|
|
Assert.Equal("UserUnblocked", dbContext.AuditLogs.Local.Single().Action);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenIdentityServiceFails_ReturnsFailureWithoutTouchingConfigs()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
var error = UserErrors.NotFound;
|
|
|
|
_identityService
|
|
.UnblockUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Failure(error));
|
|
|
|
var handler = new UnblockUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new UnblockUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(error, result.Error);
|
|
Assert.Empty(dbContext.AuditLogs);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_GatewayFails_KeepsConfigDisabledForRetryAndDoesNotNotify()
|
|
{
|
|
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.Disable();
|
|
|
|
dbContext.Nodes.Add(node);
|
|
dbContext.Inbounds.Add(inbound);
|
|
dbContext.VpnConfigs.Add(config);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_currentUser.UserId.Returns(adminId);
|
|
_identityService
|
|
.UnblockUserAsync(userId, Arg.Any<CancellationToken>())
|
|
.Returns(Result.Success());
|
|
_gateway
|
|
.UpdateClientAsync(
|
|
Arg.Any<Node>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<VpnProtocol>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<bool>(),
|
|
Arg.Any<CancellationToken>()
|
|
)
|
|
.Returns(Result.Failure(Error.Failure("Xui.UpdateClientFailed", "Нода недоступна.")));
|
|
|
|
var handler = new UnblockUserCommandHandler(
|
|
dbContext,
|
|
_identityService,
|
|
_gateway,
|
|
_notifier,
|
|
_currentUser,
|
|
_logger
|
|
);
|
|
|
|
var result = await handler.Handle(new UnblockUserCommand(userId), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(ConfigStatus.Disabled, config.Status);
|
|
await _notifier
|
|
.DidNotReceive()
|
|
.NotifyConfigStatusChangedAsync(
|
|
Arg.Any<Guid>(),
|
|
Arg.Any<Guid>(),
|
|
Arg.Any<ConfigStatus>(),
|
|
Arg.Any<CancellationToken>()
|
|
);
|
|
}
|
|
}
|