Add end-to-end notification worker tests for all notification methods

This commit is contained in:
Joseph Schorr 2017-07-14 20:51:08 +03:00
parent 348b544f23
commit 543cba352b
3 changed files with 56 additions and 6 deletions

View file

@ -40,7 +40,7 @@ class NotificationWorkerDataInterface(object):
pass pass
@abstractmethod @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. """ """ Creates a notification for testing. """
pass pass

View file

@ -29,16 +29,17 @@ class PreOCIModel(NotificationWorkerDataInterface):
def increment_notification_failure_count(self, notification): def increment_notification_failure_count(self, notification):
model.notification.increment_notification_failure_count(notification.uuid) 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') repo = model.repository.get_repository('devtable', 'simple')
method_data = { method_data = method_config or {
'target': { 'target': {
'kind': 'user', 'kind': 'user',
'name': target_username, 'name': target_username,
} }
} }
notification = model.notification.create_repo_notification(repo, 'build_success', notification = model.notification.create_repo_notification(repo, 'repo_push',
'quay_notification', method_data, {}) method_name, method_data, {})
return notification.uuid return notification.uuid
def user_has_local_notifications(self, target_username): def user_has_local_notifications(self, target_username):

View file

@ -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 workers.notificationworker.notificationworker import NotificationWorker
from test.fixtures import * from test.fixtures import *
from workers.notificationworker.models_pre_oci import pre_oci_model as model 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. # Ensure the public user doesn't have any notifications.
assert not model.user_has_local_notifications('public') assert not model.user_has_local_notifications('public')
@ -21,3 +31,42 @@ def test_basic_notification(initialized_db):
# Ensure the notification was handled. # Ensure the notification was handled.
assert model.user_has_local_notifications('public') 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]