Files
Govor/Govor.API.Tests/UnitTests/Services/Authentication/JwtServiceTests.cs
T
Artemy 4f3f4ec066 Refactor to introduce Govor.Application and Govor.Contracts layers
Moved service implementations and interfaces from Govor.API and Govor.Core to new Govor.Application and Govor.Contracts projects. Updated namespaces and references throughout the solution. Added custom exception classes for authentication and invite services. Adjusted dependency injection and project references to use the new structure. This refactor improves separation of concerns and prepares the codebase for better maintainability and scalability.
2025-06-25 15:16:14 +07:00

59 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IdentityModel.Tokens.Jwt;
using AutoFixture;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Services;
using Govor.Core.Models;
using Microsoft.Extensions.Options;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Authentication;
[TestFixture]
public class JwtServiceTests
{
private Fixture _fixture;
private Mock<IOptions<JwtOption>> _jwtOptionsMock;
private Mock<IInvitesService> _invitesServiceMock;
private IJwtService _jwtService;
private JwtOption _testJwtOptions;
[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_testJwtOptions = new JwtOption
{
SecretKeу = "THIS IS A TEST SECRET KEY THAT IS LONG ENOUGH", // Ensure key size is sufficient for HMACSHA256
Hours = 1
};
_jwtOptionsMock = new Mock<IOptions<JwtOption>>();
_jwtOptionsMock.Setup(o => o.Value).Returns(_testJwtOptions);
_invitesServiceMock = new Mock<IInvitesService>();
_jwtService = new JwtService(_jwtOptionsMock.Object, _invitesServiceMock.Object);
}
[Test]
public void GenerateJwtToken_ShouldReturnValidJwtString()
{
// Arrange
var user = _fixture.Create<User>();
var expectedRole = "User";
_invitesServiceMock.Setup(s => s.GetRole(user)).Returns(Task.FromResult(expectedRole));
// Act
var tokenString = _jwtService.GenerateJwtToken(user);
// Assert
Assert.That(tokenString, Is.Not.Null.And.Not.Empty);
// Attempt to parse the token to ensure it's a JWT
var handler = new JwtSecurityTokenHandler();
Assert.DoesNotThrow(() => handler.ReadJwtToken(tokenString));
}
}