-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1228 - Improves webhooks to send labels at once #1566
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c6b2bb0
Issue #1228 - Adds test files for webhook events
karlcow cc2a869
Issue #1228 - Removes labeler test from test_url
karlcow 3ff3e04
Issue #1228 - Adds LOCALHOST key for Webhooks
karlcow 5f9294c
Issue #1228 - Removes dump to DB feature
karlcow c49ef3e
Issue #1228 - Changes signature computation in 2 functions
karlcow c6c7980
Issue #1228 - Adds tests for webhooks
karlcow 0fea5eb
Issue #1228 - Makes labeler route slimmer
karlcow 5f5b6e9
Issue #1228 - Removes unused header
karlcow 39874cb
Issue #1228 - Uses proxy_request instead of local request
karlcow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"action": "closed", | ||
"issue": { | ||
"number": 600, | ||
"body": "<!-- @browser: Firefox 55.0 -->\n<!-- @ua_header: Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0 -->\n<!-- @reported_with: web -->\n\n**URL**: https://www.netflix.com/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"action": "opened", | ||
"issue": { | ||
"number": 600, | ||
"body": "<!-- @browser: Firefox 55.0 -->\n<!-- @ua_header: Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0 -->\n<!-- @reported_with: web -->\n\n**URL**: https://www.netflix.com/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
"""Tests for our webhooks.""" | ||
|
||
import json | ||
import os | ||
import sys | ||
import unittest | ||
|
||
from webcompat import app | ||
from webcompat.webhooks import helpers | ||
|
||
# Add webcompat module to import path | ||
sys.path.append(os.path.realpath(os.pardir)) | ||
import webcompat # nopep8 | ||
|
||
|
||
# Any request that depends on parsing HTTP Headers (basically anything | ||
# on the index route, will need to include the following: environ_base=headers | ||
headers = {'HTTP_USER_AGENT': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; ' | ||
'rv:31.0) Gecko/20100101 Firefox/31.0')} | ||
key = app.config['HOOK_SECRET_KEY'] | ||
|
||
|
||
# Some machinery for opening our test files | ||
def event_data(filename): | ||
'''return a tuple with the content and its signature.''' | ||
current_root = os.path.realpath(os.curdir) | ||
events_path = 'tests/fixtures/webhooks' | ||
path = os.path.join(current_root, events_path, filename) | ||
with open(path, 'r') as f: | ||
json_event = json.dumps(json.load(f)) | ||
signature = 'sha1={sig}'.format( | ||
sig=helpers.get_payload_signature(key, json_event)) | ||
return json_event, signature | ||
|
||
|
||
class TestWebhook(unittest.TestCase): | ||
def setUp(self): | ||
webcompat.app.config['TESTING'] = True | ||
self.app = webcompat.app.test_client() | ||
self.headers = {'content-type': 'application/json'} | ||
self.test_url = '/webhooks/labeler' | ||
self.issue_body = """ | ||
<!-- @browser: Firefox 55.0 --> | ||
<!-- @ua_header: Mozilla/5.0 (what) Gecko/20100101 Firefox/55.0 --> | ||
<!-- @reported_with: web --> | ||
|
||
**URL**: https://www.example.com/ | ||
**Browser / Version**: Firefox 55.0 | ||
<!-- @browser: Chrome 48.0 --> | ||
""" | ||
self.issue_body2 = """ | ||
<!-- @browser: Foobar --> | ||
""" | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_forbidden_get(self): | ||
"""GET is forbidden on labeler webhook.""" | ||
rv = self.app.get(self.test_url, headers=self.headers) | ||
self.assertEqual(rv.status_code, 404) | ||
|
||
def test_fail_on_missing_signature(self): | ||
"""POST without signature on labeler webhook is forbidden.""" | ||
self.headers.update({'X-GitHub-Event': 'ping'}) | ||
rv = self.app.post(self.test_url, headers=self.headers) | ||
self.assertEqual(rv.status_code, 401) | ||
|
||
def test_fail_on_bogus_signature(self): | ||
"""POST without bogus signature on labeler webhook is forbidden.""" | ||
json_event, signature = event_data('new_event_valid.json') | ||
self.headers.update({'X-GitHub-Event': 'ping', | ||
'X-Hub-Signature': 'Boo!'}) | ||
rv = self.app.post(self.test_url, | ||
data=json_event, | ||
headers=self.headers) | ||
self.assertEqual(rv.status_code, 401) | ||
|
||
def test_fail_on_invalid_event_type(self): | ||
"""POST with event not being 'issues' or 'ping' fails.""" | ||
json_event, signature = event_data('new_event_valid.json') | ||
self.headers.update({'X-GitHub-Event': 'failme', | ||
'X-Hub-Signature': signature}) | ||
rv = self.app.post(self.test_url, | ||
data=json_event, | ||
headers=self.headers) | ||
self.assertEqual(rv.status_code, 403) | ||
|
||
def test_success_on_ping_event(self): | ||
"""POST with PING events just return a 200 and contains pong.""" | ||
json_event, signature = event_data('new_event_valid.json') | ||
self.headers.update({'X-GitHub-Event': 'ping', | ||
'X-Hub-Signature': signature}) | ||
rv = self.app.post(self.test_url, | ||
data=json_event, | ||
headers=self.headers) | ||
self.assertEqual(rv.status_code, 200) | ||
self.assertIn('pong', rv.data) | ||
|
||
def test_fails_on_action_not_opened(self): | ||
"""POST with action different of opened fails.""" | ||
json_event, signature = event_data('new_event_invalid.json') | ||
self.headers.update({'X-GitHub-Event': 'issues', | ||
'X-Hub-Signature': signature}) | ||
rv = self.app.post(self.test_url, | ||
data=json_event, | ||
headers=self.headers) | ||
self.assertEqual(rv.status_code, 200) | ||
self.assertIn('cool story, bro.', rv.data) | ||
|
||
def test_extract_browser_label(self): | ||
"""Extract browser label name.""" | ||
browser_label = helpers.extract_browser_label(self.issue_body) | ||
self.assertEqual(browser_label, 'browser-firefox') | ||
browser_label_none = helpers.extract_browser_label(self.issue_body2) | ||
self.assertEqual(browser_label_none, None) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.