예제 #1
0
            public void ReturnsFalseIfUserIsNotFound()
            {
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(Enumerable.Empty<User>().AsQueryable());

                var changed = service.ChangePassword("username", "oldpwd", "newpwd");

                Assert.False(changed);
            }
예제 #2
0
            public void ReturnsFalseIfUserIsNotFound()
            {
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(Enumerable.Empty <User>().AsQueryable());

                var changed = service.ChangePassword("username", "oldpwd", "newpwd");

                Assert.False(changed);
            }
예제 #3
0
            public void UpdatesTheHashedPassword()
            {
                var hash = CryptographyService.GenerateSaltedHash("oldpwd", "PBKDF2");
                var user = new User {
                    Username = "******", HashedPassword = hash, PasswordHashAlgorithm = "PBKDF2"
                };
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.True(VerifyPasswordHash(user, "newpwd"));
            }
예제 #4
0
            public void ReturnsFalseIfPasswordDoesNotMatchUser()
            {
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[]
                        {
                            new User { Username = "******", HashedPassword = "******" }
                        }.AsQueryable());
                service.MockCrypto
                       .Setup(s => s.ValidateSaltedHash(It.IsAny<string>(), It.IsAny<string>(), Constants.Sha512HashAlgorithmId)).Returns(false);

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.False(changed);
            }
예제 #5
0
            public void ReturnsTrueWhenSuccessful()
            {
                var user = new User { Username = "******", HashedPassword = "******", PasswordHashAlgorithm = "PBKDF2" };
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());
                service.MockCrypto
                       .Setup(s => s.ValidateSaltedHash("old hash", "oldpwd", Constants.PBKDF2HashAlgorithmId)).Returns(true);
                service.MockCrypto
                       .Setup(s => s.GenerateSaltedHash("newpwd", Constants.PBKDF2HashAlgorithmId)).Returns("hash and bacon");

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.True(changed);
                Assert.Equal("hash and bacon", user.HashedPassword);
            }
예제 #6
0
            public void ReturnsFalseIfPasswordDoesNotMatchUser_PBKDF2()
            {
                var user = new User
                {
                    Username              = "******",
                    HashedPassword        = CryptographyService.GenerateSaltedHash("oldpwd", "PBKDF2"),
                    PasswordHashAlgorithm = "PBKDF2",
                };
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "not_the_password", "newpwd");

                Assert.False(changed);
            }
예제 #7
0
            public void MigratesPasswordIfHashAlgorithmIsNotPBKDF2()
            {
                var user = new User {
                    Username              = "******",
                    HashedPassword        = CryptographyService.GenerateSaltedHash("oldpwd", "SHA1"),
                    PasswordHashAlgorithm = "SHA1"
                };
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.True(changed);
                Assert.True(VerifyPasswordHash(user, "newpwd"));
                Assert.Equal("PBKDF2", user.PasswordHashAlgorithm);
            }
예제 #8
0
            public void ReturnsFalseIfPasswordDoesNotMatchUser()
            {
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(new[]
                {
                    new User {
                        Username = "******", HashedPassword = "******"
                    }
                }.AsQueryable());
                service.MockCrypto
                .Setup(s => s.ValidateSaltedHash(It.IsAny <string>(), It.IsAny <string>(), Constants.Sha512HashAlgorithmId)).Returns(false);

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.False(changed);
            }
예제 #9
0
            public void ReturnsTrueWhenSuccessful()
            {
                var user = new User {
                    Username = "******", HashedPassword = "******", PasswordHashAlgorithm = "PBKDF2"
                };
                var service = new TestableUserService();

                service.MockUserRepository
                .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());
                service.MockCrypto
                .Setup(s => s.ValidateSaltedHash("old hash", "oldpwd", Constants.PBKDF2HashAlgorithmId)).Returns(true);
                service.MockCrypto
                .Setup(s => s.GenerateSaltedHash("newpwd", Constants.PBKDF2HashAlgorithmId)).Returns("hash and bacon");

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.True(changed);
                Assert.Equal("hash and bacon", user.HashedPassword);
            }
예제 #10
0
            public void ReturnsFalseIfPasswordDoesNotMatchUser_PBKDF2()
            {
                var user = new User
                {
                    Username = "******",
                    HashedPassword = CryptographyService.GenerateSaltedHash("oldpwd", "PBKDF2"),
                    PasswordHashAlgorithm = "PBKDF2",
                };
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user}.AsQueryable());

                var changed = service.ChangePassword("user", "not_the_password", "newpwd");

                Assert.False(changed);
            }
예제 #11
0
            public void MigratesPasswordIfHashAlgorithmIsNotPBKDF2()
            {
                var user = new User {
                    Username = "******",
                    HashedPassword = CryptographyService.GenerateSaltedHash("oldpwd", "SHA1"), 
                    PasswordHashAlgorithm = "SHA1"
                };
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");

                Assert.True(changed);
                Assert.True(VerifyPasswordHash(user, "newpwd"));
                Assert.Equal("PBKDF2", user.PasswordHashAlgorithm);
            }
예제 #12
0
            public void UpdatesTheHashedPassword()
            {
                var hash = CryptographyService.GenerateSaltedHash("oldpwd", "PBKDF2");
                var user = new User { Username = "******", HashedPassword = hash, PasswordHashAlgorithm = "PBKDF2" };
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");
                Assert.True(VerifyPasswordHash(user, "newpwd"));
            }
예제 #13
0
            public void UpdatesThePasswordCredential()
            {
                var hash = CryptographyService.GenerateSaltedHash("oldpwd", "PBKDF2");
                var user = new User { 
                    Username = "******",
                    Credentials = new List<Credential>()
                    {
                        new Credential(CredentialTypes.Password.Pbkdf2, hash)
                    }
                };
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "oldpwd", "newpwd");
                var cred = user.Credentials.Single();
                Assert.Equal(CredentialTypes.Password.Pbkdf2, cred.Type);
                Assert.True(VerifyPasswordHash(cred.Value, Constants.PBKDF2HashAlgorithmId, "newpwd"));
                service.MockUserRepository.VerifyCommitted();
            }
예제 #14
0
            public void ReturnsFalseIfPasswordDoesNotMatchUser_PBKDF2Credential()
            {
                var user = new User
                {
                    Username = "******",
                    HashedPassword = "******",
                    PasswordHashAlgorithm = "SHA1",
                };
                user.Credentials.Add(CredentialBuilder.CreatePbkdf2Password("oldpwd"));
                var service = new TestableUserService();
                service.MockUserRepository
                       .Setup(r => r.GetAll()).Returns(new[] { user }.AsQueryable());

                var changed = service.ChangePassword("user", "not_the_password", "newpwd");

                Assert.False(changed);
            }