mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
65a43c09d3
Renamed IMessageService to IMessageCommandService and updated all usages accordingly. Moved and renamed test files from Govor.API.Tests to the new Govor.Application.Tests project, updating namespaces to match. Refactored message-related services and controllers to use the new naming and structure. Added Govor.Application.Tests project to the solution.
106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
using System.Security.Claims;
|
|
using Govor.Application.Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Moq;
|
|
|
|
namespace Govor.Application.Tests.Infrastructure.Extensions;
|
|
|
|
[TestFixture]
|
|
public class CurrentUserServiceTests
|
|
{
|
|
private Mock<IHttpContextAccessor> _httpContextAccessorMock;
|
|
private CurrentUserService _currentUserService;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
|
_currentUserService = new CurrentUserService(_httpContextAccessorMock.Object);
|
|
}
|
|
|
|
[Test]
|
|
public void GetCurrentUserId_ValidUserIdClaim_ReturnsGuid()
|
|
{
|
|
// Arrange
|
|
var userId = Guid.NewGuid();
|
|
var claims = new[] { new Claim("userId", userId.ToString()) };
|
|
var identity = new ClaimsIdentity(claims);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
var httpContextMock = new Mock<HttpContext>();
|
|
httpContextMock.Setup(x => x.User).Returns(principal);
|
|
|
|
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
|
|
|
// Act
|
|
var result = _currentUserService.GetCurrentUserId();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.EqualTo(userId));
|
|
}
|
|
|
|
[Test]
|
|
public void GetCurrentUserId_NoHttpContext_ThrowsUnauthorizedAccessException()
|
|
{
|
|
// Arrange
|
|
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns((HttpContext)null);
|
|
|
|
// Act & Assert
|
|
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
|
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
|
}
|
|
|
|
[Test]
|
|
public void GetCurrentUserId_NoUserIdClaim_ThrowsUnauthorizedAccessException()
|
|
{
|
|
// Arrange
|
|
var claims = new[] { new Claim("otherClaim", "value") };
|
|
var identity = new ClaimsIdentity(claims);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
var httpContextMock = new Mock<HttpContext>();
|
|
httpContextMock.Setup(x => x.User).Returns(principal);
|
|
|
|
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
|
|
|
// Act & Assert
|
|
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
|
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
|
}
|
|
|
|
[Test]
|
|
public void GetCurrentUserId_InvalidUserIdClaim_ThrowsUnauthorizedAccessException()
|
|
{
|
|
// Arrange
|
|
var claims = new[] { new Claim("userId", "invalid-guid") };
|
|
var identity = new ClaimsIdentity(claims);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
var httpContextMock = new Mock<HttpContext>();
|
|
httpContextMock.Setup(x => x.User).Returns(principal);
|
|
|
|
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
|
|
|
// Act & Assert
|
|
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
|
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
|
}
|
|
|
|
[Test]
|
|
public void GetCurrentUserId_EmptyUserIdClaim_ThrowsUnauthorizedAccessException()
|
|
{
|
|
// Arrange
|
|
var claims = new[] { new Claim("userId", "") };
|
|
var identity = new ClaimsIdentity(claims);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
var httpContextMock = new Mock<HttpContext>();
|
|
httpContextMock.Setup(x => x.User).Returns(principal);
|
|
|
|
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
|
|
|
// Act & Assert
|
|
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
|
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
|
}
|
|
} |