mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+138
@@ -0,0 +1,138 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
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
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UserId_WhenUserIsNull_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
HttpContext mockHttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = null
|
||||
};
|
||||
_mockHttpContextAccessor.HttpContext.Returns(mockHttpContext);
|
||||
|
||||
// Act
|
||||
Guid? result = _sut.UserId;
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[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
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[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
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[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
|
||||
Assert.Equal(expectedUserId, result);
|
||||
}
|
||||
|
||||
[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
|
||||
Assert.Equal(expectedUserId, result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user