From e7fec9dd20652ddfa47e544e824ca4f343aa8ec5 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 14 Jul 2017 16:48:31 +0300 Subject: [PATCH] Change get_sample_data API to not require the custom notification tuple --- endpoints/api/repositorynotification.py | 4 ++- notifications/notificationevent.py | 31 ++++++++++---------- notifications/test/test_notificationevent.py | 17 ++--------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/endpoints/api/repositorynotification.py b/endpoints/api/repositorynotification.py index ff91972c1..22634d235 100644 --- a/endpoints/api/repositorynotification.py +++ b/endpoints/api/repositorynotification.py @@ -8,10 +8,10 @@ from endpoints.api import (RepositoryParamResource, nickname, resource, require_ path_param, disallow_for_app_repositories) from endpoints.exception import NotFound from notifications import build_notification_data +from notifications.models_interface import Repository from notifications.notificationevent import NotificationEvent from notifications.notificationmethod import (NotificationMethod, CannotValidateNotificationMethodException) -from workers.notificationworker.models_pre_oci import notification from endpoints.api.repositorynotification_models_pre_oci import pre_oci_model as model logger = logging.getLogger(__name__) @@ -153,6 +153,8 @@ class TestRepositoryNotification(RepositoryParamResource): def post(self, namespace_name, repository_name, uuid): """ Queues a test notification for this repository. """ test_note = model.queue_test_notification(uuid) + event_info = NotificationEvent.get_event(test_note.event_name) + sample_data = event_info.get_sample_data(Repository(namespace, repository), event_config) if not test_note: raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid)) diff --git a/notifications/notificationevent.py b/notifications/notificationevent.py index 6b5b7fb56..87a736878 100644 --- a/notifications/notificationevent.py +++ b/notifications/notificationevent.py @@ -40,7 +40,7 @@ class NotificationEvent(object): 'notification_data': notification_data }) - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): """ Returns sample data for testing the raising of this notification, with an example notification. """ @@ -95,8 +95,8 @@ class RepoPushEvent(NotificationEvent): def get_summary(self, event_data, notification_data): return 'Repository %s updated' % (event_data['repository']) - def get_sample_data(self, notification): - return build_event_data(notification.repository, { + def get_sample_data(self, repository, event_config): + return build_event_data(repository, { 'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'}, 'pruned_image_count': 3 }) @@ -129,10 +129,9 @@ class VulnerabilityFoundEvent(NotificationEvent): return 'info' - def get_sample_data(self, notification): - event_config = notification.event_config_dict + def get_sample_data(self, repository, event_config): level = event_config.get(VulnerabilityFoundEvent.CONFIG_LEVEL, 'Critical') - return build_event_data(notification.repository, { + return build_event_data(repository, { 'tags': ['latest', 'prod', 'foo', 'bar', 'baz'], 'image': 'some-image-id', 'vulnerability': { @@ -217,9 +216,9 @@ class BuildQueueEvent(BaseBuildEvent): def get_level(self, event_data, notification_data): return 'info' - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): build_uuid = 'fake-build-id' - return build_event_data(notification.repository, { + return build_event_data(repository, { 'is_manual': False, 'build_id': build_uuid, 'build_name': 'some-fake-build', @@ -255,9 +254,9 @@ class BuildStartEvent(BaseBuildEvent): def get_level(self, event_data, notification_data): return 'info' - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): build_uuid = 'fake-build-id' - return build_event_data(notification.repository, { + return build_event_data(repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -282,9 +281,9 @@ class BuildSuccessEvent(BaseBuildEvent): def get_level(self, event_data, notification_data): return 'success' - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): build_uuid = 'fake-build-id' - return build_event_data(notification.repository, { + return build_event_data(repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -310,9 +309,9 @@ class BuildFailureEvent(BaseBuildEvent): def get_level(self, event_data, notification_data): return 'error' - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): build_uuid = 'fake-build-id' - return build_event_data(notification.repository, { + return build_event_data(repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -349,9 +348,9 @@ class BuildCancelledEvent(BaseBuildEvent): def get_level(self, event_data, notification_data): return 'info' - def get_sample_data(self, notification): + def get_sample_data(self, repository, event_config): build_uuid = 'fake-build-id' - return build_event_data(notification.repository, { + return build_event_data(repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], diff --git a/notifications/test/test_notificationevent.py b/notifications/test/test_notificationevent.py index 7429ef0a6..a5f83648d 100644 --- a/notifications/test/test_notificationevent.py +++ b/notifications/test/test_notificationevent.py @@ -1,25 +1,14 @@ +from notifications.models_interface import Repository from notifications.notificationevent import NotificationEvent -from util.morecollections import AttrDict from test.fixtures import * -def test_all_notifications(app): - # Create a test notification. - test_notification = AttrDict({ - 'repository': AttrDict({ - 'namespace_name': AttrDict(dict(username='foo')), - 'name': 'bar', - }), - 'event_config_dict': { - 'level': 'low', - }, - }) - +def test_all_notifications(initialized_db): for subc in NotificationEvent.__subclasses__(): if subc.event_name() is not None: # Create the notification event. found = NotificationEvent.get_event(subc.event_name()) - sample_data = found.get_sample_data(test_notification) + sample_data = found.get_sample_data(Repository('foo', 'bar'), {'level': 'low'}) # Make sure all calls succeed. notification_data = {