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 hypothesis import given | from hypothesis import given | ||||
from django.http.response import StreamingHttpResponse | |||||
from swh.model.identifiers import ORIGIN, SNAPSHOT, swhid | from swh.model.identifiers import ORIGIN, SNAPSHOT, swhid | ||||
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 | ||||
from swh.web.tests.auth.keycloak_mock import mock_keycloak | from swh.web.tests.auth.keycloak_mock import mock_keycloak | ||||
from swh.web.tests.auth.sample_data import oidc_profile | from swh.web.tests.auth.sample_data import oidc_profile | ||||
from swh.web.tests.strategies import origin | from swh.web.tests.strategies import origin | ||||
from swh.web.tests.utils import check_http_get_response | from swh.web.tests.utils import check_http_get_response | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | response_text = textwrap.dedent( | ||||
swh:1:cnt:07069b38087f88ec192d2c9aff75a502476fd17d | swh:1:cnt:07069b38087f88ec192d2c9aff75a502476fd17d | ||||
swh:1:cnt:f045ee845c7f14d903a2c035b2691a7c400c01f0 | swh:1:cnt:f045ee845c7f14d903a2c035b2691a7c400c01f0 | ||||
""" | """ | ||||
) | ) | ||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + graph_query, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_text, | text=response_text, | ||||
headers={"Content-Type": "text/plain"}, | 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 resp.content == response_text.encode() | assert isinstance(resp, StreamingHttpResponse) | ||||
assert b"".join(resp.streaming_content) == response_text.encode() | |||||
_response_json = { | _response_json = { | ||||
"counts": {"nodes": 17075708289, "edges": 196236587976}, | "counts": {"nodes": 17075708289, "edges": 196236587976}, | ||||
"ratios": { | "ratios": { | ||||
"compression": 0.16, | "compression": 0.16, | ||||
"bits_per_node": 58.828, | "bits_per_node": 58.828, | ||||
"bits_per_edge": 5.119, | "bits_per_edge": 5.119, | ||||
Show All 36 Lines | response_ndjson = textwrap.dedent( | ||||
["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ["swh:1:dir:644dd466d8ad527ea3a609bfd588a3244e6dafcb",\ | ||||
"swh:1:cnt:eaf025ad54b94b2fdda26af75594cfae3491ec75"] | "swh:1:cnt:eaf025ad54b94b2fdda26af75594cfae3491ec75"] | ||||
""" | """ | ||||
) | ) | ||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + graph_query, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_ndjson, | text=response_ndjson, | ||||
headers={"Content-Type": "application/x-ndjson"}, | headers={ | ||||
"Content-Type": "application/x-ndjson", | |||||
"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 resp.content_type == "application/x-ndjson" | assert isinstance(resp, StreamingHttpResponse) | ||||
assert resp.content == response_ndjson.encode() | assert resp["Content-Type"] == "application/x-ndjson" | ||||
assert b"".join(resp.streaming_content) == response_ndjson.encode() | |||||
@given(origin()) | @given(origin()) | ||||
def test_graph_response_resolve_origins( | def test_graph_response_resolve_origins( | ||||
archive_data, api_client, mocker, requests_mock, origin | archive_data, api_client, mocker, requests_mock, origin | ||||
): | ): | ||||
hasher = hashlib.sha1() | hasher = hashlib.sha1() | ||||
hasher.update(origin["url"].encode()) | hasher.update(origin["url"].encode()) | ||||
Show All 23 Lines | ): | ||||
): | ): | ||||
# 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 | ||||
requests_mock.get( | requests_mock.get( | ||||
get_config()["graph"]["server_url"] + graph_query, | get_config()["graph"]["server_url"] + graph_query, | ||||
text=response_text, | text=response_text, | ||||
headers={"Content-Type": content_type}, | 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) | ||||
assert resp.content_type == content_type | assert isinstance(resp, StreamingHttpResponse) | ||||
assert resp.content == response_text.encode() | assert resp["Content-Type"] == content_type | ||||
assert b"".join(resp.streaming_content) == response_text.encode() | |||||
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", "resolve_origins": "true"}, | query_params={"direction": "backward", "resolve_origins": "true"}, | ||||
) | ) | ||||
resp = check_http_get_response(api_client, url, status_code=200) | resp = check_http_get_response(api_client, url, status_code=200) | ||||
assert resp.content_type == content_type | assert isinstance(resp, StreamingHttpResponse) | ||||
assert resp["Content-Type"] == content_type | |||||
assert ( | assert ( | ||||
resp.content == response_text.replace(origin_swhid, origin["url"]).encode() | b"".join(resp.streaming_content) | ||||
== response_text.replace(origin_swhid, origin["url"]).encode() | |||||
) | ) | ||||
def test_graph_response_resolve_origins_nothing_to_do( | def test_graph_response_resolve_origins_nothing_to_do( | ||||
api_client, mocker, requests_mock | api_client, mocker, requests_mock | ||||
): | ): | ||||
_authenticate_graph_user(api_client, mocker) | _authenticate_graph_user(api_client, mocker) | ||||
Show All 17 Lines |