Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ApiKeysDeleteTests.cs
T
Joseph Doherty 65212dd471 fix(adminui): #1050 Galaxy ack identity gets an explicit 'Empty password' option
A blank password box means 'keep the stored password' (#997 A-4) and is refused first time,
so an account whose password is genuinely empty could not be mapped. The /apikeys editor
now has an 'Empty password' checkbox (disables + clears the box) that calls the new
IApiKeyGalaxyAckIdentityService.SetWithEmptyPasswordAsync: writes an empty password secret
first, then the user, audited like any set. The driver already accepts an empty password.
2026-12-03 15:03:27 -05:00

128 lines
5.2 KiB
C#

using Bunit;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.ApiKeyAck;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Config;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// <summary>
/// #997 A-9: deleting a key removed the row, then cleared its Galaxy ack identity, then reloaded - all in one
/// <c>try</c>. A Secrets-store failure in the middle skipped the reload (the page kept listing a key that no
/// longer exists, with live buttons) and left the ack secrets behind with no way to find them.
/// </summary>
[Trait("Category", "Unit")]
public sealed class ApiKeysDeleteTests : BunitContext
{
private const string KeyId = "0a1b2c3d4e5f6071";
public ApiKeysDeleteTests()
{
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult();
JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult();
Services.AddSingleton<AuthenticationStateProvider>(new FakeAuth());
}
private IRenderedComponent<ApiKeys> RenderPage(FakeKeys keys, FakeAck ack)
{
Services.AddSingleton<IApiKeyAdminService>(keys);
Services.AddSingleton<IApiKeyGalaxyAckIdentityService>(ack);
return Render<ApiKeys>();
}
private static void DeleteAndConfirm(IRenderedComponent<ApiKeys> cut)
{
cut.Find("table button.btn-outline-danger").Click();
cut.Find(".modal-footer .btn-danger").Click();
}
[Fact]
public void A_failing_ack_cleanup_still_reloads_the_list_and_names_the_leftover_secrets()
{
var keys = new FakeKeys();
var ack = new FakeAck { ThrowOnClear = true };
var cut = RenderPage(keys, ack);
cut.Markup.ShouldContain(KeyId);
DeleteAndConfirm(cut);
cut.WaitForAssertion(() =>
{
cut.FindAll("table").ShouldBeEmpty("the deleted key must no longer be listed");
var alert = cut.Find(".alert-danger").TextContent;
alert.ShouldContain(GalaxyAckIdentitySecrets.UserSecretName(KeyId));
alert.ShouldContain(GalaxyAckIdentitySecrets.PasswordSecretName(KeyId));
alert.ShouldContain("secret store unavailable");
});
keys.DeleteCalls.ShouldBe(1);
}
/// <summary>Positive control: with a healthy Secrets store the same flow lists nothing and shows no error.</summary>
[Fact]
public void A_clean_delete_reloads_the_list_and_shows_no_error()
{
var keys = new FakeKeys();
var ack = new FakeAck();
var cut = RenderPage(keys, ack);
DeleteAndConfirm(cut);
cut.WaitForAssertion(() => cut.FindAll("table").ShouldBeEmpty());
cut.FindAll(".alert-danger").ShouldBeEmpty();
ack.ClearCalls.ShouldBe(1);
}
private sealed class FakeKeys : IApiKeyAdminService
{
private readonly List<ApiKey> _rows =
[
new() { ApiKeyId = KeyId, Name = "ScadaBridge", SecretHash = "x", Principal = "sb", Enabled = true, CreatedUtc = DateTime.UtcNow },
];
public int DeleteCalls { get; private set; }
public Task<IReadOnlyList<ApiKey>> ListAsync(CancellationToken ct) => Task.FromResult<IReadOnlyList<ApiKey>>(_rows.ToList());
public Task<string> CreateAsync(string name, string principal, DateTime? expiresUtc, string? notes, CancellationToken ct) => throw new NotSupportedException();
public Task SetEnabledAsync(string apiKeyId, bool enabled, CancellationToken ct) => throw new NotSupportedException();
public Task DeleteAsync(string apiKeyId, CancellationToken ct)
{
DeleteCalls++;
_rows.RemoveAll(r => r.ApiKeyId == apiKeyId);
return Task.CompletedTask;
}
}
private sealed class FakeAck : IApiKeyGalaxyAckIdentityService
{
public bool ThrowOnClear { get; init; }
public int ClearCalls { get; private set; }
public Task<IReadOnlyDictionary<string, string>> ListAsync(CancellationToken ct) =>
Task.FromResult<IReadOnlyDictionary<string, string>>(new Dictionary<string, string>());
public Task SetAsync(string apiKeyId, string user, string? domain, string? password, string actor, CancellationToken ct) =>
throw new NotSupportedException();
public Task SetWithEmptyPasswordAsync(string apiKeyId, string user, string? domain, string actor, CancellationToken ct) =>
throw new NotSupportedException();
public Task ClearAsync(string apiKeyId, string actor, CancellationToken ct)
{
ClearCalls++;
return ThrowOnClear ? throw new InvalidOperationException("secret store unavailable") : Task.CompletedTask;
}
}
private sealed class FakeAuth : AuthenticationStateProvider
{
public override Task<AuthenticationState> GetAuthenticationStateAsync() =>
Task.FromResult(new AuthenticationState(new System.Security.Claims.ClaimsPrincipal(
new System.Security.Claims.ClaimsIdentity("test"))));
}
}