Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_snapshot.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-2021 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 random | import random | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.model import Snapshot | from swh.model.model import Snapshot | ||||
from swh.web.api.utils import enrich_snapshot | from swh.web.api.utils import enrich_snapshot | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.data import random_sha1 | from swh.web.tests.data import random_sha1 | ||||
from swh.web.tests.strategies import new_snapshot, snapshot | from swh.web.tests.strategies import new_snapshot | ||||
from swh.web.tests.utils import check_api_get_responses, check_http_get_response | from swh.web.tests.utils import check_api_get_responses, check_http_get_response | ||||
@given(snapshot()) | |||||
def test_api_snapshot(api_client, archive_data, snapshot): | def test_api_snapshot(api_client, archive_data, snapshot): | ||||
url = reverse("api-1-snapshot", url_args={"snapshot_id": snapshot}) | url = reverse("api-1-snapshot", url_args={"snapshot_id": snapshot}) | ||||
rv = check_api_get_responses(api_client, url, status_code=200) | rv = check_api_get_responses(api_client, url, status_code=200) | ||||
expected_data = {**archive_data.snapshot_get(snapshot), "next_branch": None} | expected_data = {**archive_data.snapshot_get(snapshot), "next_branch": None} | ||||
expected_data = enrich_snapshot(expected_data, rv.wsgi_request) | expected_data = enrich_snapshot(expected_data, rv.wsgi_request) | ||||
assert rv.data == expected_data | assert rv.data == expected_data | ||||
@given(snapshot()) | |||||
def test_api_snapshot_paginated(api_client, archive_data, snapshot): | def test_api_snapshot_paginated(api_client, archive_data, snapshot): | ||||
branches_offset = 0 | branches_offset = 0 | ||||
branches_count = 2 | branches_count = 2 | ||||
snapshot_branches = [] | snapshot_branches = [] | ||||
for k, v in sorted(archive_data.snapshot_get(snapshot)["branches"].items()): | for k, v in sorted(archive_data.snapshot_get(snapshot)["branches"].items()): | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | while branches_offset < len(snapshot_branches): | ||||
else: | else: | ||||
assert not rv.has_header("Link") | assert not rv.has_header("Link") | ||||
url = reverse("api-1-snapshot", url_args={"snapshot_id": snapshot}) | url = reverse("api-1-snapshot", url_args={"snapshot_id": snapshot}) | ||||
rv = check_api_get_responses(api_client, url, status_code=200) | rv = check_api_get_responses(api_client, url, status_code=200) | ||||
assert rv.data == whole_snapshot | assert rv.data == whole_snapshot | ||||
@given(snapshot()) | |||||
def test_api_snapshot_filtered(api_client, archive_data, snapshot): | def test_api_snapshot_filtered(api_client, archive_data, snapshot): | ||||
snapshot_branches = [] | snapshot_branches = [] | ||||
for k, v in sorted(archive_data.snapshot_get(snapshot)["branches"].items()): | for k, v in sorted(archive_data.snapshot_get(snapshot)["branches"].items()): | ||||
snapshot_branches.append( | snapshot_branches.append( | ||||
{"name": k, "target_type": v["target_type"], "target": v["target"]} | {"name": k, "target_type": v["target_type"], "target": v["target"]} | ||||
) | ) | ||||
Show All 20 Lines | def test_api_snapshot_errors(api_client): | ||||
url = reverse("api-1-snapshot", url_args={"snapshot_id": "63ce369"}) | url = reverse("api-1-snapshot", url_args={"snapshot_id": "63ce369"}) | ||||
check_api_get_responses(api_client, url, status_code=400) | check_api_get_responses(api_client, url, status_code=400) | ||||
url = reverse("api-1-snapshot", url_args={"snapshot_id": unknown_snapshot_}) | url = reverse("api-1-snapshot", url_args={"snapshot_id": unknown_snapshot_}) | ||||
check_api_get_responses(api_client, url, status_code=404) | check_api_get_responses(api_client, url, status_code=404) | ||||
@given(snapshot()) | |||||
def test_api_snapshot_uppercase(api_client, snapshot): | def test_api_snapshot_uppercase(api_client, snapshot): | ||||
url = reverse( | url = reverse( | ||||
"api-1-snapshot-uppercase-checksum", url_args={"snapshot_id": snapshot.upper()} | "api-1-snapshot-uppercase-checksum", url_args={"snapshot_id": snapshot.upper()} | ||||
) | ) | ||||
resp = check_http_get_response(api_client, url, status_code=302) | resp = check_http_get_response(api_client, url, status_code=302) | ||||
redirect_url = reverse( | redirect_url = reverse( | ||||
Show All 27 Lines |