Inheritance: IMembershipService
Esempio n. 1
0
            public void ThrowsIfPasswordIsTooShort()
            {
                var repository = new InMemoryRepository();
                var service = new MembershipService(repository, new Mock<ICryptoService>().Object);

                Assert.Throws<InvalidOperationException>(() => service.AddUser("SomeUser", "email", password: "******"));
            }
Esempio n. 2
0
            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));
            }
Esempio n. 3
0
            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));
            }
Esempio n. 4
0
            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);
            }
Esempio n. 5
0
            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));
            }
Esempio n. 6
0
            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);
            }
Esempio n. 7
0
            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);
            }
Esempio n. 8
0
 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));
 }
Esempio n. 9
0
 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));
 }
Esempio n. 10
0
            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"));
            }
Esempio n. 11
0
            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);
            }
Esempio n. 12
0
            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));
            }
Esempio n. 13
0
            public void ThrowsIfUserDoesNotExist()
            {
                var repository = new InMemoryRepository();
                var service = new MembershipService(repository, new Mock<ICryptoService>().Object);

                Assert.Throws<InvalidOperationException>(() => service.AuthenticateUser("SomeUser", "foo"));
            }
Esempio n. 14
0
            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);
            }
Esempio n. 15
0
            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);
            }
Esempio n. 16
0
            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));
            }
Esempio n. 17
0
            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));
            }
Esempio n. 18
0
            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"));
            }
Esempio n. 19
0
            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"));
            }
Esempio n. 20
0
            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"));
            }
Esempio n. 21
0
            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);
            }
Esempio n. 22
0
            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);
            }
Esempio n. 23
0
            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);
            }
Esempio n. 24
0
            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);
            }