def update_password_hash_for_user(self, user_name, new_password_hash): """Updates the password hash associated with the given user, if they exist, or returns None otherwise""" if self.user_exists(user_name): self.rconn.set(redis_keys.for_user_password_hash(user_name), new_password_hash) else: raise UserNotFoundError("Username is not found")
def test_retrieve_password_hash_for_user(dao): rconn = dao.rconn with pytest.raises(UserNotFoundError): dao.retrieve_password_hash_for_user(user) rconn.sadd(redis_keys.for_user_set(), user) rconn.set(redis_keys.for_user_password_hash(user), passhash.encode("utf8")) assert passhash.encode('utf8') == dao.retrieve_password_hash_for_user(user)
def test_add_user(dao): dao.add_user(user, email, passhash) rconn = dao.rconn assert email.encode('utf8') == rconn.get(redis_keys.for_user_email(user)) assert passhash.encode('utf8') == rconn.get( redis_keys.for_user_password_hash(user)) assert rconn.sismember(redis_keys.for_user_set(), user)
def test_retrieve_password_hash_for_user(): mock_rconn = mock.Mock() dao = UserDao(mock_rconn) dao.user_exists = mock.Mock(return_value=True) username = "******" dao.retrieve_password_hash_for_user(username) mock_rconn.get.assert_called_once_with( redis_keys.for_user_password_hash(username))
def test_update_password_has_for_user(): mock_rconn = mock.Mock() dao = UserDao(mock_rconn) dao.user_exists = mock.Mock(return_value=True) username = "******" passhash = hashlib.sha3_512(b"password").hexdigest() dao.update_password_hash_for_user(username, passhash) assert mock_rconn.set.called_once_with( redis_keys.for_user_password_hash(username), "asdf")
def add_user(self, user_name, user_email, user_password_hash): """Registers the given user, with the given name, email, and password hash. :raise ValueError: if the username is taken :raise RegistrationError: if the user couldn't be registered. """ try: if self.user_exists(user_name): raise ValueError("username is taken") (self.rconn.pipeline().sadd( redis_keys.for_user_set(), user_name).set(redis_keys.for_user_password_hash(user_name), user_password_hash).set( redis_keys.for_user_email(user_name), user_email).execute()) except redis.RedisError as e: raise RegistrationError( f"Could not register user {user_name} with email {user_email}", e)
def retrieve_password_hash_for_user(self, user_name): """Returns the password hash associated with the given user, if they exist, or returns None otherwise""" if self.user_exists(user_name): return self.rconn.get(redis_keys.for_user_password_hash(user_name)) else: raise UserNotFoundError("Username is not found")