Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_vault.py
Show All 31 Lines | |||||
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, content_type, in ( | for bundle_type, swhid, content_type, in ( | ||||
("flat", f"swh:1:dir:{directory}", "application/gzip"), | ("flat", f"swh:1:dir:{directory}", "application/gzip"), | ||||
("gitfast", f"swh:1:rev:{revision}", "application/gzip"), | ("gitfast", f"swh:1:rev:{revision}", "application/gzip"), | ||||
("git_bare", f"swh:1:rev:{revision}", "application/x-tar"), | ("git_bare", f"swh:1:rev:{revision}", "application/x-tar"), | ||||
): | ): | ||||
swhid = CoreSWHID.from_string(swhid) | |||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": str(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.replace('_', '-')}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": str(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, | ||||
"id": 1, | "id": 1, | ||||
"status": "done", | "status": "done", | ||||
"swhid": swhid, | "swhid": str(swhid), | ||||
} | } | ||||
mock_archive.vault_cook.assert_called_with( | mock_archive.vault_cook.assert_called_with(bundle_type, swhid, email) | ||||
bundle_type, CoreSWHID.from_string(swhid), email | |||||
) | |||||
rv = check_http_get_response(api_client, fetch_url, status_code=200) | rv = check_http_get_response(api_client, fetch_url, status_code=200) | ||||
assert rv["Content-Type"] == content_type | assert rv["Content-Type"] == content_type | ||||
assert rv.content == stub_fetch | assert rv.content == stub_fetch | ||||
mock_archive.vault_fetch.assert_called_with( | mock_archive.vault_fetch.assert_called_with(bundle_type, swhid) | ||||
bundle_type, CoreSWHID.from_string(swhid) | |||||
) | |||||
@given(directory(), revision(), unknown_directory(), unknown_revision()) | @given(directory(), revision(), unknown_directory(), unknown_revision()) | ||||
def test_api_vault_cook_notfound( | def test_api_vault_cook_notfound( | ||||
api_client, mocker, directory, revision, unknown_directory, unknown_revision | api_client, mocker, directory, revision, unknown_directory, unknown_revision | ||||
): | ): | ||||
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}"), | ("git_bare", f"swh:1:rev:{revision}"), | ||||
): | ): | ||||
swhid = CoreSWHID.from_string(swhid) | |||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": str(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, 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}"), | ("git_bare", f"swh:1:rev:{unknown_revision}"), | ||||
): | ): | ||||
swhid = CoreSWHID.from_string(swhid) | |||||
url = reverse( | url = reverse( | ||||
f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | f"api-1-vault-cook-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": str(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, swhid, email=None) | ||||
bundle_type, CoreSWHID.from_string(swhid), email=None | |||||
) | |||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | f"api-1-vault-fetch-{bundle_type.replace('_', '-')}", | ||||
url_args={"swhid": swhid}, | url_args={"swhid": str(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, swhid) | ||||
@pytest.mark.parametrize("bundle_type", ["flat", "gitfast", "git_bare"]) | @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( | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
@given(directory(), revision()) | @given(directory(), revision()) | ||||
def test_api_vault_cook_legacy(api_client, mocker, directory, revision): | def test_api_vault_cook_legacy(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, bundle_type, response_obj_type, obj_id in ( | for obj_type, bundle_type, response_obj_type, obj_id in ( | ||||
("directory", "flat", "directory", directory), | ("directory", "flat", "directory", directory), | ||||
("revision_gitfast", "gitfast", "revision", revision), | ("revision_gitfast", "gitfast", "revision", revision), | ||||
): | ): | ||||
swhid = f"swh:1:{obj_type[:3]}:{obj_id}" | swhid = CoreSWHID.from_string(f"swh:1:{obj_type[:3]}:{obj_id}") | ||||
fetch_url = reverse( | fetch_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": swhid}, | f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": str(swhid)}, | ||||
) | ) | ||||
stub_cook = { | stub_cook = { | ||||
"type": obj_type, | "type": obj_type, | ||||
"progress_msg": None, | "progress_msg": None, | ||||
"task_id": 1, | "task_id": 1, | ||||
"task_status": "done", | "task_status": "done", | ||||
"swhid": swhid, | "swhid": swhid, | ||||
"obj_type": response_obj_type, | "obj_type": response_obj_type, | ||||
Show All 12 Lines | ): | ||||
) | ) | ||||
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, | ||||
"id": 1, | "id": 1, | ||||
"status": "done", | "status": "done", | ||||
"swhid": swhid, | "swhid": str(swhid), | ||||
"obj_type": response_obj_type, | "obj_type": response_obj_type, | ||||
"obj_id": obj_id, | "obj_id": obj_id, | ||||
} | } | ||||
mock_archive.vault_cook.assert_called_with( | mock_archive.vault_cook.assert_called_with(bundle_type, swhid, email) | ||||
bundle_type, CoreSWHID.from_string(swhid), email | |||||
) | |||||
rv = check_http_get_response(api_client, fetch_url, status_code=200) | rv = check_http_get_response(api_client, fetch_url, 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(bundle_type, swhid) | ||||
bundle_type, CoreSWHID.from_string(swhid) | |||||
) | |||||
@given(directory(), revision()) | @given(directory(), revision()) | ||||
def test_api_vault_cook_uppercase_hash_legacy(api_client, directory, revision): | def test_api_vault_cook_uppercase_hash_legacy(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), | ||||
Show All 40 Lines | for obj_type, bundle_type, obj_id in ( | ||||
("directory", "flat", directory), | ("directory", "flat", directory), | ||||
("revision_gitfast", "gitfast", revision), | ("revision_gitfast", "gitfast", revision), | ||||
): | ): | ||||
url = reverse( | 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}, | ||||
) | ) | ||||
swhid = f"swh:1:{obj_type[:3]}:{obj_id}" | swhid = CoreSWHID.from_string(f"swh:1:{obj_type[:3]}:{obj_id}") | ||||
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, swhid) | ||||
bundle_type, CoreSWHID.from_string(swhid) | |||||
) | |||||
for obj_type, bundle_type, obj_id in ( | for obj_type, bundle_type, obj_id in ( | ||||
("directory", "flat", unknown_directory), | ("directory", "flat", unknown_directory), | ||||
("revision_gitfast", "gitfast", unknown_revision), | ("revision_gitfast", "gitfast", unknown_revision), | ||||
): | ): | ||||
swhid = f"swh:1:{obj_type[:3]}:{obj_id}" | swhid = CoreSWHID.from_string(f"swh:1:{obj_type[:3]}:{obj_id}") | ||||
url = reverse( | 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} | ||||
) | ) | ||||
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, swhid, email=None) | ||||
bundle_type, CoreSWHID.from_string(swhid), email=None | |||||
) | |||||
fetch_url = reverse( | fetch_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}, | ||||
) | ) | ||||
# Redirected to the current 'fetch' url | # Redirected to the current 'fetch' url | ||||
rv = check_http_get_response(api_client, fetch_url, status_code=302) | rv = check_http_get_response(api_client, fetch_url, status_code=302) | ||||
redirect_url = reverse( | redirect_url = reverse( | ||||
f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": swhid}, | f"api-1-vault-fetch-{bundle_type}", url_args={"swhid": str(swhid)}, | ||||
) | ) | ||||
assert rv["location"] == redirect_url | assert rv["location"] == redirect_url | ||||
rv = check_api_get_responses(api_client, redirect_url, status_code=404) | rv = check_api_get_responses(api_client, redirect_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, swhid) |