From d18ad19f80939cf35b80cfb54ec9e9716dd966dc Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Thu, 19 Jun 2025 09:51:55 +0700 Subject: [PATCH] Auth Tests --- .../UnitTests/Services/AuthServiceTests.cs | 74 +++++++++++++++++++ .../Services/Authentication/AuthService.cs | 3 +- Govor.Core/DTOs/UserDto.cs | 15 +++- .../Validators/UserValidator.cs | 2 +- 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/Govor.API.Tests/UnitTests/Services/AuthServiceTests.cs b/Govor.API.Tests/UnitTests/Services/AuthServiceTests.cs index 5328f2c..b67e805 100644 --- a/Govor.API.Tests/UnitTests/Services/AuthServiceTests.cs +++ b/Govor.API.Tests/UnitTests/Services/AuthServiceTests.cs @@ -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 _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())); } } \ No newline at end of file diff --git a/Govor.API/Services/Authentication/AuthService.cs b/Govor.API/Services/Authentication/AuthService.cs index 1b3b8f4..79779fa 100644 --- a/Govor.API/Services/Authentication/AuthService.cs +++ b/Govor.API/Services/Authentication/AuthService.cs @@ -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); } diff --git a/Govor.Core/DTOs/UserDto.cs b/Govor.Core/DTOs/UserDto.cs index 7a5032d..d7c89a0 100644 --- a/Govor.Core/DTOs/UserDto.cs +++ b/Govor.Core/DTOs/UserDto.cs @@ -1,3 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using Govor.Core.Infrastructure.Validators; + namespace Govor.Core.DTOs; -public record UserDto(string Password, string Name); \ No newline at end of file +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; } +} \ No newline at end of file diff --git a/Govor.Core/Infrastructure/Validators/UserValidator.cs b/Govor.Core/Infrastructure/Validators/UserValidator.cs index e91f577..5c51616 100644 --- a/Govor.Core/Infrastructure/Validators/UserValidator.cs +++ b/Govor.Core/Infrastructure/Validators/UserValidator.cs @@ -6,7 +6,7 @@ namespace Govor.Core.Infrastructure.Validators; public class UserValidator : IObjectValidator { 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) {