using System.Security.Claims; using Bunit; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Security; using ZB.MOM.WW.ScadaBridge.Security; using ApiKeyForm = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Admin.ApiKeyForm; namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Admin; /// /// Bundle D drill-in test (#23 M7-T12) for the API Keys edit page, re-wired for the /// inbound-API-key re-arch (C3) onto the seam. The chip /// routes operators into the central Audit Log pre-filtered by Actor = key Name AND /// Channel = ApiInbound (no other channel uses the key name as actor, but the explicit /// channel scope keeps deep links tight). Create mode suppresses the link — there's no API /// key to drill into yet. Also covers the new seam-driven list and one-time-token panel. /// public class ApiKeyFormAuditDrillinTests : BunitContext { private readonly IInboundApiKeyAdmin _admin = Substitute.For(); private readonly IInboundApiRepository _repo = Substitute.For(); public ApiKeyFormAuditDrillinTests() { Services.AddSingleton(_admin); Services.AddSingleton(_repo); // Methods still come from the SQL Server repository; default to none unless a test overrides. _repo.GetAllApiMethodsAsync(Arg.Any()) .Returns(Task.FromResult>(new List())); var claims = new[] { new Claim(JwtTokenService.UsernameClaimType, "admin"), new Claim(JwtTokenService.RoleClaimType, "Administrator"), }; var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth")); Services.AddSingleton(new TestAuthStateProvider(user)); Services.AddAuthorizationCore(); AuthorizationPolicies.AddScadaBridgeAuthorization(Services); } private static InboundApiKeyInfo Key( string keyId, string name, bool enabled = true, params string[] methods) => new(keyId, name, enabled, methods, DateTimeOffset.UnixEpoch, null); [Fact] public void EditPage_HasRecentAuditActivityLink_WithActorAndApiInboundChannel() { const string keyId = "abc123def456"; var info = Key(keyId, "Orders-Integration", true, "PlaceOrder"); _admin.ListAsync(Arg.Any()) .Returns(Task.FromResult>(new[] { info })); _admin.GetMethodsForKeyAsync(keyId, Arg.Any()) .Returns(Task.FromResult>(new[] { "PlaceOrder" })); var cut = Render(p => p.Add(c => c.KeyId, keyId)); cut.WaitForAssertion(() => { var link = cut.Find("a[data-test=\"audit-link\"]"); Assert.Equal( "/audit/log?actor=Orders-Integration&channel=ApiInbound", link.GetAttribute("href")); Assert.Contains("Recent audit activity", link.TextContent); }); } [Fact] public void CreatePage_HasNoRecentAuditActivityLink() { _admin.ListAsync(Arg.Any()) .Returns(Task.FromResult>(Array.Empty())); var cut = Render(); cut.WaitForAssertion(() => { Assert.Empty(cut.FindAll("a[data-test=\"audit-link\"]")); }); } [Fact] public async Task CreatePage_WithMethodSelected_ShowsOneTimeTokenAndKeyId() { _admin.ListAsync(Arg.Any()) .Returns(Task.FromResult>(Array.Empty())); _repo.GetAllApiMethodsAsync(Arg.Any()) .Returns(Task.FromResult>( new List { new("PlaceOrder", "return null;") { Id = 1 } })); _admin.CreateAsync("MES-Production", Arg.Any>(), Arg.Any()) .Returns(Task.FromResult(new InboundApiKeyCreated("new-key-id", "sbk_new-key-id_secret"))); var cut = Render(); // Fill name, check the only method, save. cut.WaitForElement("input[type=text]").Change("MES-Production"); cut.Find("#method-access-1").Change(true); await cut.Find("button.btn-success").ClickAsync(new()); cut.WaitForAssertion(() => { var token = cut.Find("[data-test=\"created-token\"]"); Assert.Contains("sbk_new-key-id_secret", token.TextContent); Assert.Contains("new-key-id", cut.Markup); Assert.Contains("will not be shown again", cut.Markup); }); await _admin.Received(1).CreateAsync( "MES-Production", Arg.Is>(m => m.Count == 1 && m.Contains("PlaceOrder")), Arg.Any()); } [Fact] public async Task CreatePage_WithNoMethodSelected_ShowsValidationError_AndDoesNotCreate() { _admin.ListAsync(Arg.Any()) .Returns(Task.FromResult>(Array.Empty())); _repo.GetAllApiMethodsAsync(Arg.Any()) .Returns(Task.FromResult>( new List { new("PlaceOrder", "return null;") { Id = 1 } })); var cut = Render(); cut.WaitForElement("input[type=text]").Change("MES-Production"); await cut.Find("button.btn-success").ClickAsync(new()); cut.WaitForAssertion(() => Assert.Contains("at least one API method", cut.Markup)); await _admin.DidNotReceive().CreateAsync( Arg.Any(), Arg.Any>(), Arg.Any()); } }