def test_notification_invalid_message_attribute(self, mocked_task): self.notification_options.update({'type': 'test_type'}) test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': '{notification.actor.random}', 'email_subject': '[{site.name}] {notification.actor.random}', } register_notification_type('test_type', test_type) self._create_notification() notification = notification_queryset.first() with self.assertRaises(NotificationRenderException) as context_manager: notification.message self.assertEqual( str(context_manager.exception), 'Error encountered in rendering notification message', ) with self.assertRaises(NotificationRenderException) as context_manager: notification.email_subject self.assertEqual( str(context_manager.exception), 'Error encountered in generating notification email', ) mocked_task.assert_called_with(notification_id=notification.id) unregister_notification_type('test_type')
def test_notification_type_message_template(self): message_template = { 'level': 'info', 'verb': 'message template verb', 'verbose_name': 'Message Template Type', 'email_subject': '[{site.name}] Messsage Template Subject', } with self.subTest('Register type with non existent message template'): with self.assertRaises(TemplateDoesNotExist): message_template.update({'message_template': 'wrong/path.md'}) register_notification_type('message_template', message_template) with self.subTest('Registering type with message template'): message_template.update( {'message_template': 'openwisp_notifications/default_message.md'} ) register_notification_type('message_template', message_template) self.notification_options.update({'type': 'message_template'}) self._create_notification() n = notification_queryset.first() self.assertEqual(n.type, 'message_template') self.assertEqual(n.email_subject, '[example.com] Messsage Template Subject') with self.subTest('Links in notification message'): url = _get_absolute_url( reverse('admin:openwisp_users_user_change', args=(self.admin.pk,)) ) message = ( '<p>info : None message template verb </p>\n' f'<p><a href="{url}">admin</a>' '\nreports\n<a href="#">None</a>\nmessage template verb.</p>' ) self.assertEqual(n.message, message)
def test_malformed_notifications(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'warning', 'verb': 'testing', 'message': '{notification.actor.random}', 'email_subject': '[{site.name}] {notification.actor.random}', } register_notification_type('test_type', test_type) with self.subTest('Test list notifications'): notify.send(sender=self.admin, type='default') notify.send(sender=self.admin, type='test_type') url = self._get_path('notifications_list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['count'], 1) self.assertEqual( response.data['next'], None, ) self.assertEqual(response.data['previous'], None) self.assertEqual(len(response.data['results']), 1) with self.subTest('Test retrieve notification'): [(_, [n])] = notify.send( sender=self.admin, type='test_type', target=self._get_org_user() ) url = self._get_path('notification_detail', n.pk) response = self.client.get(url) self.assertEqual(response.status_code, 404) self.assertDictEqual(response.data, {'detail': NOT_FOUND_ERROR}) unregister_notification_type('test_type')
def test_notification_type_web_notification_setting_true(self): self.notification_options.update({'target': self._get_org_user()}) test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': 'Test message', 'email_subject': 'Test Email Subject', 'web_notification': True, } register_notification_type('test_type', test_type) self.notification_options.update({'type': 'test_type'}) with self.subTest('Test user web preference not defined'): self._create_notification() self.assertEqual(notification_queryset.delete()[0], 1) with self.subTest('Test user web preference is "False"'): NotificationSetting.objects.filter( user=self.admin, type='test_type').update(web=False) self._create_notification() self.assertEqual(notification_queryset.count(), 0) unregister_notification_type('test_type')
def test_register_notification_org_user(self): self._create_staff_org_admin() queryset = NotificationSetting.objects.filter(type='test') self.assertEqual(queryset.count(), 0) register_notification_type('test', test_notification_type) self.assertEqual(queryset.count(), 1)
def test_notification_type_email_notification_setting_false(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': 'Test message', 'email_subject': 'Test Email Subject', 'email_notification': False, } register_notification_type('test_type', test_type) target_obj = self._get_org_user() self.notification_options.update({ 'type': 'test_type', 'target': target_obj }) with self.subTest('Test user email preference not defined'): self._create_notification() self.assertEqual(len(mail.outbox), 0) with self.subTest('Test user email preference is "True"'): NotificationSetting.objects.filter( user=self.admin, type='test_type', ).update(email=True) self._create_notification() self.assertEqual(len(mail.outbox), 1) unregister_notification_type('test_type')
def test_unregistered_notification_type_related_notification(self): # Notifications related to notification type should # get deleted on unregistration of notification type default_type_config = get_notification_configuration('default') self.notification_options.update({'type': 'default'}) unregister_notification_type('default') self.assertEqual(notification_queryset.count(), 0) register_notification_type('default', default_type_config)
def test_notification_type_registered(self): register_notification_type('test', test_notification_type) queryset = NotificationSetting.objects.filter(type='test') self._get_user() self.assertEqual(queryset.count(), 0) self._get_admin() self.assertEqual(queryset.count(), 1)
def test_notification_type_registered(self): register_notification_type('test', test_notification_type) queryset = NotificationSetting.objects.filter(type='test') self._get_user() self.assertEqual(queryset.count(), 0) self._get_admin() self.assertEqual(queryset.count(), 1) self.assertEquals(queryset.first().__str__(), 'Test Notification Type - default')
def test_missing_relation_object(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': ('{notification.verb} initiated by' '[{notification.actor}]({notification.actor_link}) with' ' [{notification.action_object}]({notification.action_link}) for' ' [{notification.target}]({notification.target_link}).'), 'email_subject': ('[{site.name}] {notification.verb} reported by' ' {notification.actor} with {notification.action_object} for {notification.target}' ), } register_notification_type('test_type', test_type, models=[User]) self.notification_options.pop('email_subject') self.notification_options.update({'type': 'test_type'}) with self.subTest("Missing target object after creation"): operator = self._get_operator() self.notification_options.update({'target': operator}) self._create_notification() operator.delete() n_count = notification_queryset.count() self.assertEqual(n_count, 0) with self.subTest("Missing action object after creation"): operator = self._get_operator() self.notification_options.pop('target') self.notification_options.update({'action_object': operator}) self._create_notification() operator.delete() n_count = notification_queryset.count() self.assertEqual(n_count, 0) with self.subTest("Missing actor object after creation"): operator = self._get_operator() self.notification_options.pop('action_object') self.notification_options.pop('url') self.notification_options.update({'sender': operator}) self._create_notification() operator.delete() n_count = notification_queryset.count() self.assertEqual(n_count, 0) unregister_notification_type('test_type')
def test_register_unregister_notification_type(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'test', 'verb': 'testing', 'message': '{notification.verb} initiated by {notification.actor} since {notification}', 'email_subject': '[{site.name}] {notification.verb} reported by {notification.actor}', } with self.subTest('Registering new notification type'): register_notification_type('test_type', test_type) self.notification_options.update({'type': 'test_type'}) self._create_notification() n = notification_queryset.first() self.assertEqual(n.level, 'test') self.assertEqual(n.verb, 'testing') self.assertEqual( n.message, '<p>testing initiated by admin since 0\xa0minutes</p>', ) self.assertEqual(n.email_subject, '[example.com] testing reported by admin') with self.subTest('Re-registering a notification type'): with self.assertRaises(ImproperlyConfigured): register_notification_type('test_type', test_type) with self.subTest('Check registration in NOTIFICATION_CHOICES'): notification_choice = NOTIFICATION_CHOICES[-1] self.assertTupleEqual( notification_choice, ('test_type', 'Test Notification Type') ) with self.subTest('Unregistering a notification type which does not exists'): with self.assertRaises(ImproperlyConfigured): unregister_notification_type('wrong type') with self.subTest('Unregistering a notification choice which does not exists'): with self.assertRaises(ImproperlyConfigured): _unregister_notification_choice('wrong type') with self.subTest('Unregistering "test_type"'): unregister_notification_type('test_type') with self.assertRaises(ImproperlyConfigured): get_notification_configuration('test_type') with self.subTest('Using non existing notification type for new notification'): with self.assertRaises(ImproperlyConfigured): self._create_notification() n = notification_queryset.first() with self.subTest('Check unregistration in NOTIFICATION_CHOICES'): with self.assertRaises(ImproperlyConfigured): _unregister_notification_choice('test_type')
def test_notification_invalid_message_attribute(self): self.notification_options.update({'type': 'test_type'}) test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': '{notification.actor.random}', 'email_subject': '[{site.name}] {notification.actor.random}', } register_notification_type('test_type', test_type) self._create_notification() self.assertIsNone(notification_queryset.first()) unregister_notification_type('test_type')
def test_notification_type_email_web_notification_defaults(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': 'Test message', 'email_subject': 'Test Email Subject', } register_notification_type('test_type', test_type) notification_type_config = get_notification_configuration('test_type') self.assertTrue(notification_type_config['web_notification']) self.assertTrue(notification_type_config['email_notification']) unregister_notification_type('test_type')
def test_obsolete_notifications_busy_worker(self, mocked_task): """ This test simulates deletion of related object when all celery workers are busy and related objects are not cached. """ operator = self._get_operator() test_type = { 'verbose_name': 'Test Notification Type', 'level': 'warning', 'verb': 'testing', 'message': 'Test notification for {notification.target.pk}', 'email_subject': '[{site.name}] {notification.target.pk}', } register_notification_type('test_type', test_type) notify.send(sender=self.admin, type='test_type', target=operator) notification = Notification.objects.first() self.assertEqual( notification.message, f'<p>Test notification for {operator.pk}</p>' ) operator.delete() notification.refresh_from_db() # Delete target object from cache cache_key = Notification._cache_key( notification.target_content_type_id, notification.target_object_id ) cache.delete(cache_key) self.assertIsNone(cache.get(cache_key)) with self.subTest('Test list notifications'): url = self._get_path('notifications_list') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['count'], 1) self.assertFalse(response.data['results']) with self.subTest('Test retrieve notification'): url = self._get_path('notification_read_redirect', notification.pk) response = self.client.get(url) self.assertEqual(response.status_code, 404) self.assertDictEqual(response.data, {'detail': NOT_FOUND_ERROR}) unregister_notification_type('test_type')
def test_notification_setting_full_clean(self): test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': 'Test message', 'email_subject': 'Test Email Subject', 'web_notification': False, 'email_notification': False, } register_notification_type('test_type', test_type) self._get_admin() queryset = NotificationSetting.objects.filter(type='test_type') queryset.update(email=False, web=False) notification_setting = queryset.first() notification_setting.full_clean() self.assertIsNone(notification_setting.email) self.assertIsNone(notification_setting.web) unregister_notification_type('test_type')
def test_create_with_extra_data(self): register_notification_type( 'error_type', { 'verbose_name': 'Error', 'level': 'error', 'verb': 'error', 'message': 'Error: {error}', 'email_subject': 'Error subject: {error}', }, ) error = '500 Internal Server Error' notify.send( type='error_type', url='https://localhost:8000/admin', recipient=self.admin, sender=self.admin, error=error, ) self.assertEqual(notification_queryset.count(), 1) n = notification_queryset.first() self.assertIn(f'Error: {error}', n.message) self.assertEqual(n.email_subject, f'Error subject: {error}')
def test_notification_type_web_notification_setting_false(self): target_obj = self._get_org_user() self.notification_options.update({'target': target_obj}) test_type = { 'verbose_name': 'Test Notification Type', 'level': 'info', 'verb': 'testing', 'message': 'Test message', 'email_subject': 'Test Email Subject', 'web_notification': False, } register_notification_type('test_type', test_type) self.notification_options.update({'type': 'test_type'}) with self.subTest('Test user web preference not defined'): self._create_notification() self.assertEqual(notification_queryset.count(), 0) with self.subTest('Test user email preference is "True"'): notification_setting = NotificationSetting.objects.get( user=self.admin, type='test_type', organization=target_obj.organization) notification_setting.email = True notification_setting.save() notification_setting.refresh_from_db() self.assertFalse(notification_setting.email) with self.subTest('Test user web preference is "True"'): NotificationSetting.objects.filter( user=self.admin, type='test_type').update(web=True) self._create_notification() self.assertEqual(notification_queryset.count(), 1) unregister_notification_type('test_type')
def test_misc_notification_type_validation(self): with self.subTest('Registering with incomplete notification configuration.'): with self.assertRaises(AssertionError): register_notification_type('test_type', dict()) with self.subTest('Registering with improper notification type name'): with self.assertRaises(ImproperlyConfigured): register_notification_type(['test_type'], dict()) with self.subTest('Registering with improper notification configuration'): with self.assertRaises(ImproperlyConfigured): register_notification_type('test_type', tuple()) with self.subTest('Unregistering with improper notification type name'): with self.assertRaises(ImproperlyConfigured): unregister_notification_type(dict())