示例#1
0
 def register_notification_types(self):
     register_notification_type(
         'connection_is_not_working',
         {
             'verbose_name': 'Device Connection PROBLEM',
             'verb': 'not working',
             'level': 'error',
             'email_subject': (
                 '[{site.name}] PROBLEM: Connection to '
                 'device {notification.target}'
             ),
             'message': (
                 '{notification.actor.credentials} connection to '
                 'device [{notification.target}]({notification.target_link}) '
                 'is {notification.verb}. {notification.actor.failure_reason}'
             ),
         },
     )
     register_notification_type(
         'connection_is_working',
         {
             'verbose_name': 'Device Connection RECOVERY',
             'verb': 'working',
             'level': 'info',
             'email_subject': (
                 '[{site.name}] RECOVERY: Connection to '
                 'device {notification.target}'
             ),
             'message': (
                 '{notification.actor.credentials} connection to '
                 'device [{notification.target}]({notification.target_link}) '
                 'is {notification.verb}. {notification.actor.failure_reason}'
             ),
         },
     )
def register_metric_notifications(metric_name, metric_config):
    if 'notification' not in metric_config:
        return
    register_notification_type(f'{metric_name}_problem',
                               metric_config['notification']['problem'])
    register_notification_type(f'{metric_name}_recovery',
                               metric_config['notification']['recovery'])
示例#3
0
    def register_notification_types(self):
        default_message = (
            '{notification.actor.name} for device "[{notification.target}]'
            '({notification.target_link})" {notification.verb}.'
        )

        register_notification_type(
            'threshold_crossed',
            {
                'name': 'Monitoring Alert',
                'verb': 'crossed the threshold',
                'level': 'warning',
                'email_subject': '[{site.name}] PROBLEM: {notification.actor.name} {notification.target}',
                'message': default_message,
            },
        )

        register_notification_type(
            'threshold_recovery',
            {
                'name': 'Monitoring Alert',
                'verb': 'returned within the threshold',
                'level': 'info',
                'email_subject': '[{site.name}] RECOVERY: {notification.actor.name} {notification.target}',
                'message': default_message,
            },
        )
    def test_notification_type_message_template(self):
        message_template = {
            'level': 'info',
            'verb': 'message template verb',
            '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'):
            exp_message = (
                '<p>info : None message template verb </p>\n'
                f'<p><a href="http://example.com/admin/openwisp_users/user/{self.admin.id}/change/">admin</a>'
                '\nreports\n<a href="#">None</a>\nmessage template verb.</p>'
            )
            self.assertEqual(n.message, exp_message)
示例#5
0
 def register_notification_types(self):
     register_notification_type(
         'object_created',
         {
             'verbose_name': 'Object created',
             'verb': 'created',
             'level': 'info',
             'message': '{notification.target} object {notification.verb}.',
             'email_subject': '[{site.name}] INFO: {notification.target} created',
         },
     )
    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_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)
        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')
示例#8
0
    def register_notification_types(self):
        register_notification_type(
            'config_error',
            {
                'verbose_name':
                _('Configuration ERROR'),
                'verb':
                _('encountered an error'),
                'level':
                'error',
                'email_subject':
                _('[{site.name}] ERROR: "{notification.target}" configuration '
                  '{notification.verb}'),
                'message':
                _('The configuration of [{notification.target}]'
                  '({notification.target_link}) has {notification.verb}. '
                  'The last working configuration has been restored from a backup '
                  'present on the filesystem of the device.'),
                'target_link': ('openwisp_controller.config.utils'
                                '.get_config_error_notification_target_url'),
            },
            models=[self.device_model, self.config_model],
        )

        register_notification_type(
            'device_registered',
            {
                'verbose_name':
                _('Device Registration'),
                'verb':
                _('registered successfully'),
                'level':
                'success',
                'email_subject':
                _('[{site.name}] SUCCESS: "{notification.target}"'
                  ' {notification.verb}'),
                'message':
                _('{condition} device [{notification.target}]'
                  '({notification.target_link}) has {notification.verb}.'),
            },
            models=[self.device_model],
        )

        #  Unregister default notification type
        try:
            unregister_notification_type('default')
        except ImproperlyConfigured:
            pass
 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()
     n = notification_queryset.first()
     self.assertEqual(
         n.message,
         '<p>Error while generating notification message, notification data may have been deleted.</p>',
     )
     with self.assertRaises(NotificationException):
         n.email_subject
     # Ensure email is not sent for such notification
     self.assertEqual(len(mail.outbox), 0)
     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())