mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Auth Tests
This commit is contained in:
@@ -1,11 +1,85 @@
|
||||
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<IPasswordHasher> _passwordHasherMock;
|
||||
private Mock<IJwtService> _jwtServiceMock;
|
||||
private Mock<IUsersRepository> _usersRepositoryMock;
|
||||
private IAccountService _accountService;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
|
||||
_usersRepositoryMock = new Mock<IUsersRepository>();
|
||||
_passwordHasherMock = new Mock<IPasswordHasher>();
|
||||
_jwtServiceMock = new Mock<IJwtService>();
|
||||
|
||||
_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<string>())).ReturnsAsync(true);
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<UserAlreadyExistException>(async () => await _accountService.RegistrationAsync(
|
||||
_fixture.Create<string>(), _fixture.Create<string>()));
|
||||
}
|
||||
|
||||
[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>()));
|
||||
}
|
||||
|
||||
[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>()));
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Govor.Core.Models;
|
||||
using Govor.Core.Repositories;
|
||||
using Govor.Core.Repositories.Users;
|
||||
using Govor.Core.Services;
|
||||
using Govor.Data.Repositories;
|
||||
|
||||
namespace Govor.API.Services.Authentication;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class AuthService : IAccountService
|
||||
//Role = role == "Admin" ? "Admin" : "User" // Ограничение ролей
|
||||
};
|
||||
|
||||
//await _usersRepository.AddAsync(user);
|
||||
await _usersRepository.AddAsync(user);
|
||||
|
||||
return _jwtService.GenerateJwtToken(user);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
|
||||
namespace Govor.Core.DTOs;
|
||||
|
||||
public record UserDto(string Password, string Name);
|
||||
public record UserDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(UserValidator.MAX_LENGHT_OF_NAME,
|
||||
MinimumLength = UserValidator.MIN_LENGHT_OF_NAME,
|
||||
ErrorMessage = "Username must be between 4 and 50 characters.")]
|
||||
public string Name { get; init; }
|
||||
[Required]
|
||||
[MinLength(8)]
|
||||
public string Password { get; init; }
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace Govor.Core.Infrastructure.Validators;
|
||||
public class UserValidator : IObjectValidator<User>
|
||||
{
|
||||
public const int MIN_LENGHT_OF_NAME = 4;
|
||||
public const int MAX_LENGHT_OF_NAME = 100;
|
||||
public const int MAX_LENGHT_OF_NAME = 50;
|
||||
|
||||
public void Validate(User user)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user