initial import for Open Source 🎉

This commit is contained in:
Jimmy Zelinskie 2019-11-12 11:09:47 -05:00
parent 1898c361f3
commit 9c0dd3b722
2048 changed files with 218743 additions and 0 deletions

View file

@ -0,0 +1,189 @@
import pytest
from notifications.notificationevent import (BuildSuccessEvent, NotificationEvent,
VulnerabilityFoundEvent)
from util.morecollections import AttrDict
from test.fixtures import *
@pytest.mark.parametrize('event_kind', NotificationEvent.event_names())
def test_create_notifications(event_kind):
assert NotificationEvent.get_event(event_kind) is not None
@pytest.mark.parametrize('event_name', NotificationEvent.event_names())
def test_build_notification(event_name, initialized_db):
# Create the notification event.
found = NotificationEvent.get_event(event_name)
sample_data = found.get_sample_data('foo', 'bar', {'level': 'low'})
# Make sure all calls succeed.
notification_data = {
'performer_data': {},
}
found.get_level(sample_data, notification_data)
found.get_summary(sample_data, notification_data)
found.get_message(sample_data, notification_data)
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)

View file

@ -0,0 +1,156 @@
import pytest
from mock import patch, Mock
from httmock import urlmatch, HTTMock
from data import model
from notifications.notificationmethod import (QuayNotificationMethod, EmailMethod, WebhookMethod,
FlowdockMethod, HipchatMethod, SlackMethod,
CannotValidateNotificationMethodException)
from notifications.notificationevent import NotificationEvent
from notifications.models_interface import Repository, Notification
from test.fixtures import *
def assert_validated(method, method_config, error_message, namespace_name, repo_name):
if error_message is None:
method.validate(namespace_name, repo_name, method_config)
else:
with pytest.raises(CannotValidateNotificationMethodException) as ipe:
method.validate(namespace_name, repo_name, method_config)
assert str(ipe.value) == error_message
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing target'),
({'target': {'name': 'invaliduser', 'kind': 'user'}}, 'Unknown user invaliduser'),
({'target': {'name': 'invalidorg', 'kind': 'org'}}, 'Unknown organization invalidorg'),
({'target': {'name': 'invalidteam', 'kind': 'team'}}, 'Unknown team invalidteam'),
({'target': {'name': 'devtable', 'kind': 'user'}}, None),
({'target': {'name': 'buynlarge', 'kind': 'org'}}, None),
({'target': {'name': 'owners', 'kind': 'team'}}, None),
])
def test_validate_quay_notification(method_config, error_message, initialized_db):
method = QuayNotificationMethod()
assert_validated(method, method_config, error_message, 'buynlarge', 'orgrepo')
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing e-mail address'),
({'email': 'a@b.com'}, 'The specified e-mail address is not authorized to receive '
'notifications for this repository'),
({'email': 'jschorr@devtable.com'}, None),
])
def test_validate_email(method_config, error_message, initialized_db):
method = EmailMethod()
assert_validated(method, method_config, error_message, 'devtable', 'simple')
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing webhook URL'),
({'url': 'http://example.com'}, None),
])
def test_validate_webhook(method_config, error_message, initialized_db):
method = WebhookMethod()
assert_validated(method, method_config, error_message, 'devtable', 'simple')
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing Flowdock API Token'),
({'flow_api_token': 'sometoken'}, None),
])
def test_validate_flowdock(method_config, error_message, initialized_db):
method = FlowdockMethod()
assert_validated(method, method_config, error_message, 'devtable', 'simple')
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing Hipchat Room Notification Token'),
({'notification_token': 'sometoken'}, 'Missing Hipchat Room ID'),
({'notification_token': 'sometoken', 'room_id': 'foo'}, None),
])
def test_validate_hipchat(method_config, error_message, initialized_db):
method = HipchatMethod()
assert_validated(method, method_config, error_message, 'devtable', 'simple')
@pytest.mark.parametrize('method_config,error_message', [
({}, 'Missing Slack Callback URL'),
({'url': 'http://example.com'}, None),
])
def test_validate_slack(method_config, error_message, initialized_db):
method = SlackMethod()
assert_validated(method, method_config, error_message, 'devtable', 'simple')
@pytest.mark.parametrize('target,expected_users', [
({'name': 'devtable', 'kind': 'user'}, ['devtable']),
({'name': 'buynlarge', 'kind': 'org'}, ['buynlarge']),
({'name': 'creators', 'kind': 'team'}, ['creator']),
])
def test_perform_quay_notification(target, expected_users, initialized_db):
repository = Repository('buynlarge', 'orgrepo')
notification = Notification(uuid='fake', event_name='repo_push', method_name='quay',
event_config_dict={}, method_config_dict={'target': target},
repository=repository)
event_handler = NotificationEvent.get_event('repo_push')
sample_data = event_handler.get_sample_data(repository.namespace_name, repository.name, {})
method = QuayNotificationMethod()
method.perform(notification, event_handler, {'event_data': sample_data})
# Ensure that the notification was written for all the expected users.
if target['kind'] != 'team':
user = model.user.get_namespace_user(target['name'])
assert len(model.notification.list_notifications(user, kind_name='repo_push')) > 0
def test_perform_email(initialized_db):
repository = Repository('buynlarge', 'orgrepo')
notification = Notification(uuid='fake', event_name='repo_push', method_name='email',
event_config_dict={}, method_config_dict={'email': 'test@example.com'},
repository=repository)
event_handler = NotificationEvent.get_event('repo_push')
sample_data = event_handler.get_sample_data(repository.namespace_name, repository.name, {})
mock = Mock()
def get_mock(*args, **kwargs):
return mock
with patch('notifications.notificationmethod.Message', get_mock):
method = EmailMethod()
method.perform(notification, event_handler, {'event_data': sample_data, 'performer_data': {}})
mock.send.assert_called_once()
@pytest.mark.parametrize('method, method_config, netloc', [
(WebhookMethod, {'url': 'http://testurl'}, 'testurl'),
(FlowdockMethod, {'flow_api_token': 'token'}, 'api.flowdock.com'),
(HipchatMethod, {'notification_token': 'token', 'room_id': 'foo'}, 'api.hipchat.com'),
(SlackMethod, {'url': 'http://example.com'}, 'example.com'),
])
def test_perform_http_call(method, method_config, netloc, initialized_db):
repository = Repository('buynlarge', 'orgrepo')
notification = Notification(uuid='fake', event_name='repo_push', method_name=method.method_name(),
event_config_dict={}, method_config_dict=method_config,
repository=repository)
event_handler = NotificationEvent.get_event('repo_push')
sample_data = event_handler.get_sample_data(repository.namespace_name, repository.name, {})
url_hit = [False]
@urlmatch(netloc=netloc)
def url_handler(_, __):
url_hit[0] = True
return ''
with HTTMock(url_handler):
method().perform(notification, event_handler, {'event_data': sample_data, 'performer_data': {}})
assert url_hit[0]