public async Task TestHandle_EntityExists_ShouldReturnCorrectly(string email, string password)
        {
            // Arrange
            var userProfile = new UserProfile(Guid.NewGuid().ToString(), email, "username");

            Context.UserProfiles.Add(userProfile);
            await Context.SaveChangesAsync();

            var command = new AuthenticateUserCommand {
                Email    = email,
                Password = password
            };

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(s => s.Authenticate(email, password))
            .ReturnsAsync(Result.CreateSuccess);

            var sut = new AuthenticateUserCommand.Handler(mockedUserService.Object, Mapper, Context);

            var expectedResult = Mapper.Map <UserWithProfileDto>(userProfile);

            // Act
            var actualResult = await sut.Handle(command, CancellationToken.None);

            // Assert
            actualResult.Should().BeEquivalentTo(expectedResult);
        }
        public async Task TestHandle_ShouldCallUserServiceAuthenticateCorrectly(string email, string password)
        {
            // Arrange
            var userProfile = new UserProfile(Guid.NewGuid().ToString(), email, "username");

            Context.UserProfiles.Add(userProfile);
            await Context.SaveChangesAsync();

            var command = new AuthenticateUserCommand {
                Email    = email,
                Password = password
            };

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(s => s.Authenticate(email, password))
            .ReturnsAsync(Result.CreateSuccess());

            var sut = new AuthenticateUserCommand.Handler(mockedUserService.Object, Mapper, Context);

            // Act
            await sut.Handle(command, CancellationToken.None);

            // Assert
            mockedUserService.Verify(s => s.Authenticate(email, password), Times.Once);
        }
        public async Task Handle_Should_Call_Repository_GetByEmail()
        {
            _repositoryMock.Setup(x => x.GetByEmail(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new User()));

            var command = new AuthenticateUserCommand {
                Email = EMAIL, Password = PASSWORD
            };
            var handler = new AuthenticateUserCommand.Handler(_userAuthenticationServiceMock.Object, _repositoryMock.Object);
            await handler.Handle(command, CancellationToken.None);

            _repositoryMock.Verify(x => x.GetByEmail(EMAIL, CancellationToken.None), Times.Once);
        }
        public void TestHandle_EntityDoesNotExist_ShouldThrowEntityNotFoundExceptionWithCorrectErrors(string email, string password)
        {
            // Arrange
            var command = new AuthenticateUserCommand {
                Email    = email,
                Password = password
            };

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(s => s.Authenticate(email, password))
            .ReturnsAsync(Result.CreateSuccess);

            var sut = new AuthenticateUserCommand.Handler(mockedUserService.Object, Mapper, Context);

            // Act
            var action = new Func <Task <UserWithProfileDto> >(async() => await sut.Handle(command, CancellationToken.None));

            // Assert
            action.Should().Throw <EntityNotFoundException <UserProfile> >().And.Message.Should().Contain(email);
        }
        public void TestHandle_UserServiceAuthenticateIsNotSuccess_ShouldThrowBadRequestException(string email, string password)
        {
            // Arrange
            var command = new AuthenticateUserCommand {
                Email    = email,
                Password = password
            };

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(s => s.Authenticate(email, password))
            .ReturnsAsync(Result.CreateFailure());

            var sut = new AuthenticateUserCommand.Handler(mockedUserService.Object, Mapper, Context);

            // Act
            var action = new Func <Task <UserWithProfileDto> >(async() => await sut.Handle(command, CancellationToken.None));

            // Assert
            action.Should().Throw <BadRequestException>();
        }