|
| 1 | +import pytest |
| 2 | +from unittest import mock |
| 3 | +from django.utils import timezone |
| 4 | + |
| 5 | +from osf.models import Preprint |
| 6 | +from osf_tests.factories import PreprintFactory, PreprintProviderFactory |
| 7 | +from osf.management.commands.resync_preprint_dois_v1 import ( |
| 8 | + get_preprints_needing_v1_doi, |
| 9 | + resync_preprint_dois_v1, |
| 10 | +) |
| 11 | +from website import settings |
| 12 | + |
| 13 | +pytestmark = pytest.mark.django_db |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture() |
| 17 | +def provider(): |
| 18 | + p = PreprintProviderFactory() |
| 19 | + p.doi_prefix = '10.31219' |
| 20 | + p.save() |
| 21 | + return p |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def preprint(provider): |
| 26 | + pp = PreprintFactory(provider=provider, is_published=True) |
| 27 | + old_doi = settings.DOI_FORMAT.format(prefix=provider.doi_prefix, guid=pp.get_guid()._id) |
| 28 | + pp.set_identifier_values(doi=old_doi, save=True) |
| 29 | + return pp |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture() |
| 33 | +def preprint_with_v1_doi(provider): |
| 34 | + pp = PreprintFactory(provider=provider, is_published=True) |
| 35 | + v1_doi = settings.DOI_FORMAT.format(prefix=provider.doi_prefix, guid=pp._id) |
| 36 | + pp.set_identifier_values(doi=v1_doi, save=True) |
| 37 | + return pp |
| 38 | + |
| 39 | + |
| 40 | +class TestGetPreprrintsNeedingV1Doi: |
| 41 | + |
| 42 | + def test_includes_public_preprint_without_versioned_doi(self, preprint): |
| 43 | + qs = get_preprints_needing_v1_doi() |
| 44 | + assert preprint in qs |
| 45 | + |
| 46 | + def test_excludes_preprint_with_versioned_doi(self, preprint_with_v1_doi): |
| 47 | + qs = get_preprints_needing_v1_doi() |
| 48 | + assert preprint_with_v1_doi not in qs |
| 49 | + |
| 50 | + def test_excludes_preprint_with_no_doi_if_private(self, provider): |
| 51 | + private_preprint = PreprintFactory(provider=provider, is_published=False) |
| 52 | + private_preprint.is_public = False |
| 53 | + private_preprint.save() |
| 54 | + qs = get_preprints_needing_v1_doi() |
| 55 | + assert private_preprint not in qs |
| 56 | + |
| 57 | + def test_includes_withdrawn_preprint_with_ever_public(self, provider): |
| 58 | + pp = PreprintFactory(provider=provider, is_published=True) |
| 59 | + old_doi = settings.DOI_FORMAT.format(prefix=provider.doi_prefix, guid=pp.get_guid()._id) |
| 60 | + pp.set_identifier_values(doi=old_doi, save=True) |
| 61 | + pp.date_withdrawn = timezone.now() |
| 62 | + pp.ever_public = True |
| 63 | + pp.save() |
| 64 | + qs = get_preprints_needing_v1_doi() |
| 65 | + assert pp in qs |
| 66 | + |
| 67 | + def test_excludes_withdrawn_preprint_never_public(self, provider): |
| 68 | + pp = PreprintFactory(provider=provider, is_published=False) |
| 69 | + Preprint.objects.filter(pk=pp.pk).update(date_withdrawn=timezone.now()) |
| 70 | + qs = get_preprints_needing_v1_doi() |
| 71 | + assert pp not in qs |
| 72 | + |
| 73 | + def test_excludes_version_2_preprint(self, preprint): |
| 74 | + from tests.utils import capture_notifications |
| 75 | + with capture_notifications(): |
| 76 | + v2 = PreprintFactory.create_version(preprint, is_published=True, set_doi=False) |
| 77 | + old_doi = settings.DOI_FORMAT.format(prefix=preprint.provider.doi_prefix, guid=v2.get_guid()._id) |
| 78 | + v2.set_identifier_values(doi=old_doi, save=True) |
| 79 | + qs = get_preprints_needing_v1_doi() |
| 80 | + assert v2 not in qs |
| 81 | + |
| 82 | + def test_excludes_qatest_tagged_preprint(self, preprint): |
| 83 | + preprint.add_system_tag('qatest') |
| 84 | + qs = get_preprints_needing_v1_doi() |
| 85 | + assert preprint not in qs |
| 86 | + |
| 87 | + def test_excludes_deleted_preprint(self, preprint): |
| 88 | + preprint.deleted = timezone.now() |
| 89 | + preprint.save() |
| 90 | + qs = get_preprints_needing_v1_doi() |
| 91 | + assert preprint not in qs |
| 92 | + |
| 93 | + def test_provider_filter_limits_results(self, preprint, provider): |
| 94 | + other_provider = PreprintProviderFactory() |
| 95 | + other_provider.doi_prefix = '10.12345' |
| 96 | + other_provider.save() |
| 97 | + other_preprint = PreprintFactory(provider=other_provider, is_published=True) |
| 98 | + old_doi = settings.DOI_FORMAT.format(prefix=other_provider.doi_prefix, guid=other_preprint.get_guid()._id) |
| 99 | + other_preprint.set_identifier_values(doi=old_doi, save=True) |
| 100 | + |
| 101 | + qs = get_preprints_needing_v1_doi(provider_id=provider._id) |
| 102 | + assert preprint in qs |
| 103 | + assert other_preprint not in qs |
| 104 | + |
| 105 | + def test_preprint_with_no_doi_identifier_is_included(self, provider): |
| 106 | + pp = PreprintFactory(provider=provider, is_published=True, set_doi=False) |
| 107 | + qs = get_preprints_needing_v1_doi() |
| 108 | + assert pp in qs |
| 109 | + |
| 110 | + |
| 111 | +class TestResyncPreprintDoisV1: |
| 112 | + |
| 113 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 114 | + def test_dry_run_does_not_queue_tasks(self, mock_task, preprint): |
| 115 | + resync_preprint_dois_v1(dry_run=True) |
| 116 | + mock_task.apply_async.assert_not_called() |
| 117 | + |
| 118 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 119 | + def test_live_run_queues_task_for_each_preprint(self, mock_task, preprint): |
| 120 | + resync_preprint_dois_v1(dry_run=False, rate_limit=0) |
| 121 | + mock_task.apply_async.assert_called_once_with(kwargs={'preprint_id': preprint._id}) |
| 122 | + |
| 123 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 124 | + def test_batch_size_limits_processed_count(self, mock_task, provider): |
| 125 | + preprints = [] |
| 126 | + for _ in range(5): |
| 127 | + pp = PreprintFactory(provider=provider, is_published=True) |
| 128 | + old_doi = settings.DOI_FORMAT.format(prefix=provider.doi_prefix, guid=pp.get_guid()._id) |
| 129 | + pp.set_identifier_values(doi=old_doi, save=True) |
| 130 | + preprints.append(pp) |
| 131 | + |
| 132 | + resync_preprint_dois_v1(dry_run=False, batch_size=2, rate_limit=0) |
| 133 | + assert mock_task.apply_async.call_count == 2 |
| 134 | + |
| 135 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 136 | + def test_skips_provider_without_doi_prefix(self, mock_task, provider): |
| 137 | + no_prefix_provider = PreprintProviderFactory() |
| 138 | + no_prefix_provider.doi_prefix = '' |
| 139 | + no_prefix_provider.save() |
| 140 | + pp = PreprintFactory(provider=no_prefix_provider, is_published=True) |
| 141 | + old_doi = '10.000/old-doi' |
| 142 | + pp.set_identifier_values(doi=old_doi, save=True) |
| 143 | + |
| 144 | + resync_preprint_dois_v1(dry_run=False, rate_limit=0) |
| 145 | + queued_ids = [ |
| 146 | + call.kwargs['kwargs']['preprint_id'] |
| 147 | + for call in mock_task.apply_async.call_args_list |
| 148 | + ] |
| 149 | + assert pp._id not in queued_ids |
| 150 | + |
| 151 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 152 | + def test_provider_filter_is_applied(self, mock_task, preprint, provider): |
| 153 | + other_provider = PreprintProviderFactory() |
| 154 | + other_provider.doi_prefix = '10.99999' |
| 155 | + other_provider.save() |
| 156 | + other_pp = PreprintFactory(provider=other_provider, is_published=True) |
| 157 | + old_doi = settings.DOI_FORMAT.format(prefix=other_provider.doi_prefix, guid=other_pp.get_guid()._id) |
| 158 | + other_pp.set_identifier_values(doi=old_doi, save=True) |
| 159 | + |
| 160 | + resync_preprint_dois_v1(dry_run=False, rate_limit=0, provider_id=provider._id) |
| 161 | + |
| 162 | + queued_ids = [ |
| 163 | + call.kwargs['kwargs']['preprint_id'] |
| 164 | + for call in mock_task.apply_async.call_args_list |
| 165 | + ] |
| 166 | + assert preprint._id in queued_ids |
| 167 | + assert other_pp._id not in queued_ids |
| 168 | + |
| 169 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 170 | + def test_already_versioned_doi_is_not_queued(self, mock_task, preprint_with_v1_doi): |
| 171 | + resync_preprint_dois_v1(dry_run=False, rate_limit=0) |
| 172 | + queued_ids = [ |
| 173 | + call.kwargs['kwargs']['preprint_id'] |
| 174 | + for call in mock_task.apply_async.call_args_list |
| 175 | + ] |
| 176 | + assert preprint_with_v1_doi._id not in queued_ids |
| 177 | + |
| 178 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.time.sleep') |
| 179 | + @mock.patch('osf.management.commands.resync_preprint_dois_v1.async_request_identifier_update') |
| 180 | + def test_rate_limit_triggers_sleep(self, mock_task, mock_sleep, provider): |
| 181 | + for _ in range(3): |
| 182 | + pp = PreprintFactory(provider=provider, is_published=True) |
| 183 | + old_doi = settings.DOI_FORMAT.format(prefix=provider.doi_prefix, guid=pp.get_guid()._id) |
| 184 | + pp.set_identifier_values(doi=old_doi, save=True) |
| 185 | + |
| 186 | + resync_preprint_dois_v1(dry_run=False, rate_limit=2) |
| 187 | + mock_sleep.assert_called_once() |
0 commit comments