From fc0eb7191834dbab6d0743c5d9665a33dcaacf96 Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Mon, 21 Sep 2020 16:57:32 +0300 Subject: [PATCH] Add USER_ACCOUNT_ACTIVATED signal (#23296) Plugins can listen to USER_ACCOUNT_ACTIVATED signal to perform custom logic. --- common/djangoapps/student/models.py | 2 ++ .../djangoapps/student/tests/test_activate_account.py | 11 +++++++++++ openedx/core/djangoapps/signals/signals.py | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 9c65b492d8..0938b181b9 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -70,6 +70,7 @@ from openedx.core.djangoapps.enrollments.api import ( get_enrollment_attributes, set_enrollment_attributes ) +from openedx.core.djangoapps.signals.signals import USER_ACCOUNT_ACTIVATED from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.xmodule_django.models import NoneToEmptyManager from openedx.core.djangolib.model_mixins import DeletableByUserValue @@ -839,6 +840,7 @@ class Registration(models.Model): def activate(self): self.user.is_active = True self.user.save(update_fields=['is_active']) + USER_ACCOUNT_ACTIVATED.send_robust(self.__class__, user=self.user) log.info(u'User %s (%s) account is successfully activated.', self.user.username, self.user.email) diff --git a/common/djangoapps/student/tests/test_activate_account.py b/common/djangoapps/student/tests/test_activate_account.py index cea5198d8c..ccd8c0be64 100644 --- a/common/djangoapps/student/tests/test_activate_account.py +++ b/common/djangoapps/student/tests/test_activate_account.py @@ -70,6 +70,17 @@ class TestActivateAccount(TestCase): self.assertTrue(self.user.is_active) self.assertFalse(mock_segment_identify.called) + @patch('student.models.USER_ACCOUNT_ACTIVATED') + def test_activation_signal(self, mock_signal): + """ + Verify that USER_ACCOUNT_ACTIVATED is emitted upon account email activation. + """ + assert not self.user.is_active, 'Ensure that the user starts inactive' + assert not mock_signal.send_robust.call_count, 'Ensure no signal is fired before activation' + self.registration.activate() # Until you explicitly activate it + assert self.user.is_active, 'Sanity check for .activate()' + mock_signal.send_robust.assert_called_once_with(Registration, user=self.user) # Ensure the signal is emitted + def test_account_activation_message(self): """ Verify that account correct activation message is displayed. diff --git a/openedx/core/djangoapps/signals/signals.py b/openedx/core/djangoapps/signals/signals.py index 5bab209a54..b0fd6cafa0 100644 --- a/openedx/core/djangoapps/signals/signals.py +++ b/openedx/core/djangoapps/signals/signals.py @@ -33,5 +33,7 @@ COURSE_GRADE_NOW_FAILED = Signal( ] ) -# Signal that indicates that a user has become verified +# Signal that indicates that a user has become verified for certificate purposes LEARNER_NOW_VERIFIED = Signal(providing_args=['user']) + +USER_ACCOUNT_ACTIVATED = Signal(providing_args=["user"]) # Signal indicating email verification