github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gubernator/github/main_test.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # pylint: disable=no-self-use 18 19 20 """ 21 To run these tests: 22 $ pip install webtest nosegae 23 $ nosetests --with-gae --gae-lib-root ~/google_appengine/ 24 """ 25 26 import json 27 import unittest 28 29 import webtest 30 31 from google.appengine.ext import deferred 32 from google.appengine.ext import testbed 33 34 import handlers 35 import main 36 import models 37 import secrets 38 39 app = webtest.TestApp(main.app) 40 41 42 class TestBase(unittest.TestCase): 43 def init_stubs(self): 44 self.testbed.init_memcache_stub() 45 self.testbed.init_app_identity_stub() 46 self.testbed.init_urlfetch_stub() 47 self.testbed.init_blobstore_stub() 48 self.testbed.init_datastore_v3_stub() 49 self.testbed.init_taskqueue_stub() 50 51 52 class AppTest(TestBase): 53 def setUp(self): 54 self.init_stubs() 55 self.taskqueue = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME) 56 secrets.put('github_webhook_secret', 'some_secret', per_host=False) 57 58 def get_response(self, event, body): 59 if isinstance(body, dict): 60 body = json.dumps(body) 61 signature = handlers.make_signature(body) 62 resp = app.post('/webhook', body, 63 {'X-Github-Event': event, 64 'X-Hub-Signature': signature}) 65 for task in self.taskqueue.get_filtered_tasks(): 66 deferred.run(task.payload) 67 return resp 68 69 def test_webhook(self): 70 self.get_response('test', {'action': 'blah'}) 71 hooks = list(models.GithubWebhookRaw.query()) 72 self.assertEqual(len(hooks), 1) 73 self.assertIsNotNone(hooks[0].timestamp) 74 75 def test_webhook_bad_sig(self): 76 body = json.dumps({'action': 'blah'}) 77 signature = handlers.make_signature(body + 'foo') 78 app.post('/webhook', body, 79 {'X-Github-Event': 'test', 80 'X-Hub-Signature': signature}, status=400) 81 82 def test_webhook_missing_sig(self): 83 app.post('/webhook', '{}', 84 {'X-Github-Event': 'test'}, status=400) 85 86 def test_webhook_unicode(self): 87 self.get_response('test', {'action': u'blah\u03BA'}) 88 89 def test_webhook_status(self): 90 args = { 91 'name': 'owner/repo', 92 'sha': '1234', 93 'context': 'ci', 94 'state': 'success', 95 'target_url': 'http://example.com', 96 'description': 'passed the tests!', 97 'created_at': '2016-07-07T01:58:09Z', 98 'updated_at': '2016-07-07T02:03:12Z', 99 } 100 self.get_response('status', args) 101 statuses = list(models.GHStatus.query_for_sha('owner/repo', '1234')) 102 self.assertEqual(len(statuses), 1) 103 status = statuses[0] 104 args['repo'] = args.pop('name') 105 for key, value in args.iteritems(): 106 status_val = getattr(status, key) 107 try: 108 status_val = status_val.strftime('%Y-%m-%dT%H:%M:%SZ') 109 except AttributeError: 110 pass 111 assert status_val == value, '%r != %r' % (getattr(status, key), value) 112 113 PR_EVENT_BODY = { 114 'repository': {'full_name': 'test/test'}, 115 'pull_request': { 116 'number': 123, 117 'head': {'sha': 'cafe'}, 118 'updated_at': '2016-07-07T02:03:12+00:00', 119 'state': 'open', 120 'user': {'login': 'rmmh'}, 121 'assignees': [{'login': 'spxtr'}], 122 'title': 'test pr', 123 }, 124 'action': 'opened', 125 } 126 127 def test_webhook_pr_open(self): 128 body = json.dumps(self.PR_EVENT_BODY) 129 self.get_response('pull_request', body) 130 digest = models.GHIssueDigest.get('test/test', 123) 131 self.assertTrue(digest.is_pr) 132 self.assertTrue(digest.is_open) 133 self.assertEqual(digest.involved, ['rmmh', 'spxtr']) 134 self.assertEqual(digest.payload['title'], 'test pr') 135 self.assertEqual(digest.payload['needs_rebase'], False) 136 137 def test_webhook_pr_open_and_status(self): 138 self.get_response('pull_request', self.PR_EVENT_BODY) 139 self.get_response('status', { 140 'repository': self.PR_EVENT_BODY['repository'], 141 'name': self.PR_EVENT_BODY['repository']['full_name'], 142 'sha': self.PR_EVENT_BODY['pull_request']['head']['sha'], 143 'context': 'test-ci', 144 'state': 'success', 145 'target_url': 'example.com', 146 'description': 'woop!', 147 'created_at': '2016-07-07T01:58:09Z', 148 'updated_at': '2016-07-07T02:03:15Z', 149 }) 150 digest = models.GHIssueDigest.get('test/test', 123) 151 self.assertEqual(digest.payload['status'], 152 {'test-ci': ['success', 'example.com', 'woop!']})