コード例 #1
0
        public async Task ShouldThrow_InvalidAccountException_OnPassword()
        {
            var request          = FakeRequest();
            var loginMaxAttempts = new LoginMaxAttemptFake().Builder.Generate();
            var user             = new UserFake().Builder
                                   .RuleFor(x => x.Email, f => request.Email)
                                   .Generate();

            var uowGeneralMock = new Mock <IUowGeneral>();

            uowGeneralMock.Setup(x =>
                                 x.UserRepository.FindByEmail(request.Email)).ReturnsAsync(user);
            uowGeneralMock.Setup(x =>
                                 x.LoginMaxAttemptRepository.Find(GeneralConstants.DefaultId)).ReturnsAsync(loginMaxAttempts);
            var passwordServiceMock = new Mock <IPasswordService>();

            passwordServiceMock.Setup(x =>
                                      x.Verify(request.Password, user.Password)).Returns(false);
            var accessTokenServiceMock = new Mock <IAccessTokenService>();
            var refreshTokenService    = new RefreshTokenService(uowGeneralMock.Object);

            var handler = new LoginUserCommandHandler(uowGeneralMock.Object, passwordServiceMock.Object,
                                                      accessTokenServiceMock.Object, refreshTokenService);

            await Assert.ThrowsAsync <InvalidAccountException>(() =>
                                                               handler.Handle(request, default));

            Assert.Equal(1, user.LoginAttempts);
            Assert.Null(user.FindEvent(typeof(MaxLoginAttemptsReached)));
            uowGeneralMock.Verify(x => x.SaveChanges(), Times.Once);
        }
コード例 #2
0
        public async Task Should_LoginUser()
        {
            var request          = FakeRequest();
            var loginMaxAttempts = new LoginMaxAttemptFake().Builder.Generate();
            var user             = new UserFake().Builder
                                   .RuleFor(x => x.Email, f => request.Email)
                                   .Generate();

            var uowGeneralMock = new Mock <IUowGeneral>();

            uowGeneralMock.Setup(x =>
                                 x.UserRepository.FindByEmail(request.Email)).ReturnsAsync(user);
            uowGeneralMock.Setup(x =>
                                 x.LoginMaxAttemptRepository.Find(GeneralConstants.DefaultId)).ReturnsAsync(loginMaxAttempts);
            uowGeneralMock.Setup(x =>
                                 x.RefreshTokenRepository.Create(It.IsAny <RefreshToken>())).ReturnsAsync(It.IsAny <RefreshToken>());
            var passwordServiceMock = new Mock <IPasswordService>();

            passwordServiceMock.Setup(x =>
                                      x.Verify(request.Password, user.Password)).Returns(true);
            var accessTokenServiceMock = new Mock <IAccessTokenService>();
            var refreshTokenService    = new RefreshTokenService(uowGeneralMock.Object);

            var handler = new LoginUserCommandHandler(uowGeneralMock.Object, passwordServiceMock.Object,
                                                      accessTokenServiceMock.Object, refreshTokenService);

            var response = await handler.Handle(request, default);

            Assert.NotNull(response);
            Assert.NotNull(user.FindEvent(typeof(UserLogged)));
            accessTokenServiceMock.Verify(x => x.GetAccessToken(user), Times.Once);
            uowGeneralMock.Verify(x => x.UserRepository.Update(user), Times.Once);
            uowGeneralMock.Verify(x => x.SaveChanges(), Times.Once);
        }