Refactor project files for improved readability and structure
CI / Backend (build + test) (push) Successful in 1m18s
CI / Frontend (lint + typecheck + build) (push) Successful in 31s

- 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.
This commit is contained in:
Leonid Pershin
2026-07-14 07:24:13 +03:00
parent 9d5424bb9c
commit df137ca5a7
285 changed files with 6911 additions and 2063 deletions
@@ -19,7 +19,12 @@ public class ListAllConfigsQueryHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var node = Node.Register("node-1", new Uri("https://node1.example.com"), new NodeCredentials("admin", "protected"), null);
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);
inbound.Publish("Germany", [], null);
var config = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "my-config");
@@ -29,12 +34,16 @@ public class ListAllConfigsQueryHandlerTests
dbContext.VpnConfigs.Add(config);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(new ListAllConfigsQuery(1, 20, Search: null, Status: null), CancellationToken.None);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: null),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
@@ -52,7 +61,12 @@ public class ListAllConfigsQueryHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var node = Node.Register("node-1", new Uri("https://node1.example.com"), new NodeCredentials("admin", "protected"), null);
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 matching = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "phone-config");
var other = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "laptop-config");
@@ -62,12 +76,16 @@ public class ListAllConfigsQueryHandlerTests
dbContext.VpnConfigs.AddRange(matching, other);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(new ListAllConfigsQuery(1, 20, Search: "phone", Status: null), CancellationToken.None);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: "phone", Status: null),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
@@ -80,7 +98,12 @@ public class ListAllConfigsQueryHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var node = Node.Register("node-1", new Uri("https://node1.example.com"), new NodeCredentials("admin", "protected"), null);
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 active = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "active-config");
var revoked = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "revoked-config");
@@ -91,12 +114,16 @@ public class ListAllConfigsQueryHandlerTests
dbContext.VpnConfigs.AddRange(active, revoked);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(new ListAllConfigsQuery(1, 20, Search: null, Status: ConfigStatus.Revoked), CancellationToken.None);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: ConfigStatus.Revoked),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
@@ -21,9 +21,20 @@ public class RegisterNodeCommandHandlerTests
_gateway.ValidateBaseAddress(Arg.Any<Uri>()).Returns(Result.Success());
_secretProtector.Protect("secret-password").Returns("protected-secret-password");
var handler = new RegisterNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var handler = new RegisterNodeCommandHandler(
dbContext,
_gateway,
_secretProtector,
_currentUser
);
var command = new RegisterNodeCommand("node-1", "https://node1.example.com", "admin", "secret-password", "eu-west");
var command = new RegisterNodeCommand(
"node-1",
"https://node1.example.com",
"admin",
"secret-password",
"eu-west"
);
var result = await handler.Handle(command, CancellationToken.None);
@@ -31,7 +42,10 @@ public class RegisterNodeCommandHandlerTests
Assert.Equal("node-1", result.Value.Name);
Assert.Equal("admin", result.Value.Username);
Assert.Single(dbContext.Nodes.Local);
Assert.Equal("protected-secret-password", dbContext.Nodes.Local.Single().Credentials.ProtectedPassword);
Assert.Equal(
"protected-secret-password",
dbContext.Nodes.Local.Single().Credentials.ProtectedPassword
);
}
[Fact]
@@ -39,9 +53,20 @@ public class RegisterNodeCommandHandlerTests
{
using var dbContext = InMemoryDbContextFactory.Create();
var handler = new RegisterNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var handler = new RegisterNodeCommandHandler(
dbContext,
_gateway,
_secretProtector,
_currentUser
);
var command = new RegisterNodeCommand("node-1", "not-a-uri", "admin", "secret-password", null);
var command = new RegisterNodeCommand(
"node-1",
"not-a-uri",
"admin",
"secret-password",
null
);
var result = await handler.Handle(command, CancellationToken.None);
@@ -59,9 +84,20 @@ public class RegisterNodeCommandHandlerTests
var error = Error.Validation("Nodes.SchemeNotAllowed", "Разрешён только HTTPS.");
_gateway.ValidateBaseAddress(Arg.Any<Uri>()).Returns(Result.Failure(error));
var handler = new RegisterNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var handler = new RegisterNodeCommandHandler(
dbContext,
_gateway,
_secretProtector,
_currentUser
);
var command = new RegisterNodeCommand("node-1", "http://node1.example.com", "admin", "secret-password", null);
var command = new RegisterNodeCommand(
"node-1",
"http://node1.example.com",
"admin",
"secret-password",
null
);
var result = await handler.Handle(command, CancellationToken.None);
@@ -25,20 +25,38 @@ public class ApproveRoleRequestCommandHandlerTests
await dbContext.SaveChangesAsync(CancellationToken.None);
var newRoleId = Guid.NewGuid();
_roleService.CreateRoleAsync("premium", 10, 5, Arg.Any<CancellationToken>())
_roleService
.CreateRoleAsync("premium", 10, 5, Arg.Any<CancellationToken>())
.Returns(Result.Success(new RoleDto(newRoleId, "premium", 10, 5, false)));
_roleService.ChangeUserRoleAsync(userId, newRoleId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_roleService
.ChangeUserRoleAsync(userId, newRoleId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
var handler = new ApproveRoleRequestCommandHandler(dbContext, _roleService, _notifier, _telegramNotifier, currentUser);
var handler = new ApproveRoleRequestCommandHandler(
dbContext,
_roleService,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(new ApproveRoleRequestCommand(ticket.Id), CancellationToken.None);
var result = await handler.Handle(
new ApproveRoleRequestCommand(ticket.Id),
CancellationToken.None
);
Assert.True(result.IsSuccess);
Assert.Equal(TicketStatus.Resolved, ticket.Status);
await _roleService.Received(1).CreateRoleAsync("premium", 10, 5, Arg.Any<CancellationToken>());
await _roleService.Received(1).ChangeUserRoleAsync(userId, newRoleId, Arg.Any<CancellationToken>());
await _telegramNotifier.Received(1).NotifyUserAsync(userId, Arg.Any<string>(), Arg.Any<CancellationToken>());
await _roleService
.Received(1)
.CreateRoleAsync("premium", 10, 5, Arg.Any<CancellationToken>());
await _roleService
.Received(1)
.ChangeUserRoleAsync(userId, newRoleId, Arg.Any<CancellationToken>());
await _telegramNotifier
.Received(1)
.NotifyUserAsync(userId, Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
@@ -51,16 +69,33 @@ public class ApproveRoleRequestCommandHandlerTests
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync(CancellationToken.None);
_roleService.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_roleService
.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid());
var handler = new ApproveRoleRequestCommandHandler(dbContext, _roleService, _notifier, _telegramNotifier, currentUser);
var handler = new ApproveRoleRequestCommandHandler(
dbContext,
_roleService,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(new ApproveRoleRequestCommand(ticket.Id), CancellationToken.None);
var result = await handler.Handle(
new ApproveRoleRequestCommand(ticket.Id),
CancellationToken.None
);
Assert.True(result.IsSuccess);
await _roleService.DidNotReceive().CreateRoleAsync(
Arg.Any<string>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>());
await _roleService
.DidNotReceive()
.CreateRoleAsync(
Arg.Any<string>(),
Arg.Any<int>(),
Arg.Any<int>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
@@ -72,9 +107,18 @@ public class ApproveRoleRequestCommandHandlerTests
await dbContext.SaveChangesAsync(CancellationToken.None);
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid());
var handler = new ApproveRoleRequestCommandHandler(dbContext, _roleService, _notifier, _telegramNotifier, currentUser);
var handler = new ApproveRoleRequestCommandHandler(
dbContext,
_roleService,
_notifier,
_telegramNotifier,
currentUser
);
var result = await handler.Handle(new ApproveRoleRequestCommand(ticket.Id), CancellationToken.None);
var result = await handler.Handle(
new ApproveRoleRequestCommand(ticket.Id),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(SupportErrors.NotRoleRequest, result.Error);
@@ -18,7 +18,9 @@ public class BlockUserCommandHandlerTests
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
private readonly ILogger<BlockUserCommandHandler> _logger = Substitute.For<ILogger<BlockUserCommandHandler>>();
private readonly ILogger<BlockUserCommandHandler> _logger = Substitute.For<
ILogger<BlockUserCommandHandler>
>();
[Fact]
public async Task Handle_IdentityServiceFails_ReturnsFailureWithoutTouchingConfigs()
@@ -26,17 +28,35 @@ public class BlockUserCommandHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var failure = UserErrors.NotFound;
_identityService.BlockUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Failure(failure));
_identityService
.BlockUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Failure(failure));
var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger);
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<Node>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<VpnProtocol>(),
Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
await _gateway
.DidNotReceive()
.UpdateClientAsync(
Arg.Any<Node>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<VpnProtocol>(),
Arg.Any<string>(),
Arg.Any<bool>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
@@ -46,7 +66,12 @@ public class BlockUserCommandHandlerTests
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 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");
@@ -55,24 +80,56 @@ public class BlockUserCommandHandlerTests
dbContext.VpnConfigs.Add(config);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.BlockUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_identityService
.BlockUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
_currentUser.UserId.Returns(adminId);
_gateway.UpdateClientAsync(
Arg.Any<Node>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<VpnProtocol>(),
Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
_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 BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger);
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<Node>(n => n.Id == node.Id), inbound.RemoteInboundId, config.ClientExternalId, config.Protocol,
"my-config", enable: false, Arg.Any<CancellationToken>());
await _notifier.Received(1).NotifyConfigStatusChangedAsync(userId, config.Id, ConfigStatus.Disabled, Arg.Any<CancellationToken>());
await _gateway
.Received(1)
.UpdateClientAsync(
Arg.Is<Node>(n => n.Id == node.Id),
inbound.RemoteInboundId,
config.ClientExternalId,
config.Protocol,
"my-config",
enable: false,
Arg.Any<CancellationToken>()
);
await _notifier
.Received(1)
.NotifyConfigStatusChangedAsync(
userId,
config.Id,
ConfigStatus.Disabled,
Arg.Any<CancellationToken>()
);
var audit = Assert.Single(dbContext.AuditLogs.Local);
Assert.Equal("UserBlocked", audit.Action);
@@ -85,17 +142,35 @@ public class BlockUserCommandHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
_identityService.BlockUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_identityService
.BlockUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
_currentUser.UserId.Returns(Guid.NewGuid());
var handler = new BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger);
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<Node>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<VpnProtocol>(),
Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
await _gateway
.DidNotReceive()
.UpdateClientAsync(
Arg.Any<Node>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<VpnProtocol>(),
Arg.Any<string>(),
Arg.Any<bool>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
@@ -105,7 +180,12 @@ public class BlockUserCommandHandlerTests
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 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");
@@ -114,20 +194,43 @@ public class BlockUserCommandHandlerTests
dbContext.VpnConfigs.Add(config);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.BlockUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_identityService
.BlockUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
_currentUser.UserId.Returns(adminId);
_gateway.UpdateClientAsync(
Arg.Any<Node>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<VpnProtocol>(),
Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
_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 BlockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _telegramNotifier, _currentUser, _logger);
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<Guid>(), Arg.Any<Guid>(), Arg.Any<ConfigStatus>(), Arg.Any<CancellationToken>());
await _notifier
.DidNotReceive()
.NotifyConfigStatusChangedAsync(
Arg.Any<Guid>(),
Arg.Any<Guid>(),
Arg.Any<ConfigStatus>(),
Arg.Any<CancellationToken>()
);
}
}
@@ -19,14 +19,21 @@ public class ChangeUserRoleCommandHandlerTests
var userId = Guid.NewGuid();
var roleId = Guid.NewGuid();
_roleService.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_roleService
.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
var handler = new ChangeUserRoleCommandHandler(_roleService, dbContext, _currentUser);
var result = await handler.Handle(new ChangeUserRoleCommand(userId, roleId), CancellationToken.None);
var result = await handler.Handle(
new ChangeUserRoleCommand(userId, roleId),
CancellationToken.None
);
Assert.True(result.IsSuccess);
await _roleService.Received(1).ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>());
await _roleService
.Received(1)
.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>());
Assert.Single(dbContext.AuditLogs.Local);
}
@@ -38,11 +45,16 @@ public class ChangeUserRoleCommandHandlerTests
var userId = Guid.NewGuid();
var roleId = Guid.NewGuid();
var error = UserErrors.NotFound;
_roleService.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>()).Returns(Result.Failure(error));
_roleService
.ChangeUserRoleAsync(userId, roleId, Arg.Any<CancellationToken>())
.Returns(Result.Failure(error));
var handler = new ChangeUserRoleCommandHandler(_roleService, dbContext, _currentUser);
var result = await handler.Handle(new ChangeUserRoleCommand(userId, roleId), CancellationToken.None);
var result = await handler.Handle(
new ChangeUserRoleCommand(userId, roleId),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Equal(error, result.Error);
@@ -24,13 +24,21 @@ public class DeleteUserCommandHandlerTests
var adminId = Guid.NewGuid();
_currentUser.UserId.Returns(adminId);
var handler = new DeleteUserCommandHandler(dbContext, _identityService, _gateway, _telegramNotifier, _currentUser);
var handler = new DeleteUserCommandHandler(
dbContext,
_identityService,
_gateway,
_telegramNotifier,
_currentUser
);
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>());
await _identityService
.DidNotReceive()
.DeleteUserAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
@@ -40,7 +48,12 @@ public class DeleteUserCommandHandlerTests
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 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");
@@ -51,20 +64,44 @@ public class DeleteUserCommandHandlerTests
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>())
_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());
_identityService.DeleteUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Success());
var handler = new DeleteUserCommandHandler(dbContext, _identityService, _gateway, _telegramNotifier, _currentUser);
var handler = new DeleteUserCommandHandler(
dbContext,
_identityService,
_gateway,
_telegramNotifier,
_currentUser
);
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<CancellationToken>());
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<CancellationToken>());
await _identityService.Received(1).DeleteUserAsync(userId, Arg.Any<CancellationToken>());
var audit = Assert.Single(dbContext.AuditLogs.Local);
@@ -79,15 +116,30 @@ public class DeleteUserCommandHandlerTests
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
_currentUser.UserId.Returns(Guid.NewGuid());
_identityService.DeleteUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Success());
_identityService
.DeleteUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Success());
var handler = new DeleteUserCommandHandler(dbContext, _identityService, _gateway, _telegramNotifier, _currentUser);
var handler = new DeleteUserCommandHandler(
dbContext,
_identityService,
_gateway,
_telegramNotifier,
_currentUser
);
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>());
await _gateway
.DidNotReceive()
.RemoveClientAsync(
Arg.Any<Node>(),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<VpnProtocol>(),
Arg.Any<CancellationToken>()
);
}
[Fact]
@@ -96,9 +148,17 @@ public class DeleteUserCommandHandlerTests
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));
_identityService
.DeleteUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Failure(UserErrors.NotFound));
var handler = new DeleteUserCommandHandler(dbContext, _identityService, _gateway, _telegramNotifier, _currentUser);
var handler = new DeleteUserCommandHandler(
dbContext,
_identityService,
_gateway,
_telegramNotifier,
_currentUser
);
var result = await handler.Handle(new DeleteUserCommand(userId), CancellationToken.None);
@@ -17,7 +17,9 @@ public class UnblockUserCommandHandlerTests
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>>();
private readonly ILogger<UnblockUserCommandHandler> _logger = Substitute.For<
ILogger<UnblockUserCommandHandler>
>();
[Fact]
public async Task Handle_WhenUnblockSucceeds_ReEnablesDisabledConfigsAndWritesAudit()
@@ -26,7 +28,12 @@ public class UnblockUserCommandHandlerTests
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 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();
@@ -37,22 +44,53 @@ public class UnblockUserCommandHandlerTests
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>())
_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 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>());
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);
}
@@ -64,9 +102,18 @@ public class UnblockUserCommandHandlerTests
var userId = Guid.NewGuid();
var error = UserErrors.NotFound;
_identityService.UnblockUserAsync(userId, Arg.Any<CancellationToken>()).Returns(Result.Failure(error));
_identityService
.UnblockUserAsync(userId, Arg.Any<CancellationToken>())
.Returns(Result.Failure(error));
var handler = new UnblockUserCommandHandler(dbContext, _identityService, _gateway, _notifier, _currentUser, _logger);
var handler = new UnblockUserCommandHandler(
dbContext,
_identityService,
_gateway,
_notifier,
_currentUser,
_logger
);
var result = await handler.Handle(new UnblockUserCommand(userId), CancellationToken.None);
@@ -82,7 +129,12 @@ public class UnblockUserCommandHandlerTests
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 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();
@@ -93,19 +145,41 @@ public class UnblockUserCommandHandlerTests
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>())
_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 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>());
await _notifier
.DidNotReceive()
.NotifyConfigStatusChangedAsync(
Arg.Any<Guid>(),
Arg.Any<Guid>(),
Arg.Any<ConfigStatus>(),
Arg.Any<CancellationToken>()
);
}
}