diff --git a/zephyr/tests.py b/zephyr/tests.py index e1c1c6836e..96466d6811 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -168,11 +168,14 @@ class AuthedTestCase(TestCase): return open(os.path.join(os.path.dirname(__file__), "fixtures/%s/%s_%s.%s" % (type, type, action,file_type))).read() + def subscribe_to_stream(self, email, stream_name): + stream, _ = create_stream_if_needed(Realm.objects.get(domain="humbughq.com"), stream_name) + user_profile = self.get_user_profile(email) + do_add_subscription(user_profile, stream, no_log=True) + def send_json_payload(self, email, url, payload, stream_name=None, **post_params): if stream_name != None: - stream, _ = create_stream_if_needed(Realm.objects.get(domain="humbughq.com"), stream_name) - user_profile = self.get_user_profile(email) - do_add_subscription(user_profile, stream, no_log=True) + self.subscribe_to_stream(email, stream_name) result = self.client.post(url, payload, **post_params) self.assert_json_success(result) @@ -2659,6 +2662,48 @@ class GithubHookTests(AuthedTestCase): * [a47fd41](http://github.com/mojombo/grit/commit/a47fd41f3aa4610ea527dcc1669dfdb9c15c5425): add more comments throughout """) + def test_spam_branch_is_ignored(self): + email = "hamlet@humbughq.com" + api_key = self.get_api_key(email) + stream = 'commits' + data = {'email': email, + 'api-key': api_key, + 'branches': 'dev,staging', + 'stream': stream, + 'event': 'push', + 'payload': self.fixture_data('github', 'sample')} + url = '/api/v1/external/github' + + # We subscribe to the stream in this test, even though + # it won't get written, to avoid failing for the wrong + # reason. + self.subscribe_to_stream(email, stream) + + prior_count = len(Message.objects.filter()) + + result = self.client.post(url, data) + self.assert_json_success(result) + + after_count = len(Message.objects.filter()) + self.assertEqual(prior_count, after_count) + + + def test_user_specified_branches(self): + email = "hamlet@humbughq.com" + api_key = self.get_api_key(email) + stream = 'my_commits' + data = {'email': email, + 'api-key': api_key, + 'stream': stream, + 'branches': 'master,staging', + 'event': 'push', + 'payload': self.fixture_data('github', 'sample')} + msg = self.send_json_payload(email, "/api/v1/external/github", + data, + stream_name=stream) + self.assertEqual(msg.subject, "grit") + self.assert_content(msg) + def test_user_specified_stream(self): # Around May 2013 the github webhook started to specify the stream. # Before then, the stream was hard coded to "commits". diff --git a/zephyr/views.py b/zephyr/views.py index 4a7e6677aa..074f2313ce 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1460,6 +1460,7 @@ def build_message_from_gitlog(user_profile, name, ref, commits, before, after, u @has_request_variables def api_github_landing(request, user_profile, event=POST, payload=POST(converter=json_to_dict), + branches=POST(default=''), stream=POST(default='commits')): # TODO: this should all be moved to an external bot repository = payload['repository'] @@ -1485,6 +1486,13 @@ def api_github_landing(request, user_profile, event=POST, if short_ref != 'master' and user_profile.realm.domain in ['customer18.invalid', 'humbughq.com']: return json_success() + if branches: + # If we are given a whitelist of branches, then we silently ignore + # any push notification on a branch that is not in our whitelist. + if short_ref not in re.split('[\s,;|]+', branches): + return json_success() + + subject, content = build_message_from_gitlog(user_profile, repository['name'], payload['ref'], payload['commits'], payload['before'], payload['after'],