def test_should_get_NotAuthorizedError_if_the_admin_is_not_logged_in(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: None,
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "id": "id",
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.register_user(data)
    assert exception.value.data == {
        "msg": "This operation is not authorized. Please, log in."
    }
def test_should_register_the_new_user_if_the_request_is_OK_and_the_admin_is_logged_in(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "id": "test-id",
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    user_interactor.register_user(data)
    user = user_interactor.get_user_by_id("test-id")
    assert user.id == "test-id"
    assert user.username == "test-username"
    assert user.name == "test-name"
    assert user.password == hash_password("test-password")
def test_should_raise_BadRequestError_if_there_are_not_the_required_fields_in_the_request(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)

    all_fields_missing = {}
    with pytest.raises(BadRequestError) as exception:
        user_interactor.register_user(all_fields_missing)
    assert exception.value.data == {
        "id": "REQUIRED",
        "username": "******",
        "name": "REQUIRED",
        "password": "******",
    }

    id_missing = {
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.register_user(id_missing)
    assert exception.value.data == {
        "id": "REQUIRED",
    }

    username_missing = {
        "id": "test-id",
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.register_user(username_missing)
    assert exception.value.data == {
        "username": "******",
    }

    name_missing = {
        "id": "test-id",
        "username": "******",
        "password": "******",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.register_user(name_missing)
    assert exception.value.data == {
        "name": "REQUIRED",
    }

    password_missing = {
        "id": "test-id",
        "username": "******",
        "name": "test-name",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.register_user(password_missing)
    assert exception.value.data == {
        "password": "******",
    }