Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_collection_post_atom.py
# Copyright (C) 2017-2021 The Software Heritage developers | # Copyright (C) 2017-2021 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 | ||||
"""Tests the handling of the Atom content when doing a POST Col-IRI.""" | """Tests the handling of the Atom content when doing a POST Col-IRI.""" | ||||
from io import BytesIO | from io import BytesIO | ||||
import textwrap | |||||
import uuid | import uuid | ||||
import warnings | import warnings | ||||
import attr | import attr | ||||
from django.urls import reverse_lazy as reverse | from django.urls import reverse_lazy as reverse | ||||
import pytest | import pytest | ||||
from rest_framework import status | from rest_framework import status | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | else: | ||||
method = getattr(swh_storage, object_type_name + "_add") | method = getattr(swh_storage, object_type_name + "_add") | ||||
with warnings.catch_warnings(): | with warnings.catch_warnings(): | ||||
# hypothesis doesn't like us using .example(), but we know what we're doing | # hypothesis doesn't like us using .example(), but we know what we're doing | ||||
warnings.simplefilter("ignore") | warnings.simplefilter("ignore") | ||||
obj = strategy().example() | obj = strategy().example() | ||||
method([attr.evolve(obj, id=swhid.object_id)]) | method([attr.evolve(obj, id=swhid.object_id)]) | ||||
def _assert_deposit_info_on_metadata( | |||||
swh_storage, metadata_swhid, deposit, metadata_fetcher | |||||
): | |||||
swh_authority = MetadataAuthority( | |||||
MetadataAuthorityType.REGISTRY, | |||||
"http://deposit.softwareheritage.example/", | |||||
metadata=None, | |||||
) | |||||
page_results = swh_storage.raw_extrinsic_metadata_get(metadata_swhid, swh_authority) | |||||
assert len(page_results.results) == 1 | |||||
assert page_results.next_page_token is None | |||||
expected_xml_data = textwrap.dedent( | |||||
f"""\ | |||||
<deposit xmlns="https://www.softwareheritage.org/schema/2018/deposit"> | |||||
<deposit_id>{deposit.id}</deposit_id> | |||||
<deposit_client>https://hal-test.archives-ouvertes.fr/</deposit_client> | |||||
<deposit_collection>test</deposit_collection> | |||||
</deposit> | |||||
""" | |||||
) | |||||
assert page_results == PagedResult( | |||||
results=[ | |||||
RawExtrinsicMetadata( | |||||
target=metadata_swhid, | |||||
discovery_date=deposit.complete_date, | |||||
authority=swh_authority, | |||||
fetcher=attr.evolve(metadata_fetcher, metadata=None), | |||||
format="xml-deposit-info", | |||||
metadata=expected_xml_data.encode(), | |||||
) | |||||
], | |||||
next_page_token=None, | |||||
) | |||||
def test_post_deposit_atom_201_even_with_decimal( | def test_post_deposit_atom_201_even_with_decimal( | ||||
authenticated_client, deposit_collection, atom_dataset | authenticated_client, deposit_collection, atom_dataset | ||||
): | ): | ||||
"""Posting an initial atom entry should return 201 with deposit receipt | """Posting an initial atom entry should return 201 with deposit receipt | ||||
""" | """ | ||||
atom_error_with_decimal = atom_dataset["error-with-decimal"] | atom_error_with_decimal = atom_dataset["error-with-decimal"] | ||||
▲ Show 20 Lines • Show All 472 Lines • ▼ Show 20 Lines | metadata_fetcher = MetadataFetcher( | ||||
metadata=config.tool["configuration"], | metadata=config.tool["configuration"], | ||||
) | ) | ||||
actual_fetcher = swh_storage.metadata_fetcher_get( | actual_fetcher = swh_storage.metadata_fetcher_get( | ||||
config.tool["name"], config.tool["version"] | config.tool["name"], config.tool["version"] | ||||
) | ) | ||||
assert actual_fetcher == metadata_fetcher | assert actual_fetcher == metadata_fetcher | ||||
# Get the deposited metadata object and check it: | |||||
page_results = swh_storage.raw_extrinsic_metadata_get( | page_results = swh_storage.raw_extrinsic_metadata_get( | ||||
swhid_target, metadata_authority | swhid_target, metadata_authority | ||||
) | ) | ||||
assert len(page_results.results) == 1 | assert len(page_results.results) == 1 | ||||
assert page_results.next_page_token is None | assert page_results.next_page_token is None | ||||
metadata_context = compute_metadata_context(swhid_reference) | metadata_context = compute_metadata_context(swhid_reference) | ||||
assert page_results == PagedResult( | metadata = RawExtrinsicMetadata( | ||||
results=[ | |||||
RawExtrinsicMetadata( | |||||
target=swhid_target, | target=swhid_target, | ||||
discovery_date=deposit.complete_date, | discovery_date=deposit.complete_date, | ||||
authority=attr.evolve(metadata_authority, metadata=None), | authority=attr.evolve(metadata_authority, metadata=None), | ||||
fetcher=attr.evolve(metadata_fetcher, metadata=None), | fetcher=attr.evolve(metadata_fetcher, metadata=None), | ||||
format="sword-v2-atom-codemeta", | format="sword-v2-atom-codemeta", | ||||
metadata=xml_data.encode(), | metadata=xml_data.encode(), | ||||
**metadata_context, | **metadata_context, | ||||
) | ) | ||||
], | assert page_results == PagedResult(results=[metadata], next_page_token=None,) | ||||
next_page_token=None, | |||||
# Get metadata about the deposited metadata object and check it: | |||||
_assert_deposit_info_on_metadata( | |||||
swh_storage, metadata.swhid(), deposit, metadata_fetcher | |||||
) | ) | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"url", ["https://gitlab.org/user/repo", "https://whatever.else/repo",] | "url", ["https://gitlab.org/user/repo", "https://whatever.else/repo",] | ||||
) | ) | ||||
def test_deposit_metadata_origin( | def test_deposit_metadata_origin( | ||||
url, authenticated_client, deposit_collection, atom_dataset, swh_storage, | url, authenticated_client, deposit_collection, atom_dataset, swh_storage, | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | metadata_fetcher = MetadataFetcher( | ||||
metadata=config.tool["configuration"], | metadata=config.tool["configuration"], | ||||
) | ) | ||||
actual_fetcher = swh_storage.metadata_fetcher_get( | actual_fetcher = swh_storage.metadata_fetcher_get( | ||||
config.tool["name"], config.tool["version"] | config.tool["name"], config.tool["version"] | ||||
) | ) | ||||
assert actual_fetcher == metadata_fetcher | assert actual_fetcher == metadata_fetcher | ||||
# Get the deposited metadata object and check it: | |||||
page_results = swh_storage.raw_extrinsic_metadata_get( | page_results = swh_storage.raw_extrinsic_metadata_get( | ||||
origin_swhid, metadata_authority | origin_swhid, metadata_authority | ||||
) | ) | ||||
assert len(page_results.results) == 1 | assert len(page_results.results) == 1 | ||||
assert page_results.next_page_token is None | assert page_results.next_page_token is None | ||||
assert page_results == PagedResult( | metadata = RawExtrinsicMetadata( | ||||
results=[ | |||||
RawExtrinsicMetadata( | |||||
target=origin_swhid, | target=origin_swhid, | ||||
discovery_date=deposit.complete_date, | discovery_date=deposit.complete_date, | ||||
authority=attr.evolve(metadata_authority, metadata=None), | authority=attr.evolve(metadata_authority, metadata=None), | ||||
fetcher=attr.evolve(metadata_fetcher, metadata=None), | fetcher=attr.evolve(metadata_fetcher, metadata=None), | ||||
format="sword-v2-atom-codemeta", | format="sword-v2-atom-codemeta", | ||||
metadata=xml_data.encode(), | metadata=xml_data.encode(), | ||||
) | ) | ||||
], | assert page_results == PagedResult(results=[metadata], next_page_token=None,) | ||||
next_page_token=None, | |||||
# Get metadata about the deposited metadata object and check it: | |||||
_assert_deposit_info_on_metadata( | |||||
swh_storage, metadata.swhid(), deposit, metadata_fetcher | |||||
) | ) | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"swhid", | "swhid", | ||||
[ | [ | ||||
"swh:1:cnt:01b5c8cc985d190b5a7ef4878128ebfdc2358f49", | "swh:1:cnt:01b5c8cc985d190b5a7ef4878128ebfdc2358f49", | ||||
"swh:1:dir:11b5c8cc985d190b5a7ef4878128ebfdc2358f49", | "swh:1:dir:11b5c8cc985d190b5a7ef4878128ebfdc2358f49", | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |