Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_deposit_private_read_metadata.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
from django.urls import reverse | from django.urls import reverse | ||||
from rest_framework import status | from rest_framework import status | ||||
from swh.deposit import __version__ | from swh.deposit import __version__, utils | ||||
from swh.deposit.config import EDIT_SE_IRI, PRIVATE_GET_DEPOSIT_METADATA, SWH_PERSON | from swh.deposit.config import EDIT_SE_IRI, PRIVATE_GET_DEPOSIT_METADATA, SWH_PERSON | ||||
from swh.deposit.models import Deposit | from swh.deposit.models import Deposit | ||||
from swh.deposit.parsers import parse_xml | |||||
PRIVATE_GET_DEPOSIT_METADATA_NC = PRIVATE_GET_DEPOSIT_METADATA + "-nc" | PRIVATE_GET_DEPOSIT_METADATA_NC = PRIVATE_GET_DEPOSIT_METADATA + "-nc" | ||||
def private_get_raw_url_endpoints(collection, deposit): | def private_get_raw_url_endpoints(collection, deposit): | ||||
"""There are 2 endpoints to check (one with collection, one without)""" | """There are 2 endpoints to check (one with collection, one without)""" | ||||
deposit_id = deposit if isinstance(deposit, int) else deposit.id | deposit_id = deposit if isinstance(deposit, int) else deposit.id | ||||
return [ | return [ | ||||
reverse(PRIVATE_GET_DEPOSIT_METADATA, args=[collection.name, deposit_id]), | reverse(PRIVATE_GET_DEPOSIT_METADATA, args=[collection.name, deposit_id]), | ||||
reverse(PRIVATE_GET_DEPOSIT_METADATA_NC, args=[deposit_id]), | reverse(PRIVATE_GET_DEPOSIT_METADATA_NC, args=[deposit_id]), | ||||
] | ] | ||||
def update_deposit(authenticated_client, collection, deposit, atom_dataset): | |||||
for atom_data in ["entry-data2", "entry-data3"]: | |||||
update_deposit_with_metadata( | |||||
authenticated_client, collection, deposit, atom_dataset[atom_data] | |||||
) | |||||
return deposit | |||||
def update_deposit_with_metadata(authenticated_client, collection, deposit, metadata): | def update_deposit_with_metadata(authenticated_client, collection, deposit, metadata): | ||||
# update deposit's metadata | # update deposit's metadata | ||||
response = authenticated_client.post( | response = authenticated_client.post( | ||||
reverse(EDIT_SE_IRI, args=[collection.name, deposit.id]), | reverse(EDIT_SE_IRI, args=[collection.name, deposit.id]), | ||||
content_type="application/atom+xml;type=entry", | content_type="application/atom+xml;type=entry", | ||||
data=metadata, | data=metadata, | ||||
HTTP_SLUG=deposit.external_id, | HTTP_SLUG=deposit.external_id, | ||||
HTTP_IN_PROGRESS=True, | HTTP_IN_PROGRESS=True, | ||||
) | ) | ||||
assert response.status_code == status.HTTP_201_CREATED | assert response.status_code == status.HTTP_201_CREATED | ||||
return deposit | return deposit | ||||
def test_read_metadata( | def test_read_metadata( | ||||
authenticated_client, deposit_collection, partial_deposit, atom_dataset | authenticated_client, deposit_collection, partial_deposit, atom_dataset | ||||
): | ): | ||||
"""Private metadata read api to existing deposit should return metadata | """Private metadata read api to existing deposit should return metadata | ||||
""" | """ | ||||
deposit = partial_deposit | deposit = partial_deposit | ||||
deposit.external_id = "some-external-id" | deposit.external_id = "some-external-id" | ||||
deposit.save() | deposit.save() | ||||
deposit = update_deposit( | |||||
authenticated_client, deposit_collection, deposit, atom_dataset | metadata_xml_atoms = [ | ||||
atom_dataset[atom_key] for atom_key in ["entry-data2", "entry-data3"] | |||||
] | |||||
metadata_xml_raws = [parse_xml(xml) for xml in metadata_xml_atoms] | |||||
for atom_xml in metadata_xml_atoms: | |||||
deposit = update_deposit_with_metadata( | |||||
authenticated_client, deposit_collection, deposit, atom_xml, | |||||
) | ) | ||||
for url in private_get_raw_url_endpoints(deposit_collection, deposit): | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
response = authenticated_client.get(url) | response = authenticated_client.get(url) | ||||
assert response.status_code == status.HTTP_200_OK | assert response.status_code == status.HTTP_200_OK | ||||
assert response._headers["content-type"][1] == "application/json" | assert response._headers["content-type"][1] == "application/json" | ||||
data = response.json() | data = response.json() | ||||
assert data == { | |||||
expected_meta = { | |||||
"origin": { | "origin": { | ||||
"type": "deposit", | "type": "deposit", | ||||
"url": "https://hal-test.archives-ouvertes.fr/some-external-id", | "url": "https://hal-test.archives-ouvertes.fr/some-external-id", | ||||
}, | }, | ||||
"origin_metadata": { | "origin_metadata": { | ||||
"metadata": { | "metadata_raw": metadata_xml_atoms, | ||||
"author": ["some awesome author", "another one", "no one"], | "metadata_dict": utils.merge(*metadata_xml_raws), | ||||
"codemeta:dateCreated": "2017-10-07T15:17:08Z", | |||||
"external_identifier": "some-external-id", | |||||
"url": "https://hal-test.archives-ouvertes.fr/some-external-id", # noqa | |||||
}, | |||||
"provider": { | "provider": { | ||||
"metadata": {}, | "metadata": {}, | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
Show All 16 Lines | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
}, | }, | ||||
"client": "test", | "client": "test", | ||||
"id": deposit.id, | "id": deposit.id, | ||||
"collection": "test", | "collection": "test", | ||||
"revision_parents": [], | "revision_parents": [], | ||||
}, | }, | ||||
} | } | ||||
assert data == expected_meta | |||||
def test_read_metadata_revision_with_parent( | def test_read_metadata_revision_with_parent( | ||||
authenticated_client, deposit_collection, partial_deposit, atom_dataset | authenticated_client, deposit_collection, partial_deposit, atom_dataset | ||||
): | ): | ||||
"""Private read metadata to a deposit (with parent) returns metadata | """Private read metadata to a deposit (with parent) returns metadata | ||||
""" | """ | ||||
deposit = partial_deposit | deposit = partial_deposit | ||||
deposit.external_id = "some-external-id" | deposit.external_id = "some-external-id" | ||||
deposit.save() | deposit.save() | ||||
deposit = update_deposit( | metadata_xml_atoms = [ | ||||
authenticated_client, deposit_collection, deposit, atom_dataset | atom_dataset[atom_key] for atom_key in ["entry-data2", "entry-data3"] | ||||
] | |||||
metadata_xml_raws = [parse_xml(xml) for xml in metadata_xml_atoms] | |||||
for atom_xml in metadata_xml_atoms: | |||||
deposit = update_deposit_with_metadata( | |||||
authenticated_client, deposit_collection, deposit, atom_xml, | |||||
) | ) | ||||
rev_id = "da78a9d4cf1d5d29873693fd496142e3a18c20fa" | rev_id = "da78a9d4cf1d5d29873693fd496142e3a18c20fa" | ||||
swhid = "swh:1:rev:%s" % rev_id | swhid = "swh:1:rev:%s" % rev_id | ||||
fake_parent = Deposit( | fake_parent = Deposit( | ||||
swhid=swhid, client=deposit.client, collection=deposit.collection | swhid=swhid, client=deposit.client, collection=deposit.collection | ||||
) | ) | ||||
fake_parent.save() | fake_parent.save() | ||||
deposit.parent = fake_parent | deposit.parent = fake_parent | ||||
deposit.save() | deposit.save() | ||||
for url in private_get_raw_url_endpoints(deposit_collection, deposit): | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
response = authenticated_client.get(url) | response = authenticated_client.get(url) | ||||
assert response.status_code == status.HTTP_200_OK | assert response.status_code == status.HTTP_200_OK | ||||
assert response._headers["content-type"][1] == "application/json" | assert response._headers["content-type"][1] == "application/json" | ||||
data = response.json() | data = response.json() | ||||
assert data == { | |||||
expected_meta = { | |||||
"origin": { | "origin": { | ||||
"type": "deposit", | "type": "deposit", | ||||
"url": "https://hal-test.archives-ouvertes.fr/some-external-id", | "url": "https://hal-test.archives-ouvertes.fr/some-external-id", | ||||
}, | }, | ||||
"origin_metadata": { | "origin_metadata": { | ||||
"metadata": { | "metadata_raw": metadata_xml_atoms, | ||||
"author": ["some awesome author", "another one", "no one"], | "metadata_dict": utils.merge(*metadata_xml_raws), | ||||
"codemeta:dateCreated": "2017-10-07T15:17:08Z", | |||||
"external_identifier": "some-external-id", | |||||
"url": "https://hal-test.archives-ouvertes.fr/some-external-id", # noqa | |||||
}, | |||||
"provider": { | "provider": { | ||||
"metadata": {}, | "metadata": {}, | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
Show All 16 Lines | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
}, | }, | ||||
"client": "test", | "client": "test", | ||||
"id": deposit.id, | "id": deposit.id, | ||||
"collection": "test", | "collection": "test", | ||||
"revision_parents": [rev_id], | "revision_parents": [rev_id], | ||||
}, | }, | ||||
} | } | ||||
assert data == expected_meta | |||||
def test_read_metadata_3( | def test_read_metadata_3( | ||||
authenticated_client, deposit_collection, partial_deposit, atom_dataset | authenticated_client, deposit_collection, partial_deposit, atom_dataset | ||||
): | ): | ||||
"""date(Created|Published) provided, uses author/committer date | """date(Created|Published) provided, uses author/committer date | ||||
""" | """ | ||||
deposit = partial_deposit | deposit = partial_deposit | ||||
deposit.external_id = "hal-01243065" | deposit.external_id = "hal-01243065" | ||||
deposit.save() | deposit.save() | ||||
deposit = update_deposit( | |||||
authenticated_client, deposit_collection, deposit, atom_dataset | |||||
) | |||||
# add metadata to the deposit with datePublished and dateCreated | # add metadata to the deposit with datePublished and dateCreated | ||||
codemeta_entry_data = ( | codemeta_entry_data = ( | ||||
atom_dataset["metadata"] | atom_dataset["metadata"] | ||||
% """ | % """ | ||||
<codemeta:dateCreated>2015-04-06T17:08:47+02:00</codemeta:dateCreated> | <codemeta:dateCreated>2015-04-06T17:08:47+02:00</codemeta:dateCreated> | ||||
<codemeta:datePublished>2017-05-03T16:08:47+02:00</codemeta:datePublished> | <codemeta:datePublished>2017-05-03T16:08:47+02:00</codemeta:datePublished> | ||||
""" | """ | ||||
) | ) | ||||
metadata_xml_atoms = [ | |||||
atom_dataset["entry-data2"], | |||||
atom_dataset["entry-data3"], | |||||
codemeta_entry_data, | |||||
] | |||||
metadata_xml_raws = [parse_xml(xml) for xml in metadata_xml_atoms] | |||||
for atom_xml in metadata_xml_atoms: | |||||
update_deposit_with_metadata( | update_deposit_with_metadata( | ||||
authenticated_client, deposit_collection, deposit, codemeta_entry_data | authenticated_client, deposit_collection, deposit, atom_xml, | ||||
) | ) | ||||
for url in private_get_raw_url_endpoints(deposit_collection, deposit): | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
response = authenticated_client.get(url) | response = authenticated_client.get(url) | ||||
assert response.status_code == status.HTTP_200_OK | assert response.status_code == status.HTTP_200_OK | ||||
assert response._headers["content-type"][1] == "application/json" | assert response._headers["content-type"][1] == "application/json" | ||||
data = response.json() | data = response.json() | ||||
assert data == { | |||||
metadata = { | |||||
"author": [ | |||||
"some awesome author", | |||||
"another one", | |||||
"no one", | |||||
{"email": "hal@ccsd.cnrs.fr", "name": "HAL"}, | |||||
], | |||||
"client": "hal", | |||||
"codemeta:applicationCategory": "test", | |||||
"codemeta:author": {"codemeta:name": "Morane Gruenpeter"}, | |||||
"codemeta:dateCreated": [ | |||||
"2017-10-07T15:17:08Z", | |||||
"2015-04-06T17:08:47+02:00", | |||||
], | |||||
"codemeta:datePublished": "2017-05-03T16:08:47+02:00", | |||||
"codemeta:description": "this is the description", | |||||
"codemeta:developmentStatus": "stable", | |||||
"codemeta:keywords": "DSP programming", | |||||
"codemeta:license": [ | |||||
{"codemeta:name": "GNU General Public License v3.0 only"}, | |||||
{ | |||||
"codemeta:name": "CeCILL " | |||||
"Free " | |||||
"Software " | |||||
"License " | |||||
"Agreement " | |||||
"v1.1" | |||||
}, | |||||
], | |||||
"codemeta:programmingLanguage": ["php", "python", "C"], | |||||
"codemeta:runtimePlatform": "phpstorm", | |||||
"codemeta:url": "https://hal-test.archives-ouvertes.fr/hal-01243065", # noqa | |||||
"codemeta:version": "1", | |||||
"external_identifier": ["some-external-id", "hal-01243065"], | |||||
"id": "hal-01243065", | |||||
"title": "Composing a Web of Audio Applications", | |||||
"url": "https://hal-test.archives-ouvertes.fr/some-external-id", | |||||
} | |||||
expected_meta = { | |||||
"origin": { | "origin": { | ||||
"type": "deposit", | "type": "deposit", | ||||
"url": "https://hal-test.archives-ouvertes.fr/hal-01243065", | "url": "https://hal-test.archives-ouvertes.fr/hal-01243065", | ||||
}, | }, | ||||
"origin_metadata": { | "origin_metadata": { | ||||
"metadata": metadata, | "metadata_raw": metadata_xml_atoms, | ||||
"metadata_dict": utils.merge(*metadata_xml_raws), | |||||
"provider": { | "provider": { | ||||
"metadata": {}, | "metadata": {}, | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
Show All 15 Lines | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
"timestamp": {"microseconds": 0, "seconds": 1507389428}, | "timestamp": {"microseconds": 0, "seconds": 1507389428}, | ||||
}, | }, | ||||
"client": deposit_collection.name, | "client": deposit_collection.name, | ||||
"id": deposit.id, | "id": deposit.id, | ||||
"collection": deposit_collection.name, | "collection": deposit_collection.name, | ||||
"revision_parents": [], | "revision_parents": [], | ||||
}, | }, | ||||
} | } | ||||
assert data == expected_meta | |||||
def test_read_metadata_4( | def test_read_metadata_4( | ||||
authenticated_client, deposit_collection, atom_dataset, partial_deposit | authenticated_client, deposit_collection, atom_dataset, partial_deposit | ||||
): | ): | ||||
"""dateCreated/datePublished not provided, revision uses complete_date | """dateCreated/datePublished not provided, revision uses complete_date | ||||
""" | """ | ||||
Show All 9 Lines | ): | ||||
for url in private_get_raw_url_endpoints(deposit_collection, deposit): | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
response = authenticated_client.get(url) | response = authenticated_client.get(url) | ||||
assert response.status_code == status.HTTP_200_OK | assert response.status_code == status.HTTP_200_OK | ||||
assert response._headers["content-type"][1] == "application/json" | assert response._headers["content-type"][1] == "application/json" | ||||
data = response.json() | data = response.json() | ||||
metadata = { | |||||
"author": {"email": "hal@ccsd.cnrs.fr", "name": "HAL"}, | |||||
"client": "hal", | |||||
"codemeta:applicationCategory": "test", | |||||
"codemeta:author": {"codemeta:name": "Morane Gruenpeter"}, | |||||
"codemeta:description": "this is the description", | |||||
"codemeta:developmentStatus": "stable", | |||||
"codemeta:keywords": "DSP programming", | |||||
"codemeta:license": [ | |||||
{ | |||||
"codemeta:name": "GNU " | |||||
"General " | |||||
"Public " | |||||
"License " | |||||
"v3.0 " | |||||
"only" | |||||
}, | |||||
{ | |||||
"codemeta:name": "CeCILL " | |||||
"Free " | |||||
"Software " | |||||
"License " | |||||
"Agreement " | |||||
"v1.1" | |||||
}, | |||||
], | |||||
"codemeta:programmingLanguage": ["php", "python", "C"], | |||||
"codemeta:runtimePlatform": "phpstorm", | |||||
"codemeta:url": "https://hal-test.archives-ouvertes.fr/hal-01243065", | |||||
"codemeta:version": "1", | |||||
"external_identifier": "hal-01243065", | |||||
"id": "hal-01243065", | |||||
"title": "Composing a Web of Audio Applications", | |||||
} | |||||
expected_origin = { | expected_origin = { | ||||
"type": "deposit", | "type": "deposit", | ||||
"url": "https://hal-test.archives-ouvertes.fr/%s" % (deposit.external_id), | "url": "https://hal-test.archives-ouvertes.fr/%s" % (deposit.external_id), | ||||
} | } | ||||
expected_origin_metadata = { | expected_origin_metadata = { | ||||
"metadata": metadata, | "metadata_raw": [codemeta_entry_data], | ||||
"metadata_dict": parse_xml(codemeta_entry_data), | |||||
"provider": { | "provider": { | ||||
"metadata": {}, | "metadata": {}, | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | for url in private_get_raw_url_endpoints(deposit_collection, deposit): | ||||
assert response._headers["content-type"][1] == "application/json" | assert response._headers["content-type"][1] == "application/json" | ||||
data = response.json() | data = response.json() | ||||
expected_origin = { | expected_origin = { | ||||
"type": "deposit", | "type": "deposit", | ||||
"url": "https://hal-test.archives-ouvertes.fr/external-id-partial", | "url": "https://hal-test.archives-ouvertes.fr/external-id-partial", | ||||
} | } | ||||
metadata = { | |||||
"author": {"email": "hal@ccsd.cnrs.fr", "name": "HAL"}, | |||||
"client": "hal", | |||||
"codemeta:applicationCategory": "test", | |||||
"codemeta:author": {"codemeta:name": "Morane Gruenpeter"}, | |||||
"codemeta:dateCreated": [ | |||||
"2015-04-06T17:08:47+02:00", | |||||
"2016-04-06T17:08:47+02:00", | |||||
], | |||||
"codemeta:datePublished": [ | |||||
"2017-05-03T16:08:47+02:00", | |||||
"2018-05-03T16:08:47+02:00", | |||||
], | |||||
"codemeta:description": "this is the description", | |||||
"codemeta:developmentStatus": "stable", | |||||
"codemeta:keywords": "DSP programming", | |||||
"codemeta:license": [ | |||||
{ | |||||
"codemeta:name": "GNU " | |||||
"General " | |||||
"Public " | |||||
"License " | |||||
"v3.0 " | |||||
"only" | |||||
}, | |||||
{ | |||||
"codemeta:name": "CeCILL " | |||||
"Free " | |||||
"Software " | |||||
"License " | |||||
"Agreement " | |||||
"v1.1" | |||||
}, | |||||
], | |||||
"codemeta:programmingLanguage": ["php", "python", "C"], | |||||
"codemeta:runtimePlatform": "phpstorm", | |||||
"codemeta:url": "https://hal-test.archives-ouvertes.fr/hal-01243065", # noqa | |||||
"codemeta:version": "1", | |||||
"external_identifier": "hal-01243065", | |||||
"id": "hal-01243065", | |||||
"title": "Composing a Web of Audio Applications", | |||||
} | |||||
expected_origin_metadata = { | expected_origin_metadata = { | ||||
"metadata": metadata, | "metadata_raw": [codemeta_entry_data], | ||||
"metadata_dict": parse_xml(codemeta_entry_data), | |||||
"provider": { | "provider": { | ||||
"metadata": {}, | "metadata": {}, | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |