Add IsAvailable property to Inbound and update related logic
- Introduced a new boolean property, `IsAvailable`, to the `Inbound` entity to track the availability status of inbounds based on synchronization results. - Updated the `SyncNodeCommandHandler` to mark inbounds as unavailable if they are not present in the latest synchronization but have existing configurations, preventing their deletion. - Enhanced the `MarkUnavailable` method to set both `IsAvailable` and `IsPublished` to false, reflecting the new status accurately. - Modified the frontend components to display the availability status of inbounds, ensuring users are informed of their current state. - Updated tests to cover the new behavior regarding inbound availability and its impact on revocation processes.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Admin.Nodes;
|
||||
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.Nodes;
|
||||
|
||||
public class SyncNodeCommandHandlerTests
|
||||
{
|
||||
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
|
||||
|
||||
private static Node CreateNode() =>
|
||||
Node.Register(
|
||||
"node-1",
|
||||
new Uri("https://node1.example.com"),
|
||||
new NodeCredentials("admin", "protected"),
|
||||
null
|
||||
);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenInboundGoneFromPanelAndHasNoConfigs_RemovesIt()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var node = CreateNode();
|
||||
var inbound = Inbound.FromRemote(node.Id, "old-id", VpnProtocol.Vless, "old", 443);
|
||||
|
||||
dbContext.Nodes.Add(node);
|
||||
dbContext.Inbounds.Add(inbound);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_gateway
|
||||
.ListInboundsAsync(Arg.Any<Node>(), Arg.Any<CancellationToken>())
|
||||
.Returns(
|
||||
Result.Success<IReadOnlyList<RemoteInboundInfo>>(
|
||||
[new RemoteInboundInfo("new-id", VpnProtocol.Vless, "new", 8443)]
|
||||
)
|
||||
);
|
||||
|
||||
var handler = new SyncNodeCommandHandler(dbContext, _gateway);
|
||||
var result = await handler.Handle(new SyncNodeCommand(node.Id), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.DoesNotContain(dbContext.Inbounds.Local, i => i.RemoteInboundId == "old-id");
|
||||
Assert.Contains(dbContext.Inbounds.Local, i => i.RemoteInboundId == "new-id");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenInboundGoneFromPanelButHasConfigs_MarksUnavailableInsteadOfRemoving()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var node = CreateNode();
|
||||
var inbound = Inbound.FromRemote(node.Id, "old-id", VpnProtocol.Vless, "old", 443);
|
||||
inbound.Publish("Old", [Guid.NewGuid()]);
|
||||
var config = VpnConfig.Create(Guid.NewGuid(), inbound.Id, VpnProtocol.Vless, null);
|
||||
config.AssignRemoteClient("external-id");
|
||||
|
||||
dbContext.Nodes.Add(node);
|
||||
dbContext.Inbounds.Add(inbound);
|
||||
dbContext.VpnConfigs.Add(config);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_gateway
|
||||
.ListInboundsAsync(Arg.Any<Node>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success<IReadOnlyList<RemoteInboundInfo>>([]));
|
||||
|
||||
var handler = new SyncNodeCommandHandler(dbContext, _gateway);
|
||||
var result = await handler.Handle(new SyncNodeCommand(node.Id), CancellationToken.None);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var stored = Assert.Single(dbContext.Inbounds.Local, i => i.RemoteInboundId == "old-id");
|
||||
Assert.False(stored.IsAvailable);
|
||||
Assert.False(stored.IsPublished);
|
||||
}
|
||||
}
|
||||
+48
@@ -85,6 +85,54 @@ public class RevokeVpnConfigCommandHandlerTests
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenInboundIsUnavailable_RevokesLocallyWithoutCallingGateway()
|
||||
{
|
||||
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 inbound = Inbound.FromRemote(node.Id, "1", VpnProtocol.Vless, "remark", 443);
|
||||
inbound.MarkUnavailable();
|
||||
var config = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, null);
|
||||
config.AssignRemoteClient("external-id");
|
||||
|
||||
dbContext.Nodes.Add(node);
|
||||
dbContext.Inbounds.Add(inbound);
|
||||
dbContext.VpnConfigs.Add(config);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
var handler = new RevokeVpnConfigCommandHandler(
|
||||
dbContext,
|
||||
_gateway,
|
||||
_notifier,
|
||||
FakeCurrentUser.Authenticated(userId),
|
||||
_logger
|
||||
);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new RevokeVpnConfigCommand(config.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(ConfigStatus.Revoked, config.Status);
|
||||
await _gateway
|
||||
.DidNotReceive()
|
||||
.RemoveClientAsync(
|
||||
Arg.Any<Node>(),
|
||||
Arg.Any<string>(),
|
||||
Arg.Any<string>(),
|
||||
Arg.Any<VpnProtocol>(),
|
||||
Arg.Any<CancellationToken>()
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenAlreadyRevoked_IsIdempotentAndDoesNotCallGateway()
|
||||
{
|
||||
|
||||
@@ -60,4 +60,24 @@ public class InboundTests
|
||||
Assert.False(inbound.IsPublished);
|
||||
Assert.Contains(roleId, inbound.AllowedRoleIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromRemote_CreatesAvailableInbound()
|
||||
{
|
||||
var inbound = Inbound.FromRemote(Guid.NewGuid(), "12", VpnProtocol.Vless, "Germany", 443);
|
||||
|
||||
Assert.True(inbound.IsAvailable);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MarkUnavailable_SetsIsAvailableAndIsPublishedFalse()
|
||||
{
|
||||
var inbound = Inbound.FromRemote(Guid.NewGuid(), "12", VpnProtocol.Vless, "Germany", 443);
|
||||
inbound.Publish("Germany", [Guid.NewGuid()]);
|
||||
|
||||
inbound.MarkUnavailable();
|
||||
|
||||
Assert.False(inbound.IsAvailable);
|
||||
Assert.False(inbound.IsPublished);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user