using AutoFixture; using Govor.API.Services.Authentication; using Govor.Core.Infrastructure.Extensions; using Govor.Core.Models; using Govor.Core.Repositories.Users; using Govor.Core.Services; using Moq; namespace Govor.API.Tests.UnitTests.Services; [TestFixture] public class AuthServiceTests { private Fixture _fixture; private Mock _passwordHasherMock; private Mock _jwtServiceMock; private Mock _usersRepositoryMock; private IAccountService _accountService; [SetUp] public void SetUp() { _fixture = new Fixture(); _usersRepositoryMock = new Mock(); _passwordHasherMock = new Mock(); _jwtServiceMock = new Mock(); _accountService = new AuthService( _usersRepositoryMock.Object, _jwtServiceMock.Object, _passwordHasherMock.Object ); } [Test] public void Given_ExistUser_When_Register_Should_Throw_UserAlreadyExistsException() { // Arrange _usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny())).ReturnsAsync(true); // Act & Assert Assert.ThrowsAsync(async () => await _accountService.RegistrationAsync( _fixture.Create(), _fixture.Create())); } [Test] public void Given_NotExistUser_When_Register_Should_Dont_Throw_UserNotRegisteredException() { // Arrange _usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny())).ReturnsAsync(false); // Act & Assert Assert.DoesNotThrowAsync(async () => await _accountService.RegistrationAsync( _fixture.Create(), _fixture.Create())); } [Test] public void Given_WrongPassword_When_Login_Should_Throw_LoginUserException() { // Arrange _usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny())).ReturnsAsync(true); _passwordHasherMock.Setup(h => h.Verify(It.IsAny(), It.IsAny())).Returns(false); _usersRepositoryMock.Setup(u => u.FindByUsernameAsync(It.IsAny())) .ReturnsAsync(() => _fixture.Create()); // Act & Assert Assert.ThrowsAsync(async () => await _accountService.LoginAsync( _fixture.Create(), _fixture.Create())); } [Test] public void Given_NotExistUser_When_Login_Should_Throw_UserNotRegisteredException() { // Arrange _usersRepositoryMock.Setup(r => r.ExistsUsernameAsync(It.IsAny())).ReturnsAsync(false); // Act & Assert Assert.ThrowsAsync(async () => await _accountService.LoginAsync( _fixture.Create(), _fixture.Create())); } }