Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_graph.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import hashlib | import hashlib | ||||
import re | |||||
import textwrap | import textwrap | ||||
from urllib.parse import unquote, urlparse | |||||
import pytest | |||||
from django.http.response import StreamingHttpResponse | from django.http.response import StreamingHttpResponse | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.swhids import ExtendedObjectType, ExtendedSWHID | from swh.model.swhids import ExtendedObjectType, ExtendedSWHID | ||||
from swh.web.api.views.graph import API_GRAPH_PERM | from swh.web.api.views.graph import API_GRAPH_PERM | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.config import SWH_WEB_INTERNAL_SERVER_NAME, get_config | from swh.web.config import SWH_WEB_INTERNAL_SERVER_NAME, get_config | ||||
▲ Show 20 Lines • Show All 269 Lines • ▼ Show 20 Lines | requests_mock.get( | ||||
status_code=404, | status_code=404, | ||||
) | ) | ||||
url = reverse("api-1-graph", url_args={"graph_query": graph_query}) | url = reverse("api-1-graph", url_args={"graph_query": graph_query}) | ||||
resp = check_http_get_response(api_client, url, status_code=404) | resp = check_http_get_response(api_client, url, status_code=404) | ||||
assert resp.content_type == content_type | assert resp.content_type == content_type | ||||
assert resp.content == f'"{error_message}"'.encode() | assert resp.content == f'"{error_message}"'.encode() | ||||
@pytest.mark.parametrize( | |||||
"graph_query, query_params, expected_graph_query_params", | |||||
[ | |||||
("stats", {}, ""), | |||||
("stats", {"resolve_origins": "true"}, "resolve_origins=true"), | |||||
("stats?a=1", {}, "a=1"), | |||||
("stats%3Fb=2", {}, "b=2"), | |||||
("stats?a=1", {"resolve_origins": "true"}, "a=1&resolve_origins=true"), | |||||
("stats%3Fb=2", {"resolve_origins": "true"}, "b=2&resolve_origins=true"), | |||||
("stats/?a=1", {"a": "2"}, "a=1&a=2"), | |||||
("stats/%3Fa=1", {"a": "2"}, "a=1&a=2"), | |||||
], | |||||
) | |||||
def test_graph_query_params( | |||||
api_client, | |||||
keycloak_oidc, | |||||
requests_mock, | |||||
graph_query, | |||||
query_params, | |||||
expected_graph_query_params, | |||||
): | |||||
_authenticate_graph_user(api_client, keycloak_oidc) | |||||
requests_mock.get( | |||||
re.compile(get_config()["graph"]["server_url"]), | |||||
json=_response_json, | |||||
headers={"Content-Type": "application/json"}, | |||||
) | |||||
url = reverse( | |||||
"api-1-graph", url_args={"graph_query": graph_query}, query_params=query_params, | |||||
) | |||||
check_http_get_response(api_client, url, status_code=200) | |||||
url = requests_mock.request_history[0].url | |||||
parsed_url = urlparse(url) | |||||
assert parsed_url.path == f"/graph/{unquote(graph_query).split('?')[0]}" | |||||
assert parsed_url.query == expected_graph_query_params |