示例#1
0
def test_api_known_invalid_swhid(api_client):
    invalid_swhid_sha1 = ["swh:1:cnt:8068d0075010b590762c6cb5682ed53cb3c13de;"]
    invalid_swhid_type = ["swh:1:cnn:8068d0075010b590762c6cb5682ed53cb3c13deb"]

    url = reverse("api-1-known")

    check_api_post_responses(api_client, url, data=invalid_swhid_sha1, status_code=400)

    check_api_post_responses(api_client, url, data=invalid_swhid_type, status_code=400)
示例#2
0
def test_api_known_swhid_some_present(api_client, content, directory):
    content_ = gen_swhid(CONTENT, content["sha1_git"])
    directory_ = gen_swhid(DIRECTORY, directory)
    unknown_revision_ = gen_swhid(REVISION, random_sha1())
    unknown_release_ = gen_swhid(RELEASE, random_sha1())
    unknown_snapshot_ = gen_swhid(SNAPSHOT, random_sha1())

    input_swhids = [
        content_,
        directory_,
        unknown_revision_,
        unknown_release_,
        unknown_snapshot_,
    ]

    url = reverse("api-1-known")

    resp = check_api_post_responses(api_client, url, data=input_swhids, status_code=200)

    assert resp.data == {
        content_: {"known": True},
        directory_: {"known": True},
        unknown_revision_: {"known": False},
        unknown_release_: {"known": False},
        unknown_snapshot_: {"known": False},
    }
示例#3
0
def test_api_vault_cook_notfound(api_client, mocker, directory, revision,
                                 unknown_directory, unknown_revision):
    mock_vault = mocker.patch("swh.web.common.archive.vault")
    mock_vault.cook.side_effect = NotFoundExc("object not found")
    mock_vault.fetch.side_effect = NotFoundExc("cooked archive not found")
    mock_vault.progress.side_effect = NotFoundExc("cooking request not found")

    for obj_type, obj_id in (
        ("directory", directory),
        ("revision_gitfast", revision),
    ):

        obj_name = obj_type.split("_")[0]

        url = reverse(
            f"api-1-vault-cook-{obj_type}",
            url_args={f"{obj_type[:3]}_id": obj_id},
        )

        rv = check_api_get_responses(api_client, url, status_code=404)

        assert rv.data["exception"] == "NotFoundExc"
        assert (rv.data["reason"] ==
                f"Cooking of {obj_name} '{obj_id}' was never requested.")
        mock_vault.progress.assert_called_with(obj_type,
                                               hashutil.hash_to_bytes(obj_id))

    for obj_type, obj_id in (
        ("directory", unknown_directory),
        ("revision_gitfast", unknown_revision),
    ):
        obj_name = obj_type.split("_")[0]

        url = reverse(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)

        assert rv.data["exception"] == "NotFoundExc"
        assert rv.data["reason"] == f"{obj_name.title()} '{obj_id}' not found."
        mock_vault.cook.assert_called_with(obj_type,
                                           hashutil.hash_to_bytes(obj_id),
                                           email=None)

        fetch_url = reverse(
            f"api-1-vault-fetch-{obj_type}",
            url_args={f"{obj_type[:3]}_id": obj_id},
        )

        rv = check_api_get_responses(api_client, fetch_url, status_code=404)
        assert rv.data["exception"] == "NotFoundExc"
        assert (rv.data["reason"] ==
                f"Cooked archive for {obj_name} '{obj_id}' not found.")
        mock_vault.fetch.assert_called_with(obj_type,
                                            hashutil.hash_to_bytes(obj_id))
示例#4
0
def test_api_known_raises_large_payload_error(api_client):
    random_swhid = "swh:1:cnt:8068d0075010b590762c6cb5682ed53cb3c13deb"
    limit = 10000
    err_msg = "The maximum number of SWHIDs this endpoint can receive is 1000"

    swhids = [random_swhid for i in range(limit)]

    url = reverse("api-1-known")
    resp = check_api_post_responses(api_client, url, data=swhids, status_code=413)

    assert resp.data == {"exception": "LargePayloadExc", "reason": err_msg}
