public void ThrowsIfPasswordIsTooShort() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AddUser("SomeUser", "email", password: "******")); }
public void ThrowsIfNameIsInValid() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AddUser("some in valid name", "email", password: null)); }
public void ThrowsIfNameIsNullOrEmpty() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AddUser(null, "email", password: null)); Assert.Throws<InvalidOperationException>(() => service.AddUser(String.Empty, "email", password: null)); }
public void UnicodeNameIsValid() { // Fix issue #370 var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); var user = service.AddUser("ТарасБуга", "email", "password"); Assert.Equal("ТарасБуга", user.Name); }
public void ThrowsIfNameInUse() { var repository = new InMemoryRepository(); repository.Add(new ChatUser() { Name = "taken" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AddUser("taken", "email", password: null)); }
public void AddsUserToRepository() { var crypto = new Mock<ICryptoService>(); crypto.Setup(c => c.CreateSalt()).Returns("salted"); var repository = new InMemoryRepository(); var service = new MembershipService(repository, crypto.Object); service.AddUser("SomeUser", "email", password: "******"); var user = repository.GetUserByName("SomeUser"); Assert.NotNull(user); Assert.Equal("email", user.Email); Assert.Equal("SomeUser", user.Name); Assert.Equal("salted", user.Salt); Assert.Equal("8f5793009fe15c2227e3528d0507413a83dff10635d3a6acf1ba3229a03380d8", user.HashedPassword); }
public void EnsuresStoredPasswordIsSalted() { var crypto = new Mock<ICryptoService>(); crypto.Setup(c => c.CreateSalt()).Returns("salted"); var repository = new InMemoryRepository(); var user = new ChatUser { Name = "foo", HashedPassword = "******" }; repository.Add(user); var service = new MembershipService(repository, crypto.Object); Assert.True(service.TryAuthenticateUser("foo", "passwords", out user)); Assert.Equal("salted", user.Salt); Assert.Equal("9ce70d2ab42c9a9012ed6f80f85ab400ef1483f70e227a42b6d77faea204db26", user.HashedPassword); }
public void DoesNotThrowIfSaltedPasswordsMatch() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "foo", Salt = "salted", HashedPassword = "******" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); ChatUser user; Assert.True(service.TryAuthenticateUser("foo", "password", out user)); }
public void DoesNotThrowIfPasswordsMatch() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "foo", HashedPassword = "******" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); ChatUser user; Assert.True(service.TryAuthenticateUser("foo", "passwords", out user)); }
public void ThrowsUserPasswordDoesNotMatchOldPassword() { var repository = new InMemoryRepository(); var user = new ChatUser { Name = "Test", Salt = "salt", HashedPassword = "******".ToSha256("salt") }; repository.Add(user); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.ChangeUserPassword(user, "passwor", "foo")); }
public void UpdatesUserName() { var repository = new InMemoryRepository(); var user = new ChatUser { Name = "Test" }; repository.Add(user); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); service.ChangeUserName(user, "Test2"); Assert.Equal("Test2", user.Name); }
public void ThrowsIfUserPasswordNotSet() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "SomeUser" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); ChatUser user; Assert.False(service.TryAuthenticateUser("SomeUser", "password", out user)); }
public void ThrowsIfUserDoesNotExist() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AuthenticateUser("SomeUser", "foo")); }
public void AddsNumberToUserNameIfTaken() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "david", Id = "1" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); service.AddUser("david", "provider", "identity", email: null); var user = repository.GetUserByIdentity("provider", "identity"); Assert.NotNull(user); Assert.Equal("david1", user.Name); Assert.Null(user.Identity); Assert.Null(user.Email); Assert.Null(user.Hash); Assert.Equal(1, user.Identities.Count); Assert.Equal("identity", user.Identities.First().Identity); }
public void AddsAuthUserToRepository() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); service.AddUser("SomeUser", "provider", "identity", "email"); var user = repository.GetUserByIdentity("provider", "identity"); Assert.NotNull(user); Assert.Equal("SomeUser", user.Name); Assert.Null(user.Identity); Assert.Null(user.Email); Assert.Equal("0c83f57c786a0b4a39efab23731c7ebc", user.Hash); Assert.Equal(1, user.Identities.Count); Assert.Equal("email", user.Identities.First().Email); }
public void ThrowsIfUserDoesNotExist() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); ChatUser user; Assert.False(service.TryAuthenticateUser("SomeUser", "foo", out user)); }
public void ThrowsIfUserPasswordDoesNotMatch() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "SomeUser", Salt = "salt", HashedPassword = "******".ToSha256("salt") }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); ChatUser user; Assert.False(service.TryAuthenticateUser("SomeUser", "foo", out user)); }
public void ThrowsIfUserPasswordDoesNotMatch() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "SomeUser", Salt = "salt", HashedPassword = "******".ToSha256("salt") }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AuthenticateUser("SomeUser", "foo")); }
public void ThrowsIfUserNameIsSame() { var repository = new InMemoryRepository(); var user = new ChatUser { Name = "Test" }; repository.Add(user); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.ChangeUserName(user, "Test")); }
public void ThrowsIfUserPasswordNotSet() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "SomeUser" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); Assert.Throws<InvalidOperationException>(() => service.AuthenticateUser("SomeUser", "password")); }
public void EnsuresSaltedPassword() { var crypto = new Mock<ICryptoService>(); crypto.Setup(c => c.CreateSalt()).Returns("salt"); var repository = new InMemoryRepository(); var user = new ChatUser { Name = "Test", Salt = null, HashedPassword = "******".ToSha256(null) }; repository.Add(user); var service = new MembershipService(repository, crypto.Object); service.ChangeUserPassword(user, "password", "password"); Assert.Equal("salt", user.Salt); Assert.Equal("password".ToSha256("salt"), user.HashedPassword); }
public void AddsAuthUserToRepository() { var repository = new InMemoryRepository(); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "SomeUser")); claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "provider")); claims.Add(new Claim(ClaimTypes.NameIdentifier, "identity")); claims.Add(new Claim(ClaimTypes.Email, "email")); var claimsIdentity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); service.AddUser(claimsPrincipal); var user = repository.GetUserByIdentity("provider", "identity"); Assert.NotNull(user); Assert.Equal("SomeUser", user.Name); Assert.Null(user.Identity); Assert.Null(user.Email); Assert.Equal("0c83f57c786a0b4a39efab23731c7ebc", user.Hash); Assert.Equal(1, user.Identities.Count); Assert.Equal("email", user.Identities.First().Email); }
public void UpatesUserPassword() { var repository = new InMemoryRepository(); var user = new ChatUser { Name = "Test", Salt = "pepper", HashedPassword = "******".ToSha256("pepper") }; repository.Add(user); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); service.ChangeUserPassword(user, "password", "password2"); Assert.Equal("password2".ToSha256("pepper"), user.HashedPassword); }
public void AddsNumberToUserNameIfTaken() { var repository = new InMemoryRepository(); repository.Add(new ChatUser { Name = "david", Id = "1" }); var service = new MembershipService(repository, new Mock<ICryptoService>().Object); var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "david")); claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "provider")); claims.Add(new Claim(ClaimTypes.NameIdentifier, "identity")); var claimsIdentity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); service.AddUser(claimsPrincipal); var user = repository.GetUserByIdentity("provider", "identity"); Assert.NotNull(user); Assert.Equal("david1", user.Name); Assert.Null(user.Identity); Assert.Null(user.Email); Assert.Null(user.Hash); Assert.Equal(1, user.Identities.Count); Assert.Equal("identity", user.Identities.First().Identity); }