Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_raw.py
- This file was added.
# Copyright (C) 2015-2019 The Software Heritage developers | ||||||||||||||||||||||||||
vlorentz: ditto | ||||||||||||||||||||||||||
# See the AUTHORS file at the top-level directory of this distribution | ||||||||||||||||||||||||||
# License: GNU Affero General Public License version 3, or any later version | ||||||||||||||||||||||||||
# See top-level LICENSE file for more information | ||||||||||||||||||||||||||
import hashlib | ||||||||||||||||||||||||||
from swh.model.git_objects import content_git_object | ||||||||||||||||||||||||||
from swh.web.common.utils import reverse | ||||||||||||||||||||||||||
from swh.web.tests.data import random_swhid | ||||||||||||||||||||||||||
from swh.web.tests.utils import ( | ||||||||||||||||||||||||||
check_api_get_responses, | ||||||||||||||||||||||||||
check_http_get_response, | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
def test_api_raw_not_found(api_client): | ||||||||||||||||||||||||||
unknown_swhid = random_swhid() | ||||||||||||||||||||||||||
url = reverse("api-1-raw-object", url_args={"swhid": str(unknown_swhid)}) | ||||||||||||||||||||||||||
rv = check_api_get_responses(api_client, url, status_code=404) | ||||||||||||||||||||||||||
assert rv.data == { | ||||||||||||||||||||||||||
"exception": "NotFoundExc", | ||||||||||||||||||||||||||
"reason": f"Object with id {unknown_swhid} not found.", | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Done Inline Actions
Could you make this test run for all SWHID types? vlorentz: Could you make this test run for all SWHID types? | ||||||||||||||||||||||||||
Done Inline ActionsI have made a fixture that does this. Ericson2314: I have made a fixture that does this. | ||||||||||||||||||||||||||
def test_api_raw_content(api_client, archive_data, content): | ||||||||||||||||||||||||||
object_id = content["sha1_git"] | ||||||||||||||||||||||||||
object_ty = "cnt" | ||||||||||||||||||||||||||
url = reverse( | ||||||||||||||||||||||||||
"api-1-raw-object", | ||||||||||||||||||||||||||
url_args={"swhid": f"swh:1:{object_ty}:{object_id}"}, | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
rv = check_http_get_response(api_client, url, status_code=200) | ||||||||||||||||||||||||||
assert rv["Content-Type"] == "application/octet-stream" | ||||||||||||||||||||||||||
assert ( | ||||||||||||||||||||||||||
rv["Content-disposition"] | ||||||||||||||||||||||||||
== f"attachment; filename=swh_1_{object_ty}_{object_id}_raw" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
expected_data = archive_data.content_get_data(object_id) | ||||||||||||||||||||||||||
assert rv.content == content_git_object(expected_data) | ||||||||||||||||||||||||||
sha1_git = hashlib.new("sha1", rv.content).digest() | ||||||||||||||||||||||||||
assert sha1_git == object_id | ||||||||||||||||||||||||||
def _test_api_raw_hash(api_client, archive_data, object_id, object_ty): | ||||||||||||||||||||||||||
url = reverse( | ||||||||||||||||||||||||||
"api-1-raw-object", | ||||||||||||||||||||||||||
url_args={"swhid": f"swh:1:{object_ty}:{object_id}"}, | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
rv = check_http_get_response(api_client, url, status_code=200) | ||||||||||||||||||||||||||
assert rv["Content-Type"] == "application/octet-stream" | ||||||||||||||||||||||||||
assert ( | ||||||||||||||||||||||||||
rv["Content-disposition"] | ||||||||||||||||||||||||||
== f"attachment; filename=swh_1_{object_ty}_{object_id}_raw" | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
sha1_git = hashlib.new("sha1", rv.content).digest() | ||||||||||||||||||||||||||
assert sha1_git == object_id | ||||||||||||||||||||||||||
def test_api_raw_directory(api_client, archive_data, directory): | ||||||||||||||||||||||||||
_test_api_raw_hash(api_client, archive_data, directory, "dir") | ||||||||||||||||||||||||||
def test_api_raw_revision(api_client, archive_data, revision): | ||||||||||||||||||||||||||
_test_api_raw_hash(api_client, archive_data, revision["id"], "rev") | ||||||||||||||||||||||||||
def test_api_raw_release(api_client, archive_data, release): | ||||||||||||||||||||||||||
_test_api_raw_hash(api_client, archive_data, release["id"], "rel") | ||||||||||||||||||||||||||
def test_api_raw_snapshot(api_client, archive_data, snapshot): | ||||||||||||||||||||||||||
_test_api_raw_hash(api_client, archive_data, snapshot["id"], "snp") |
ditto