2016-09-14 20:48:17 +00:00
|
|
|
import unittest
|
|
|
|
|
2016-10-14 16:10:54 +00:00
|
|
|
from endpoints.notificationevent import (BuildSuccessEvent, NotificationEvent,
|
|
|
|
VulnerabilityFoundEvent)
|
2016-09-14 20:48:17 +00:00
|
|
|
from util.morecollections import AttrDict
|
|
|
|
|
2016-09-21 18:37:23 +00:00
|
|
|
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'))
|
2016-11-29 16:48:08 +00:00
|
|
|
self.assertIsNotNone(NotificationEvent.get_event('build_cancelled'))
|
2016-10-14 16:10:54 +00:00
|
|
|
self.assertIsNotNone(NotificationEvent.get_event('vulnerability_found'))
|
2016-09-21 18:37:23 +00:00
|
|
|
|
|
|
|
|
2016-09-14 20:48:17 +00:00
|
|
|
class TestShouldPerform(unittest.TestCase):
|
2016-09-21 18:59:01 +00:00
|
|
|
def test_build_emptyjson(self):
|
|
|
|
notification_data = AttrDict({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': None,
|
2016-09-21 18:59:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# No build data at all.
|
|
|
|
self.assertTrue(BuildSuccessEvent().should_perform({}, notification_data))
|
|
|
|
|
2016-09-14 20:48:17 +00:00
|
|
|
def test_build_nofilter(self):
|
|
|
|
notification_data = AttrDict({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {},
|
2016-09-14 20:48:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# 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({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"ref-regex": ""},
|
2016-09-14 20:48:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# 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({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"ref-regex": "]["},
|
2016-09-14 20:48:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# 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({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"ref-regex": "refs/heads/master"},
|
2016-09-14 20:48:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# 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({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"ref-regex": "refs/heads/.+"},
|
2016-09-14 20:48:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
|
2016-10-14 16:10:54 +00:00
|
|
|
def test_vulnerability_notification_nolevel(self):
|
|
|
|
notification_data = AttrDict({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {},
|
2016-10-14 16:10:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# No level specified.
|
|
|
|
self.assertTrue(VulnerabilityFoundEvent().should_perform({}, notification_data))
|
|
|
|
|
|
|
|
|
|
|
|
def test_vulnerability_notification_nopvulninfo(self):
|
|
|
|
notification_data = AttrDict({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"level": 3},
|
2016-10-14 16:10:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# No vuln info.
|
|
|
|
self.assertFalse(VulnerabilityFoundEvent().should_perform({}, notification_data))
|
|
|
|
|
|
|
|
|
|
|
|
def test_vulnerability_notification_normal(self):
|
|
|
|
notification_data = AttrDict({
|
2017-07-12 12:50:47 +00:00
|
|
|
'event_config_dict': {"level": 3},
|
2016-10-14 16:10:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
info = {"vulnerability": {"priority": "Critical"}}
|
|
|
|
self.assertTrue(VulnerabilityFoundEvent().should_perform(info, notification_data))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-14 20:48:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|