From bdc8653cca70478ba9bc712d664a50f73de85f8c Mon Sep 17 00:00:00 2001 From: Bianca Severino Date: Tue, 7 Sep 2021 14:21:27 -0400 Subject: [PATCH] fix: use pending name change in IDV post_save signal --- common/djangoapps/student/models_api.py | 11 ++++ lms/djangoapps/verify_student/signals.py | 8 ++- .../verify_student/tests/test_signals.py | 50 +++++++++++++++---- requirements/edx/base.txt | 2 +- requirements/edx/development.txt | 2 +- requirements/edx/testing.txt | 2 +- 6 files changed, 59 insertions(+), 16 deletions(-) diff --git a/common/djangoapps/student/models_api.py b/common/djangoapps/student/models_api.py index 4d34d6ed1d..44ab0ec6c0 100644 --- a/common/djangoapps/student/models_api.py +++ b/common/djangoapps/student/models_api.py @@ -109,6 +109,17 @@ def get_course_access_role(user, org, course_id, role): return course_access_role +def get_pending_name_change(user): + """ + Return a string representing the user's pending name change, or None if it does not exist. + """ + try: + pending_name_change = _PendingNameChange.objects.get(user=user) + return pending_name_change.new_name + except _PendingNameChange.DoesNotExist: + return None + + def do_name_change_request(user, new_name, rationale): """ Create a name change request. This either updates the user's current PendingNameChange, or creates diff --git a/lms/djangoapps/verify_student/signals.py b/lms/djangoapps/verify_student/signals.py index 022269e4b9..8e8c16a535 100644 --- a/lms/djangoapps/verify_student/signals.py +++ b/lms/djangoapps/verify_student/signals.py @@ -8,6 +8,7 @@ from django.core.exceptions import ObjectDoesNotExist from django.dispatch import Signal from django.dispatch.dispatcher import receiver +from common.djangoapps.student.models_api import get_name, get_pending_name_change from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_LMS_CRITICAL from xmodule.modulestore.django import SignalHandler, modulestore @@ -48,11 +49,14 @@ def send_idv_update(sender, instance, **kwargs): # pylint: disable=unused-argum as opposed to relying only on the post_save signal to avoid the chance that other apps import the SoftwareSecurePhotoVerification model. """ + # Prioritize pending name change over current profile name, if the user has one + full_name = get_pending_name_change(instance.user) or get_name(instance.user.id) + 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 + photo_id_name=instance.name, + full_name=full_name ) diff --git a/lms/djangoapps/verify_student/tests/test_signals.py b/lms/djangoapps/verify_student/tests/test_signals.py index 4621691ac4..31e5eaa085 100644 --- a/lms/djangoapps/verify_student/tests/test_signals.py +++ b/lms/djangoapps/verify_student/tests/test_signals.py @@ -8,6 +8,7 @@ from datetime import timedelta from django.utils.timezone import now from unittest.mock import patch +from common.djangoapps.student.models_api import do_name_change_request from common.djangoapps.student.tests.factories import UserFactory from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification, VerificationDeadline from lms.djangoapps.verify_student.signals import _listen_for_course_publish, _listen_for_lms_retire @@ -112,17 +113,23 @@ class PostSavePhotoVerificationTest(ModuleStoreTestCase): the verification attempt that was updated. """ + def setUp(self): + super().setUp() + self.user = UserFactory.create() + self.photo_id_name = 'Bob Doe' + self.face_image_url = 'https://test.face' + self.photo_id_image_url = 'https://test.photo' + self.photo_id_key = 'test+key' + @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' + user=self.user, + name=self.photo_id_name, + face_image_url=self.face_image_url, + photo_id_image_url=self.photo_id_image_url, + photo_id_key=self.photo_id_key ) self.assertTrue(mock_signal.called) mock_signal.assert_called_with( @@ -130,8 +137,8 @@ class PostSavePhotoVerificationTest(ModuleStoreTestCase): attempt_id=attempt.id, user_id=attempt.user.id, status=attempt.status, - full_name=attempt.name, - profile_name=attempt.user.profile.name + photo_id_name=attempt.name, + full_name=attempt.user.profile.name ) mock_signal.reset_mock() @@ -143,6 +150,27 @@ class PostSavePhotoVerificationTest(ModuleStoreTestCase): attempt_id=attempt.id, user_id=attempt.user.id, status=attempt.status, - full_name=attempt.name, - profile_name=attempt.user.profile.name + photo_id_name=attempt.name, + full_name=attempt.user.profile.name + ) + + @patch('lms.djangoapps.verify_student.signals.idv_update_signal.send') + def test_post_save_signal_pending_name(self, mock_signal): + pending_name_change = do_name_change_request(self.user, 'Pending Name', 'test')[0] + + attempt = SoftwareSecurePhotoVerification.objects.create( + user=self.user, + name=self.photo_id_name, + face_image_url=self.face_image_url, + photo_id_image_url=self.photo_id_image_url, + photo_id_key=self.photo_id_key + ) + + mock_signal.assert_called_with( + sender='idv_update', + attempt_id=attempt.id, + user_id=attempt.user.id, + status=attempt.status, + photo_id_name=attempt.name, + full_name=pending_name_change.new_name ) diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt index 16450a60c1..f0c8916eb3 100644 --- a/requirements/edx/base.txt +++ b/requirements/edx/base.txt @@ -455,7 +455,7 @@ edx-i18n-tools==0.7.0 # via ora2 edx-milestones==0.3.2 # via -r requirements/edx/base.in -edx-name-affirmation==0.9.1 +edx-name-affirmation==0.9.2 # via -r requirements/edx/base.in edx-opaque-keys[django]==2.2.2 # via diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index a4a9e52b93..d95f867234 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -559,7 +559,7 @@ edx-lint==5.1.0 # via -r requirements/edx/testing.txt edx-milestones==0.3.2 # via -r requirements/edx/testing.txt -edx-name-affirmation==0.9.1 +edx-name-affirmation==0.9.2 # via -r requirements/edx/testing.txt edx-opaque-keys[django]==2.2.2 # via diff --git a/requirements/edx/testing.txt b/requirements/edx/testing.txt index a4625a87be..cda576dc8e 100644 --- a/requirements/edx/testing.txt +++ b/requirements/edx/testing.txt @@ -541,7 +541,7 @@ edx-lint==5.1.0 # via -r requirements/edx/testing.in edx-milestones==0.3.2 # via -r requirements/edx/base.txt -edx-name-affirmation==0.9.1 +edx-name-affirmation==0.9.2 # via -r requirements/edx/base.txt edx-opaque-keys[django]==2.2.2 # via