Exemple #1
0
        public async Task CreateUserAsync(UserDTO model)
        {
            var userRecord = await _unitOfWork.Users.GetUserAsync(model.Email);

            if (userRecord != null)
            {
                throw new BadRequestException("User with this email alredy exists");
            }
            var encodedPassword = PasswordHasher.EncodePasswordToBase64(model.Password);
            await _unitOfWork.Users.CreateUserAsync(new User { Email = model.Email, Password = encodedPassword });

            await _unitOfWork.CommitAsync();
        }
        public async Task BuildTokenAsyncThrowInvalidPasswordException([Frozen] Mock <IUnitOfWork> unitOfWorkMock,
                                                                       [Frozen] Mock <IUserRepository> userRepositoryMock, LoginModel loginModel, User user, AuthService sut)
        {
            // Arrange
            var invalidPassword = "******";

            user.Password = PasswordHasher.EncodePasswordToBase64(invalidPassword);
            userRepositoryMock.Setup(x => x.GetUserAsync(loginModel.Email)).ReturnsAsync(user);
            unitOfWorkMock.Setup(x => x.Users).Returns(userRepositoryMock.Object);

            // Act
            Func <Task> act = async() => await sut.BuildTokenAsync(loginModel);

            // Assert
            Exception result = await Assert.ThrowsAsync <InvalidCredsException>(act);

            Assert.Equal("Invalid password!", result.Message);
        }
        public async Task CreateUserAsyncSuccessful([Frozen] Mock <IUnitOfWork> unitOfWorkMock,
                                                    [Frozen] Mock <IUserRepository> userRepositoryMock, UserDTO model, UserService sut)
        {
            // Arrange
            var encodedPassword = PasswordHasher.EncodePasswordToBase64(model.Password);

            userRepositoryMock.Setup(x => x.GetUserAsync(model.Email)).ReturnsAsync((User)null);
            unitOfWorkMock.Setup(x => x.Users).Returns(userRepositoryMock.Object);

            // Act
            await sut.CreateUserAsync(model);

            // Assert
            userRepositoryMock.Verify(x => x.GetUserAsync(model.Email), Times.Once);
            userRepositoryMock.Verify(x => x.CreateUserAsync(It.Is <User>(user => user.Email.Equals(model.Email))), Times.Once);
            unitOfWorkMock.Verify(x => x.Users, Times.Exactly(2));
            unitOfWorkMock.Verify(x => x.CommitAsync(), Times.Once);
        }
        public async Task BuildTokenAsyncSucessful([Frozen] Mock <IJwtTokenBuilder> jwtTokenBuilderMock, [Frozen] Mock <IUnitOfWork> unitOfWorkMock,
                                                   [Frozen] Mock <IUserRepository> userRepositoryMock, LoginModel loginModel, User user, string token, AuthService sut)
        {
            // Arrange
            var expireIn = 30;

            user.Password = PasswordHasher.EncodePasswordToBase64(loginModel.Password);
            userRepositoryMock.Setup(x => x.GetUserAsync(loginModel.Email)).ReturnsAsync(user);
            unitOfWorkMock.Setup(x => x.Users).Returns(userRepositoryMock.Object);
            jwtTokenBuilderMock.Setup(x => x.BuildToken(It.IsAny <DateTime>(), expireIn, It.IsAny <Claim[]>()))
            .Returns(token);

            // Act
            var result = await sut.BuildTokenAsync(loginModel);

            // Assert
            Assert.Equal(token, result);
            userRepositoryMock.Verify(x => x.GetUserAsync(loginModel.Email), Times.Once);
            unitOfWorkMock.Verify(x => x.Users, Times.Once);
            jwtTokenBuilderMock.Verify(x => x.BuildToken(It.IsAny <DateTime>(), expireIn, It.IsAny <Claim[]>()), Times.Once);
        }