def test_query_failure():
    """Mock server failure response and assert SystemExit"""
    with patch.object(PublicationDataApi, 'call_api', new=dummy_call_api):
        client = PublicationDataApi("dummy-url", "dummy-user",
                                    "dummy-password")
        with pytest.raises(PublicationDataApiException):
            _ = client.run_query("{query{}}")
def test_no_header_without_setting():
    """Make sure that the endpoint has no authorization header if either username or password is missing"""
    client1 = PublicationDataApi("no-url", "dummy", None)
    client2 = PublicationDataApi("no-url", None, "abc")
    client3 = PublicationDataApi("no-url", None, None)
    headers = [
        client.endpoint.base_headers for client in [client1, client2, client3]
    ]
    assert 'Authorization' not in headers[0]
    assert 'Authorization' not in headers[1]
    assert 'Authorization' not in headers[2]
Ejemplo n.º 3
0
def main(size, after, url, username, password):
    """Console script for srgssr_publication_data_api."""
    variables = {'first': size}
    if after:
        variables['after'] = after
    client = PublicationDataApi(url, username, password)
    op = client.query_op(first=non_null(int), after=str)
    op.faro_items(first=Variable('first'), after=Variable('after'))
    print(f"Executing GraphQL Query: {op}")
    result=client.run_query(op, variables)
    if result:
        print(result)
        return 0
    else:
        return 1
def program():
    # Retrieve environment variables
    username = os.getenv('USER_NAME')
    if username is None:
        raise OSError("USER_NAME environment is not set.")

    password = os.getenv('USER_PASSWORD')
    if password is None:
        raise OSError("USER_PASSWORD environment is not set.")

    url = os.getenv('PDP_API')
    if url is None:
        raise OSError("PDP_API environment is not set.")

    # retrieve available query types
    client = PublicationDataApi(url, username, password)
    op = client.query_op()

    # define query
    op.faro_items(first=3)

    # print graphQL query
    print("GraphQL Query:")
    print(op)

    # execute query on endpoint
    data = client.run_query(op)

    # interpret results into native objects
    results = data.faro_items
    print("GraphQL Results:")
    print(results)

    print(f"Number of Results: {len(results)}")

    print(f"Type of the result: {type(results)}")
def test_operation_is_working():
    """Test that the query operation can be retrieved easily"""
    client = PublicationDataApi("dummy", "dummy", "dummy")
    op = client.query_op()
    assert op is not None
    assert isinstance(op, Operation)
def test_get_endpoint_from_env():
    """Test endpoint object creation"""
    client = PublicationDataApi("dummy", "dummy", "dummy")
    assert client.endpoint is not None
def test_raise_missing_endpoint():
    """Remove env variable PDP_API and assert OSError"""
    with pytest.raises(ValueError):
        _ = PublicationDataApi(None, None, None)
def test_assemble_authorization_headers():
    """Test assembly of basic HTTP authentication headers"""
    client = PublicationDataApi("no-url", "dummy", "password")
    headers = client.endpoint.base_headers
    assert 'Authorization' in headers
    assert headers['Authorization'] == 'Basic ZHVtbXk6cGFzc3dvcmQ='