Changeset View
Standalone View
swh/deposit/tests/cli/test_client.py
Show All 19 Lines | from swh.deposit.cli.client import ( | ||||
InputError, | InputError, | ||||
_collection, | _collection, | ||||
_url, | _url, | ||||
generate_metadata, | generate_metadata, | ||||
generate_slug, | generate_slug, | ||||
) | ) | ||||
from swh.deposit.client import MaintenanceError, PublicApiDepositClient | from swh.deposit.client import MaintenanceError, PublicApiDepositClient | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from swh.model.exceptions import ValidationError | |||||
from ..conftest import TEST_USER | from ..conftest import TEST_USER | ||||
@pytest.fixture | @pytest.fixture | ||||
def datadir(request): | def datadir(request): | ||||
"""Override default datadir to target main test datadir""" | """Override default datadir to target main test datadir""" | ||||
return os.path.join(os.path.dirname(str(request.fspath)), "../data") | return os.path.join(os.path.dirname(str(request.fspath)), "../data") | ||||
▲ Show 20 Lines • Show All 596 Lines • ▼ Show 20 Lines | ): | ||||
actual_result = json.loads(result.output) | actual_result = json.loads(result.output) | ||||
assert "error" in actual_result | assert "error" in actual_result | ||||
assert actual_result == { | assert actual_result == { | ||||
"error": "You can only update metadata on deposit with status 'done'", | "error": "You can only update metadata on deposit with status 'done'", | ||||
"detail": "The deposit 321 has status 'partial'", | "detail": "The deposit 321 has status 'partial'", | ||||
"deposit_status": "partial", | "deposit_status": "partial", | ||||
"deposit_id": 321, | "deposit_id": 321, | ||||
} | } | ||||
def test_cli_metadata_only_deposit_full_metadata_file( | |||||
datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, | |||||
): | |||||
"""Post metadata-only deposit through cli | |||||
The metadata file posted by the client already contains the swhid | |||||
""" | |||||
api_url_basename = "deposit.test.metadataonly" | |||||
swhid = "swh:1:dir:ef04a768181417fbc5eef4243e2507915f24deea" | |||||
metadata = atom_dataset["entry-data-with-swhid"].format(swhid=swhid) | |||||
metadata_path = os.path.join(tmp_path, "entry-data-with-swhid.xml") | |||||
with open(metadata_path, "w") as m: | |||||
m.write(metadata) | |||||
expected_deposit_status = { | |||||
vlorentz: `open(receipt_path, "r")`
But I would rather not parse the config file in the test, that's… | |||||
Done Inline Actionslol, thanks! ardumont: lol, thanks! | |||||
"deposit_id": "100", | |||||
"deposit_status": "done", | |||||
"deposit_date": "2020-10-08T13:52:34.509655", | |||||
} | |||||
assert expected_deposit_status["deposit_status"] == "done" | |||||
# fmt: off | |||||
result = cli_runner.invoke( | |||||
cli, | |||||
[ | |||||
"metadata-only", | |||||
"--url", f"https://{api_url_basename}/1", | |||||
"--username", TEST_USER["username"], | |||||
"--password", TEST_USER["password"], | |||||
"--metadata", metadata_path, | |||||
"--format", "json", | |||||
], | |||||
Not Done Inline Actionswhy does a metadata-only deposit have a SWHID? vlorentz: why does a metadata-only deposit have a SWHID? | |||||
Done Inline Actionswell, that's the input the user gave. Also that's what the server does. ardumont: well, that's the input the user gave.
We still have to reference it?
Also that's what the… | |||||
Not Done Inline ActionsI don't think the server should return it. It's supposed to be the SWHID of the deposit itself... vlorentz: I don't think the server should return it.
It's supposed to be the SWHID of the deposit itself. | |||||
Done Inline Actions
The server needs fixing then... in another diff but i'm not sure about that, see next point.
I'm half-convinced. ardumont: > I don't think the server should return it.
The server needs fixing then... in another diff… | |||||
Not Done Inline Actions
It's returned on "GET State-IRI" after the deposit is loaded. vlorentz: > How do we compute that though, we don't have any way to do that, do we?
It's returned on… | |||||
) | |||||
# fmt: on | |||||
assert result.exit_code == 0, result.output | |||||
actual_deposit_status = json.loads(result.output) | |||||
assert "error" not in actual_deposit_status | |||||
assert actual_deposit_status == expected_deposit_status | |||||
def test_cli_metadata_only_deposit_invalid_swhid( | |||||
datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, | |||||
): | |||||
"""Post metadata-only deposit through cli with invalid swhid raises | |||||
""" | |||||
api_url_basename = "deposit.test.metadataonly" | |||||
invalid_swhid = "ssh:2:sth:xxx" | |||||
metadata = atom_dataset["entry-data-with-swhid"].format(swhid=invalid_swhid) | |||||
metadata_path = os.path.join(tmp_path, "entry-data-with-swhid.xml") | |||||
with open(metadata_path, "w") as f: | |||||
f.write(metadata) | |||||
# fmt: off | |||||
with pytest.raises(ValidationError, match="Invalid"): | |||||
cli_runner.invoke( | |||||
cli, | |||||
[ | |||||
"metadata-only", | |||||
"--url", f"https://{api_url_basename}/1", | |||||
"--username", TEST_USER["username"], | |||||
"--password", TEST_USER["password"], | |||||
Not Done Inline Actionswhat is the point of doing that? vlorentz: what is the point of doing that? | |||||
Done Inline ActionsIt's a bit tedious and potentially error-prone to write a correct (with the right namespace and all [1]): <swh:deposit> <swh:reference> <swh:object swhid="{swhid}" /> </swh:reference> </swh:deposit> within your xml. [1] which i forgot to add in this very diff... ardumont: It's a bit tedious and potentially error-prone to write a correct (with the right namespace and… | |||||
Not Done Inline ActionsI don't see what makes it harder than having codemeta metadata, it's just one copy-paste away vlorentz: I don't see what makes it harder than having codemeta metadata, it's just one copy-paste away | |||||
Done Inline Actionsok, so to conclude no --swhidthen, right? ardumont: ok, so to conclude no `--swhid`then, right? | |||||
"--metadata", metadata_path, | |||||
"--format", "json", | |||||
], | |||||
catch_exceptions=False, | |||||
) | |||||
# fmt: on | |||||
def test_cli_metadata_only_deposit_no_swhid( | |||||
datadir, requests_mock_datadir, cli_runner, atom_dataset, tmp_path, | |||||
): | |||||
"""Post metadata-only deposit through cli with invalid swhid raises | |||||
""" | |||||
api_url_basename = "deposit.test.metadataonly" | |||||
metadata = atom_dataset["entry-data-minimal"] | |||||
metadata_path = os.path.join(tmp_path, "entry-data-minimal.xml") | |||||
with open(metadata_path, "w") as f: | |||||
f.write(metadata) | |||||
with pytest.raises(InputError, match="SWHID must be provided"): | |||||
cli_runner.invoke( | |||||
cli, | |||||
[ | |||||
"metadata-only", | |||||
"--url", f"https://{api_url_basename}/1", | |||||
"--username", TEST_USER["username"], | |||||
"--password", TEST_USER["password"], | |||||
"--metadata", metadata_path, | |||||
"--format", "json", | |||||
], | |||||
catch_exceptions=False, | |||||
) | |||||
# fmt: on |
open(receipt_path, "r")
But I would rather not parse the config file in the test, that's just duplicating the client code; so keep the hardcoded expected_deposit_status.