Refactors the default notification option. Also ensures that notifications and destinations are easier to test. (#437)

This commit is contained in:
kevgliss 2016-10-09 00:06:53 -07:00 committed by GitHub
parent 72a390c563
commit 96e42c793e
9 changed files with 69 additions and 13 deletions

View File

@ -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))

View File

@ -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=[]

View File

@ -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

View File

@ -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():

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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'
}
}

View File

@ -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