Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_vault.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.vault.exc import NotFoundExc | from swh.vault.exc import NotFoundExc | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.api.views import check_api_get_responses, check_api_post_responses | |||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
directory, | directory, | ||||
revision, | revision, | ||||
unknown_directory, | unknown_directory, | ||||
unknown_revision, | unknown_revision, | ||||
) | ) | ||||
from swh.web.tests.utils import ( | |||||
check_api_get_responses, | |||||
check_api_post_responses, | |||||
check_http_get_response, | |||||
check_http_post_response, | |||||
) | |||||
@given(directory(), revision()) | @given(directory(), revision()) | ||||
def test_api_vault_cook(api_client, mocker, directory, revision): | def test_api_vault_cook(api_client, mocker, directory, revision): | ||||
mock_archive = mocker.patch("swh.web.api.views.vault.archive") | mock_archive = mocker.patch("swh.web.api.views.vault.archive") | ||||
for obj_type, obj_id in ( | for obj_type, obj_id in ( | ||||
("directory", directory), | ("directory", directory), | ||||
Show All 29 Lines | ): | ||||
stub_cook["fetch_url"] | stub_cook["fetch_url"] | ||||
) | ) | ||||
assert rv.data == stub_cook | assert rv.data == stub_cook | ||||
mock_archive.vault_cook.assert_called_with( | mock_archive.vault_cook.assert_called_with( | ||||
obj_type, hashutil.hash_to_bytes(obj_id), email | obj_type, hashutil.hash_to_bytes(obj_id), email | ||||
) | ) | ||||
rv = api_client.get(fetch_url) | rv = check_http_get_response(api_client, fetch_url, status_code=200) | ||||
assert rv.status_code == 200 | |||||
assert rv["Content-Type"] == "application/gzip" | assert rv["Content-Type"] == "application/gzip" | ||||
assert rv.content == stub_fetch | assert rv.content == stub_fetch | ||||
mock_archive.vault_fetch.assert_called_with( | mock_archive.vault_fetch.assert_called_with( | ||||
obj_type, hashutil.hash_to_bytes(obj_id) | obj_type, hashutil.hash_to_bytes(obj_id) | ||||
) | ) | ||||
@given(directory(), revision()) | @given(directory(), revision()) | ||||
def test_api_vault_cook_uppercase_hash(api_client, directory, revision): | def test_api_vault_cook_uppercase_hash(api_client, directory, revision): | ||||
for obj_type, obj_id in ( | for obj_type, obj_id in ( | ||||
("directory", directory), | ("directory", directory), | ||||
("revision_gitfast", revision), | ("revision_gitfast", revision), | ||||
): | ): | ||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{obj_type}-uppercase-checksum", | f"api-1-vault-cook-{obj_type}-uppercase-checksum", | ||||
url_args={f"{obj_type[:3]}_id": obj_id.upper()}, | url_args={f"{obj_type[:3]}_id": obj_id.upper()}, | ||||
) | ) | ||||
rv = api_client.post(url, {"email": "test@test.mail"}) | rv = check_http_post_response( | ||||
api_client, url, data={"email": "test@test.mail"}, status_code=302 | |||||
assert rv.status_code == 302 | ) | ||||
redirect_url = reverse( | redirect_url = reverse( | ||||
f"api-1-vault-cook-{obj_type}", url_args={f"{obj_type[:3]}_id": obj_id} | f"api-1-vault-cook-{obj_type}", url_args={f"{obj_type[:3]}_id": obj_id} | ||||
) | ) | ||||
assert rv["location"] == redirect_url | assert rv["location"] == redirect_url | ||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{obj_type}-uppercase-checksum", | f"api-1-vault-fetch-{obj_type}-uppercase-checksum", | ||||
url_args={f"{obj_type[:3]}_id": obj_id.upper()}, | url_args={f"{obj_type[:3]}_id": obj_id.upper()}, | ||||
) | ) | ||||
rv = api_client.get(fetch_url) | rv = check_http_get_response(api_client, fetch_url, status_code=302) | ||||
assert rv.status_code == 302 | |||||
redirect_url = reverse( | redirect_url = reverse( | ||||
f"api-1-vault-fetch-{obj_type}", url_args={f"{obj_type[:3]}_id": obj_id}, | f"api-1-vault-fetch-{obj_type}", url_args={f"{obj_type[:3]}_id": obj_id}, | ||||
) | ) | ||||
assert rv["location"] == redirect_url | assert rv["location"] == redirect_url | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |