From 5739e2ef4db179157fc1a396ae3c68a341fb4e6c Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 14 Jul 2017 16:16:37 +0300 Subject: [PATCH] Move notifications test into notifications package --- notifications/notificationevent.py | 5 + notifications/test/test_notifications.py | 171 +++++++++++++++++++++ test/test_notifications.py | 184 ----------------------- 3 files changed, 176 insertions(+), 184 deletions(-) create mode 100644 notifications/test/test_notifications.py delete mode 100644 test/test_notifications.py diff --git a/notifications/notificationevent.py b/notifications/notificationevent.py index 0401e1b05..7de742624 100644 --- a/notifications/notificationevent.py +++ b/notifications/notificationevent.py @@ -67,6 +67,11 @@ class NotificationEvent(object): raise InvalidNotificationEventException('Unable to find event: %s' % eventname) + @classmethod + def event_names(cls): + for subc in cls.__subclasses__(): + if subc.event_name() is not None: + yield subc.event_name() @staticmethod def _get_event(cls, eventname): diff --git a/notifications/test/test_notifications.py b/notifications/test/test_notifications.py new file mode 100644 index 000000000..a075ce30e --- /dev/null +++ b/notifications/test/test_notifications.py @@ -0,0 +1,171 @@ +import pytest + +from notifications.notificationevent import (BuildSuccessEvent, NotificationEvent, + VulnerabilityFoundEvent) +from util.morecollections import AttrDict + + +@pytest.mark.parametrize('event_kind', NotificationEvent.event_names()) +def test_create_notifications(event_kind): + assert NotificationEvent.get_event(event_kind) is not None + + +def test_build_emptyjson(): + notification_data = AttrDict({ + 'event_config_dict': None, + }) + + # No build data at all. + assert BuildSuccessEvent().should_perform({}, notification_data) + +def test_build_nofilter(): + notification_data = AttrDict({ + 'event_config_dict': {}, + }) + + # No build data at all. + assert BuildSuccessEvent().should_perform({}, notification_data) + + # With trigger metadata but no ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': {}, + }, notification_data) + + # With trigger metadata and a ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/somebranch', + }, + }, notification_data) + + +def test_build_emptyfilter(): + notification_data = AttrDict({ + 'event_config_dict': {"ref-regex": ""}, + }) + + # No build data at all. + assert BuildSuccessEvent().should_perform({}, notification_data) + + # With trigger metadata but no ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': {}, + }, notification_data) + + # With trigger metadata and a ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/somebranch', + }, + }, notification_data) + + +def test_build_invalidfilter(): + notification_data = AttrDict({ + 'event_config_dict': {"ref-regex": "]["}, + }) + + # No build data at all. + assert not BuildSuccessEvent().should_perform({}, notification_data) + + # With trigger metadata but no ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': {}, + }, notification_data) + + # With trigger metadata and a ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/somebranch', + }, + }, notification_data) + + +def test_build_withfilter(): + notification_data = AttrDict({ + 'event_config_dict': {"ref-regex": "refs/heads/master"}, + }) + + # No build data at all. + assert not BuildSuccessEvent().should_perform({}, notification_data) + + # With trigger metadata but no ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': {}, + }, notification_data) + + # With trigger metadata and a not-matching ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/somebranch', + }, + }, notification_data) + + # With trigger metadata and a matching ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/master', + }, + }, notification_data) + + +def test_build_withwildcardfilter(): + notification_data = AttrDict({ + 'event_config_dict': {"ref-regex": "refs/heads/.+"}, + }) + + # No build data at all. + assert not BuildSuccessEvent().should_perform({}, notification_data) + + # With trigger metadata but no ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': {}, + }, notification_data) + + # With trigger metadata and a not-matching ref. + assert not BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/tags/sometag', + }, + }, notification_data) + + # With trigger metadata and a matching ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/master', + }, + }, notification_data) + + # With trigger metadata and another matching ref. + assert BuildSuccessEvent().should_perform({ + 'trigger_metadata': { + 'ref': 'refs/heads/somebranch', + }, + }, notification_data) + + +def test_vulnerability_notification_nolevel(): + notification_data = AttrDict({ + 'event_config_dict': {}, + }) + + # No level specified. + assert VulnerabilityFoundEvent().should_perform({}, notification_data) + + +def test_vulnerability_notification_nopvulninfo(): + notification_data = AttrDict({ + 'event_config_dict': {"level": 3}, + }) + + # No vuln info. + assert not VulnerabilityFoundEvent().should_perform({}, notification_data) + + +def test_vulnerability_notification_normal(): + notification_data = AttrDict({ + 'event_config_dict': {"level": 3}, + }) + + info = {"vulnerability": {"priority": "Critical"}} + assert VulnerabilityFoundEvent().should_perform(info, notification_data) diff --git a/test/test_notifications.py b/test/test_notifications.py deleted file mode 100644 index 486bef19d..000000000 --- a/test/test_notifications.py +++ /dev/null @@ -1,184 +0,0 @@ -import unittest - -from notifications.notificationevent import (BuildSuccessEvent, NotificationEvent, - VulnerabilityFoundEvent) -from util.morecollections import AttrDict - -class TestCreate(unittest.TestCase): - def test_create_notifications(self): - self.assertIsNotNone(NotificationEvent.get_event('repo_push')) - self.assertIsNotNone(NotificationEvent.get_event('build_queued')) - self.assertIsNotNone(NotificationEvent.get_event('build_success')) - self.assertIsNotNone(NotificationEvent.get_event('build_failure')) - self.assertIsNotNone(NotificationEvent.get_event('build_start')) - self.assertIsNotNone(NotificationEvent.get_event('build_cancelled')) - self.assertIsNotNone(NotificationEvent.get_event('vulnerability_found')) - - -class TestShouldPerform(unittest.TestCase): - def test_build_emptyjson(self): - notification_data = AttrDict({ - 'event_config_dict': None, - }) - - # No build data at all. - self.assertTrue(BuildSuccessEvent().should_perform({}, notification_data)) - - def test_build_nofilter(self): - notification_data = AttrDict({ - 'event_config_dict': {}, - }) - - # No build data at all. - self.assertTrue(BuildSuccessEvent().should_perform({}, notification_data)) - - # With trigger metadata but no ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': {}, - }, notification_data)) - - # With trigger metadata and a ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/somebranch', - }, - }, notification_data)) - - - def test_build_emptyfilter(self): - notification_data = AttrDict({ - 'event_config_dict': {"ref-regex": ""}, - }) - - # No build data at all. - self.assertTrue(BuildSuccessEvent().should_perform({}, notification_data)) - - # With trigger metadata but no ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': {}, - }, notification_data)) - - # With trigger metadata and a ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/somebranch', - }, - }, notification_data)) - - - def test_build_invalidfilter(self): - notification_data = AttrDict({ - 'event_config_dict': {"ref-regex": "]["}, - }) - - # No build data at all. - self.assertFalse(BuildSuccessEvent().should_perform({}, notification_data)) - - # With trigger metadata but no ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': {}, - }, notification_data)) - - # With trigger metadata and a ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/somebranch', - }, - }, notification_data)) - - - def test_build_withfilter(self): - notification_data = AttrDict({ - 'event_config_dict': {"ref-regex": "refs/heads/master"}, - }) - - # No build data at all. - self.assertFalse(BuildSuccessEvent().should_perform({}, notification_data)) - - # With trigger metadata but no ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': {}, - }, notification_data)) - - # With trigger metadata and a not-matching ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/somebranch', - }, - }, notification_data)) - - # With trigger metadata and a matching ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/master', - }, - }, notification_data)) - - - def test_build_withwildcardfilter(self): - notification_data = AttrDict({ - 'event_config_dict': {"ref-regex": "refs/heads/.+"}, - }) - - # No build data at all. - self.assertFalse(BuildSuccessEvent().should_perform({}, notification_data)) - - # With trigger metadata but no ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': {}, - }, notification_data)) - - # With trigger metadata and a not-matching ref. - self.assertFalse(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/tags/sometag', - }, - }, notification_data)) - - # With trigger metadata and a matching ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/master', - }, - }, notification_data)) - - # With trigger metadata and another matching ref. - self.assertTrue(BuildSuccessEvent().should_perform({ - 'trigger_metadata': { - 'ref': 'refs/heads/somebranch', - }, - }, notification_data)) - - - def test_vulnerability_notification_nolevel(self): - notification_data = AttrDict({ - 'event_config_dict': {}, - }) - - # No level specified. - self.assertTrue(VulnerabilityFoundEvent().should_perform({}, notification_data)) - - - def test_vulnerability_notification_nopvulninfo(self): - notification_data = AttrDict({ - 'event_config_dict': {"level": 3}, - }) - - # No vuln info. - self.assertFalse(VulnerabilityFoundEvent().should_perform({}, notification_data)) - - - def test_vulnerability_notification_normal(self): - notification_data = AttrDict({ - 'event_config_dict': {"level": 3}, - }) - - info = {"vulnerability": {"priority": "Critical"}} - self.assertTrue(VulnerabilityFoundEvent().should_perform(info, notification_data)) - - - - -if __name__ == '__main__': - unittest.main() -