def email_marketing_register_user(sender, user, registration, **kwargs): # pylint: disable=unused-argument """ Called after user created and saved Args: sender: Not used user: The user object for the user being changed registration: The user registration profile to activate user account kwargs: Not used """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # ignore anonymous users if user.is_anonymous(): return # perform update asynchronously update_user.delay(_create_sailthru_user_vars(user, user.profile, registration=registration), user.email, site=_get_current_site(), new_user=True)
def test_update_user_error_nonretryable(self, mock_sailthru, mock_log_error, mock_retry): """ Ensure that non-retryable error is not retried """ mock_sailthru.return_value = SailthruResponse(JsonResponse({'error': 1, 'errormsg': 'Got an error'})) update_user.delay({}, self.user.email) self.assertTrue(mock_log_error.called) self.assertFalse(mock_retry.called)
def test_add_user_list_not_called_on_white_label_domain(self, mock_sailthru_post): """ test user is not added to Sailthru user lists if registered from a whitel labe site """ existing_site = Site.objects.create(domain='testwhitelabel.com', name='White Label') site_dict = {'id': existing_site.id, 'domain': existing_site.domain, 'name': existing_site.name} update_user.delay( {'gender': 'm', 'username': '******', 'activated': 1}, TEST_EMAIL, site=site_dict, new_user=True ) self.assertFalse(mock_sailthru_post.called)
def email_marketing_user_field_changed(sender, user=None, table=None, setting=None, old_value=None, new_value=None, **kwargs): # pylint: disable=unused-argument """ Update a single user/profile field Args: sender: Not used user: The user object for the user being changed table: The name of the table being updated setting: The name of the setting being updated old_value: Prior value new_value: New value kwargs: Not used """ # ignore anonymous users if user.is_anonymous(): return # ignore anything but User, Profile or UserPreference tables if table not in { 'auth_user', 'auth_userprofile', 'user_api_userpreference' }: return # ignore anything not in list of fields to handle if setting in CHANGED_FIELDNAMES: # skip if not enabled # the check has to be here rather than at the start of the method to avoid # accessing the config during migration 0001_date__add_ecommerce_service_user email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # set the activation flag when the user is marked as activated update_user.delay(_create_sailthru_user_vars(user, user.profile), user.email, site=_get_current_site(), new_user=False, activation=(setting == 'is_active') and new_value is True) elif setting == 'email': # email update is special case email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return update_user_email.delay(user.email, old_value)
def test_email_not_sent_to_enterprise_learners(self, mock_sailthru_post): """ tests that welcome email is not sent to the enterprise learner """ mock_sailthru_post.return_value = SailthruResponse( JsonResponse({'ok': True})) update_user.delay(sailthru_vars={ 'is_enterprise_learner': True, 'enterprise_name': 'test name', }, email=self.user.email) self.assertNotEqual(mock_sailthru_post.call_args[0][0], "send")
def update_email_marketing_user_with_enterprise_vars(sender, instance, **kwargs): # pylint: disable=unused-argument, invalid-name """ Update the SailThru user with enterprise-related vars. """ user = User.objects.get(id=instance.user_id) # perform update asynchronously update_user.delay(sailthru_vars={ 'is_enterprise_learner': True, 'enterprise_name': instance.enterprise_customer.name, }, email=user.email)
def test_update_user_error_retryable(self, mock_sailthru, mock_log_error, mock_retry): """ Ensure that retryable error is retried """ mock_sailthru.return_value = SailthruResponse( JsonResponse({ 'error': 43, 'errormsg': 'Got an error' })) update_user.delay({}, self.user.email) assert mock_log_error.called assert mock_retry.called
def test_update_user_error_logging(self, mock_sailthru, mock_log_error): """ Ensure that error returned from Sailthru api is logged """ mock_sailthru.return_value = SailthruResponse(JsonResponse({'error': 100, 'errormsg': 'Got an error'})) update_user.delay({}, self.user.email) self.assertTrue(mock_log_error.called) # force Sailthru API exception mock_log_error.reset_mock() mock_sailthru.side_effect = SailthruClientError update_user.delay({}, self.user.email) self.assertTrue(mock_log_error.called) # force Sailthru API exception on 2nd call mock_log_error.reset_mock() mock_sailthru.side_effect = [SailthruResponse(JsonResponse({'ok': True})), SailthruClientError] update_user.delay({}, self.user.email, activation=True) self.assertTrue(mock_log_error.called) # force Sailthru API error return on 2nd call mock_log_error.reset_mock() mock_sailthru.side_effect = [SailthruResponse(JsonResponse({'ok': True})), SailthruResponse(JsonResponse({'error': 100, 'errormsg': 'Got an error'}))] update_user.delay({}, self.user.email, activation=True) self.assertTrue(mock_log_error.called)
def test_just_return_tasks(self, mock_sailthru, mock_log_error): """ Ensure that disabling Sailthru just returns """ update_email_marketing_config(enabled=False) update_user.delay(self.user.username, self.user.email) self.assertFalse(mock_log_error.called) self.assertFalse(mock_sailthru.called) update_user_email.delay(self.user.username, "*****@*****.**") self.assertFalse(mock_log_error.called) self.assertFalse(mock_sailthru.called) update_email_marketing_config(enabled=True)
def email_marketing_user_field_changed(sender, user=None, table=None, setting=None, old_value=None, new_value=None, **kwargs): # pylint: disable=unused-argument """ Update a single user/profile field Args: sender: Not used user: The user object for the user being changed table: The name of the table being updated setting: The name of the setting being updated old_value: Prior value new_value: New value kwargs: Not used """ # ignore anonymous users if user.is_anonymous(): return # ignore anything but User or Profile table if table != 'auth_user' and table != 'auth_userprofile': return # ignore anything not in list of fields to handle if setting in CHANGED_FIELDNAMES: # skip if not enabled # the check has to be here rather than at the start of the method to avoid # accessing the config during migration 0001_date__add_ecommerce_service_user email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # perform update asynchronously, flag if activation update_user.delay(user.username, new_user=False, activation=(setting == 'is_active') and new_value is True) elif setting == 'email': # email update is special case email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return update_user_email.delay(user.username, old_value)
def email_marketing_user_field_changed(sender, user=None, table=None, setting=None, old_value=None, new_value=None, **kwargs): # pylint: disable=unused-argument """ Update a single user/profile field Args: sender: Not used user: The user object for the user being changed table: The name of the table being updated setting: The name of the setting being updated old_value: Prior value new_value: New value kwargs: Not used """ # ignore anonymous users if user.is_anonymous(): return # ignore anything but User or Profile table if table != 'auth_user' and table != 'auth_userprofile': return # ignore anything not in list of fields to handle if setting in CHANGED_FIELDNAMES: # skip if not enabled # the check has to be here rather than at the start of the method to avoid # accessing the config during migration 0001_date__add_ecommerce_service_user email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # perform update asynchronously, flag if activation update_user.delay(_create_sailthru_user_vars(user, user.profile), user.email, site=_get_current_site(), new_user=False, activation=(setting == 'is_active') and new_value is True) elif setting == 'email': # email update is special case email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return update_user_email.delay(user.email, old_value)
def test_add_user(self, mock_sailthru_get, mock_sailthru_post, mock_log_error): """ test async method in tasks that actually updates Sailthru """ site_dict = {'id': self.site.id, 'domain': self.site.domain, 'name': self.site.name} mock_sailthru_post.return_value = SailthruResponse(JsonResponse({'ok': True})) mock_sailthru_get.return_value = SailthruResponse(JsonResponse({'lists': [{'name': 'new list'}], 'ok': True})) update_user.delay( {'gender': 'm', 'username': '******', 'activated': 1}, TEST_EMAIL, site_dict, new_user=True ) self.assertFalse(mock_log_error.called) self.assertEqual(mock_sailthru_post.call_args[0][0], "user") userparms = mock_sailthru_post.call_args[0][1] self.assertEqual(userparms['key'], "email") self.assertEqual(userparms['id'], TEST_EMAIL) self.assertEqual(userparms['vars']['gender'], "m") self.assertEqual(userparms['vars']['username'], "test") self.assertEqual(userparms['vars']['activated'], 1) self.assertEqual(userparms['lists']['new list'], 1)
def email_marketing_register_user(sender, user=None, profile=None, **kwargs): # pylint: disable=unused-argument """ Called after user created and saved Args: sender: Not used user: The user object for the user being changed profile: The user profile for the user being changed kwargs: Not used """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # ignore anonymous users if user.is_anonymous(): return # perform update asynchronously update_user.delay(user.username, new_user=True)
def email_marketing_register_user(sender, user=None, profile=None, **kwargs): # pylint: disable=unused-argument """ Called after user created and saved Args: sender: Not used user: The user object for the user being changed profile: The user profile for the user being changed kwargs: Not used """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # ignore anonymous users if user.is_anonymous(): return # perform update asynchronously update_user.delay(_create_sailthru_user_vars(user, user.profile), user.email, new_user=True)
def email_marketing_register_user(sender, user, registration, **kwargs): # pylint: disable=unused-argument """ Called after user created and saved Args: sender: Not used user: The user object for the user being changed registration: The user registration profile to activate user account kwargs: Not used """ email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # ignore anonymous users if user.is_anonymous: return # perform update asynchronously update_user.delay(_create_sailthru_user_vars(user, user.profile, registration=registration), user.email, site=_get_current_site(), new_user=True)
def test_add_user(self, mock_sailthru_get, mock_sailthru_post, mock_log_error): """ test async method in tasks that actually updates Sailthru """ site_dict = { 'id': self.site.id, 'domain': self.site.domain, 'name': self.site.name } mock_sailthru_post.return_value = SailthruResponse( JsonResponse({'ok': True})) mock_sailthru_get.return_value = SailthruResponse( JsonResponse({ 'lists': [{ 'name': 'new list' }], 'ok': True })) update_user.delay({ 'gender': 'm', 'username': '******', 'activated': 1 }, TEST_EMAIL, site_dict, new_user=True) assert not mock_log_error.called assert mock_sailthru_post.call_args[0][0] == 'user' userparms = mock_sailthru_post.call_args[0][1] assert userparms['key'] == 'email' assert userparms['id'] == TEST_EMAIL assert userparms['vars']['gender'] == 'm' assert userparms['vars']['username'] == 'test' assert userparms['vars']['activated'] == 1 assert userparms['lists']['new list'] == 1
def email_marketing_user_field_changed(sender, user=None, table=None, setting=None, old_value=None, new_value=None, **kwargs): # pylint: disable=unused-argument """ Update a single user/profile field Args: sender: Not used user: The user object for the user being changed table: The name of the table being updated setting: The name of the setting being updated old_value: Prior value new_value: New value kwargs: Not used """ # ignore anonymous users if user.is_anonymous: return # ignore anything but User, Profile or UserPreference tables if table not in { 'auth_user', 'auth_userprofile', 'user_api_userpreference' }: return # ignore anything not in list of fields to handle if setting in CHANGED_FIELDNAMES: # skip if not enabled # the check has to be here rather than at the start of the method to avoid # accessing the config during migration 0001_date__add_ecommerce_service_user email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # Is the status of the user account changing to active? is_activation = (setting == 'is_active') and new_value is True # Is this change in the context of an SSO-initiated registration? third_party_provider = None if third_party_auth.is_enabled(): running_pipeline = third_party_auth.pipeline.get( crum.get_current_request()) if running_pipeline: third_party_provider = third_party_auth.provider.Registry.get_from_pipeline( running_pipeline) # Send a welcome email if the user account is being activated # and we are not in a SSO registration flow whose associated # identity provider is configured to allow for the sending # of a welcome email. send_welcome_email = is_activation and ( third_party_provider is None or third_party_provider.send_welcome_email) # set the activation flag when the user is marked as activated update_user.delay(_create_sailthru_user_vars(user, user.profile), user.email, site=_get_current_site(), new_user=False, activation=send_welcome_email) elif setting == 'email': # email update is special case email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return update_user_email.delay(user.email, old_value)
def email_marketing_user_field_changed(sender, user=None, table=None, setting=None, old_value=None, new_value=None, **kwargs): # pylint: disable=unused-argument """ Update a single user/profile field Args: sender: Not used user: The user object for the user being changed table: The name of the table being updated setting: The name of the setting being updated old_value: Prior value new_value: New value kwargs: Not used """ # ignore anonymous users if user.is_anonymous: return # ignore anything but User, Profile or UserPreference tables if table not in {'auth_user', 'auth_userprofile', 'user_api_userpreference'}: return # ignore anything not in list of fields to handle if setting in CHANGED_FIELDNAMES: # skip if not enabled # the check has to be here rather than at the start of the method to avoid # accessing the config during migration 0001_date__add_ecommerce_service_user email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return # Is the status of the user account changing to active? is_activation = (setting == 'is_active') and new_value is True # Is this change in the context of an SSO-initiated registration? third_party_provider = None if third_party_auth.is_enabled(): running_pipeline = third_party_auth.pipeline.get(crum.get_current_request()) if running_pipeline: third_party_provider = third_party_auth.provider.Registry.get_from_pipeline(running_pipeline) # Send a welcome email if the user account is being activated # and we are not in a SSO registration flow whose associated # identity provider is configured to allow for the sending # of a welcome email. send_welcome_email = is_activation and ( third_party_provider is None or third_party_provider.send_welcome_email ) # set the activation flag when the user is marked as activated update_user.delay(_create_sailthru_user_vars(user, user.profile), user.email, site=_get_current_site(), new_user=False, activation=send_welcome_email) elif setting == 'email': # email update is special case email_config = EmailMarketingConfiguration.current() if not email_config.enabled: return update_user_email.delay(user.email, old_value)