From 51e4ea7fceaf14d5880b3d11a928c7cdbf8d302d Mon Sep 17 00:00:00 2001 From: mkovalua Date: Fri, 31 Jul 2026 14:53:57 +0300 Subject: [PATCH] Fix wrong moderation_state after terminate_embargo() --- osf/models/registrations.py | 3 +++ osf_tests/test_sanctions.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/osf/models/registrations.py b/osf/models/registrations.py index 74cd7bf5f8e..e9e74cd1db7 100644 --- a/osf/models/registrations.py +++ b/osf/models/registrations.py @@ -605,6 +605,9 @@ def terminate_embargo(self, forced=False): self.refresh_from_db() if self.is_pending_embargo_termination: self.embargo_termination_approval.accept() + # accept() above updates moderation_state through a different in-memory + # Registration; refresh again or set_privacy() below will overwrite it + self.refresh_from_db() for node in self.node_and_primary_descendants(): node.set_privacy( diff --git a/osf_tests/test_sanctions.py b/osf_tests/test_sanctions.py index 863b67de4ef..02e6548958b 100644 --- a/osf_tests/test_sanctions.py +++ b/osf_tests/test_sanctions.py @@ -6,7 +6,7 @@ from django.utils import timezone from transitions import MachineError -from osf.models import NodeLog +from osf.models import NodeLog, Registration from osf.exceptions import NodeStateError from osf_tests import factories from osf_tests.utils import mock_archive @@ -74,6 +74,16 @@ def test_terminate_embargo_log_is_nouser(self, node, user, registration): assert last_log.action == NodeLog.EMBARGO_TERMINATED assert last_log.user is None + def test_terminate_embargo_on_fresh_object_with_pending_termination_sets_correct_state(self, registration, user): + with capture_notifications(): + registration.request_embargo_termination(user) + assert registration.is_pending_embargo_termination is True + fresh_registration = Registration.objects.get(pk=registration.pk) + fresh_registration.terminate_embargo() + assert fresh_registration.is_public is True + assert fresh_registration.is_embargoed is False + assert fresh_registration.moderation_state == 'accepted' + @pytest.mark.django_db class TestRegistrationEmbargoTermination: