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(); userRepoMock.Setup(repo => repo.Get(It.IsAny())) .ReturnsAsync(new UserDto()); var authService = new AuthService(userRepoMock.Object); var user = new UserDto { Name = "John Doe", Password = "password" }; Assert.ThrowsAsync(() => authService.Register(user)); } [Fact] public void Register_WhenUserDoesNotExist_ThenCreateUser_ReturnsUserViewModel() { var userRepoMock = new Mock(); userRepoMock.Setup(repo => repo.Get(It.IsAny())).ReturnsAsync((UserDto)null); userRepoMock.Setup(repo => repo.Create(It.IsAny())).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()), Times.Once); Assert.NotNull(result); Assert.IsType>(result); } [Fact] public void Login_WhenUserDoesNotExist_ThrowsUserNotFoundException() { var userRepoMock = new Mock(); userRepoMock.Setup(repo => repo.Get(It.IsAny())).ReturnsAsync((UserDto)null); var authService = new AuthService(userRepoMock.Object); var user = new UserLoginDto { Name = "John Doe", Password = "password" }; Assert.ThrowsAsync(() => authService.Login(user)); } [Fact] public void Login_WhenUserExists_ReturnsUserViewModel() { var userRepoMock = new Mock(); userRepoMock.Setup(repo => repo.Get(It.IsAny())).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()), Times.Once); Assert.NotNull(result); Assert.IsType>(result); } [Fact] public void Login_WhenWrongLoginData_ThrowsArgumentException() { var userRepoMock = new Mock(); userRepoMock.Setup(repo => repo.Get(It.IsAny())).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(() => authService.Login(user1)); Assert.ThrowsAsync(() => authService.Login(user2)); Assert.ThrowsAsync(() => authService.Login(user3)); Assert.ThrowsAsync(() => authService.Login(user4)); } }