Example #1
0
def test_access_request_from_graphql_context(
    auth_starlette: Starlette,
    ttftt: TartifletteApp,
    authorization: str,
    expected_user: str,
):
    # See also: `tests/resolvers.py` for the `whoami` resolver.
    mount.starlette(auth_starlette, "/", ttftt)
    with TestClient(auth_starlette) as client:
        response = client.post("/",
                               json={"query": "{ whoami }"},
                               headers={"Authorization": authorization})
    assert response.status_code == 200
    assert response.json() == {"data": {"whoami": expected_user}}
Example #2
0
def test_endpoint_paths_when_mounted(starlette: Starlette, engine: Engine,
                                     mount_path: str) -> None:
    ttftt = TartifletteApp(engine=engine, graphiql=True, subscriptions=True)
    mount.starlette(starlette, mount_path, ttftt)

    with TestClient(starlette) as client:
        response = client.get(mount_path, headers={"accept": "text/html"})

    assert response.status_code == 200

    graphql_endpoint = mount_path + "/"
    assert fr"var graphQLEndpoint = `{graphql_endpoint}`;" in response.text

    subscriptions_endpoint = mount_path + "/subscriptions"
    assert fr"var subscriptionsEndpoint = `{subscriptions_endpoint}`;" in response.text
Example #3
0
def test_starlette_mount(starlette: Starlette, engine: Engine, mount_path: str,
                         path: str):
    kwargs = omit_none({"engine": engine, "path": path})

    app = TartifletteApp(**kwargs)
    mount.starlette(starlette, mount_path, app)

    query = "{ hello }"
    full_path = mount_path.rstrip("/") + ("/" if path is None else path)
    assert "//" not in full_path

    url = f"{full_path}?query={query}"
    with TestClient(starlette) as client:
        response = client.get(url)
        graphiql_response = client.get(url, headers={"accept": "text/html"})

    assert response.status_code == 200
    assert response.json() == {"data": {"hello": "Hello stranger"}}

    assert graphiql_response.status_code == 200
    assert full_path in graphiql_response.text