Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/cli/test_client.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 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.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | def test_collection_ok(): | ||||
mock_client = MagicMock() | mock_client = MagicMock() | ||||
mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | mock_client.service_document.return_value = EXAMPLE_SERVICE_DOCUMENT | ||||
collection_name = _collection(mock_client) | collection_name = _collection(mock_client) | ||||
assert collection_name == 'softcol' | assert collection_name == 'softcol' | ||||
def test_single_minimal_deposit( | def test_single_minimal_deposit( | ||||
sample_archive, mocker, caplog, client_mock, slug): | sample_archive, mocker, caplog, client_mock, slug, tmp_path): | ||||
""" from: | """ from: | ||||
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 | ||||
metadata_path = os.path.join(tmp_path, 'metadata.xml') | |||||
mocker.patch('swh.deposit.cli.client.tempfile.TemporaryDirectory', | |||||
return_value=contextlib.nullcontext(str(tmp_path))) | |||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke(cli, [ | result = runner.invoke(cli, [ | ||||
'upload', | 'upload', | ||||
'--url', 'mock://deposit.swh/1', | '--url', 'mock://deposit.swh/1', | ||||
'--username', TEST_USER['username'], | '--username', TEST_USER['username'], | ||||
'--password', TEST_USER['password'], | '--password', TEST_USER['password'], | ||||
'--name', 'test-project', | '--name', 'test-project', | ||||
'--archive', sample_archive['path'], | '--archive', sample_archive['path'], | ||||
'--author', '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 == [ | assert caplog.record_tuples == [ | ||||
('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | ('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | ||||
] | ] | ||||
client_mock.deposit_create.assert_called_once_with( | client_mock.deposit_create.assert_called_once_with( | ||||
archive=sample_archive['path'], | archive=sample_archive['path'], | ||||
collection='softcol', in_progress=False, metadata=None, | collection='softcol', in_progress=False, metadata=metadata_path, | ||||
slug=slug) | slug=slug) | ||||
with open(metadata_path) as fd: | |||||
assert fd.read() == f'''\ | |||||
<?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>{slug}</codemeta:identifier> | |||||
\t<codemeta:author> | |||||
\t\t<codemeta:name>Jane Doe</codemeta:name> | |||||
\t</codemeta:author> | |||||
</entry>''' | |||||
def test_single_deposit_slug_collection( | def test_single_deposit_slug_generation( | ||||
sample_archive, mocker, caplog, client_mock): | sample_archive, mocker, caplog, tmp_path, client_mock): | ||||
""" from: | """ from: | ||||
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' | slug = 'my-slug' | ||||
collection = 'my-collection' | collection = 'my-collection' | ||||
metadata_path = os.path.join(tmp_path, 'metadata.xml') | |||||
mocker.patch('swh.deposit.cli.client.tempfile.TemporaryDirectory', | |||||
return_value=contextlib.nullcontext(str(tmp_path))) | |||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke(cli, [ | result = runner.invoke(cli, [ | ||||
'upload', | 'upload', | ||||
'--url', 'mock://deposit.swh/1', | '--url', 'mock://deposit.swh/1', | ||||
'--username', TEST_USER['username'], | '--username', TEST_USER['username'], | ||||
'--password', TEST_USER['password'], | '--password', TEST_USER['password'], | ||||
'--name', 'test-project', | '--name', 'test-project', | ||||
'--archive', sample_archive['path'], | '--archive', sample_archive['path'], | ||||
'--slug', slug, | '--slug', slug, | ||||
'--collection', collection, | '--collection', collection, | ||||
'--author', '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 == [ | assert caplog.record_tuples == [ | ||||
('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | ('swh.deposit.cli.client', logging.INFO, '{"foo": "bar"}'), | ||||
] | ] | ||||
client_mock.deposit_create.assert_called_once_with( | client_mock.deposit_create.assert_called_once_with( | ||||
archive=sample_archive['path'], | archive=sample_archive['path'], | ||||
collection=collection, in_progress=False, metadata=None, | collection=collection, in_progress=False, metadata=metadata_path, | ||||
slug=slug) | slug=slug) | ||||
with open(metadata_path) as fd: | |||||
assert fd.read() == '''\ | |||||
<?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, | sample_archive, atom_dataset, mocker, caplog, datadir, | ||||
client_mock, slug): | 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 | ||||
slug = generate_slug() | slug = generate_slug() | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |