diff --git a/api_tests/actions/views/test_action_list.py b/api_tests/actions/views/test_action_list.py index d04b57c1a43..bda6be6890a 100644 --- a/api_tests/actions/views/test_action_list.py +++ b/api_tests/actions/views/test_action_list.py @@ -154,7 +154,6 @@ def test_bad_requests(self, app, url, preprint, provider, moderator): ('initial', 'edit_comment'), ('initial', 'reject'), ('initial', 'withdraw'), - ('pending', 'submit'), ('rejected', 'reject'), ('rejected', 'submit'), ('rejected', 'withdraw'), diff --git a/osf/models/base.py b/osf/models/base.py index 594522ed63a..a118f0e1a2b 100644 --- a/osf/models/base.py +++ b/osf/models/base.py @@ -57,10 +57,10 @@ def coerce_guid(maybe_guid, create_if_needed=False): raise InvalidGuid(f'guid does not exist ({maybe_guid})') return guid if isinstance(maybe_guid, str): - try: - return Guid.objects.get(_id=maybe_guid) - except Guid.DoesNotExist: + guid = Guid.load(maybe_guid) + if not guid: raise InvalidGuid(f'guid does not exist ({maybe_guid})') + return guid raise InvalidGuid(f'cannot coerce {type(maybe_guid)} ({maybe_guid}) into Guid') diff --git a/osf_tests/factories.py b/osf_tests/factories.py index 9ae6a84db2c..c923c62e6b1 100644 --- a/osf_tests/factories.py +++ b/osf_tests/factories.py @@ -734,11 +734,24 @@ def _create(cls, target_class, *args, **kwargs): # Step 1: create prerpint, guid and versioned guid instance = cls._build(target_class, *args, **kwargs) instance.save(guid_ready=False) - guid = models.Guid.objects.create( - referent=instance, - content_type=ContentType.objects.get_for_model(instance), - object_id=instance.pk, - ) + # If use_guid is passed, use that guid instead of creating a new one + if use_guid := kwargs.pop('use_guid', None): + guid = use_guid + guid.referent = instance + guid.content_type = ContentType.objects.get_for_model(instance) + guid.object_id = instance.pk + guid.save() + else: + guid = models.Guid.objects.create( + referent=instance, + content_type=ContentType.objects.get_for_model(instance), + object_id=instance.pk, + ) + # If guid__id is passed, set the guid's id to that value + if guid__id := kwargs.pop('set_guid', None): + guid._id = guid__id + guid.save() + models.GuidVersionsThrough.objects.create( referent=instance, content_type=ContentType.objects.get_for_model(instance), diff --git a/osf_tests/management_commands/test_check_crossref_dois.py b/osf_tests/management_commands/test_check_crossref_dois.py index 8a1e45cc975..c4e37d9c389 100644 --- a/osf_tests/management_commands/test_check_crossref_dois.py +++ b/osf_tests/management_commands/test_check_crossref_dois.py @@ -22,7 +22,7 @@ def preprint(self): @pytest.fixture() def stuck_preprint(self): - preprint = PreprintFactory(set_doi=False) + preprint = PreprintFactory(set_doi=False, set_guid='guid0') preprint.date_published = preprint.date_published - timedelta(days=settings.DAYS_CROSSREF_DOIS_MUST_BE_STUCK_BEFORE_EMAIL + 1) # match guid to the fixture crossref_works_response.json guid = preprint.guids.first() diff --git a/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.json b/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.json index c6d4527b189..96e1b51c490 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.json +++ b/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.json @@ -48,7 +48,7 @@ ], "fundingReferences": [], "identifier": { - "identifier": "11.pp/FK2osf.io/w4ibb", + "identifier": "11.pp/FK2osf.io/w4ibb_v1", "identifierType": "DOI" }, "publicationYear": "2123", diff --git a/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.xml b/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.xml index 762cc061c53..57d9d39eb1f 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.xml +++ b/osf_tests/metadata/expected_metadata_files/preprint_basic.datacite.xml @@ -1,6 +1,6 @@ - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 Person McNamington diff --git a/osf_tests/metadata/expected_metadata_files/preprint_basic.turtle b/osf_tests/metadata/expected_metadata_files/preprint_basic.turtle index ee7e866827b..d630032f71b 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_basic.turtle +++ b/osf_tests/metadata/expected_metadata_files/preprint_basic.turtle @@ -13,7 +13,7 @@ dcterms:description "this is a preprint description!" ; dcterms:hasVersion ; dcterms:identifier "http://localhost:5000/w4ibb", - "https://doi.org/11.pp/FK2osf.io/w4ibb" ; + "https://doi.org/11.pp/FK2osf.io/w4ibb_v1" ; dcterms:modified "2123-05-04" ; dcterms:publisher ; dcterms:subject , @@ -22,7 +22,7 @@ ; dcterms:title "this is a preprint title!" ; dcterms:type ; - owl:sameAs ; + owl:sameAs ; dcat:accessService ; osf:hostingInstitution ; osf:isSupplementedBy ; @@ -94,4 +94,3 @@ a skos:Concept ; skos:inScheme ; skos:prefLabel "wibbble" . - diff --git a/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.json b/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.json index 7858cc20bd9..5d9516b2b7a 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.json +++ b/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.json @@ -48,7 +48,7 @@ ], "fundingReferences": [], "identifier": { - "identifier": "11.pp/FK2osf.io/w4ibb", + "identifier": "11.pp/FK2osf.io/w4ibb_v1", "identifierType": "DOI" }, "publicationYear": "2123", diff --git a/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.xml b/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.xml index 5803ee9f2f0..94739e3b322 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.xml +++ b/osf_tests/metadata/expected_metadata_files/preprint_full.datacite.xml @@ -1,6 +1,6 @@ - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 Person McNamington diff --git a/osf_tests/metadata/expected_metadata_files/preprint_full.turtle b/osf_tests/metadata/expected_metadata_files/preprint_full.turtle index cdf665fd5fe..56486195974 100644 --- a/osf_tests/metadata/expected_metadata_files/preprint_full.turtle +++ b/osf_tests/metadata/expected_metadata_files/preprint_full.turtle @@ -13,7 +13,7 @@ dcterms:description "this is a preprint description!" ; dcterms:hasVersion ; dcterms:identifier "http://localhost:5000/w4ibb", - "https://doi.org/11.pp/FK2osf.io/w4ibb" ; + "https://doi.org/11.pp/FK2osf.io/w4ibb_v1" ; dcterms:modified "2123-05-04" ; dcterms:publisher ; dcterms:subject , @@ -22,7 +22,7 @@ ; dcterms:title "this is a preprint title!" ; dcterms:type ; - owl:sameAs ; + owl:sameAs ; dcat:accessService ; osf:hostingInstitution ; osf:isSupplementedBy ; @@ -124,4 +124,3 @@ a skos:Concept ; skos:inScheme ; skos:prefLabel "wibbble" . - diff --git a/osf_tests/metadata/expected_metadata_files/project_basic.datacite.json b/osf_tests/metadata/expected_metadata_files/project_basic.datacite.json index 0de527a5356..1f85e773f2f 100644 --- a/osf_tests/metadata/expected_metadata_files/project_basic.datacite.json +++ b/osf_tests/metadata/expected_metadata_files/project_basic.datacite.json @@ -60,7 +60,7 @@ "relationType": "HasVersion" }, { - "relatedIdentifier": "11.pp/FK2osf.io/w4ibb", + "relatedIdentifier": "11.pp/FK2osf.io/w4ibb_v1", "relatedIdentifierType": "DOI", "relationType": "IsSupplementTo" } @@ -85,7 +85,7 @@ "publicationYear": "2123", "publisher": "PP the Preprint Provider", "relatedItemIdentifier": { - "relatedItemIdentifier": "11.pp/FK2osf.io/w4ibb", + "relatedItemIdentifier": "11.pp/FK2osf.io/w4ibb_v1", "relatedItemIdentifierType": "DOI" }, "relatedItemType": "Preprint", diff --git a/osf_tests/metadata/expected_metadata_files/project_basic.datacite.xml b/osf_tests/metadata/expected_metadata_files/project_basic.datacite.xml index e1f3e6394c0..8b4abfb5d87 100644 --- a/osf_tests/metadata/expected_metadata_files/project_basic.datacite.xml +++ b/osf_tests/metadata/expected_metadata_files/project_basic.datacite.xml @@ -37,7 +37,7 @@ http://localhost:5000/w5ibb - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 @@ -49,7 +49,7 @@ RegiProvi the Registration Provider - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 this is a preprint title! diff --git a/osf_tests/metadata/expected_metadata_files/project_basic.turtle b/osf_tests/metadata/expected_metadata_files/project_basic.turtle index aa8244da1fd..832dd6170c0 100644 --- a/osf_tests/metadata/expected_metadata_files/project_basic.turtle +++ b/osf_tests/metadata/expected_metadata_files/project_basic.turtle @@ -33,11 +33,11 @@ dcterms:created "2123-05-04" ; dcterms:creator ; dcterms:identifier "http://localhost:5000/w4ibb", - "https://doi.org/11.pp/FK2osf.io/w4ibb" ; + "https://doi.org/11.pp/FK2osf.io/w4ibb_v1" ; dcterms:publisher ; dcterms:title "this is a preprint title!" ; dcterms:type ; - owl:sameAs . + owl:sameAs . a osf:Registration ; dcterms:created "2123-05-04" ; diff --git a/osf_tests/metadata/expected_metadata_files/project_full.datacite.json b/osf_tests/metadata/expected_metadata_files/project_full.datacite.json index 463d8af4d88..4ead1090105 100644 --- a/osf_tests/metadata/expected_metadata_files/project_full.datacite.json +++ b/osf_tests/metadata/expected_metadata_files/project_full.datacite.json @@ -94,7 +94,7 @@ "relationType": "HasVersion" }, { - "relatedIdentifier": "11.pp/FK2osf.io/w4ibb", + "relatedIdentifier": "11.pp/FK2osf.io/w4ibb_v1", "relatedIdentifierType": "DOI", "relationType": "IsSupplementTo" } @@ -119,7 +119,7 @@ "publicationYear": "2123", "publisher": "PP the Preprint Provider", "relatedItemIdentifier": { - "relatedItemIdentifier": "11.pp/FK2osf.io/w4ibb", + "relatedItemIdentifier": "11.pp/FK2osf.io/w4ibb_v1", "relatedItemIdentifierType": "DOI" }, "relatedItemType": "Preprint", diff --git a/osf_tests/metadata/expected_metadata_files/project_full.datacite.xml b/osf_tests/metadata/expected_metadata_files/project_full.datacite.xml index eff81de6099..e8649704d02 100644 --- a/osf_tests/metadata/expected_metadata_files/project_full.datacite.xml +++ b/osf_tests/metadata/expected_metadata_files/project_full.datacite.xml @@ -55,7 +55,7 @@ http://localhost:5000/w5ibb - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 @@ -67,7 +67,7 @@ RegiProvi the Registration Provider - 11.pp/FK2osf.io/w4ibb + 11.pp/FK2osf.io/w4ibb_v1 this is a preprint title! diff --git a/osf_tests/metadata/expected_metadata_files/project_full.turtle b/osf_tests/metadata/expected_metadata_files/project_full.turtle index 63946b2f80b..3fbc31f7ba0 100644 --- a/osf_tests/metadata/expected_metadata_files/project_full.turtle +++ b/osf_tests/metadata/expected_metadata_files/project_full.turtle @@ -39,11 +39,11 @@ dcterms:created "2123-05-04" ; dcterms:creator ; dcterms:identifier "http://localhost:5000/w4ibb", - "https://doi.org/11.pp/FK2osf.io/w4ibb" ; + "https://doi.org/11.pp/FK2osf.io/w4ibb_v1" ; dcterms:publisher ; dcterms:title "this is a preprint title!" ; dcterms:type ; - owl:sameAs . + owl:sameAs . a osf:Registration ; dcterms:created "2123-05-04" ; diff --git a/osf_tests/metadata/test_osf_gathering.py b/osf_tests/metadata/test_osf_gathering.py index 4c064c8a690..4c95711c615 100644 --- a/osf_tests/metadata/test_osf_gathering.py +++ b/osf_tests/metadata/test_osf_gathering.py @@ -124,7 +124,7 @@ def test_setupdata(self): assert self.registrationfocus.iri == OSFIO[self.registration._id] assert self.registrationfocus.rdftype == OSF.Registration assert self.registrationfocus.dbmodel is self.registration - assert self.preprintfocus.iri == OSFIO[self.preprint._id] + assert self.preprintfocus.iri == OSFIO[self.preprint.get_guid()._id] assert self.preprintfocus.rdftype == OSF.Preprint assert self.preprintfocus.dbmodel is self.preprint assert self.filefocus.iri == OSFIO[self.file.get_guid()._id] diff --git a/osf_tests/metadata/test_serialized_metadata.py b/osf_tests/metadata/test_serialized_metadata.py index c8a0eee95ac..9300f20f778 100644 --- a/osf_tests/metadata/test_serialized_metadata.py +++ b/osf_tests/metadata/test_serialized_metadata.py @@ -250,6 +250,7 @@ def setUp(self): subjects=[ [parent_subject._id, child_subject._id], ], + use_guid=osfguid_sequence.get_or_create(id=-1)[0] ) self.registration = factories.RegistrationFactory( is_public=True, diff --git a/osf_tests/metrics/reporters/test_public_item_usage_reporter.py b/osf_tests/metrics/reporters/test_public_item_usage_reporter.py index b75c420b1a2..69bd266285a 100644 --- a/osf_tests/metrics/reporters/test_public_item_usage_reporter.py +++ b/osf_tests/metrics/reporters/test_public_item_usage_reporter.py @@ -27,8 +27,7 @@ def _patch_settings(self): @pytest.fixture def item0(self): - _item0 = factories.PreprintFactory(is_public=True) - _item0._id = 'item0' + _item0 = factories.PreprintFactory(is_public=True, set_guid='item0') return _item0 @pytest.fixture @@ -186,7 +185,7 @@ def test_reporter(self, ym_empty, ym_sparse, ym_busy, sparse_month_usage, busy_m _sparse_item0, _sparse_item1, _sparse_item2 = sorted(_sparse, key=attrgetter('item_osfid')) # sparse-month item0 assert isinstance(_sparse_item0, PublicItemUsageReport) - assert _sparse_item0.item_osfid == 'item0' + assert _sparse_item0.item_osfid == 'item0_v1' assert _sparse_item0.provider_id == [item0.provider._id] assert _sparse_item0.platform_iri == ['http://osf.example'] assert _sparse_item0.view_count == 3 @@ -217,7 +216,7 @@ def test_reporter(self, ym_empty, ym_sparse, ym_busy, sparse_month_usage, busy_m _busy_item0, _busy_item1, _busy_item2 = sorted(_busy, key=attrgetter('item_osfid')) # busy-month item0 assert isinstance(_busy_item0, PublicItemUsageReport) - assert _busy_item0.item_osfid == 'item0' + assert _busy_item0.item_osfid == 'item0_v1' assert _busy_item0.provider_id == [item0.provider._id] assert _busy_item0.platform_iri == ['http://osf.example'] assert _busy_item0.view_count == 4 * 7 diff --git a/osf_tests/test_guid.py b/osf_tests/test_guid.py index 7c467e2aa10..af4ab7c139d 100644 --- a/osf_tests/test_guid.py +++ b/osf_tests/test_guid.py @@ -1,18 +1,23 @@ from unittest import mock -import pytest from urllib.parse import quote from django.utils import timezone from django.core.exceptions import MultipleObjectsReturned -from framework.auth import Auth - +import pytest -from osf.models import Guid, NodeLicenseRecord, OSFUser, GuidVersionsThrough +from framework.auth import Auth +from osf.models import Guid, GuidVersionsThrough, NodeLicenseRecord, OSFUser, Preprint from osf.models.base import VersionedGuidMixin -from osf_tests.factories import AuthUserFactory, UserFactory, NodeFactory, NodeLicenseRecordFactory, \ - RegistrationFactory, PreprintFactory, PreprintProviderFactory from osf.utils.permissions import ADMIN -from osf.models import Preprint +from osf_tests.factories import ( + AuthUserFactory, + NodeFactory, + NodeLicenseRecordFactory, + PreprintFactory, + PreprintProviderFactory, + RegistrationFactory, + UserFactory, +) from tests.base import OsfTestCase from tests.test_websitefiles import TestFile from website.settings import MFR_SERVER_URL, WATERBUTLER_URL @@ -45,6 +50,7 @@ def test_short_guid_gets_generated_on_creation(self, Factory): assert obj._id assert len(obj._id) == 5 + @pytest.mark.django_db class TestReferent: @@ -458,6 +464,7 @@ def test_resolve_guid_download_errors(self): res = self.app.get(pp.url + 'download', auth=non_contrib.auth) assert res.status_code == 410 + @pytest.fixture() def creator(): return AuthUserFactory() @@ -470,6 +477,7 @@ def auth(creator): def preprint_provider(): return PreprintProviderFactory() + @pytest.mark.django_db class TestGuidVersionsThrough: def test_creation_versioned_guid(self, creator, preprint_provider): @@ -480,19 +488,18 @@ def test_creation_versioned_guid(self, creator, preprint_provider): description='Abstract' ) assert preprint.guids.count() == 1 - preprint_guid = preprint.guids.first() - version_entry = preprint.versioned_guids.first() assert preprint.creator == creator assert preprint.provider == preprint_provider assert preprint.title == 'Preprint' assert preprint.description == 'Abstract' - assert preprint_guid._id == version_entry.guid._id + preprint_guid = preprint.guids.first() assert preprint_guid.referent == preprint assert preprint_guid.content_type.model == 'preprint' assert preprint_guid.object_id == preprint.pk assert preprint_guid.is_versioned is True + version_entry = preprint.versioned_guids.first() assert version_entry.guid == preprint_guid assert version_entry.referent == preprint assert version_entry.content_type.model == 'preprint' @@ -505,7 +512,7 @@ def test_create_version(self, creator, preprint_provider): preprint_guid = preprint.guids.first() preprint_metadata = { - 'subjects': [[el] for el in preprint.subjects.all().values_list('_id', flat=True)], + 'subjects': [el for el in preprint.subjects.all().values_list('_id', flat=True)], 'original_publication_date': preprint.original_publication_date, 'custom_publication_citation': preprint.custom_publication_citation, 'article_doi': preprint.article_doi, @@ -526,12 +533,8 @@ def test_create_version(self, creator, preprint_provider): 'copyright_holders': preprint.license.copyright_holders, 'year': preprint.license.year } - auth = Auth(user=creator) - new_preprint, data_for_update = Preprint.create_version( - create_from_guid=preprint._id, - auth=auth - ) + new_preprint, data_for_update = Preprint.create_version(create_from_guid=preprint._id, auth=auth) tags = data_for_update.pop('tags') assert list(tags) == list(preprint.tags.all().values_list('name', flat=True)) assert preprint_metadata == data_for_update @@ -560,10 +563,9 @@ def test_versioned_preprint_id_property(self, creator, preprint_provider): description='Abstract' ) preprint_guid = preprint.guids.first() - expected_guid = f'{preprint_guid._id}{VersionedGuidMixin.GUID_VERSION_DELIMITER}1' - + expected_guid = f'{preprint_guid._id}{VersionedGuidMixin.GUID_VERSION_DELIMITER}{VersionedGuidMixin.INITIAL_VERSION_NUMBER}' assert preprint._id == expected_guid GuidVersionsThrough.objects.filter(guid=preprint_guid).delete() - + preprint._id = None assert preprint._id is None