From 543cba352bb93a355a2615d5dc65f74a81df41ec Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 14 Jul 2017 20:51:08 +0300 Subject: [PATCH] Add end-to-end notification worker tests for all notification methods --- .../notificationworker/models_interface.py | 2 +- workers/notificationworker/models_pre_oci.py | 9 ++-- .../test/test_notificationworker.py | 51 ++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/workers/notificationworker/models_interface.py b/workers/notificationworker/models_interface.py index c0caefa94..f2c54902b 100644 --- a/workers/notificationworker/models_interface.py +++ b/workers/notificationworker/models_interface.py @@ -40,7 +40,7 @@ class NotificationWorkerDataInterface(object): pass @abstractmethod - def create_notification_for_testing(self, target_username): + def create_notification_for_testing(self, target_username, method_name=None, method_config=None): """ Creates a notification for testing. """ pass diff --git a/workers/notificationworker/models_pre_oci.py b/workers/notificationworker/models_pre_oci.py index 9e77f2e55..388c698f7 100644 --- a/workers/notificationworker/models_pre_oci.py +++ b/workers/notificationworker/models_pre_oci.py @@ -29,16 +29,17 @@ class PreOCIModel(NotificationWorkerDataInterface): def increment_notification_failure_count(self, notification): model.notification.increment_notification_failure_count(notification.uuid) - def create_notification_for_testing(self, target_username): + def create_notification_for_testing(self, target_username, method_name='quay_notification', + method_config=None): repo = model.repository.get_repository('devtable', 'simple') - method_data = { + method_data = method_config or { 'target': { 'kind': 'user', 'name': target_username, } } - notification = model.notification.create_repo_notification(repo, 'build_success', - 'quay_notification', method_data, {}) + notification = model.notification.create_repo_notification(repo, 'repo_push', + method_name, method_data, {}) return notification.uuid def user_has_local_notifications(self, target_username): diff --git a/workers/notificationworker/test/test_notificationworker.py b/workers/notificationworker/test/test_notificationworker.py index 92d218aca..76fd47532 100644 --- a/workers/notificationworker/test/test_notificationworker.py +++ b/workers/notificationworker/test/test_notificationworker.py @@ -1,10 +1,20 @@ +import pytest + +from mock import patch, Mock +from httmock import urlmatch, HTTMock + +from notifications.notificationmethod import (QuayNotificationMethod, EmailMethod, WebhookMethod, + FlowdockMethod, HipchatMethod, SlackMethod, + CannotValidateNotificationMethodException) +from notifications.notificationevent import RepoPushEvent +from notifications.models_interface import Repository from workers.notificationworker.notificationworker import NotificationWorker from test.fixtures import * from workers.notificationworker.models_pre_oci import pre_oci_model as model -def test_basic_notification(initialized_db): +def test_basic_notification_endtoend(initialized_db): # Ensure the public user doesn't have any notifications. assert not model.user_has_local_notifications('public') @@ -21,3 +31,42 @@ def test_basic_notification(initialized_db): # Ensure the notification was handled. assert model.user_has_local_notifications('public') + + +@pytest.mark.parametrize('method,method_config,netloc', [ + (QuayNotificationMethod, {'target': {'name': 'devtable', 'kind': 'user'}}, None), + (EmailMethod, {'email': 'jschorr@devtable.com'}, None), + (WebhookMethod, {'url': 'http://example.com'}, 'example.com'), + (FlowdockMethod, {'flow_api_token': 'sometoken'}, 'api.flowdock.com'), + (HipchatMethod, {'notification_token': 'token', 'room_id': 'foo'}, 'api.hipchat.com'), + (SlackMethod, {'url': 'http://example.com'}, 'example.com'), +]) +def test_notifications(method, method_config, netloc, initialized_db): + url_hit = [False] + @urlmatch(netloc=netloc) + def url_handler(_, __): + url_hit[0] = True + return '' + + mock = Mock() + def get_mock(*args, **kwargs): + return mock + + with patch('notifications.notificationmethod.Message', get_mock): + with HTTMock(url_handler): + # Add a basic build notification. + notification_uuid = model.create_notification_for_testing('public', + method_name=method.method_name(), + method_config=method_config) + event_data = RepoPushEvent().get_sample_data(Repository('devtable', 'simple'), {}) + + # Fire off the queue processing. + worker = NotificationWorker(None) + worker.process_queue_item({ + 'notification_uuid': notification_uuid, + 'event_data': event_data, + 'performer_data': {}, + }) + + if netloc is not None: + assert url_hit[0]