513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
99 lines
4.1 KiB
C#
99 lines
4.1 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
|
|
using ZB.MOM.WW.ScadaBridge.Security;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Auth;
|
|
|
|
/// <summary>
|
|
/// Regression tests for the shared claim lookups. Ten components each copy-pasted a
|
|
/// <c>GetCurrentUserAsync</c> helper using the magic string
|
|
/// <c>FindFirst("Username")</c>, and <c>NavMenu</c>/<c>Dashboard</c> used
|
|
/// <c>FindFirst("DisplayName")</c>. A rename of the claim type in
|
|
/// <see cref="JwtTokenService"/> (the single source of truth) would have
|
|
/// silently broken every call site. The shared
|
|
/// <see cref="ClaimsPrincipalExtensions"/> helpers now resolve the claim type
|
|
/// through the <c>JwtTokenService</c> constants.
|
|
/// </summary>
|
|
public class ClaimsPrincipalExtensionsTests
|
|
{
|
|
private static ClaimsPrincipal Principal(params Claim[] claims)
|
|
=> new(new ClaimsIdentity(claims, authenticationType: "TestCookie"));
|
|
|
|
/// <summary>Verifies that the username lookup resolves the claim the JWT token service issues.</summary>
|
|
[Fact]
|
|
public void GetUsername_ResolvesTheJwtTokenServiceUsernameClaim()
|
|
{
|
|
var principal = Principal(
|
|
new Claim(JwtTokenService.UsernameClaimType, "alice"));
|
|
|
|
Assert.Equal("alice", principal.GetUsername());
|
|
}
|
|
|
|
/// <summary>Verifies that the username lookup falls back to unknown when the claim is absent.</summary>
|
|
[Fact]
|
|
public void GetUsername_FallsBackToUnknown_WhenClaimAbsent()
|
|
{
|
|
var principal = Principal();
|
|
|
|
Assert.Equal(ClaimsPrincipalExtensions.UnknownUser, principal.GetUsername());
|
|
}
|
|
|
|
/// <summary>Verifies that the display-name lookup resolves the claim the JWT token service issues.</summary>
|
|
[Fact]
|
|
public void GetDisplayName_ResolvesTheJwtTokenServiceDisplayNameClaim()
|
|
{
|
|
var principal = Principal(
|
|
new Claim(JwtTokenService.DisplayNameClaimType, "Alice Anderson"));
|
|
|
|
Assert.Equal("Alice Anderson", principal.GetDisplayName());
|
|
}
|
|
|
|
/// <summary>Verifies that the display-name lookup returns null when the claim is absent.</summary>
|
|
[Fact]
|
|
public void GetDisplayName_IsNull_WhenClaimAbsent()
|
|
{
|
|
Assert.Null(Principal().GetDisplayName());
|
|
}
|
|
|
|
/// <summary>Verifies that the current-username lookup reads the username from the authentication state.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task GetCurrentUsernameAsync_ReadsUsernameFromAuthState()
|
|
{
|
|
var principal = Principal(
|
|
new Claim(JwtTokenService.UsernameClaimType, "bob"));
|
|
var provider = new StubAuthStateProvider(
|
|
new AuthenticationState(principal));
|
|
|
|
Assert.Equal("bob", await provider.GetCurrentUsernameAsync());
|
|
}
|
|
|
|
/// <summary>Verifies that the username lookup follows a rename of the claim type in the JWT token service.</summary>
|
|
[Fact]
|
|
public void Username_LookupTracksAJwtTokenServiceRename()
|
|
{
|
|
// The lookup must NOT use a hard-coded "Username" literal: if the
|
|
// constant's *value* is ever changed, the helper must follow it. Build a
|
|
// principal whose claim carries the JwtTokenService constant's current
|
|
// value and confirm the helper finds it via that same constant.
|
|
var principal = Principal(
|
|
new Claim(JwtTokenService.UsernameClaimType, "carol"));
|
|
|
|
Assert.Equal("carol",
|
|
principal.FindFirst(JwtTokenService.UsernameClaimType)?.Value);
|
|
Assert.Equal("carol", principal.GetUsername());
|
|
}
|
|
|
|
private sealed class StubAuthStateProvider : AuthenticationStateProvider
|
|
{
|
|
private readonly AuthenticationState _state;
|
|
/// <summary>Initializes the provider with a fixed authentication state.</summary>
|
|
/// <param name="state">The authentication state to return.</param>
|
|
public StubAuthStateProvider(AuthenticationState state) => _state = state;
|
|
/// <inheritdoc />
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
=> Task.FromResult(_state);
|
|
}
|
|
}
|