Enhance node update functionality to include base address
CI / Backend (build + test) (push) Successful in 1m25s
CI / Frontend (lint + typecheck + build) (push) Successful in 33s

- Added `BaseAddress` property to `UpdateNodeCommand` and `UpdateNodeBody` for improved node management.
- Implemented validation for the base address in `UpdateNodeCommandHandler`, ensuring it is a valid absolute URI.
- Updated `Node` class to support address updates, including logic to invalidate cached clients on address changes.
- Enhanced frontend components to handle base address input in the node editing dialog and API requests.
- Updated validation rules to enforce base address requirements in `UpdateNodeCommandValidator`.
This commit is contained in:
Leonid Pershin
2026-07-23 01:05:02 +03:00
parent 6b449dbfb5
commit ee6b8a35b8
10 changed files with 270 additions and 10 deletions
@@ -0,0 +1,212 @@
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.Nodes;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Nodes;
public class UpdateNodeCommandHandlerTests
{
private readonly IXuiPanelGateway _gateway = Substitute.For<IXuiPanelGateway>();
private readonly ISecretProtector _secretProtector = Substitute.For<ISecretProtector>();
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
private static Node SeedNode(
string baseAddress = "https://node1.example.com",
string username = "admin",
string protectedPassword = "protected-old-password"
) => Node.Register("node-1", new Uri(baseAddress), new NodeCredentials(username, protectedPassword), null);
[Fact]
public async Task Handle_WhenAddressChanged_ValidatesAndUpdatesAddressAndInvalidatesClient()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
_gateway.ValidateBaseAddress(Arg.Any<Uri>()).Returns(Result.Success());
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
node.Id,
"node-1",
"https://node1-new.example.com",
null,
true,
null,
null
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal("https://node1-new.example.com/", node.BaseAddress.ToString());
_gateway.Received(1).ValidateBaseAddress(Arg.Is<Uri>(u => u!.Host == "node1-new.example.com"));
_gateway.Received(1).InvalidateClient(node.Id);
}
[Fact]
public async Task Handle_WhenAddressUnchanged_DoesNotValidateOrInvalidateClient()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
node.Id,
"node-1-renamed",
"https://node1.example.com",
"eu",
true,
null,
null
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal("node-1-renamed", node.Name);
_gateway.DidNotReceive().ValidateBaseAddress(Arg.Any<Uri>());
_gateway.DidNotReceive().InvalidateClient(Arg.Any<Guid>());
}
[Fact]
public async Task Handle_WithInvalidBaseAddress_ReturnsValidationErrorWithoutChangingNode()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(node.Id, "node-1", "not-a-uri", null, true, null, null);
var result = await handler.Handle(command, CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(NodeErrors.InvalidBaseAddress, result.Error);
Assert.Equal("https://node1.example.com/", node.BaseAddress.ToString());
_gateway.DidNotReceive().ValidateBaseAddress(Arg.Any<Uri>());
}
[Fact]
public async Task Handle_WhenGatewayRejectsNewBaseAddress_ReturnsFailureWithoutChangingNode()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
var error = Error.Validation("Nodes.SchemeNotAllowed", "Разрешён только HTTPS.");
_gateway.ValidateBaseAddress(Arg.Any<Uri>()).Returns(Result.Failure(error));
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
node.Id,
"node-1",
"http://node1-new.example.com",
null,
true,
null,
null
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(error, result.Error);
Assert.Equal("https://node1.example.com/", node.BaseAddress.ToString());
_gateway.DidNotReceive().InvalidateClient(Arg.Any<Guid>());
}
[Fact]
public async Task Handle_WhenUsernameAndPasswordProvided_UpdatesCredentialsAndInvalidatesClient()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
_secretProtector.Protect("new-password").Returns("protected-new-password");
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
node.Id,
"node-1",
"https://node1.example.com",
null,
true,
"new-admin",
"new-password"
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal("new-admin", node.Credentials.Username);
Assert.Equal("protected-new-password", node.Credentials.ProtectedPassword);
_gateway.Received(1).InvalidateClient(node.Id);
}
[Fact]
public async Task Handle_WhenOnlyUsernameProvided_DoesNotChangeCredentials()
{
using var dbContext = InMemoryDbContextFactory.Create();
var node = SeedNode();
dbContext.Nodes.Add(node);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
node.Id,
"node-1",
"https://node1.example.com",
null,
true,
"new-admin",
null
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal("admin", node.Credentials.Username);
Assert.Equal("protected-old-password", node.Credentials.ProtectedPassword);
_gateway.DidNotReceive().InvalidateClient(Arg.Any<Guid>());
}
[Fact]
public async Task Handle_WhenNodeNotFound_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var handler = new UpdateNodeCommandHandler(dbContext, _gateway, _secretProtector, _currentUser);
var command = new UpdateNodeCommand(
Guid.NewGuid(),
"node-1",
"https://node1.example.com",
null,
true,
null,
null
);
var result = await handler.Handle(command, CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(NodeErrors.NotFound, result.Error);
}
}