Ejemplo n.º 1
0
        public void Name()
        {
            var user = new User
            {
                FirstName = "Bill",
                LastName = "Jones"
            };

            var result = Mapper.Map<UserListItem>(user);

            Assert.Equal("Jones, Bill", result.Name);
        }
Ejemplo n.º 2
0
        public void SendResetPasswordLinkThrowsExceptionWhenUserInvalid()
        {
            var users = new List<User>
            {
                new User {UserName = "******"}
            };

            var user = new User();

            _repository.Setup(x => x.GetAll()).Returns(users.AsQueryable());
            _repository.Setup(x => x.Update(It.IsAny<User>())).Callback<User>(u => user = u);

            var ex = Assert.Throws<Exception>(() => _service.SendResetPasswordLink("bogus user", "url"));

            Assert.Equal("The provided userName does not exist.", ex.Message);
            Assert.Null(user.ResetPasswordKey);
        }
Ejemplo n.º 3
0
        public void SendResetPasswordLinkShouldAddGuidToUser()
        {
            var users = new List<User>
            {
                new User {UserName = "******"}
            };

            var user = new User();

            _repository.Setup(x => x.GetAll()).Returns(users.AsQueryable());
            _repository.Setup(x => x.Update(It.IsAny<User>())).Callback<User>(u => user = u);
            _service.SendResetPasswordLink("bill", "url");

            Assert.NotNull(user.ResetPasswordKey);
        }
Ejemplo n.º 4
0
        public void ResetPassword()
        {
            var user = new User
            {
                Password = "******",
                PasswordSalt = "salt",
                ResetPasswordKey = Guid.NewGuid()
            };

            _repository.Setup(x => x.Update(It.IsAny<User>())).Callback<User>(u => user = u);
            _service.ResetPassword(user, "newpassword");

            Assert.NotEqual("password", user.Password);
            Assert.NotEqual("salt", user.PasswordSalt);
            Assert.Null(user.ResetPasswordKey);

            // new password should be encrypted
            Assert.NotEqual("newpassword", user.Password);
        }
Ejemplo n.º 5
0
        public void RegisterShouldSetDefaultRole()
        {
            var roles = new List<Role>
            {
                new Role {Id = 1, RoleName = "User"},
                new Role {Id = 2, RoleName = "Admin"}
            };

            var user = new User
            {
                UserName = "******",
                Password = "******"
            };

            _roleRepository.Setup(x => x.GetAll()).Returns(roles.AsQueryable());
            _repository.Setup(x => x.Insert(It.IsAny<User>())).Callback<User>(u => user = u);
            _service.Register(user);

            Assert.Equal(1, user.RoleId);
        }
Ejemplo n.º 6
0
        public void CreateUserShouldEncryptPassword()
        {
            var roles = new List<Role>
            {
                new Role {Id = 1, RoleName = "User"},
                new Role {Id = 2, RoleName = "Admin"}
            };

            var user = new User
            {
                Password = "******"
            };

            _repository.Setup(x => x.Insert(It.IsAny<User>())).Callback<User>(u => user = u);
            _roleRepository.Setup(x => x.GetAll()).Returns(roles.AsQueryable());
            _cryptographyService.Setup(x => x.GetSalt()).Returns("passwordsalt");
            _cryptographyService.Setup(x => x.HashPassword(It.IsAny<string>(), It.IsAny<string>())).Returns("hashedpassword");

            _service.Register(user);

            Assert.Equal("hashedpassword", user.Password);
            Assert.Equal("passwordsalt", user.PasswordSalt);
        }
Ejemplo n.º 7
0
 public void Update(User user)
 {
     _repository.Update(user);
     _repository.Save();
 }
Ejemplo n.º 8
0
 public void Restore(User user)
 {
     user.IsDeleted = false;
     _repository.Update(user);
     _repository.Save();
 }
Ejemplo n.º 9
0
        public void ResetPassword(User user, string password)
        {
            user.PasswordSalt = _cryptographyService.GetSalt();
            user.Password = _cryptographyService.HashPassword(password, user.PasswordSalt);
            user.ResetPasswordKey = null;

            _repository.Update(user);
            _repository.Save();
        }
Ejemplo n.º 10
0
        public void Register(User user)
        {
            var defaultRole = _roleRepository.GetAll().Single(x => x.RoleName == "User");

            user.PasswordSalt = _cryptographyService.GetSalt();
            user.Password = _cryptographyService.HashPassword(user.Password, user.PasswordSalt);
            user.RoleId = defaultRole.Id;

            _repository.Insert(user);
            _repository.Save();
        }
Ejemplo n.º 11
0
 public void Delete(User user)
 {
     user.IsDeleted = true;
     _repository.Update(user);
     _repository.Save();
 }