Heimdall Gitea / Detect CI scope (pull_request) Successful in 8s
Heimdall Gitea / Build and tests (pull_request) Failing after 15m39s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 5s
71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using Heimdall.Web.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.JSInterop;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
public sealed class AuthHelperTests
|
|
{
|
|
[Theory]
|
|
[InlineData(200, true)]
|
|
[InlineData(204, true)]
|
|
[InlineData(401, false)]
|
|
[InlineData(500, false)]
|
|
[InlineData(0, false)]
|
|
public async Task SignInAsync_UsesOnlyTheReturnedHttpStatus(int statusCode, bool expected)
|
|
{
|
|
var jsRuntime = new StubJsRuntime(statusCode);
|
|
|
|
var result = await AuthHelper.SignInAsync(jsRuntime, new RecordingLogger(), new { accessToken = "synthetic" });
|
|
|
|
result.ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SignInAsync_LogsInteropFailureTypeWithoutExceptionMessage()
|
|
{
|
|
const string privateDetail = "synthetic-private-detail";
|
|
var jsRuntime = new StubJsRuntime(new JSException(privateDetail));
|
|
var logger = new RecordingLogger();
|
|
|
|
(await AuthHelper.SignInAsync(jsRuntime, logger, new { accessToken = "synthetic" })).ShouldBeFalse();
|
|
|
|
var logged = string.Join(" ", logger.Messages);
|
|
logged.ShouldContain(nameof(JSException));
|
|
logged.ShouldNotContain(privateDetail);
|
|
}
|
|
|
|
private sealed class RecordingLogger : ILogger
|
|
{
|
|
public List<string> Messages { get; } = [];
|
|
|
|
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
|
Func<TState, Exception?, string> formatter)
|
|
=> Messages.Add(formatter(state, exception));
|
|
}
|
|
|
|
private sealed class StubJsRuntime : IJSRuntime
|
|
{
|
|
private readonly int _statusCode;
|
|
private readonly Exception? _exception;
|
|
|
|
public StubJsRuntime(int statusCode) => _statusCode = statusCode;
|
|
public StubJsRuntime(Exception exception) => _exception = exception;
|
|
|
|
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object?[]? args)
|
|
=> InvokeAsync<TValue>(identifier, CancellationToken.None, args);
|
|
|
|
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object?[]? args)
|
|
{
|
|
if (_exception is not null)
|
|
return ValueTask.FromException<TValue>(_exception);
|
|
|
|
return ValueTask.FromResult((TValue)(object)_statusCode);
|
|
}
|
|
}
|
|
}
|