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 contextlib | ||||
import logging | import logging | ||||
import os | import os | ||||
import re | |||||
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 | ||||
from swh.deposit.cli.client import ( | from swh.deposit.cli.client import ( | ||||
generate_slug, _url, _client, _collection, InputError) | generate_slug, _url, _client, _collection, InputError) | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | |||||
\t<codemeta:name>test-project</codemeta:name> | \t<codemeta:name>test-project</codemeta:name> | ||||
\t<codemeta:identifier>{slug}</codemeta:identifier> | \t<codemeta:identifier>{slug}</codemeta:identifier> | ||||
\t<codemeta:author> | \t<codemeta:author> | ||||
\t\t<codemeta:name>Jane Doe</codemeta:name> | \t\t<codemeta:name>Jane Doe</codemeta:name> | ||||
\t</codemeta:author> | \t</codemeta:author> | ||||
</entry>''' | </entry>''' | ||||
def test_metadata_validation(sample_archive, mocker, caplog, tmp_path): | |||||
""" from: | |||||
https://docs.softwareheritage.org/devel/swh-deposit/getting-started.html#single-deposit | |||||
""" # noqa | |||||
slug = generate_slug() | |||||
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"}' | |||||
metadata_path = os.path.join(tmp_path, 'metadata.xml') | |||||
mocker.patch('swh.deposit.cli.client.tempfile.TemporaryDirectory', | |||||
return_value=contextlib.nullcontext(str(tmp_path))) | |||||
with open(metadata_path, 'a'): | |||||
pass # creates the file | |||||
runner = CliRunner() | |||||
# Test missing author | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--name', 'test-project', | |||||
'--archive', sample_archive['path'], | |||||
]) | |||||
assert result.exit_code == 1, result.output | |||||
assert result.output == '' | |||||
assert len(caplog.record_tuples) == 1 | |||||
(_logger, level, message) = caplog.record_tuples[0] | |||||
assert level == logging.ERROR | |||||
assert ' --author ' in message | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
mock_client.reset_mock() | |||||
# Test missing name | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--archive', sample_archive['path'], | |||||
'--author', 'Jane Doe', | |||||
]) | |||||
assert result.exit_code == 1, result.output | |||||
assert result.output == '' | |||||
assert len(caplog.record_tuples) == 1 | |||||
(_logger, level, message) = caplog.record_tuples[0] | |||||
assert level == logging.ERROR | |||||
assert ' --name ' in message | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
mock_client.reset_mock() | |||||
# Test both --metadata and --author | |||||
result = runner.invoke(cli, [ | |||||
'upload', | |||||
'--url', 'mock://deposit.swh/1', | |||||
'--username', TEST_USER['username'], | |||||
'--password', TEST_USER['password'], | |||||
'--archive', sample_archive['path'], | |||||
'--metadata', metadata_path, | |||||
'--author', 'Jane Doe', | |||||
]) | |||||
assert result.exit_code == 1, result.output | |||||
assert result.output == '' | |||||
assert len(caplog.record_tuples) == 1 | |||||
(_logger, level, message) = caplog.record_tuples[0] | |||||
assert level == logging.ERROR | |||||
assert re.search('--metadata.*is incompatible with', message) | |||||
# Clear mocking state | |||||
caplog.clear() | |||||
ardumont: Don't we want to also show that the correct combinations work:
- --author and --name… | |||||
Done Inline Actions
Tested in test_single_minimal_deposit
Tested in test_multisteps_deposit. vlorentz: > --author and --name
Tested in test_single_minimal_deposit
> --metadata
Tested in… | |||||
mock_client.reset_mock() | |||||
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, 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' | ||||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |
Don't we want to also show that the correct combinations work: