Skip to content

Commit b002f52

Browse files
committed
Raise exceptions on invalid requests
1 parent 9521287 commit b002f52

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

lambdawebhook/hook.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import hashlib
55

6-
# Add the ./site-packages directory to the path for Lambda to load our libs
6+
# Add the lib directory to the path for Lambda to load our libs
77
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
88
import requests # NOQA
99
import hmac # NOQA
@@ -17,7 +17,6 @@ def verify_signature(secret, signature, payload):
1717

1818
def lambda_handler(event, context):
1919
print 'Webhook received'
20-
print event['secret']
2120
verified = verify_signature(event['secret'],
2221
event['x_hub_signature'],
2322
event['payload'])
@@ -30,9 +29,9 @@ def lambda_handler(event, context):
3029
'X-Hub-Signature': event['x_hub_signature']
3130
},
3231
json=event['payload'])
33-
return {'status': {response.status_code: response.reason}}
32+
response.raise_for_status()
3433
else:
35-
return {'status': {403: 'Forbidden'}}
34+
raise requests.HTTPError('400 Client Error: Bad Request')
3635

3736

3837
if __name__ == "__main__":

test/unit_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import json
55
import httpretty
6+
from requests import HTTPError
67

78

89
def load_test_event():
@@ -12,7 +13,6 @@ def load_test_event():
1213
githubevent['payload'] = json.dumps(githubevent['payload'])
1314
return githubevent
1415

15-
1616
githubevent = load_test_event()
1717

1818

@@ -31,11 +31,11 @@ def runTest(self):
3131
class LambdaHandlerTestCase(unittest.TestCase):
3232
@httpretty.activate
3333
def runTest(self):
34+
invalidevent = load_test_event()
35+
invalidevent['secret'] = 'invalidsecret'
36+
self.assertRaises(HTTPError, lambdawebhook.hook.lambda_handler, invalidevent, {})
37+
3438
# Check return codes
3539
httpretty.register_uri(httpretty.POST, 'https://localhost/github-webhook/',
3640
status=200)
37-
self.assertEqual(lambdawebhook.hook.lambda_handler(githubevent, ''), {'status': {200: 'OK'}})
38-
39-
httpretty.register_uri(httpretty.POST, 'https://localhost/github-webhook/',
40-
status=403)
41-
self.assertEqual(lambdawebhook.hook.lambda_handler(githubevent, ''), {'status': {403: 'Forbidden'}})
41+
self.assertIsNone(lambdawebhook.hook.lambda_handler(githubevent, {}))

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ setenv =
1313
deps=
1414
nose
1515
httpretty
16+
requests
1617

1718
[testenv:flake8]
1819
basepython = python2.7

0 commit comments

Comments
 (0)