Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_revision.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-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 | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.from_disk import DentryPerms | from swh.model.from_disk import DentryPerms | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Directory, | Directory, | ||||
DirectoryEntry, | DirectoryEntry, | ||||
Revision, | Revision, | ||||
RevisionType, | RevisionType, | ||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
) | ) | ||||
from swh.web.api.utils import enrich_content, enrich_directory_entry, enrich_revision | from swh.web.api.utils import enrich_content, enrich_directory_entry, enrich_revision | ||||
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_person, new_swh_date, revision | from swh.web.tests.strategies import new_person, new_swh_date | ||||
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(revision()) | |||||
def test_api_revision(api_client, archive_data, revision): | def test_api_revision(api_client, archive_data, revision): | ||||
url = reverse("api-1-revision", url_args={"sha1_git": revision}) | url = reverse("api-1-revision", url_args={"sha1_git": revision}) | ||||
rv = check_api_get_responses(api_client, url, status_code=200) | rv = check_api_get_responses(api_client, url, status_code=200) | ||||
expected_revision = archive_data.revision_get(revision) | expected_revision = archive_data.revision_get(revision) | ||||
enrich_revision(expected_revision, rv.wsgi_request) | enrich_revision(expected_revision, rv.wsgi_request) | ||||
assert rv.data == expected_revision | assert rv.data == expected_revision | ||||
def test_api_revision_not_found(api_client): | def test_api_revision_not_found(api_client): | ||||
unknown_revision_ = random_sha1() | unknown_revision_ = random_sha1() | ||||
url = reverse("api-1-revision", url_args={"sha1_git": unknown_revision_}) | url = reverse("api-1-revision", url_args={"sha1_git": unknown_revision_}) | ||||
rv = check_api_get_responses(api_client, url, status_code=404) | rv = check_api_get_responses(api_client, url, status_code=404) | ||||
assert rv.data == { | assert rv.data == { | ||||
"exception": "NotFoundExc", | "exception": "NotFoundExc", | ||||
"reason": "Revision with sha1_git %s not found." % unknown_revision_, | "reason": "Revision with sha1_git %s not found." % unknown_revision_, | ||||
} | } | ||||
@given(revision()) | |||||
def test_api_revision_raw_ok(api_client, archive_data, revision): | def test_api_revision_raw_ok(api_client, archive_data, revision): | ||||
url = reverse("api-1-revision-raw-message", url_args={"sha1_git": revision}) | url = reverse("api-1-revision-raw-message", url_args={"sha1_git": revision}) | ||||
expected_message = archive_data.revision_get(revision)["message"] | expected_message = archive_data.revision_get(revision)["message"] | ||||
rv = check_http_get_response(api_client, url, status_code=200) | rv = check_http_get_response(api_client, url, status_code=200) | ||||
assert rv["Content-Type"] == "application/octet-stream" | assert rv["Content-Type"] == "application/octet-stream" | ||||
assert rv.content == expected_message.encode() | assert rv.content == expected_message.encode() | ||||
def test_api_revision_raw_ko_no_rev(api_client): | def test_api_revision_raw_ko_no_rev(api_client): | ||||
unknown_revision_ = random_sha1() | unknown_revision_ = random_sha1() | ||||
url = reverse( | url = reverse( | ||||
"api-1-revision-raw-message", url_args={"sha1_git": unknown_revision_} | "api-1-revision-raw-message", url_args={"sha1_git": unknown_revision_} | ||||
) | ) | ||||
rv = check_api_get_responses(api_client, url, status_code=404) | rv = check_api_get_responses(api_client, url, status_code=404) | ||||
assert rv.data == { | assert rv.data == { | ||||
"exception": "NotFoundExc", | "exception": "NotFoundExc", | ||||
"reason": "Revision with sha1_git %s not found." % unknown_revision_, | "reason": "Revision with sha1_git %s not found." % unknown_revision_, | ||||
} | } | ||||
@given(revision()) | |||||
def test_api_revision_log(api_client, archive_data, revision): | def test_api_revision_log(api_client, archive_data, revision): | ||||
limit = 10 | limit = 10 | ||||
url = reverse( | url = reverse( | ||||
"api-1-revision-log", | "api-1-revision-log", | ||||
url_args={"sha1_git": revision}, | url_args={"sha1_git": revision}, | ||||
query_params={"limit": limit}, | query_params={"limit": limit}, | ||||
) | ) | ||||
Show All 26 Lines | def test_api_revision_directory_ko_not_found(api_client): | ||||
url = reverse("api-1-revision-directory", {"sha1_git": sha1_git}) | url = reverse("api-1-revision-directory", {"sha1_git": sha1_git}) | ||||
rv = check_api_get_responses(api_client, url, status_code=404) | rv = check_api_get_responses(api_client, url, status_code=404) | ||||
assert rv.data == { | assert rv.data == { | ||||
"exception": "NotFoundExc", | "exception": "NotFoundExc", | ||||
"reason": f"Revision with sha1_git {sha1_git} not found.", | "reason": f"Revision with sha1_git {sha1_git} not found.", | ||||
} | } | ||||
@given(revision()) | |||||
def test_api_revision_directory_ok_returns_dir_entries( | def test_api_revision_directory_ok_returns_dir_entries( | ||||
api_client, archive_data, revision | api_client, archive_data, revision | ||||
): | ): | ||||
url = reverse("api-1-revision-directory", {"sha1_git": revision}) | url = reverse("api-1-revision-directory", {"sha1_git": revision}) | ||||
rv = check_api_get_responses(api_client, url, status_code=200) | rv = check_api_get_responses(api_client, url, status_code=200) | ||||
rev_data = archive_data.revision_get(revision) | rev_data = archive_data.revision_get(revision) | ||||
dir_content = archive_data.directory_ls(rev_data["directory"]) | dir_content = archive_data.directory_ls(rev_data["directory"]) | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | ): | ||||
assert rv.data == { | assert rv.data == { | ||||
"content": enrich_content(cnt_data, request=rv.wsgi_request), | "content": enrich_content(cnt_data, request=rv.wsgi_request), | ||||
"path": content_path, | "path": content_path, | ||||
"type": "file", | "type": "file", | ||||
"revision": revision_id, | "revision": revision_id, | ||||
} | } | ||||
@given(revision(), new_person(), new_swh_date()) | @given(new_person(), new_swh_date()) | ||||
def test_api_revision_directory_ok_returns_revision( | def test_api_revision_directory_ok_returns_revision( | ||||
api_client, archive_data, revision, person, date | api_client, archive_data, revision, person, date | ||||
): | ): | ||||
rev_path = "foo" | rev_path = "foo" | ||||
_dir = Directory( | _dir = Directory( | ||||
entries=( | entries=( | ||||
DirectoryEntry( | DirectoryEntry( | ||||
name=rev_path.encode(), | name=rev_path.encode(), | ||||
Show All 27 Lines | ): | ||||
assert rv.data == { | assert rv.data == { | ||||
"content": enrich_revision(rev_data, request=rv.wsgi_request), | "content": enrich_revision(rev_data, request=rv.wsgi_request), | ||||
"path": rev_path, | "path": rev_path, | ||||
"type": "rev", | "type": "rev", | ||||
"revision": revision_id, | "revision": revision_id, | ||||
} | } | ||||
@given(revision()) | |||||
def test_api_revision_uppercase(api_client, revision): | def test_api_revision_uppercase(api_client, revision): | ||||
url = reverse( | url = reverse( | ||||
"api-1-revision-uppercase-checksum", url_args={"sha1_git": revision.upper()} | "api-1-revision-uppercase-checksum", url_args={"sha1_git": revision.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("api-1-revision", url_args={"sha1_git": revision}) | redirect_url = reverse("api-1-revision", url_args={"sha1_git": revision}) | ||||
assert resp["location"] == redirect_url | assert resp["location"] == redirect_url |