Exemplo n.º 1
0
def generate_farm_registration_link():
    token = create_farm_api_token(farm_id=[],
                                  scopes=["farm:create", "farm:info"])

    server_host = settings.SERVER_HOST
    link = f"{server_host}/register-farm?api_token={token.decode()}"

    return link
Exemplo n.º 2
0
def generate_farm_authorization_link(farm_id):
    token = create_farm_api_token(
        farm_id=[farm_id], scopes=["farm:read", "farm:authorize", "farm.info"])

    server_host = settings.SERVER_HOST
    link = f"{server_host}/authorize-farm/?farm_id={farm_id}&api_token={token.decode()}"

    return link
Exemplo n.º 3
0
def test_farm_create_oauth_scope():
    def settings_open_registration():
        return Settings(AGGREGATOR_OPEN_FARM_REGISTRATION=True,
                        AGGREGATOR_INVITE_FARM_REGISTRATION=True)

    def settings_invite_registration():
        return Settings(AGGREGATOR_OPEN_FARM_REGISTRATION=False,
                        AGGREGATOR_INVITE_FARM_REGISTRATION=True)

    def settings_closed_registration():
        return Settings(AGGREGATOR_OPEN_FARM_REGISTRATION=False,
                        AGGREGATOR_INVITE_FARM_REGISTRATION=False)

    client = TestClient(app)

    # Disable Open Farm Registration, assert the endpoint is not publicly accessible.
    app.dependency_overrides[utils.get_settings] = settings_closed_registration
    r = client.post(f"{settings.API_V1_STR}/farms/")
    assert r.status_code == 401

    # Disable Invite Farm Registration, assert the endpoint is not accessible with access token.
    token = create_farm_api_token(farm_id=[],
                                  scopes=["farm:create", "farm:info"])
    app.dependency_overrides[utils.get_settings] = settings_closed_registration
    r = client.post(f"{settings.API_V1_STR}/farms/",
                    headers={"api-token": token.decode("utf-8")})
    assert r.status_code == 401

    # Enable Invite Farm Registration, assert the endpoint is not publicly accessible.
    app.dependency_overrides[utils.get_settings] = settings_invite_registration
    r = client.post(f"{settings.API_V1_STR}/farms/")
    assert r.status_code == 401

    # Enable Invite Farm Registration, assert the endpoint is accessible with access token.
    token = create_farm_api_token(farm_id=[],
                                  scopes=["farm:create", "farm:info"])
    app.dependency_overrides[utils.get_settings] = settings_invite_registration
    r = client.post(f"{settings.API_V1_STR}/farms/",
                    headers={"api-token": token.decode("utf-8")})
    assert r.status_code == 422

    # Enable Open Farm Registration, assert the endpoint is publicly accessible.
    app.dependency_overrides[utils.get_settings] = settings_open_registration
    r = client.post(f"{settings.API_V1_STR}/farms/")
    assert r.status_code == 422
Exemplo n.º 4
0
def test_create_api_token():
    farm_id_list = [1, 2, 3]
    scopes = ["scope1", "scope2"]

    token = create_farm_api_token(farm_id_list, scopes)
    assert token is not None

    token_data = _validate_token(token)
    assert token_data is not None

    # api_tokens are not associated with a user.
    assert token_data.user_id is None

    # Check that farm_id and scopes match.
    assert token_data.farm_id == farm_id_list
    assert token_data.scopes == scopes