- 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.
80 lines
3.0 KiB
C#
80 lines
3.0 KiB
C#
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);
|
|
}
|
|
}
|