| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using AutoMapper;
- using Diligent.WebAPI.Business.Interfaces;
- using Diligent.WebAPI.Data.Entities;
- using Diligent.WebAPI.Host.DTOs.Customer;
- using Diligent.WebAPI.Host.Mapper;
- using Diligent.WebAPI.Host.Mediator.Authentication.Commands;
- using Diligent.WebAPI.Host.Mediator.Authentication.Handlers;
- using Diligent.WebAPI.Host.Mediator.Authentication.Queries;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Identity;
- using Moq;
-
- namespace Tests
- {
- [TestFixture]
- public class AuthenticationHandlerTests
- {
- private Mock<IAuthenticationService> _authenticationServiceMock;
- private IMapper _mapper;
- private Mock<ICustomerRepository> _customerServiceMock;
- private Mock<UserManager<Customer>> _userManagerMock;
- private readonly Customer _customer = new()
- {
- Id = Guid.NewGuid(),
- Email = "user@gmail.com",
- FirstName = "User",
- LastName = "User",
- Notifications = new List<Notification>(),
- Roles = new List<Guid> { Guid.NewGuid() },
- UserName = "user12"
- };
- private readonly CustomerCreateDTO _customerCreateDTO = new()
- {
- Email = "user@gmail.com",
- FirstName = "User",
- LastName = "User",
- Username = "user12"
- };
-
- [SetUp]
- public void Setup()
- {
- _authenticationServiceMock = new Mock<IAuthenticationService>();
- _customerServiceMock = new Mock<ICustomerRepository>();
- _userManagerMock = new Mock<UserManager<Customer>>(Mock.Of<IUserStore<Customer>>(), null, null, null, null, null, null, null, null);
- var configuration = new MapperConfiguration(cfg => cfg.AddProfile(new CustomerMappingProfile()));
- _mapper = new Mapper(configuration);
- }
-
- [Test]
- public void LoginUser_UserIsNotAuthenticated_ThrowBadHttpRequestException()
- {
- _authenticationServiceMock.Setup(a => a.ValidateCustomer(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(false));
- var query = new LoginUserQuery(new CustomerLoginDTO { Username = "user1", Password = "somePassword" });
- var handler = new LoginUserHandler(_userManagerMock.Object, _authenticationServiceMock.Object, _mapper, _customerServiceMock.Object);
-
- Assert.That(async () => await handler.Handle(query, new CancellationToken()), Throws.Exception.TypeOf<BadHttpRequestException>());
- }
- [Test]
- public async Task LoginUser_UserIsAuthenticated_ReturnUserObject()
- {
- var list = new List<string> { "Customer" };
- _authenticationServiceMock.Setup(a => a.ValidateCustomer(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(true));
- _authenticationServiceMock.Setup(a => a.GenerateToken()).Returns(Task.FromResult("someToken"));
- _customerServiceMock.Setup(c => c.GetCustomer(It.IsAny<string>())).Returns(Task.FromResult(_customer));
- _userManagerMock.Setup(u => u.GetRolesAsync(It.IsAny<Customer>())).Returns(Task.FromResult((IList<string>)list));
- var query = new LoginUserQuery(new CustomerLoginDTO { Username = "user1", Password = "somePassword" });
- var handler = new LoginUserHandler(_userManagerMock.Object, _authenticationServiceMock.Object, _mapper, _customerServiceMock.Object);
-
- var result = await handler.Handle(query, new CancellationToken());
-
- Assert.That(result.Id, Is.EqualTo(_customer.Id.ToString()));
- }
-
- [Test]
- public void RegisterUser_ErrorWhenCreatingUser_ThrowBadHttpRequestException()
- {
- _userManagerMock.Setup(u => u.CreateAsync(It.IsAny<Customer>(), It.IsAny<string>())).ReturnsAsync(() => IdentityResult.Failed());
- var command = new RegisterUserCommand(_customerCreateDTO);
- var handler = new RegisterUserHandler(_userManagerMock.Object, _authenticationServiceMock.Object, _mapper);
-
- Assert.That(() => handler.Handle(command, new CancellationToken()), Throws.Exception.TypeOf<BadHttpRequestException>());
- }
-
- [Test]
- public async Task RegisterUser_ThereIsNoError_ReturnObject()
- {
- _userManagerMock.Setup(u => u.CreateAsync(It.IsAny<Customer>(), It.IsAny<string>())).ReturnsAsync(() => IdentityResult.Success);
- _userManagerMock.Setup(u => u.AddToRoleAsync(It.IsAny<Customer>(), It.IsAny<string>())).ReturnsAsync(() => IdentityResult.Success);
- _authenticationServiceMock.Setup(u => u.ValidateCustomer(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(true));
- _authenticationServiceMock.Setup(u => u.GenerateToken()).Returns(Task.FromResult("dasdada"));
- _userManagerMock.Setup(u => u.GetRolesAsync(It.IsAny<Customer>())).Returns(Task.FromResult((IList<string>)new List<string> { "role" }));
- var command = new RegisterUserCommand(_customerCreateDTO);
- var handler = new RegisterUserHandler(_userManagerMock.Object, _authenticationServiceMock.Object, _mapper);
-
- var result = await handler.Handle(command, new CancellationToken());
-
- Assert.That(result, Is.Not.Null);
- }
- }
- }
|