From 96e42c793e91e745cf5ed2c4aa60d7444fbdc976 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Sun, 9 Oct 2016 00:06:53 -0700 Subject: [PATCH] Refactors the default notification option. Also ensures that notifications and destinations are easier to test. (#437) --- lemur/factory.py | 11 ++++++++++- lemur/notifications/service.py | 2 +- lemur/tests/conf.py | 2 -- lemur/tests/conftest.py | 16 ++++++++++++++++ lemur/tests/factories.py | 4 ++-- lemur/tests/plugins/destination_plugin.py | 16 ++++++++++++++++ lemur/tests/plugins/notification_plugin.py | 17 +++++++++++++++++ lemur/tests/test_destinations.py | 4 ++-- lemur/tests/test_notifications.py | 10 +++++----- 9 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 lemur/tests/plugins/destination_plugin.py create mode 100644 lemur/tests/plugins/notification_plugin.py diff --git a/lemur/factory.py b/lemur/factory.py index 9a13c0e5..13b9c8ba 100644 --- a/lemur/factory.py +++ b/lemur/factory.py @@ -153,9 +153,10 @@ def install_plugins(app): """ Installs new issuers that are not currently bundled with Lemur. - :param settings: + :param app: :return: """ + from lemur.plugins import plugins from lemur.plugins.base import register # entry_points={ # 'lemur.plugins': [ @@ -170,3 +171,11 @@ def install_plugins(app): app.logger.error("Failed to load plugin %r:\n%s\n" % (ep.name, traceback.format_exc())) else: register(plugin) + + # ensure that we have some way to notify + with app.app_context(): + try: + slug = app.config.get("LEMUR_DEFAULT_NOTIFICATION_PLUGIN", "email-notification") + plugins.get(slug) + except KeyError: + raise Exception("Unable to location notification plugin: {slug}. Ensure that LEMUR_DEFAULT_NOTIFICATION_PLUGIN is set to a valid and installed notification plugin.".format(slug=slug)) diff --git a/lemur/notifications/service.py b/lemur/notifications/service.py index 400f5cac..0b2d353a 100644 --- a/lemur/notifications/service.py +++ b/lemur/notifications/service.py @@ -236,7 +236,7 @@ def create_default_expiration_notifications(name, recipients): inter.extend(options) n = create( label="{name}_{interval}_DAY".format(name=name, interval=i), - plugin_name="email-notification", + plugin_name=current_app.config.get("LEMUR_DEFAULT_NOTIFICATION_PLUGIN", "email-notification"), options=list(inter), description="Default {interval} day expiration notification".format(interval=i), certificates=[] diff --git a/lemur/tests/conf.py b/lemur/tests/conf.py index fa175519..c4c5bbd9 100644 --- a/lemur/tests/conf.py +++ b/lemur/tests/conf.py @@ -46,7 +46,6 @@ LEMUR_DEFAULT_LOCATION = 'Los Gatos' LEMUR_DEFAULT_ORGANIZATION = 'Example, Inc.' LEMUR_DEFAULT_ORGANIZATIONAL_UNIT = 'Example' - # Database # modify this if you are not using a local database @@ -54,7 +53,6 @@ SQLALCHEMY_DATABASE_URI = 'postgresql://lemur:lemur@localhost:5432/lemur' SQLALCHEMY_TRACK_MODIFICATIONS = False # AWS - LEMUR_INSTANCE_PROFILE = 'Lemur' # Issuers diff --git a/lemur/tests/conftest.py b/lemur/tests/conftest.py index eb9d7491..834b50d9 100644 --- a/lemur/tests/conftest.py +++ b/lemur/tests/conftest.py @@ -140,6 +140,22 @@ def issuer_plugin(): return TestIssuerPlugin +@pytest.fixture +def notification_plugin(): + from lemur.plugins.base import register + from .plugins.notification_plugin import TestNotificationPlugin + register(TestNotificationPlugin) + return TestNotificationPlugin + + +@pytest.fixture +def destination_plugin(): + from lemur.plugins.base import register + from .plugins.destination_plugin import TestDestinationPlugin + register(TestDestinationPlugin) + return TestDestinationPlugin + + @pytest.yield_fixture(scope="function") def logged_in_user(session, app): with app.test_request_context(): diff --git a/lemur/tests/factories.py b/lemur/tests/factories.py index e37325ae..c39aef9e 100644 --- a/lemur/tests/factories.py +++ b/lemur/tests/factories.py @@ -137,7 +137,7 @@ class AuthorityFactory(BaseFactory): class DestinationFactory(BaseFactory): """Destination factory.""" - plugin_name = Sequence(lambda n: 'destination{0}'.format(n)) + plugin_name = 'test-destination' label = Sequence(lambda n: 'destination{0}'.format(n)) class Meta: @@ -147,7 +147,7 @@ class DestinationFactory(BaseFactory): class NotificationFactory(BaseFactory): """Notification factory.""" - plugin_name = Sequence(lambda n: 'notification{0}'.format(n)) + plugin_name = 'test-notification' label = Sequence(lambda n: 'notification{0}'.format(n)) class Meta: diff --git a/lemur/tests/plugins/destination_plugin.py b/lemur/tests/plugins/destination_plugin.py new file mode 100644 index 00000000..f77085ec --- /dev/null +++ b/lemur/tests/plugins/destination_plugin.py @@ -0,0 +1,16 @@ +from lemur.plugins.bases import DestinationPlugin + + +class TestDestinationPlugin(DestinationPlugin): + title = 'Test' + slug = 'test-destination' + description = 'Enables testing' + + author = 'Kevin Glisson' + author_url = 'https://github.com/netflix/lemur.git' + + def __init__(self, *args, **kwargs): + super(TestDestinationPlugin, self).__init__(*args, **kwargs) + + def upload(self, name, body, private_key, cert_chain, options, **kwargs): + return diff --git a/lemur/tests/plugins/notification_plugin.py b/lemur/tests/plugins/notification_plugin.py new file mode 100644 index 00000000..4ebba3df --- /dev/null +++ b/lemur/tests/plugins/notification_plugin.py @@ -0,0 +1,17 @@ +from lemur.plugins.bases import NotificationPlugin + + +class TestNotificationPlugin(NotificationPlugin): + title = 'Test' + slug = 'test-notification' + description = 'Enables testing' + + author = 'Kevin Glisson' + author_url = 'https://github.com/netflix/lemur.git' + + def __init__(self, *args, **kwargs): + super(TestNotificationPlugin, self).__init__(*args, **kwargs) + + @staticmethod + def send(event_type, message, targets, options, **kwargs): + return diff --git a/lemur/tests/test_destinations.py b/lemur/tests/test_destinations.py index 0a52b54c..d4c2444a 100644 --- a/lemur/tests/test_destinations.py +++ b/lemur/tests/test_destinations.py @@ -6,7 +6,7 @@ from lemur.destinations.views import * # noqa from .vectors import VALID_ADMIN_HEADER_TOKEN, VALID_USER_HEADER_TOKEN -def test_destination_input_schema(client, destination): +def test_destination_input_schema(client, destination_plugin, destination): from lemur.destinations.schemas import DestinationInputSchema input_data = { @@ -15,7 +15,7 @@ def test_destination_input_schema(client, destination): 'description': 'my destination', 'active': True, 'plugin': { - 'slug': 'aws-destination' + 'slug': 'test-destination' } } diff --git a/lemur/tests/test_notifications.py b/lemur/tests/test_notifications.py index bde3c81e..65911e29 100644 --- a/lemur/tests/test_notifications.py +++ b/lemur/tests/test_notifications.py @@ -6,7 +6,7 @@ from lemur.notifications.views import * # noqa from .vectors import VALID_ADMIN_HEADER_TOKEN, VALID_USER_HEADER_TOKEN -def test_notification_input_schema(client, notification): +def test_notification_input_schema(client, notification_plugin, notification): from lemur.notifications.schemas import NotificationInputSchema input_data = { @@ -15,7 +15,7 @@ def test_notification_input_schema(client, notification): 'description': 'my notification', 'active': True, 'plugin': { - 'slug': 'email-notification' + 'slug': 'test-notification' } } @@ -29,8 +29,8 @@ def test_notification_input_schema(client, notification): (VALID_ADMIN_HEADER_TOKEN, 200), ('', 401) ]) -def test_notification_get(client, token, status): - assert client.get(api.url_for(Notifications, notification_id=1), headers=token).status_code == status +def test_notification_get(client, notification_plugin, notification, token, status): + assert client.get(api.url_for(Notifications, notification_id=notification.id), headers=token).status_code == status @pytest.mark.parametrize("token,status", [ @@ -83,7 +83,7 @@ def test_notification_list_post_(client, token, status): (VALID_ADMIN_HEADER_TOKEN, 200), ('', 401) ]) -def test_notification_list_get(client, token, status): +def test_notification_list_get(client, notification_plugin, notification, token, status): assert client.get(api.url_for(NotificationsList), headers=token).status_code == status