def setUp(self): super().setUp() self.enable_saml() self.provider = self.configure_saml_provider( name="Test", slug=self.slug, enabled=True, enable_sso_id_verification=True, ) self.user_social_auth1 = UserSocialAuthFactory( slug=self.slug, provider=self.provider.backend_name) self.user_social_auth1.save() self.user1 = self.user_social_auth1.user
def setUp(self): super(TestBackfillSSOVerificationsCommand, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments self.enable_saml() self.provider = self.configure_saml_provider( name="Test", slug=self.slug, enabled=True, enable_sso_id_verification=True, ) self.user_social_auth1 = UserSocialAuthFactory( slug=self.slug, provider=self.provider.backend_name) self.user_social_auth1.save() self.user1 = self.user_social_auth1.user
def test_multiple_social_auth_records(self): """ Test we only alter one UserSocialAuth record if a learner has two """ auth1 = UserSocialAuthFactory.create(slug=self.provider_slug) auth2 = UserSocialAuthFactory.create(slug=self.provider_slug, user=auth1.user) new_urn = '9001' email = auth1.user.email assert email == auth2.user.email self._call_command( self._format_single_email_uid_pair_json(email, new_urn)) auths = UserSocialAuth.objects.filter(user__email=email, uid=self._format_slug_urn_pair( self.provider_slug, new_urn)) assert auths.count() == 1
def test_single_mapping(self): new_urn = '9001' auth = UserSocialAuthFactory.create(slug=self.provider_slug) email = auth.user.email old_uid = auth.uid self._call_command(self._format_single_email_uid_pair_json(email, new_urn)) auth.refresh_from_db() assert auth.uid == self._format_slug_urn_pair(self.provider_slug, new_urn) assert not auth.uid == old_uid
def test_learner_duplicated_in_mapping(self, mock_log): auth = UserSocialAuthFactory() email = auth.user.email new_urn = '9001' mock_info = mock_log.info self._call_command('[{}]'.format(','.join( [self._format_email_uid_pair(email, new_urn) for _ in range(5)]))) mock_info.assert_any_call( u'Number of mappings in the mapping file where the ' u'identified user has already been processed: 4')
def test_learner_missed_by_mapping_file(self, mock_log): auth = UserSocialAuthFactory() # pylint disable required b/c this lint rule is confused about subfactories email = auth.user.email new_urn = '9001' mock_info = mock_log.info self._call_command( self._format_single_email_uid_pair_json('different' + email, new_urn)) mock_info.assert_any_call( u'Number of users with {slug} UserSocialAuth records ' u'for which there was no mapping in the provided file: 1'.format( slug=self.provider_slug))
def test_post_save_occurs(self): """ Test the signals downstream of this update are called with appropriate arguments """ auth = UserSocialAuthFactory.create(slug=self.provider_slug) new_urn = '9001' email = auth.user.email with patch('lms.djangoapps.program_enrollments.signals.matriculate_learner') as signal_handler_mock: self._call_command(self._format_single_email_uid_pair_json(email, new_urn)) assert signal_handler_mock.called # first positional arg matches the user whose auth was updated assert signal_handler_mock.call_args[0][0].id == auth.user.id # second positional arg matches the urn we changed assert signal_handler_mock.call_args[0][1] == self._format_slug_urn_pair(self.provider_slug, new_urn)
def test_several_learners(self, mock_log): auths = [UserSocialAuthFactory() for _ in range(5)] new_urn = '9001' mock_info = mock_log.info self._call_command('[{}]'.format(','.join([ self._format_email_uid_pair(auth.user.email, new_urn + str(ind)) for ind, auth in enumerate(auths) ]))) for ind, auth in enumerate(auths): auth.refresh_from_db() assert auth.uid == self._format_slug_urn_pair( self.provider_slug, new_urn + str(ind)) mock_info.assert_any_call( 'Number of mappings in the mapping file updated: 5')
class TestBackfillSSOVerificationsCommand(TestCase): """ Tests for management command for backfilling SSO verification records """ slug = 'test' def setUp(self): super().setUp() self.enable_saml() self.provider = self.configure_saml_provider( name="Test", slug=self.slug, enabled=True, enable_sso_id_verification=True, ) self.user_social_auth1 = UserSocialAuthFactory( slug=self.slug, provider=self.provider.backend_name) self.user_social_auth1.save() self.user1 = self.user_social_auth1.user def test_fails_without_required_param(self): with pytest.raises(CommandError): call_command('backfill_sso_verifications_for_old_account_links') def test_fails_without_named_provider_config(self): with pytest.raises(CommandError): call_command('backfill_sso_verifications_for_old_account_links', '--provider-slug', 'gatech') def test_sso_updated_single_user(self): assert SSOVerification.objects.count() == 0 call_command('backfill_sso_verifications_for_old_account_links', '--provider-slug', self.provider.provider_id) assert SSOVerification.objects.count() > 0 assert SSOVerification.objects.get().user.id == self.user1.id def test_performance(self): # TODO #self.assertNumQueries(1) call_command('backfill_sso_verifications_for_old_account_links', '--provider-slug', self.provider.provider_id) #self.assertNumQueries(100) def test_signal_called(self): with patch( 'openedx.core.djangoapps.signals.signals.LEARNER_NOW_VERIFIED.send_robust' ) as mock_signal: call_command('backfill_sso_verifications_for_old_account_links', '--provider-slug', self.provider.provider_id) # lint-amnesty, pylint: disable=line-too-long assert mock_signal.call_count == 1 def test_fine_with_multiple_verification_records(self): """ Testing there are no issues with excluding learners with multiple sso verifications """ SSOVerificationFactory( status='approved', user=self.user1, ) SSOVerificationFactory( status='approved', user=self.user1, ) assert SSOVerification.objects.count() == 2 call_command('backfill_sso_verifications_for_old_account_links', '--provider-slug', self.provider.provider_id) assert SSOVerification.objects.count() == 2