From c38da4e105d5ef1c359bdb1e2de8e40c22032c67 Mon Sep 17 00:00:00 2001 From: Ihor Sokhan Date: Fri, 11 Apr 2025 17:44:20 +0300 Subject: [PATCH 1/2] additional tests for this task --- admin_tests/preprints/test_views.py | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/admin_tests/preprints/test_views.py b/admin_tests/preprints/test_views.py index 1fb9d68482d..39a40cb8d07 100644 --- a/admin_tests/preprints/test_views.py +++ b/admin_tests/preprints/test_views.py @@ -22,6 +22,8 @@ from osf.models.spam import SpamStatus from osf.utils.workflows import DefaultStates, RequestTypes from osf.utils.permissions import ADMIN +from api.providers.workflows import Workflows +from framework.postcommit_tasks.handlers import get_task_from_postcommit_queue from admin_tests.utilities import setup_view, setup_log_view, handle_post_view_request @@ -797,3 +799,69 @@ def test_admin_user_can_publish_preprint(self, user, preprint, plain_view): preprint.reload() assert preprint.is_published + + @mock.patch('website.preprints.tasks.update_or_create_preprint_identifiers') + def test_doi_request_is_called_for_post_moderation(self, create_identifier_mock, user, preprint, plain_view): + unpublished_post_moderation_preprint = PreprintFactory( + is_published=False, + reviews_workflow=Workflows.POST_MODERATION.value + ) + admin_group = Group.objects.get(name='osf_admin') + + request = RequestFactory().post(reverse('preprints:make-published', kwargs={'guid': unpublished_post_moderation_preprint._id})) + request.user = user + + admin_group.permissions.add(Permission.objects.get(codename='change_node')) + user.groups.add(admin_group) + + plain_view.as_view()(request, guid=unpublished_post_moderation_preprint._id) + + assert create_identifier_mock.assert_called_with(unpublished_post_moderation_preprint) is None + + @mock.patch('website.preprints.tasks.update_or_create_preprint_identifiers') + def test_doi_request_is_not_called_for_pre_moderation(self, create_identifier_mock, user, preprint, plain_view): + unpublished_pre_moderation_preprint = PreprintFactory( + is_published=False, + reviews_workflow=Workflows.PRE_MODERATION.value + ) + admin_group = Group.objects.get(name='osf_admin') + + request = RequestFactory().post(reverse('preprints:make-published', kwargs={'guid': unpublished_pre_moderation_preprint._id})) + request.user = user + + admin_group.permissions.add(Permission.objects.get(codename='change_node')) + user.groups.add(admin_group) + + plain_view.as_view()(request, guid=unpublished_pre_moderation_preprint._id) + + assert not create_identifier_mock.called + + def test_share_update_is_called_when_make_preprint_published(self, user, preprint, plain_view): + unpublished_post_moderation_preprint = PreprintFactory( + is_published=False, + reviews_workflow=Workflows.POST_MODERATION.value + ) + + assert not unpublished_post_moderation_preprint.is_published + + admin_group = Group.objects.get(name='osf_admin') + + request = RequestFactory().post(reverse('preprints:make-published', kwargs={'guid': unpublished_post_moderation_preprint._id})) + request.user = user + + admin_group.permissions.add(Permission.objects.get(codename='change_node')) + user.groups.add(admin_group) + + plain_view.as_view()(request, guid=unpublished_post_moderation_preprint._id) + + unpublished_post_moderation_preprint.reload() + assert unpublished_post_moderation_preprint.is_published + + updated_task = get_task_from_postcommit_queue( + 'website.preprints.tasks.on_preprint_updated', + predicate=lambda task: task.kwargs['preprint_id'] == unpublished_post_moderation_preprint._id + ) + + assert updated_task + # ensure this field is passed to celery task so that update_share is called for it + assert 'is_published' in updated_task.kwargs['saved_fields'] From a6150e69a2f1187221a1071af6d8e08de7d6b79a Mon Sep 17 00:00:00 2001 From: Ihor Sokhan Date: Mon, 14 Apr 2025 14:41:43 +0300 Subject: [PATCH 2/2] tests --- admin_tests/preprints/test_views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/admin_tests/preprints/test_views.py b/admin_tests/preprints/test_views.py index 39a40cb8d07..de7fdcb3ddb 100644 --- a/admin_tests/preprints/test_views.py +++ b/admin_tests/preprints/test_views.py @@ -834,9 +834,11 @@ def test_doi_request_is_not_called_for_pre_moderation(self, create_identifier_mo plain_view.as_view()(request, guid=unpublished_pre_moderation_preprint._id) + assert unpublished_pre_moderation_preprint.provider and unpublished_pre_moderation_preprint.provider.reviews_workflow == Workflows.PRE_MODERATION.value assert not create_identifier_mock.called - def test_share_update_is_called_when_make_preprint_published(self, user, preprint, plain_view): + @mock.patch('website.preprints.tasks.update_or_enqueue_on_preprint_updated') + def test_share_update_is_called_when_make_preprint_published(self, on_preprint_updated_mock, user, preprint, plain_view): unpublished_post_moderation_preprint = PreprintFactory( is_published=False, reviews_workflow=Workflows.POST_MODERATION.value