Refactor message service and move tests to Application.Tests

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.
This commit is contained in:
Artemy
2025-07-10 18:05:10 +07:00
parent a43a26b307
commit 65a43c09d3
33 changed files with 157 additions and 45 deletions
@@ -0,0 +1,103 @@
using AutoFixture;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Exceptions.AuthService;
using Govor.Application.Interfaces.Authentication;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Core.Repositories.Admins;
using Moq;
namespace Govor.Application.Tests.Services.Authentication;
[TestFixture]
public class AuthServiceTests
{
private Fixture _fixture;
private Mock<IPasswordHasher> _passwordHasherMock;
private Mock<IJwtService> _jwtServiceMock;
private Mock<IUsersRepository> _usersRepositoryMock;
private Mock<IAdminsRepository> _adminsRepositoryMock;
private Mock<IUsernameValidator> _usernameValidatorMock;
private IAccountService _accountService;
[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_usersRepositoryMock = new Mock<IUsersRepository>();
_passwordHasherMock = new Mock<IPasswordHasher>();
_jwtServiceMock = new Mock<IJwtService>();
_adminsRepositoryMock = new Mock<IAdminsRepository>();
_usernameValidatorMock = new Mock<IUsernameValidator>();
_accountService = new AuthService(
_usersRepositoryMock.Object,
_jwtServiceMock.Object,
_passwordHasherMock.Object,
_adminsRepositoryMock.Object,
_usernameValidatorMock.Object
);
}
[Test]
public void Given_ExistUser_When_Register_Should_Throw_UserAlreadyExistsException()
{
// Arrange
_usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny<string>())).ReturnsAsync(true);
// Act & Assert
Assert.ThrowsAsync<UserAlreadyExistException>(async () => await _accountService.RegistrationAsync(
_fixture.Create<string>(), _fixture.Create<string>(), _fixture.Create<Invitation>()));
}
[Test]
public void Given_NotExistUser_When_Register_Should_Dont_Throw_UserNotRegisteredException()
{
// Arrange
_usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny<string>())).ReturnsAsync(false);
// Act & Assert
Assert.DoesNotThrowAsync(async () => await _accountService.RegistrationAsync(
_fixture.Create<string>(), _fixture.Create<string>(), _fixture.Create<Invitation>()));
}
[Test]
public void Given_WrongPassword_When_Login_Should_Throw_LoginUserException()
{
// Arrange
_usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny<string>())).ReturnsAsync(true);
_passwordHasherMock.Setup(h => h.Verify(It.IsAny<string>(),
It.IsAny<string>())).Returns(false);
_usersRepositoryMock.Setup(u => u.FindByUsernameAsync(It.IsAny<string>()))
.ReturnsAsync(() => _fixture.Create<User>());
// Act & Assert
Assert.ThrowsAsync<LoginUserException>(async () => await _accountService.LoginAsync(
_fixture.Create<string>(), _fixture.Create<string>()));
}
[Test]
public void Given_NotExistUser_When_Login_Should_Throw_UserNotRegisteredException()
{
// Arrange
_usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny<string>())).ReturnsAsync(false);
// Act & Assert
Assert.ThrowsAsync<UserNotRegisteredException>(async () => await _accountService.LoginAsync(
_fixture.Create<string>(), _fixture.Create<string>()));
}
}
@@ -0,0 +1,60 @@
using System.IdentityModel.Tokens.Jwt;
using AutoFixture;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Core.Models;
using Microsoft.Extensions.Options;
using Moq;
namespace Govor.Application.Tests.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 async Task GenerateJwtToken_ShouldReturnValidJwtString()
{
// Arrange
var user = _fixture.Create<User>();
var expectedRole = "User";
_invitesServiceMock.Setup(s => s.GetRoleAsync(user)).Returns(Task.FromResult(expectedRole));
// Act
var tokenString = await _jwtService.GenerateJwtTokenAsync(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));
}
}