示例#5
0
def test_api_vault_cook(api_client, mocker, directory, revision):
    mock_archive = mocker.patch("swh.web.api.views.vault.archive")

    for obj_type, obj_id in (
        ("directory", directory),
        ("revision_gitfast", revision),
    ):

        fetch_url = reverse(
            f"api-1-vault-fetch-{obj_type}",
            url_args={f"{obj_type[:3]}_id": obj_id},
        )
        stub_cook = {
            "type": obj_type,
            "progress_msg": None,
            "task_id": 1,
            "task_status": "done",
            "object_id": obj_id,
        }
        stub_fetch = b"content"

        mock_archive.vault_cook.return_value = stub_cook
        mock_archive.vault_fetch.return_value = stub_fetch

        email = "*****@*****.**"
        url = reverse(
            f"api-1-vault-cook-{obj_type}",
            url_args={f"{obj_type[:3]}_id": obj_id},
            query_params={"email": email},
        )

        rv = check_api_post_responses(api_client,
                                      url,
                                      data=None,
                                      status_code=200)
        assert rv.data == {
            "fetch_url": rv.wsgi_request.build_absolute_uri(fetch_url),
            "obj_type": obj_type,
            "progress_message": None,
            "id": 1,
            "status": "done",
            "obj_id": obj_id,
        }
        mock_archive.vault_cook.assert_called_with(
            obj_type, hashutil.hash_to_bytes(obj_id), email)

        rv = check_http_get_response(api_client, fetch_url, status_code=200)
        assert rv["Content-Type"] == "application/gzip"
        assert rv.content == stub_fetch
        mock_archive.vault_fetch.assert_called_with(
            obj_type, hashutil.hash_to_bytes(obj_id))
示例#6
0
def test_api_known_swhid_all_present(
    api_client, content, directory, release, revision, snapshot
):
    input_swhids = [
        gen_swhid(CONTENT, content["sha1_git"]),
        gen_swhid(DIRECTORY, directory),
        gen_swhid(REVISION, revision),
        gen_swhid(RELEASE, release),
        gen_swhid(SNAPSHOT, snapshot),
    ]

    url = reverse("api-1-known")

    resp = check_api_post_responses(api_client, url, data=input_swhids, status_code=200)

    assert resp.data == {swhid: {"known": True} for swhid in input_swhids}
示例#7
0
def test_api_check_content_known_post(api_client, content):
    url = reverse("api-1-content-known")
    rv = check_api_post_responses(api_client,
                                  url,
                                  data={"q": content["sha1"]},
                                  status_code=200)

    assert rv.data == {
        "search_res": [{
            "found": True,
            "sha1": content["sha1"]
        }],
        "search_stats": {
            "nbfiles": 1,
            "pct": 100.0
        },
    }
示例#8
0
def check_created_save_request_status(
    api_client,
    mocker,
    origin_url,
    expected_request_status,
    scheduler_task_status=None,
    scheduler_task_run_status=None,
    expected_task_status=None,
    visit_date=None,
):

    mock_scheduler = mocker.patch("swh.web.common.origin_save.scheduler")
    if scheduler_task_status is None:
        mock_scheduler.get_tasks.return_value = []
    else:
        mock_scheduler.get_tasks.return_value = [
            {
                "priority": "high",
                "policy": "oneshot",
                "type": "load-git",
                "arguments": {"kwargs": {"repo_url": origin_url}, "args": []},
                "status": scheduler_task_status,
                "id": 1,
            }
        ]

    if scheduler_task_run_status is None:
        mock_scheduler.get_task_runs.return_value = []
    else:
        mock_scheduler.get_task_runs.return_value = [
            {
                "backend_id": "f00c712c-e820-41ce-a07c-9bf8df914205",
                "ended": datetime.now(tz=timezone.utc) + timedelta(minutes=5),
                "id": 1,
                "metadata": {},
                "scheduled": datetime.now(tz=timezone.utc),
                "started": None,
                "status": scheduler_task_run_status,
                "task": 1,
            }
        ]

    mock_scheduler.create_tasks.return_value = [
        {
            "priority": "high",
            "policy": "oneshot",
            "type": "load-git",
            "arguments": {"kwargs": {"repo_url": origin_url}, "args": []},
            "status": "next_run_not_scheduled",
            "id": 1,
        }
    ]

    url = reverse(
        "api-1-save-origin", url_args={"visit_type": "git", "origin_url": origin_url}
    )

    mock_visit_date = mocker.patch(
        ("swh.web.common.origin_save." "_get_visit_info_for_save_request")
    )
    mock_visit_date.return_value = (visit_date, None)

    if expected_request_status != SAVE_REQUEST_REJECTED:
        response = check_api_post_responses(api_client, url, data=None, status_code=200)
        assert response.data["save_request_status"] == expected_request_status
        assert response.data["save_task_status"] == expected_task_status
    else:
        check_api_post_responses(api_client, url, data=None, status_code=403)