Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_graph.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 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 json | import json | ||||
import textwrap | import textwrap | ||||
from urllib.parse import quote | |||||
from hypothesis import given | from hypothesis import given | ||||
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.identifiers import ExtendedObjectType, ExtendedSWHID | from swh.model.identifiers import ExtendedObjectType, ExtendedSWHID | ||||
from swh.web.api.views.graph import API_GRAPH_PERM | from swh.web.api.views.graph import API_GRAPH_PERM | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | response_text = textwrap.dedent( | ||||
swh:1:cnt:5f9981d52202815aa947f85b9dfa191b66f51138 | swh:1:cnt:5f9981d52202815aa947f85b9dfa191b66f51138 | ||||
swh:1:cnt:00a685ec51bcdf398c15d588ecdedb611dbbab4b | swh:1:cnt:00a685ec51bcdf398c15d588ecdedb611dbbab4b | ||||
swh:1:cnt:e1cf1ea335106a0197a2f92f7804046425a7d3eb | swh:1:cnt:e1cf1ea335106a0197a2f92f7804046425a7d3eb | ||||
swh:1:cnt:07069b38087f88ec192d2c9aff75a502476fd17d | swh:1:cnt:07069b38087f88ec192d2c9aff75a502476fd17d | ||||
swh:1:cnt:f045ee845c7f14d903a2c035b2691a7c400c01f0 | swh:1:cnt:f045ee845c7f14d903a2c035b2691a7c400c01f0 | ||||
""" | """ | ||||
) | ) | ||||
# ensure compatibility with all requests-mock versions | |||||
# https://github.com/jamielennox/requests-mock/commit/f072845 | |||||
for path in (graph_query, quote(graph_query)): | |||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + path, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_text, | text=response_text, | ||||
headers={"Content-Type": "text/plain", "Transfer-Encoding": "chunked"}, | headers={"Content-Type": "text/plain", "Transfer-Encoding": "chunked"}, | ||||
) | ) | ||||
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( | resp = check_http_get_response( | ||||
api_client, url, status_code=200, content_type="text/plain" | api_client, url, status_code=200, content_type="text/plain" | ||||
) | ) | ||||
assert isinstance(resp, StreamingHttpResponse) | assert isinstance(resp, StreamingHttpResponse) | ||||
assert b"".join(resp.streaming_content) == response_text.encode() | assert b"".join(resp.streaming_content) == response_text.encode() | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | response_ndjson = textwrap.dedent( | ||||
"swh:1:cnt:acfb7cabd63b368a03a9df87670ece1488c8bce0"] | "swh:1:cnt:acfb7cabd63b368a03a9df87670ece1488c8bce0"] | ||||
["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ||||
"swh:1:cnt:2a0837708151d76edf28fdbb90dc3eabc676cff3"] | "swh:1:cnt:2a0837708151d76edf28fdbb90dc3eabc676cff3"] | ||||
["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ||||
"swh:1:cnt:eaf025ad54b94b2fdda26af75594cfae3491ec75"] | "swh:1:cnt:eaf025ad54b94b2fdda26af75594cfae3491ec75"] | ||||
""" | """ | ||||
) | ) | ||||
# ensure compatibility with all requests-mock versions | |||||
# https://github.com/jamielennox/requests-mock/commit/f072845 | |||||
for path in (graph_query, quote(graph_query)): | |||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + path, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_ndjson, | text=response_ndjson, | ||||
headers={ | headers={ | ||||
"Content-Type": "application/x-ndjson", | "Content-Type": "application/x-ndjson", | ||||
"Transfer-Encoding": "chunked", | "Transfer-Encoding": "chunked", | ||||
}, | }, | ||||
) | ) | ||||
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=200) | resp = check_http_get_response(api_client, url, status_code=200) | ||||
assert isinstance(resp, StreamingHttpResponse) | assert isinstance(resp, StreamingHttpResponse) | ||||
assert resp["Content-Type"] == "application/x-ndjson" | assert resp["Content-Type"] == "application/x-ndjson" | ||||
assert b"".join(resp.streaming_content) == response_ndjson.encode() | assert b"".join(resp.streaming_content) == response_ndjson.encode() | ||||
Show All 33 Lines | for graph_query, response_text, content_type in ( | ||||
f'["{snapshot_swhid}", "{origin_swhid}"]\n', | f'["{snapshot_swhid}", "{origin_swhid}"]\n', | ||||
"application/x-ndjson", | "application/x-ndjson", | ||||
), | ), | ||||
): | ): | ||||
# set two lines response to check resolved origins cache | # set two lines response to check resolved origins cache | ||||
response_text = response_text + response_text | response_text = response_text + response_text | ||||
# ensure compatibility with all requests-mock versions | |||||
# https://github.com/jamielennox/requests-mock/commit/f072845 | |||||
for path in (graph_query, quote(graph_query)): | |||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + path, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_text, | text=response_text, | ||||
headers={"Content-Type": content_type, "Transfer-Encoding": "chunked"}, | headers={"Content-Type": content_type, "Transfer-Encoding": "chunked"}, | ||||
) | ) | ||||
url = reverse( | url = reverse( | ||||
"api-1-graph", | "api-1-graph", | ||||
url_args={"graph_query": graph_query}, | url_args={"graph_query": graph_query}, | ||||
query_params={"direction": "backward"}, | query_params={"direction": "backward"}, | ||||
) | ) | ||||
resp = check_http_get_response(api_client, url, status_code=200) | resp = check_http_get_response(api_client, url, status_code=200) | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |