|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +"""Tests for our webhooks.""" |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import unittest |
| 13 | + |
| 14 | +from webcompat import app |
| 15 | +from webcompat.webhooks import helpers |
| 16 | + |
| 17 | +# Add webcompat module to import path |
| 18 | +sys.path.append(os.path.realpath(os.pardir)) |
| 19 | +import webcompat # nopep8 |
| 20 | + |
| 21 | +# The key is being used for testing and computing the signature. |
| 22 | +key = app.config['HOOK_SECRET_KEY'] |
| 23 | + |
| 24 | + |
| 25 | +# Some machinery for opening our test files |
| 26 | +def event_data(filename): |
| 27 | + '''return a tuple with the content and its signature.''' |
| 28 | + current_root = os.path.realpath(os.curdir) |
| 29 | + events_path = 'tests/fixtures/webhooks' |
| 30 | + path = os.path.join(current_root, events_path, filename) |
| 31 | + with open(path, 'r') as f: |
| 32 | + json_event = json.dumps(json.load(f)) |
| 33 | + signature = 'sha1={sig}'.format( |
| 34 | + sig=helpers.get_payload_signature(key, json_event)) |
| 35 | + return json_event, signature |
| 36 | + |
| 37 | + |
| 38 | +class TestWebhook(unittest.TestCase): |
| 39 | + def setUp(self): |
| 40 | + webcompat.app.config['TESTING'] = True |
| 41 | + self.app = webcompat.app.test_client() |
| 42 | + self.headers = {'content-type': 'application/json'} |
| 43 | + self.test_url = '/webhooks/labeler' |
| 44 | + self.issue_body = """ |
| 45 | + <!-- @browser: Firefox 55.0 --> |
| 46 | + <!-- @ua_header: Mozilla/5.0 (what) Gecko/20100101 Firefox/55.0 --> |
| 47 | + <!-- @reported_with: web --> |
| 48 | +
|
| 49 | + **URL**: https://www.example.com/ |
| 50 | + **Browser / Version**: Firefox 55.0 |
| 51 | + <!-- @browser: Chrome 48.0 --> |
| 52 | + """ |
| 53 | + self.issue_body2 = """ |
| 54 | + <!-- @browser: Foobar --> |
| 55 | + """ |
| 56 | + |
| 57 | + def tearDown(self): |
| 58 | + pass |
| 59 | + |
| 60 | + def test_forbidden_get(self): |
| 61 | + """GET is forbidden on labeler webhook.""" |
| 62 | + rv = self.app.get(self.test_url, headers=self.headers) |
| 63 | + self.assertEqual(rv.status_code, 404) |
| 64 | + |
| 65 | + def test_fail_on_missing_signature(self): |
| 66 | + """POST without signature on labeler webhook is forbidden.""" |
| 67 | + self.headers.update({'X-GitHub-Event': 'ping'}) |
| 68 | + rv = self.app.post(self.test_url, headers=self.headers) |
| 69 | + self.assertEqual(rv.status_code, 401) |
| 70 | + |
| 71 | + def test_fail_on_bogus_signature(self): |
| 72 | + """POST without bogus signature on labeler webhook is forbidden.""" |
| 73 | + json_event, signature = event_data('new_event_valid.json') |
| 74 | + self.headers.update({'X-GitHub-Event': 'ping', |
| 75 | + 'X-Hub-Signature': 'Boo!'}) |
| 76 | + rv = self.app.post(self.test_url, |
| 77 | + data=json_event, |
| 78 | + headers=self.headers) |
| 79 | + self.assertEqual(rv.status_code, 401) |
| 80 | + |
| 81 | + def test_fail_on_invalid_event_type(self): |
| 82 | + """POST with event not being 'issues' or 'ping' fails.""" |
| 83 | + json_event, signature = event_data('new_event_valid.json') |
| 84 | + self.headers.update({'X-GitHub-Event': 'failme', |
| 85 | + 'X-Hub-Signature': signature}) |
| 86 | + rv = self.app.post(self.test_url, |
| 87 | + data=json_event, |
| 88 | + headers=self.headers) |
| 89 | + self.assertEqual(rv.status_code, 403) |
| 90 | + |
| 91 | + def test_success_on_ping_event(self): |
| 92 | + """POST with PING events just return a 200 and contains pong.""" |
| 93 | + json_event, signature = event_data('new_event_valid.json') |
| 94 | + self.headers.update({'X-GitHub-Event': 'ping', |
| 95 | + 'X-Hub-Signature': signature}) |
| 96 | + rv = self.app.post(self.test_url, |
| 97 | + data=json_event, |
| 98 | + headers=self.headers) |
| 99 | + self.assertEqual(rv.status_code, 200) |
| 100 | + self.assertIn('pong', rv.data) |
| 101 | + |
| 102 | + def test_fails_on_action_not_opened(self): |
| 103 | + """POST with action different of opened fails.""" |
| 104 | + json_event, signature = event_data('new_event_invalid.json') |
| 105 | + self.headers.update({'X-GitHub-Event': 'issues', |
| 106 | + 'X-Hub-Signature': signature}) |
| 107 | + rv = self.app.post(self.test_url, |
| 108 | + data=json_event, |
| 109 | + headers=self.headers) |
| 110 | + self.assertEqual(rv.status_code, 200) |
| 111 | + self.assertIn('cool story, bro.', rv.data) |
| 112 | + |
| 113 | + def test_extract_browser_label(self): |
| 114 | + """Extract browser label name.""" |
| 115 | + browser_label = helpers.extract_browser_label(self.issue_body) |
| 116 | + self.assertEqual(browser_label, 'browser-firefox') |
| 117 | + browser_label_none = helpers.extract_browser_label(self.issue_body2) |
| 118 | + self.assertEqual(browser_label_none, None) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + unittest.main() |
0 commit comments