domBudg/back/Services.Tests/Domain/AuthServiceTests.cs

83 lines
3.3 KiB
C#

using Contracts.DTO;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Moq;
using Services.Domain;
using Services.Support.Exceptions;
namespace Services.Tests.Domain;
public class AuthServiceTests
{
[Fact]
public void Register_WhenUserExists_ThrowsAlreadyExistsException()
{
var userRepoMock = new Mock<IUserRepo>();
userRepoMock.Setup(repo => repo.Get(It.IsAny<UserSearch>()))
.ReturnsAsync(new UserDto());
var authService = new AuthService(userRepoMock.Object);
var user = new UserDto { Name = "John Doe", Password = "password" };
Assert.ThrowsAsync<AlreadyExistsException>(() => authService.Register(user));
}
[Fact]
public void Register_WhenUserDoesNotExist_ThenCreateUser_ReturnsUserViewModel()
{
var userRepoMock = new Mock<IUserRepo>();
userRepoMock.Setup(repo => repo.Get(It.IsAny<UserSearch>())).ReturnsAsync((UserDto)null);
userRepoMock.Setup(repo => repo.Create(It.IsAny<UserDto>())).ReturnsAsync(new UserDto());
var authService = new AuthService(userRepoMock.Object);
var user = new UserDto { Name = "John Doe", Password = "password" };
var result = authService.Register(user);
userRepoMock.Verify(repo => repo.Create(It.IsAny<UserDto>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<Task<UserViewModel>>(result);
}
[Fact]
public void Login_WhenUserDoesNotExist_ThrowsUserNotFoundException()
{
var userRepoMock = new Mock<IUserRepo>();
userRepoMock.Setup(repo => repo.Get(It.IsAny<UserSearch>())).ReturnsAsync((UserDto)null);
var authService = new AuthService(userRepoMock.Object);
var user = new UserLoginDto { Name = "John Doe", Password = "password" };
Assert.ThrowsAsync<UserNotFoundException>(() => authService.Login(user));
}
[Fact]
public void Login_WhenUserExists_ReturnsUserViewModel()
{
var userRepoMock = new Mock<IUserRepo>();
userRepoMock.Setup(repo => repo.Get(It.IsAny<UserSearch>())).ReturnsAsync(new UserDto());
var authService = new AuthService(userRepoMock.Object);
var user = new UserLoginDto { Name = "John Doe", Password = "password" };
var result = authService.Login(user);
userRepoMock.Verify(repo => repo.Get(It.IsAny<UserSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<Task<UserViewModel>>(result);
}
[Fact]
public void Login_WhenWrongLoginData_ThrowsArgumentException()
{
var userRepoMock = new Mock<IUserRepo>();
userRepoMock.Setup(repo => repo.Get(It.IsAny<UserSearch>())).ReturnsAsync((UserDto)null);
var authService = new AuthService(userRepoMock.Object);
UserLoginDto user1 = null;
UserLoginDto user2 = new() { Name = "", Password = "password" };
UserLoginDto user3 = new() { Name = "John Doe", Password = "" };
UserLoginDto user4 = new() { Name = "", Password = "" };
Assert.ThrowsAsync<ArgumentException>(() => authService.Login(user1));
Assert.ThrowsAsync<ArgumentException>(() => authService.Login(user2));
Assert.ThrowsAsync<ArgumentException>(() => authService.Login(user3));
Assert.ThrowsAsync<ArgumentException>(() => authService.Login(user4));
}
}