Implements authorization caching and check in the web client

This commit is contained in:
BinaryAlley committed 2024-12-06 18:46:48 +02:00
1 parent 7bca5b1ba3
commit 8eb1b9f13c
108 files changed
+5931 -73

No files matched your search

@@ -0,0 +1,136 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Presentation.Api.Common.Authentication;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Common.Authentication;
/// <summary>
/// Contains unit tests for the <see cref="HttpContextCurrentUserService"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class HttpContextCurrentUserServiceTests
{
private readonly IHttpContextAccessor _mockHttpContextAccessor;
private readonly HttpContextCurrentUserService _sut;
/// <summary>
/// Initializes a new instance of the <see cref="HttpContextCurrentUserServiceTests"/> class.
/// </summary>
public HttpContextCurrentUserServiceTests()
{
_mockHttpContextAccessor = Substitute.For<IHttpContextAccessor>();
_sut = new HttpContextCurrentUserService(_mockHttpContextAccessor);
}
[Fact]
public void UserId_WhenHttpContextIsNull_ShouldReturnNull()
{
// Arrange
_mockHttpContextAccessor.HttpContext.Returns((HttpContext?)null);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().BeNull();
}
[Fact]
public void UserId_WhenUserIsNull_ShouldReturnNull()
{
// Arrange
HttpContext mockHttpContext = new DefaultHttpContext();
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().BeNull();
}
[Fact]
public void UserId_WhenNameIdentifierClaimIsMissing_ShouldReturnNull()
{
// Arrange
HttpContext mockHttpContext = new DefaultHttpContext();
ClaimsIdentity identity = new(
[
new(ClaimTypes.Name, "testuser")
]);
mockHttpContext.User = new ClaimsPrincipal(identity);
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().BeNull();
}
[Fact]
public void UserId_WhenNameIdentifierClaimIsInvalidGuid_ShouldReturnNull()
{
// Arrange
HttpContext mockHttpContext = new DefaultHttpContext();
ClaimsIdentity identity = new(
[
new(ClaimTypes.NameIdentifier, "not-a-guid")
]);
mockHttpContext.User = new ClaimsPrincipal(identity);
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().BeNull();
}
[Fact]
public void UserId_WhenNameIdentifierClaimIsValidGuid_ShouldReturnGuid()
{
// Arrange
Guid expectedUserId = Guid.NewGuid();
HttpContext mockHttpContext = new DefaultHttpContext();
ClaimsIdentity identity = new(
[
new(ClaimTypes.NameIdentifier, expectedUserId.ToString())
]);
mockHttpContext.User = new ClaimsPrincipal(identity);
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().Be(expectedUserId);
}
[Fact]
public void UserId_WhenMultipleNameIdentifierClaimsExist_ShouldReturnFirstValidGuid()
{
// Arrange
Guid expectedUserId = Guid.NewGuid();
HttpContext mockHttpContext = new DefaultHttpContext();
ClaimsIdentity identity = new(
[
new(ClaimTypes.NameIdentifier, expectedUserId.ToString()),
new(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())
]);
mockHttpContext.User = new ClaimsPrincipal(identity);
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
// Act
Guid? result = _sut.UserId;
// Assert
result.Should().Be(expectedUserId);
}
}