log all social auth creation failures (#21470)

logging on social auth creation failures
This commit is contained in:
Zachary Hancock
2019-08-28 11:39:24 -04:00
committed by GitHub
parent fb2595e783
commit b9a2646e4d
2 changed files with 46 additions and 8 deletions

View File

@@ -26,20 +26,34 @@ def _listen_for_lms_retire(sender, **kwargs): # pylint: disable=unused-argument
@receiver(post_save, sender=UserSocialAuth)
def matriculate_learner(sender, instance, created, **kwargs): # pylint: disable=unused-argument
def listen_for_social_auth_creation(sender, instance, created, **kwargs): # pylint: disable=unused-argument
"""
Post-save signal to update any waiting program enrollments with a user,
and enroll the user in any waiting course enrollments.
In most cases this will just short-circuit. Enrollments will only be updated
for a SAML provider with a linked organization.
Post-save signal that will attempt to link a social auth entry with waiting enrollments
"""
if not created:
return
try:
user = instance.user
provider_slug, external_user_key = instance.uid.split(':')
matriculate_learner(instance.user, instance.uid)
except Exception as e:
logger.warning(
u'Unable to link waiting enrollments for user %s, social auth creation failed: %s',
instance.user.id,
e,
)
raise
def matriculate_learner(user, uid):
"""
Update any waiting program enrollments with a user,
and enroll the user in any waiting course enrollments.
In most cases this will just short-circuit. Enrollments will only be updated
for a SAML provider with a linked organization.
"""
try:
provider_slug, external_user_key = uid.split(':')
authorizing_org = SAMLProviderConfig.objects.current_set().get(slug=provider_slug).organization
if not authorizing_org:

View File

@@ -358,3 +358,27 @@ class SocialAuthEnrollmentCompletionSignalTest(CacheIsolationTestCase):
error_tmpl.format(self.user.id, program_course_enrollments[0].id, 'something has gone wrong')
)
)
def test_log_on_unexpected_exception(self):
"""
unexpected errors as part of the account linking process should be logged and re-raised
"""
program_enrollment = self._create_waiting_program_enrollment()
self._create_waiting_course_enrollments(program_enrollment)
with mock.patch('lms.djangoapps.program_enrollments.models.ProgramCourseEnrollment.enroll') as enrollMock:
enrollMock.side_effect = Exception('unexpected error')
with LogCapture(logger.name) as log:
with self.assertRaisesRegex(Exception, 'unexpected error'):
UserSocialAuth.objects.create(
user=self.user,
uid='{0}:{1}'.format(self.provider_slug, self.external_id),
)
error_tmpl = u'Unable to link waiting enrollments for user {}, social auth creation failed: {}'
log.check_present(
(
logger.name,
'WARNING',
error_tmpl.format(self.user.id, 'unexpected error')
)
)