Skip to content

Commit

Permalink
feat: add signal emitters for IDV (#28511)
Browse files Browse the repository at this point in the history
MST-805. A signal should be emitted upon an IDV attempt being submitted or reviewed for consumption by other applications.
  • Loading branch information
alangsto authored Aug 26, 2021
1 parent 0b4aaa9 commit b6cb629
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lms/djangoapps/verify_student/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""


from django.db.models.signals import post_save
from django.core.exceptions import ObjectDoesNotExist
from django.dispatch import Signal
from django.dispatch.dispatcher import receiver

from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_LMS_CRITICAL
Expand All @@ -12,6 +14,10 @@
from .models import SoftwareSecurePhotoVerification, VerificationDeadline


# Signal for emitting IDV submission and review updates
idv_update_signal = Signal(providing_args=["attempt_id", "user_id", "status", "full_name", "profile_name"])


@receiver(SignalHandler.course_published)
def _listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable=unused-argument
"""
Expand All @@ -32,3 +38,21 @@ def _listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable
def _listen_for_lms_retire(sender, **kwargs): # pylint: disable=unused-argument
user = kwargs.get('user')
SoftwareSecurePhotoVerification.retire_user(user.id)


@receiver(post_save, sender=SoftwareSecurePhotoVerification)
def send_idv_update(sender, instance, **kwargs): # pylint: disable=unused-argument
"""
Catches the post save signal from the SoftwareSecurePhotoVerification model, and emits
another signal with limited information from the model. We are choosing to re-emit a signal
as opposed to relying only on the post_save signal to avoid the chance that other apps
import the SoftwareSecurePhotoVerification model.
"""
idv_update_signal.send(
sender='idv_update',
attempt_id=instance.id,
user_id=instance.user.id,
status=instance.status,
full_name=instance.name,
profile_name=instance.user.profile.name
)
44 changes: 44 additions & 0 deletions lms/djangoapps/verify_student/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import timedelta

from django.utils.timezone import now
from unittest.mock import patch

from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification, VerificationDeadline
Expand Down Expand Up @@ -102,3 +103,46 @@ def test_idempotent(self):
# All values for this user should now be empty string
for field in ('name', 'face_image_url', 'photo_id_image_url', 'photo_id_key'):
assert '' == getattr(ver_obj, field)


class PostSavePhotoVerificationTest(ModuleStoreTestCase):
"""
Tests for the post_save signal on the SoftwareSecurePhotoVerification model.
This receiver should emit another signal that contains limited data about
the verification attempt that was updated.
"""

@patch('lms.djangoapps.verify_student.signals.idv_update_signal.send')
def test_post_save_signal(self, mock_signal):
user = UserFactory.create()

# create new softwaresecureverification
attempt = SoftwareSecurePhotoVerification.objects.create(
user=user,
name='Bob Doe',
face_image_url='https://test.face',
photo_id_image_url='https://test.photo',
photo_id_key='test+key'
)
self.assertTrue(mock_signal.called)
mock_signal.assert_called_with(
sender='idv_update',
attempt_id=attempt.id,
user_id=attempt.user.id,
status=attempt.status,
full_name=attempt.name,
profile_name=attempt.user.profile.name
)
mock_signal.reset_mock()

attempt.mark_ready()

self.assertTrue(mock_signal.called)
mock_signal.assert_called_with(
sender='idv_update',
attempt_id=attempt.id,
user_id=attempt.user.id,
status=attempt.status,
full_name=attempt.name,
profile_name=attempt.user.profile.name
)

0 comments on commit b6cb629

Please sign in to comment.