コード例 #1
0
        public void GetUserId_General(string login, string password)
        {
            var repositoryMock = new Mock<ILoginAuthenticationRepository>(MockBehavior.Loose);
            var passwordValidatorMock = new Mock<IPasswordValidator>(MockBehavior.Loose);
            var saltedHashGenerator = new SaltedHashGenerator();

            var service = new LoginAuthenticationService(repositoryMock.Object, passwordValidatorMock.Object, saltedHashGenerator);

            var userId = service.GetUserId(login, password);

            Assert.IsNull(userId);
            repositoryMock.Verify(repository => repository.GetByLogin(login));
        }
コード例 #2
0
        public void GetUserId_CheckPassword(bool loginCorrect, bool passwordCorrect)
        {
            const string login = "******";
            var inputPass = "******";
            var dbPass = passwordCorrect ? inputPass : "******";

            var repositoryMock = new Mock<ILoginAuthenticationRepository>(MockBehavior.Loose);
            var passwordValidatorMock = new Mock<IPasswordValidator>(MockBehavior.Loose);
            var saltedHashGenerator = new SaltedHashGenerator();

            byte[] salt;
            var dbHash = saltedHashGenerator.GenerateSaltedHash(dbPass, out salt, 32);

            var loginAuthentication = new LoginAuthentication
            {
                Id = long.MaxValue - 1,
                UserId = long.MaxValue,
                Salt = salt,
                PasswordHash = dbHash
            };

            repositoryMock.Setup(repository => repository.GetByLogin(login)).Returns(loginCorrect ? loginAuthentication : null);

            var service = new LoginAuthenticationService(repositoryMock.Object, passwordValidatorMock.Object, saltedHashGenerator);
            var userId = service.GetUserId(login, inputPass);

            if (loginCorrect && passwordCorrect)
            {
                Assert.NotNull(userId);
                Assert.AreEqual(loginAuthentication.UserId, userId.Value);
            }
            else
            {
                Assert.IsNull(userId);
            }
        }
コード例 #3
0
        public void SetPassword_General(long userId, string login, string password)
        {
            var repositoryMock = new Mock<ILoginAuthenticationRepository>(MockBehavior.Loose);
            var passwordValidatorMock = new Mock<IPasswordValidator>(MockBehavior.Loose);
            var saltedHashGenerator = new SaltedHashGenerator();

            var service = new LoginAuthenticationService(repositoryMock.Object, passwordValidatorMock.Object, saltedHashGenerator);

            service.SetPassword(userId, login, password);

            byte[] salt;
            var expectedPasswordHash = saltedHashGenerator.GenerateSaltedHash(password, out salt, LoginAuthenticationService.SALT_LENGTH);

            passwordValidatorMock.Verify(validator => validator.Validate(password));
            repositoryMock.Verify(repository => repository.GetByUserId(userId));
            repositoryMock.Verify(repository => repository.Save(It.Is<LoginAuthentication>(
                authentication =>
                    authentication.Id == 0
                    && authentication.UserId == userId
                    && authentication.LoginName == login
                    && authentication.Salt.SequenceEqual(salt)
                    && authentication.PasswordHash.SequenceEqual(expectedPasswordHash)
            )));
        }