diff --git a/swh/deposit/cli/client.py b/swh/deposit/cli/client.py --- a/swh/deposit/cli/client.py +++ b/swh/deposit/cli/client.py @@ -516,7 +516,7 @@ url = _url(url) logger.debug("Status deposit") with trap_and_report_exceptions(): - client = PublicApiDepositClient(url=url, auth=(username, password)) + client = PublicApiDepositClient(url=_url(url), auth=(username, password)) collection = _collection(client) print_result( @@ -573,3 +573,29 @@ result = client.deposit_metadata_only(collection, metadata_path) print_result(result, output_format) + + +@deposit.command("list") +@credentials_decorator +@output_format_decorator +@click.option( + "--page", default=1, help="Page number when requesting more information", +) +@click.option( + "--page-size", default=100, help="Page number when requesting more information", +) +@click.pass_context +def deposit_list(ctx, url, username, password, output_format, page, page_size): + """Deposit metadata only upload + + """ + from swh.deposit.client import PublicApiDepositClient + + url = _url(url) + logger.debug("List deposits for user %s", username) + with trap_and_report_exceptions(): + client = PublicApiDepositClient(url=_url(url), auth=(username, password)) + collection = _collection(client) + result = client.deposit_list(collection, page=page, page_size=page_size) + + print_result(result, output_format) diff --git a/swh/deposit/tests/cli/test_client.py b/swh/deposit/tests/cli/test_client.py --- a/swh/deposit/tests/cli/test_client.py +++ b/swh/deposit/tests/cli/test_client.py @@ -645,7 +645,7 @@ @pytest.mark.parametrize( - "output_format,callable_fn", + "output_format,parser_fn", [ ("json", json.loads), ("yaml", yaml.safe_load), @@ -656,7 +656,7 @@ ], ) def test_cli_deposit_status_with_output_format( - output_format, callable_fn, datadir, slug, requests_mock_datadir, caplog, cli_runner + output_format, parser_fn, datadir, slug, requests_mock_datadir, caplog, cli_runner ): """Check deposit status cli with all possible output formats (json, yaml, logging). @@ -697,7 +697,7 @@ else: result_output = result.output - actual_deposit = callable_fn(result_output) + actual_deposit = parser_fn(result_output) assert actual_deposit == expected_deposit_status @@ -956,3 +956,78 @@ actual_error = api_call.parse_result_error(error_xml) assert actual_error == {"error": summary} + + +@pytest.mark.parametrize( + "output_format,parser_fn", + [ + ("json", json.loads), + ("yaml", yaml.safe_load), + ( + "logging", + ast.literal_eval, + ), # not enough though, the caplog fixture is needed + ], +) +def test_cli_deposit_collection_list( + output_format, parser_fn, datadir, slug, requests_mock_datadir, caplog, cli_runner +): + """Check deposit status cli with all possible output formats (json, yaml, logging). + + """ + api_url_basename = "deposit.test.list" + + expected_deposits = { + "count": "3", + "deposits": [ + { + "external_id": "check-deposit-2020-10-09T13:10:00.000000", + "id": "1031", + "status": "rejected", + "status_detail": "Deposit without archive", + }, + { + "external_id": "check-deposit-2020-10-10T13:20:00.000000", + "id": "1032", + "status": "rejected", + "status_detail": "Deposit without archive", + }, + { + "complete_date": "2020-10-08T13:52:34.509655", + "external_id": "check-deposit-2020-10-08T13:52:34.509655", + "id": "1033", + "reception_date": "2020-10-08T13:50:30", + "status": "done", + "status_detail": "The deposit has been successfully loaded into " + "the Software Heritage archive", + "swhid": "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea", + "swhid_context": "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea;origin=https://www.softwareheritage.org/check-deposit-2020-10-08T13:52:34.509655;visit=swh:1:snp:c477c6ef51833127b13a86ece7d75e5b3cc4e93d;anchor=swh:1:rev:f26f3960c175f15f6e24200171d446b86f6f7230;path=/", # noqa + }, + ], + } + + # fmt: off + result = cli_runner.invoke( + cli, + [ + "list", + "--url", f"https://{api_url_basename}/1", + "--username", TEST_USER["username"], + "--password", TEST_USER["password"], + "--page", 1, + "--page-size", 10, + "--format", output_format, + ], + ) + # fmt: on + assert result.exit_code == 0, f"unexpected output: {result.output}" + + if output_format == "logging": + assert len(caplog.record_tuples) == 1 + # format: (, , ) + _, _, result_output = caplog.record_tuples[0] + else: + result_output = result.output + + actual_deposit = parser_fn(result_output) + assert actual_deposit == expected_deposits diff --git a/swh/deposit/tests/data/https_deposit.test.list/1_servicedocument b/swh/deposit/tests/data/https_deposit.test.list/1_servicedocument new file mode 100644 --- /dev/null +++ b/swh/deposit/tests/data/https_deposit.test.list/1_servicedocument @@ -0,0 +1,26 @@ + + + + 2.0 + 209715200 + + + The Software Heritage (SWH) Archive + + test Software Collection + application/zip + application/x-tar + Collection Policy + Software Heritage Archive + Collect, Preserve, Share + false + false + http://purl.org/net/sword/package/SimpleZip + https://deposit.test.status/1/test/ + test + + + diff --git a/swh/deposit/tests/data/https_deposit.test.list/1_test,page=1,page_size=10 b/swh/deposit/tests/data/https_deposit.test.list/1_test,page=1,page_size=10 new file mode 100644 --- /dev/null +++ b/swh/deposit/tests/data/https_deposit.test.list/1_test,page=1,page_size=10 @@ -0,0 +1,28 @@ + + 3 + + 1031 + rejected + Deposit without archive + check-deposit-2020-10-09T13:10:00.000000 + + + 1032 + rejected + Deposit without archive + check-deposit-2020-10-10T13:20:00.000000 + + + 1033 + 2020-10-08T13:50:30 + 2020-10-08T13:52:34.509655 + done + The deposit has been successfully loaded into the Software Heritage archive + swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea + swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea;origin=https://www.softwareheritage.org/check-deposit-2020-10-08T13:52:34.509655;visit=swh:1:snp:c477c6ef51833127b13a86ece7d75e5b3cc4e93d;anchor=swh:1:rev:f26f3960c175f15f6e24200171d446b86f6f7230;path=/ + check-deposit-2020-10-08T13:52:34.509655 + +