Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_vault.py
Show All 29 Lines | |||||
@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 bundle_type, swhid in ( | for bundle_type, swhid in ( | ||||
("flat", f"swh:1:dir:{directory}"), | ("flat", f"swh:1:dir:{directory}"), | ||||
("gitfast", f"swh:1:rev:{revision}"), | ("gitfast", f"swh:1:rev:{revision}"), | ||||
("git_bare", f"swh:1:rev:{revision}"), | |||||
): | ): | ||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": swhid}, | f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | |||||
) | ) | ||||
stub_cook = { | stub_cook = { | ||||
"type": bundle_type, | "type": bundle_type, | ||||
"progress_msg": None, | "progress_msg": None, | ||||
"task_id": 1, | "task_id": 1, | ||||
"task_status": "done", | "task_status": "done", | ||||
"swhid": swhid, | "swhid": swhid, | ||||
} | } | ||||
stub_fetch = b"content" | stub_fetch = b"content" | ||||
mock_archive.vault_cook.return_value = stub_cook | mock_archive.vault_cook.return_value = stub_cook | ||||
mock_archive.vault_fetch.return_value = stub_fetch | mock_archive.vault_fetch.return_value = stub_fetch | ||||
email = "test@test.mail" | email = "test@test.mail" | ||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": swhid}, | ||||
query_params={"email": email}, | query_params={"email": email}, | ||||
) | ) | ||||
rv = check_api_post_responses(api_client, url, data=None, status_code=200) | rv = check_api_post_responses(api_client, url, data=None, status_code=200) | ||||
assert rv.data == { | assert rv.data == { | ||||
"fetch_url": rv.wsgi_request.build_absolute_uri(fetch_url), | "fetch_url": rv.wsgi_request.build_absolute_uri(fetch_url), | ||||
"progress_message": None, | "progress_message": None, | ||||
Show All 20 Lines | ): | ||||
mock_vault = mocker.patch("swh.web.common.archive.vault") | mock_vault = mocker.patch("swh.web.common.archive.vault") | ||||
mock_vault.cook.side_effect = NotFoundExc("object not found") | mock_vault.cook.side_effect = NotFoundExc("object not found") | ||||
mock_vault.fetch.side_effect = NotFoundExc("cooked archive not found") | mock_vault.fetch.side_effect = NotFoundExc("cooked archive not found") | ||||
mock_vault.progress.side_effect = NotFoundExc("cooking request not found") | mock_vault.progress.side_effect = NotFoundExc("cooking request not found") | ||||
for bundle_type, swhid in ( | for bundle_type, swhid in ( | ||||
("flat", f"swh:1:dir:{directory}"), | ("flat", f"swh:1:dir:{directory}"), | ||||
("gitfast", f"swh:1:rev:{revision}"), | ("gitfast", f"swh:1:rev:{revision}"), | ||||
("git_bare", f"swh:1:rev:{revision}"), | |||||
): | ): | ||||
url = reverse(f"api-1-vault-cook-{bundle_type}", url_args={"swhid": swhid}) | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | |||||
url_args={"swhid": swhid}, | |||||
) | |||||
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["exception"] == "NotFoundExc" | assert rv.data["exception"] == "NotFoundExc" | ||||
assert rv.data["reason"] == f"Cooking of {swhid} was never requested." | assert rv.data["reason"] == f"Cooking of {swhid} was never requested." | ||||
mock_vault.progress.assert_called_with( | mock_vault.progress.assert_called_with( | ||||
bundle_type, CoreSWHID.from_string(swhid) | bundle_type, CoreSWHID.from_string(swhid) | ||||
) | ) | ||||
for bundle_type, swhid in ( | for bundle_type, swhid in ( | ||||
("flat", f"swh:1:dir:{unknown_directory}"), | ("flat", f"swh:1:dir:{unknown_directory}"), | ||||
("gitfast", f"swh:1:rev:{unknown_revision}"), | ("gitfast", f"swh:1:rev:{unknown_revision}"), | ||||
("git_bare", f"swh:1:rev:{unknown_revision}"), | |||||
): | ): | ||||
url = reverse(f"api-1-vault-cook-{bundle_type}", url_args={"swhid": swhid}) | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | |||||
url_args={"swhid": swhid}, | |||||
) | |||||
rv = check_api_post_responses(api_client, url, data=None, status_code=404) | rv = check_api_post_responses(api_client, url, data=None, status_code=404) | ||||
assert rv.data["exception"] == "NotFoundExc" | assert rv.data["exception"] == "NotFoundExc" | ||||
assert rv.data["reason"] == f"{swhid} not found." | assert rv.data["reason"] == f"{swhid} not found." | ||||
mock_vault.cook.assert_called_with( | mock_vault.cook.assert_called_with( | ||||
bundle_type, CoreSWHID.from_string(swhid), email=None | bundle_type, CoreSWHID.from_string(swhid), email=None | ||||
) | ) | ||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": swhid}, | f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | |||||
) | ) | ||||
rv = check_api_get_responses(api_client, fetch_url, status_code=404) | rv = check_api_get_responses(api_client, fetch_url, status_code=404) | ||||
assert rv.data["exception"] == "NotFoundExc" | assert rv.data["exception"] == "NotFoundExc" | ||||
assert rv.data["reason"] == f"Cooked archive for {swhid} not found." | assert rv.data["reason"] == f"Cooked archive for {swhid} not found." | ||||
mock_vault.fetch.assert_called_with(bundle_type, CoreSWHID.from_string(swhid)) | mock_vault.fetch.assert_called_with(bundle_type, CoreSWHID.from_string(swhid)) | ||||
@pytest.mark.parametrize("bundle_type", ["flat", "gitfast"]) | @pytest.mark.parametrize("bundle_type", ["flat", "gitfast", "git_bare"]) | ||||
def test_api_vault_cook_error_content(api_client, mocker, bundle_type): | def test_api_vault_cook_error_content(api_client, mocker, bundle_type): | ||||
swhid = "swh:1:cnt:" + "0" * 40 | swhid = "swh:1:cnt:" + "0" * 40 | ||||
email = "test@test.mail" | email = "test@test.mail" | ||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": swhid}, | ||||
query_params={"email": email}, | query_params={"email": email}, | ||||
) | ) | ||||
rv = check_api_post_responses(api_client, url, data=None, status_code=400) | rv = check_api_post_responses(api_client, url, data=None, status_code=400) | ||||
assert rv.data == { | assert rv.data == { | ||||
"exception": "BadInputExc", | "exception": "BadInputExc", | ||||
"reason": ( | "reason": ( | ||||
"Content objects do not need to be cooked, " | "Content objects do not need to be cooked, " | ||||
"use `/api/1/content/raw/` instead." | "use `/api/1/content/raw/` instead." | ||||
), | ), | ||||
} | } | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"bundle_type,swhid_type,hint", | "bundle_type,swhid_type,hint", | ||||
[ | [ | ||||
("flat", "rev", True), | ("flat", "rev", True), | ||||
("flat", "rel", False), | ("flat", "rel", False), | ||||
("flat", "snp", False), | ("flat", "snp", False), | ||||
("gitfast", "dir", True), | ("gitfast", "dir", True), | ||||
("gitfast", "rel", False), | ("gitfast", "rel", False), | ||||
("gitfast", "snp", False), | ("gitfast", "snp", False), | ||||
("git_bare", "dir", True), | |||||
("git_bare", "rel", False), | |||||
("git_bare", "snp", False), | |||||
], | ], | ||||
) | ) | ||||
def test_api_vault_cook_error(api_client, mocker, bundle_type, swhid_type, hint): | def test_api_vault_cook_error(api_client, mocker, bundle_type, swhid_type, hint): | ||||
swhid = f"swh:1:{swhid_type}:" + "0" * 40 | swhid = f"swh:1:{swhid_type}:" + "0" * 40 | ||||
email = "test@test.mail" | email = "test@test.mail" | ||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": swhid}, | ||||
query_params={"email": email}, | query_params={"email": email}, | ||||
) | ) | ||||
rv = check_api_post_responses(api_client, url, data=None, status_code=400) | rv = check_api_post_responses(api_client, url, data=None, status_code=400) | ||||
assert rv.data["exception"] == "BadInputExc" | assert rv.data["exception"] == "BadInputExc" | ||||
if hint: | if hint: | ||||
assert re.match( | assert re.match( | ||||
▲ Show 20 Lines • Show All 158 Lines • Show Last 20 Lines |