def test_delete_user_returns_one_when_user_deleted_successfully(self):
        user_id = 'userid1'
        password_hash = 'hash1'

        db_access.create_user(user_id, password_hash)
        result = db_access.delete_user(user_id)
        assert result == 1
    def test_update_failed_logins_updates_the_user_when_one_exists(self):
        user_id = 'userid1'
        failed_logins = 1234

        db_access.create_user(user_id, 'hash1')
        db_access.update_failed_logins(user_id, 1234)

        assert db_access.get_failed_logins(user_id) == failed_logins
    def test_delete_user_deletes_user_when_one_exists(self):
        user_id = 'userid1'
        password_hash = 'hash1'

        db_access.create_user(user_id, password_hash)
        assert db_access.get_user(user_id, password_hash) is not None
        db_access.delete_user(user_id)
        assert db_access.get_user(user_id, password_hash) is None
    def test_create_user_does_not_change_existing_user_when_duplicate(self):
        user_id = 'userid1'
        password_hash_1 = 'hash1'
        password_hash_2 = 'hash2'

        db_access.create_user(user_id, password_hash_1)
        db_access.create_user(user_id, password_hash_2)

        assert db_access.get_user(user_id, password_hash_1) is not None
        assert db_access.get_user(user_id, password_hash_2) is None
    def test_update_user_updates_user_when_one_exists(self):
        user_id = 'userid1'
        password_hash_1 = 'hash1'
        password_hash_2 = 'hash2'

        db_access.create_user(user_id, password_hash_1)
        db_access.update_user(user_id, password_hash_2)

        user = db_access.get_user(user_id, password_hash_2)
        assert user is not None
    def test_create_user_creates_new_user_when_id_not_used_yet(self):
        user_id = 'userid1'
        password_hash = 'passwordhash1'

        db_access.create_user(user_id, password_hash)
        user = db_access.get_user(user_id, password_hash)

        assert user is not None
        assert user.user_id == user_id
        assert user.password_hash == password_hash
        assert user.failed_logins == 0
def create_user():
    request_json = _try_get_request_json(request)
    if request_json and _is_create_request_data_valid(request_json):
        user = request_json['user']
        user_id = user['user_id']
        password = user['password']
        # TODO: common code
        password_hash = security.get_user_password_hash(
            user_id,
            password,
            app.config['PASSWORD_SALT']
        )
        if db_access.create_user(user_id, password_hash):
            auditing.audit('Created user {}'.format(user_id))
            return Response(json.dumps({'created': True}), mimetype=JSON_CONTENT_TYPE)
        else:
            response_body = json.dumps({'error': 'User already exists'})
            return Response(response_body, 409, mimetype=JSON_CONTENT_TYPE)
    else:
        return INVALID_REQUEST_RESPONSE
 def test_create_user_returns_false_when_duplicate(self):
     user_id = 'userid1'
     db_access.create_user(user_id, 'hash1')
     result = db_access.create_user(user_id, 'hash2')
     assert result is False
 def test_create_user_returns_true_when_user_created(self):
     result = db_access.create_user('userid1', 'hash1')
     assert result is True
 def test_update_failed_logins_returns_one_when_user_successfully_updated(self):
     user_id = 'userid1'
     db_access.create_user(user_id, 'hash1')
     result = db_access.update_failed_logins(user_id, 1234)
     assert result == 1
    def test_update_user_returns_one_when_user_updated_successfully(self):
        user_id = 'userid1'

        db_access.create_user(user_id, 'hash1')
        result = db_access.update_user(user_id, 'hash2')
        assert result == 1