Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/cli/test_client.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-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 General Public License version 3, or any later version | # License: GNU 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 | ||||
import contextlib | import contextlib | ||||
import json | |||||
import logging | import logging | ||||
import os | import os | ||||
from unittest.mock import MagicMock | from unittest.mock import MagicMock | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import pytest | import pytest | ||||
from swh.deposit.cli import deposit as cli | from swh.deposit.cli import deposit as cli | ||||
Show All 23 Lines | |||||
@pytest.fixture | @pytest.fixture | ||||
def slug(): | def slug(): | ||||
return generate_slug() | return generate_slug() | ||||
@pytest.fixture | @pytest.fixture | ||||
def client_mock(mocker, slug): | |||||
"""A successful deposit client with hard-coded default values | |||||
""" | |||||
mocker.patch("swh.deposit.cli.client.generate_slug", return_value=slug) | |||||
mock_client = MagicMock() | |||||
mocker.patch("swh.deposit.cli.client._client", return_value=mock_client) | |||||
mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | |||||
mock_client.deposit_create.return_value = '{"foo": "bar"}' | |||||
return mock_client | |||||
@pytest.fixture | |||||
def client_mock_api_down(mocker, slug): | def client_mock_api_down(mocker, slug): | ||||
"""A mock client whose connection with api fails due to maintenance issue | """A mock client whose connection with api fails due to maintenance issue | ||||
""" | """ | ||||
mocker.patch("swh.deposit.cli.client.generate_slug", return_value=slug) | mocker.patch("swh.deposit.cli.client.generate_slug", return_value=slug) | ||||
mock_client = MagicMock() | mock_client = MagicMock() | ||||
mocker.patch("swh.deposit.cli.client._client", return_value=mock_client) | mocker.patch("swh.deposit.cli.client._client", return_value=mock_client) | ||||
mock_client.service_document.side_effect = MaintenanceError( | mock_client.service_document.side_effect = MaintenanceError( | ||||
▲ Show 20 Lines • Show All 311 Lines • ▼ Show 20 Lines | ): | ||||
] | ] | ||||
with open(metadata_path) as fd: | with open(metadata_path) as fd: | ||||
metadata_xml = fd.read() | metadata_xml = fd.read() | ||||
actual_metadata = parse_xml(metadata_xml) | actual_metadata = parse_xml(metadata_xml) | ||||
assert actual_metadata["codemeta:identifier"] is not None | assert actual_metadata["codemeta:identifier"] is not None | ||||
def test_multisteps_deposit( | def test_multisteps_deposit(sample_archive, datadir, slug, requests_mock_datadir): | ||||
sample_archive, atom_dataset, mocker, caplog, datadir, client_mock, slug | """ First deposit a partial deposit (no metadata, only archive), then update the metadata part. | ||||
): | |||||
""" from: | |||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#multisteps-deposit | https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#multisteps-deposit | ||||
""" # noqa | """ # noqa | ||||
slug = generate_slug() | api_url = "https://deposit.test.metadata/1" | ||||
mocker.patch("swh.deposit.cli.client.generate_slug", return_value=slug) | |||||
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#create-an-incomplete-deposit | |||||
client_mock.deposit_create.return_value = '{"deposit_id": "42"}' | |||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke( | result = runner.invoke( | ||||
cli, | cli, | ||||
[ | [ | ||||
"upload", | "upload", | ||||
"--url", | "--url", | ||||
"mock://deposit.swh/1", | api_url, | ||||
"--username", | "--username", | ||||
TEST_USER["username"], | TEST_USER["username"], | ||||
"--password", | "--password", | ||||
TEST_USER["password"], | TEST_USER["password"], | ||||
"--archive", | "--archive", | ||||
sample_archive["path"], | sample_archive["path"], | ||||
"--partial", | "--partial", | ||||
"--slug", | |||||
slug, | |||||
"--format", | |||||
"json", | |||||
], | ], | ||||
) | ) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, f"unexpected output: {result.output}" | ||||
assert result.output == "" | actual_deposit = json.loads(result.output) | ||||
assert caplog.record_tuples == [ | assert actual_deposit == { | ||||
("swh.deposit.cli.client", logging.INFO, '{"deposit_id": "42"}'), | "deposit_id": "666", | ||||
] | "deposit_status": "partial", | ||||
"deposit_status_detail": None, | |||||
client_mock.deposit_create.assert_called_once_with( | "deposit_date": "Oct. 8, 2020, 4:57 p.m.", | ||||
archive=sample_archive["path"], | } | ||||
collection="softcol", | |||||
in_progress=True, | |||||
metadata=None, | |||||
slug=slug, | |||||
) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
client_mock.reset_mock() | |||||
# https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#add-content-or-metadata-to-the-deposit | # https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#add-content-or-metadata-to-the-deposit | ||||
metadata_path = os.path.join(datadir, "atom", "entry-data-deposit-binary.xml") | metadata_path = os.path.join(datadir, "atom", "entry-data-deposit-binary.xml") | ||||
# Update deposit with metadata | |||||
result = runner.invoke( | result = runner.invoke( | ||||
cli, | cli, | ||||
[ | [ | ||||
"upload", | "upload", | ||||
"--url", | "--url", | ||||
"mock://deposit.swh/1", | api_url, | ||||
"--username", | "--username", | ||||
TEST_USER["username"], | TEST_USER["username"], | ||||
"--password", | "--password", | ||||
TEST_USER["password"], | TEST_USER["password"], | ||||
"--metadata", | "--metadata", | ||||
metadata_path, | metadata_path, | ||||
"--deposit-id", | |||||
666, | |||||
"--slug", | |||||
slug, | |||||
"--format", | |||||
"json", | |||||
], | ], | ||||
) | ) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, f"unexpected output: {result.output}" | ||||
assert result.output == "" | assert result.output is not None | ||||
assert caplog.record_tuples == [ | actual_deposit = json.loads(result.output) | ||||
("swh.deposit.cli.client", logging.INFO, '{"deposit_id": "42"}'), | # deposit update scenario actually returns a deposit status dict | ||||
] | assert actual_deposit["deposit_id"] == "666" | ||||
# FIXME: should be "deposited" but current limitation in the | |||||
client_mock.deposit_create.assert_called_once_with( | # requests_mock_datadir_visits use, cannot find a way to make it work right now | ||||
archive=None, | assert actual_deposit["deposit_status"] == "partial" | ||||
collection="softcol", | |||||
in_progress=False, | |||||
metadata=metadata_path, | |||||
slug=slug, | |||||
) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
client_mock.reset_mock() |