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); }
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); }
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")); }
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); }
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); }
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); }
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); }
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); }
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); }
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(); }
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); }