From 7898ed3a83daef960b5d54c33cb3f3c88b51d4f0 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Thu, 14 Sep 2023 13:45:33 -0700 Subject: [PATCH] Add test to ensure that initialization started signals are fired. Manually fire initialization started signal. --- morango/api/viewsets.py | 4 ++++ morango/sync/utils.py | 8 ++++++++ tests/testapp/tests/test_api.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/morango/api/viewsets.py b/morango/api/viewsets.py index 2435e6c..433b288 100644 --- a/morango/api/viewsets.py +++ b/morango/api/viewsets.py @@ -383,6 +383,10 @@ def create(self, request): # noqa: C901 is_push=is_a_push, ) + # Manually fire the initializing started signal, as the default context + # stage is initializing, so otherwise this never gets fired. + session_controller.signals.initializing.started.fire(context=context) + # If both client and ourselves allow async, we just return accepted status, and the client # should PATCH the transfer_session to the appropriate stage. If not async, we wait until # queuing is complete diff --git a/morango/sync/utils.py b/morango/sync/utils.py index a025b55..8908b94 100644 --- a/morango/sync/utils.py +++ b/morango/sync/utils.py @@ -181,6 +181,14 @@ def connect(self, handler): """ self._handlers.append(handler) + def disconnect(self, handler): + """ + Removes a callable handler that would be called when the signal is fired. + + :type handler: function + """ + self._handlers.remove(handler) + def fire(self, **kwargs): """ Fires the handler functions connected via `connect`. diff --git a/tests/testapp/tests/test_api.py b/tests/testapp/tests/test_api.py index 01d7399..b8c2ef3 100644 --- a/tests/testapp/tests/test_api.py +++ b/tests/testapp/tests/test_api.py @@ -10,12 +10,14 @@ from django.utils import timezone from django.utils.functional import SimpleLazyObject from facility_profile.models import MyUser +from mock import Mock from rest_framework.test import APITestCase as BaseTestCase from .compat import EnvironmentVarGuard from morango.api.serializers import BufferSerializer from morango.api.serializers import CertificateSerializer from morango.api.serializers import InstanceIDSerializer +from morango.api.viewsets import session_controller from morango.constants import transfer_stages from morango.constants import transfer_statuses from morango.models.certificates import Certificate @@ -790,6 +792,19 @@ def test_transfersession_creation_fails_for_nonexistent_syncsession(self): syncsession=syncsession, ) + def test_transfersession_creation_calls_initializing_started_handler(self): + mock = Mock() + session_controller.signals.initializing.started.connect(mock) + try: + + self.make_transfersession_creation_request( + filter=str(self.sub_subset_cert1_with_key.get_scope().write_filter), + push=True, + ) + self.assertTrue(mock.called) + finally: + session_controller.signals.initializing.started.disconnect(mock) + def test_transfersession_can_be_deleted(self): self.test_transfersession_can_be_created()