def test_multiple_notification_preference(self): """ Test to check for the notification preferences list values """ notification_preference_daily = NotificationPreference( name='daily-digest-emails', display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') notification_preference_weekly = NotificationPreference( name='weekly-digest-emails', display_name='Weekly Emails', display_description='Weekly Digestion Email ', default_value='false') notification_preference1 = set_notification_preference( notification_preference_daily) notification_preference2 = set_notification_preference( notification_preference_weekly) response = self.client.get( reverse('edx_notifications.consumer.notification_preferences')) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 2) self._compare_notification_preference_to_result( notification_preference1, results[0]) self._compare_notification_preference_to_result( notification_preference2, results[1])
def create_default_notification_preferences(): """ This function installs two default system-defined Notification Preferences for daily and weekly Digests. """ store_provider = SQLNotificationStoreProvider() daily_digest_preference = NotificationPreference( name=const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, display_name=_('Receive daily email'), display_description=_('This setting will cause a daily digest of all notifications to be sent to your' ' registered email address'), default_value=const.NOTIFICATIONS_PREFERENCE_DAILYDIGEST_DEFAULT ) store_provider.save_notification_preference(daily_digest_preference) weekly_digest_preference = NotificationPreference( name=const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, display_name=_('Receive weekly email'), display_description=_('This setting will cause a weekly digest of all notifications to be sent to your' ' registered email address'), default_value=const.NOTIFICATIONS_PREFERENCE_WEEKLYDIGEST_DEFAULT ) store_provider.save_notification_preference(weekly_digest_preference)
def test_single_digest(self): """ Make sure when we select one digest, the other preference is de-selected """ notification_preference_daily = NotificationPreference( name=const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') set_notification_preference(notification_preference_daily) notification_preference_weekly = NotificationPreference( name=const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') set_notification_preference(notification_preference_weekly) response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=[const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME]), data={'value': 'true'}) self.assertEqual(response.status_code, 200) response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=[const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME]), data={'value': 'false'}) self.assertEqual(response.status_code, 200) # now set the weekly and make sure the daily gets turned off response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=[const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME]), data={'value': 'true'}) self.assertEqual(response.status_code, 200) response = self.client.get( reverse('edx_notifications.consumer.user_preferences.detail', args=[const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME])) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 1) self.assertEqual(results[0]['value'], 'false') response = self.client.get( reverse('edx_notifications.consumer.user_preferences.detail', args=[const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME])) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 1) self.assertEqual(results[0]['value'], 'true')
def test_get_specific_user_preferences(self): """ Test specific preference setting for the user """ # test bad preference name send 400 error response response = self.client.get( reverse('edx_notifications.consumer.user_preferences.detail', args=['bad-name'])) self.assertEqual(response.status_code, 404) notification_preference_daily = NotificationPreference( name='daily-digest-emails', display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') notification_preference = set_notification_preference( notification_preference_daily) user_preference = set_user_notification_preference( 1, notification_preference.name, 'Test User 1') # hit the api with the valid preference name response = self.client.get( reverse('edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails'])) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 1) self.assertEqual(user_preference.user_id, results[0]['user_id']) self.assertEqual(user_preference.value, results[0]['value']) self._compare_notification_preference_to_result( user_preference.preference, results[0]['preference'])
def test_user_preferences_list(self): """ Test User Preferences List """ notification_preference_daily = NotificationPreference( name='daily-digest-emails', display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') notification_preference = set_notification_preference( notification_preference_daily) user_preference = set_user_notification_preference( 1, notification_preference.name, 'Test User 1') response = self.client.get( reverse('edx_notifications.consumer.user_preferences')) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 1) self.assertEqual(user_preference.user_id, results[0]['user_id']) self.assertEqual(user_preference.value, results[0]['value']) self._compare_notification_preference_to_result( user_preference.preference, results[0]['preference'])
def to_data_object(self, options=None): # pylint: disable=unused-argument """ Generate a NotificationPreference data object """ return NotificationPreference( name=self.name, display_name=self.display_name, display_description=self.display_description, default_value=self.default_value)
def _save_notification_preference(self, name, display_name, display_description=''): """ Helper method to create a new notification_preference """ test_notification_preference = NotificationPreference( name=name, display_name=display_name, display_description=display_description ) with self.assertNumQueries(3): test_notification_preference_saved = self.provider.save_notification_preference(test_notification_preference) self.assertIsNotNone(test_notification_preference_saved) self.assertIsNotNone(test_notification_preference_saved.name) return test_notification_preference_saved
def test_set_specific_user_preferences(self): """ Test to set the specific user preferences. """ notification_preference_daily = NotificationPreference( name='daily-digest-emails', display_name='Daily Emails', display_description='Daily Digestion Email ', default_value='false') set_notification_preference(notification_preference_daily) data = {'value': "true"} # post the api with the valid data # this will create a new user preference response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails']), data=data) self.assertEqual(response.status_code, 200) data = {'value': "false"} # post the api with the valid data # this will create a new user preference response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails']), data=data) self.assertEqual(response.status_code, 200) data = {} # missing value in json gives 404 error response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails']), data=data) self.assertEqual(response.status_code, 404) # valid data and invalid arg in the url gives 404 error data.clear() data = {'value': 'User Preference updated Value'} response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=['invalid-value']), data=data) self.assertEqual(response.status_code, 404) # post the api with the valid data response = self.client.post(reverse( 'edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails']), data=data) self.assertEqual(response.status_code, 200) # now check if the data is updated response = self.client.get( reverse('edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails'])) self.assertEqual(response.status_code, 200) results = json.loads(response.content.decode('utf-8')) self.assertEqual(len(results), 1) self.assertEqual(data['value'], results[0]['value'])