using AutoMapper; using Diligent.WebAPI.Business.Interfaces; using Diligent.WebAPI.Business.Services; using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Host.Mapper; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Moq; namespace Tests { public class AuthenticationServiceTests { private Mock _authenticationServiceMock; private IAuthenticationService _authenticationService; private IConfiguration _configuration; private Mock> _userManagerMock; private readonly Customer _customer = new() { Id = Guid.NewGuid(), Email = "user@gmail.com", FirstName = "User", LastName = "User", Notifications = new List(), Roles = new List { Guid.NewGuid() }, UserName = "user12" }; [SetUp] public void Setup() { var inMemorySettings = new Dictionary { {"JwtSettings:jwtSecret", "Ovo je neka sifra koja treba biti tajna"}, {"JwtSettings:validIssuer", "http://localhost:5116"}, {"JwtSettings:validAudience", "http://localhost:3000"}, }; _configuration = new ConfigurationBuilder() .AddInMemoryCollection(inMemorySettings) .Build(); _authenticationServiceMock = new Mock(); _userManagerMock = new Mock>(Mock.Of>(), null, null, null, null, null, null, null, null); _authenticationService = new AuthenticationService(_userManagerMock.Object, _configuration); var configuration = new MapperConfiguration(cfg => cfg.AddProfile(new CustomerMappingProfile())); } [Test] public async Task ValidateCustomer_CustomerIsNull_CustomerIsNotValid() { _userManagerMock.Setup(u => u.FindByNameAsync(It.IsAny())).Returns(Task.FromResult(null)); await _authenticationServiceMock.Object.ValidateCustomer("dasdas", "dasdasd"); var result = await _authenticationService.ValidateCustomer(It.IsAny(), It.IsAny()); Assert.That(result, Is.False); } [Test] public async Task ValidateCustomer_CustomerIsNotNullAndUserCredentialsAreNotValid_CustomerIsNotValid() { _userManagerMock.Setup(u => u.FindByNameAsync(It.IsAny())).Returns(Task.FromResult(_customer)); _userManagerMock.Setup(u => u.CheckPasswordAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); await _authenticationServiceMock.Object.ValidateCustomer("dasdas", "dasdasd"); var result = await _authenticationService.ValidateCustomer(It.IsAny(), It.IsAny()); Assert.That(result, Is.False); } [Test] public async Task ValidateCustomer_CustomerIsNotNullAndUserCredentialsAreValid_CustomerIsValid() { _userManagerMock.Setup(u => u.FindByNameAsync(It.IsAny())).Returns(Task.FromResult(_customer)); _userManagerMock.Setup(u => u.CheckPasswordAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); var result = await _authenticationService.ValidateCustomer("dasdasd", "dasdasd"); Assert.That(result, Is.True); } [Test] public async Task GenerateToken_UserIsNotValid_ReturnNull() { _userManagerMock.Setup(u => u.GetRolesAsync(It.IsAny())).ReturnsAsync((IList)new List { "roles" }); var result = await _authenticationService.GenerateToken(); Assert.That(result, Is.Null); } [Test] public async Task GenerateToken_UserIsValid_ReturnToken() { _userManagerMock.Setup(u => u.GetRolesAsync(It.IsAny())).ReturnsAsync((IList)new List { "roles" }); _userManagerMock.Setup(u => u.FindByNameAsync(It.IsAny())).Returns(Task.FromResult(_customer)); _userManagerMock.Setup(u => u.CheckPasswordAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); await _authenticationService.ValidateCustomer("dasdas", "dasd"); //user must be first valid and then we generate token for him var result = await _authenticationService.GenerateToken(); Assert.That(result, Is.Not.Null); } } }