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 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 | ||||
from swh.deposit.cli.client import InputError, _client, _collection, _url, generate_slug | from swh.deposit.cli.client import InputError, _client, _collection, _url, generate_slug | ||||
from swh.deposit.client import MaintenanceError, PublicApiDepositClient | from swh.deposit.client import MaintenanceError, PublicApiDepositClient | ||||
from swh.deposit.parsers import parse_xml | |||||
from ..conftest import TEST_USER | from ..conftest import TEST_USER | ||||
EXAMPLE_SERVICE_DOCUMENT = { | EXAMPLE_SERVICE_DOCUMENT = { | ||||
"service": {"workspace": {"collection": {"sword:name": "softcol",}}} | "service": {"workspace": {"collection": {"sword:name": "softcol",}}} | ||||
} | } | ||||
▲ Show 20 Lines • Show All 299 Lines • ▼ Show 20 Lines | ): | ||||
assert message == ( | assert message == ( | ||||
"Problem during parsing options: Using a metadata file " | "Problem during parsing options: Using a metadata file " | ||||
"(--metadata) is incompatible with --author and --name, " | "(--metadata) is incompatible with --author and --name, " | ||||
"which are used to generate one." | "which are used to generate one." | ||||
) | ) | ||||
def test_single_deposit_slug_generation( | def test_single_deposit_slug_generation( | ||||
sample_archive, mocker, caplog, tmp_path, client_mock | sample_archive, mocker, caplog, tmp_path, requests_mock_datadir | ||||
): | ): | ||||
""" from: | """Single deposit scenario without providing the slug, the slug is generated nonetheless | ||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit | https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit | ||||
""" # noqa | """ # noqa | ||||
slug = "my-slug" | |||||
collection = "my-collection" | |||||
metadata_path = os.path.join(tmp_path, "metadata.xml") | metadata_path = os.path.join(tmp_path, "metadata.xml") | ||||
mocker.patch( | mocker.patch( | ||||
"tempfile.TemporaryDirectory", | "tempfile.TemporaryDirectory", | ||||
return_value=contextlib.nullcontext(str(tmp_path)), | return_value=contextlib.nullcontext(str(tmp_path)), | ||||
) | ) | ||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke( | result = runner.invoke( | ||||
cli, | cli, | ||||
[ | [ | ||||
"upload", | "upload", | ||||
"--url", | "--url", | ||||
"mock://deposit.swh/1", | "https://deposit.swh.test/1", | ||||
"--username", | "--username", | ||||
TEST_USER["username"], | TEST_USER["username"], | ||||
"--password", | "--password", | ||||
TEST_USER["password"], | TEST_USER["password"], | ||||
"--name", | "--name", | ||||
"test-project", | "test-project", | ||||
"--archive", | "--archive", | ||||
sample_archive["path"], | sample_archive["path"], | ||||
"--slug", | |||||
slug, | |||||
"--collection", | |||||
collection, | |||||
"--author", | "--author", | ||||
"Jane Doe", | "Jane Doe", | ||||
], | ], | ||||
) | ) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, result.output | ||||
assert result.output == "" | assert result.output == "" | ||||
assert caplog.record_tuples == [ | |||||
("swh.deposit.cli.client", logging.INFO, '{"foo": "bar"}'), | |||||
] | |||||
client_mock.deposit_create.assert_called_once_with( | interesting_records = [] | ||||
archive=sample_archive["path"], | for record in caplog.record_tuples: | ||||
collection=collection, | if record[0] == "swh.deposit.cli.client": | ||||
in_progress=False, | interesting_records.append(record) | ||||
metadata=metadata_path, | |||||
slug=slug, | assert len(interesting_records) == 1 | ||||
) | assert interesting_records == [ | ||||
( | |||||
"swh.deposit.cli.client", | |||||
logging.INFO, | |||||
"{'deposit_id': '615', 'deposit_status': 'partial', 'deposit_status_detail': None, 'deposit_date': 'Oct. 8, 2020, 4:57 p.m.'}", # noqa | |||||
), | |||||
] | |||||
with open(metadata_path) as fd: | with open(metadata_path) as fd: | ||||
assert ( | metadata_xml = fd.read() | ||||
fd.read() | actual_metadata = parse_xml(metadata_xml) | ||||
== """\ | assert actual_metadata["codemeta:identifier"] is not None | ||||
<?xml version="1.0" encoding="utf-8"?> | |||||
<entry xmlns="http://www.w3.org/2005/Atom" \ | |||||
xmlns:codemeta="https://doi.org/10.5063/SCHEMA/CODEMETA-2.0"> | |||||
\t<codemeta:name>test-project</codemeta:name> | |||||
\t<codemeta:identifier>my-slug</codemeta:identifier> | |||||
\t<codemeta:author> | |||||
\t\t<codemeta:name>Jane Doe</codemeta:name> | |||||
\t</codemeta:author> | |||||
</entry>""" | |||||
) | |||||
def test_multisteps_deposit( | def test_multisteps_deposit( | ||||
sample_archive, atom_dataset, mocker, caplog, datadir, client_mock, slug | sample_archive, atom_dataset, mocker, caplog, datadir, client_mock, slug | ||||
): | ): | ||||
""" from: | """ 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 | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |