Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Security.Tests/AutoLoginAuthenticationHandlerTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
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.
2026-10-07 15:05:49 -04:00

88 lines
4.0 KiB
C#

using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.Security;
using ZB.MOM.WW.ScadaBridge.Security.Auth;
using Xunit;
namespace ZB.MOM.WW.ScadaBridge.Security.Tests;
public class AutoLoginAuthenticationHandlerTests
{
/// <summary>
/// Minimal <see cref="IOptionsMonitor{T}"/> stub for the scheme options the base
/// <see cref="AuthenticationHandler{TOptions}"/> resolves during InitializeAsync. This
/// test project deliberately carries no mocking library, so the seam is hand-rolled.
/// </summary>
private sealed class StubOptionsMonitor : IOptionsMonitor<AuthenticationSchemeOptions>
{
private readonly AuthenticationSchemeOptions _value = new();
/// <summary>Gets the fixed options value.</summary>
public AuthenticationSchemeOptions CurrentValue => _value;
/// <summary>Returns the fixed options value regardless of name.</summary>
/// <param name="name">The options name, ignored.</param>
/// <returns>The fixed options value.</returns>
public AuthenticationSchemeOptions Get(string? name) => _value;
/// <summary>Ignores the change listener; the fake never raises change notifications.</summary>
/// <param name="listener">The change listener, ignored.</param>
/// <returns>Always <see langword="null"/>.</returns>
public IDisposable? OnChange(Action<AuthenticationSchemeOptions, string?> listener) => null;
}
private static async Task<AutoLoginAuthenticationHandler> CreateAsync(string user = "multi-role")
{
var schemeOptions = new StubOptionsMonitor();
var opts = Options.Create(new AuthDisableLoginOptions { DisableLogin = true, User = user });
var handler = new AutoLoginAuthenticationHandler(
schemeOptions, NullLoggerFactory.Instance, UrlEncoder.Default, opts, TimeProvider.System);
await handler.InitializeAsync(
new AuthenticationScheme(
CookieAuthenticationDefaults.AuthenticationScheme, null, typeof(AutoLoginAuthenticationHandler)),
new DefaultHttpContext());
return handler;
}
/// <summary>Verifies that authenticate, when grants all roles, system wide as configured user.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Authenticate_GrantsAllRoles_SystemWide_AsConfiguredUser()
{
var handler = await CreateAsync("multi-role");
var result = await handler.AuthenticateAsync();
Assert.True(result.Succeeded);
var p = result.Principal!;
Assert.True(p.Identity!.IsAuthenticated); // first gate checked by [Authorize] + Blazor AuthenticationStateProvider
Assert.Equal("multi-role", p.Identity!.Name);
foreach (var role in Roles.All)
Assert.True(p.IsInRole(role), $"expected role {role}");
Assert.Empty(p.FindAll(JwtTokenService.SiteIdClaimType)); // system-wide => no scope claims
}
/// <summary>Verifies that authenticate, when blank user, falls back to multi role.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Authenticate_BlankUser_FallsBackToMultiRole()
{
var handler = await CreateAsync(" ");
var result = await handler.AuthenticateAsync();
Assert.Equal("multi-role", result.Principal!.Identity!.Name);
}
/// <summary>Verifies that sign in and sign out, when are no ops, do not throw.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task SignInAndSignOut_AreNoOps_DoNotThrow()
{
var handler = await CreateAsync();
await handler.SignInAsync(new ClaimsPrincipal(), null);
await handler.SignOutAsync(null);
}
}