github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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 import handlers 32 import main 33 import models 34 35 app = webtest.TestApp(main.app) 36 37 38 class TestBase(unittest.TestCase): 39 def init_stubs(self): 40 self.testbed.init_memcache_stub() 41 self.testbed.init_app_identity_stub() 42 self.testbed.init_urlfetch_stub() 43 self.testbed.init_blobstore_stub() 44 self.testbed.init_datastore_v3_stub() 45 46 47 class AppTest(TestBase): 48 def setUp(self): 49 self.init_stubs() 50 51 def get_response(self, event, body): 52 if isinstance(body, dict): 53 body = json.dumps(body) 54 signature = handlers.make_signature(body) 55 return app.post('/webhook', body, 56 {'X-Github-Event': event, 57 'X-Hub-Signature': signature}) 58 59 def test_webhook(self): 60 self.get_response('test', {'action': 'blah'}) 61 hooks = list(models.GithubWebhookRaw.query()) 62 self.assertEqual(len(hooks), 1) 63 self.assertIsNotNone(hooks[0].timestamp) 64 65 def test_webhook_bad_sig(self): 66 body = json.dumps({'action': 'blah'}) 67 signature = handlers.make_signature(body + 'foo') 68 app.post('/webhook', body, 69 {'X-Github-Event': 'test', 70 'X-Hub-Signature': signature}, status=400) 71 72 def test_webhook_missing_sig(self): 73 app.post('/webhook', '{}', 74 {'X-Github-Event': 'test'}, status=400) 75 76 def test_webhook_unicode(self): 77 self.get_response('test', {'action': u'blah\u03BA'}) 78 79 def test_webhook_status(self): 80 args = { 81 'name': 'owner/repo', 82 'sha': '1234', 83 'context': 'ci', 84 'state': 'success', 85 'target_url': 'http://example.com', 86 'description': 'passed the tests!', 87 'created_at': '2016-07-07T01:58:09Z', 88 'updated_at': '2016-07-07T02:03:12Z', 89 } 90 self.get_response('status', args) 91 statuses = list(models.GHStatus.query_for_sha('owner/repo', '1234')) 92 self.assertEqual(len(statuses), 1) 93 status = statuses[0] 94 args['repo'] = args.pop('name') 95 for key, value in args.iteritems(): 96 status_val = getattr(status, key) 97 try: 98 status_val = status_val.strftime('%Y-%m-%dT%H:%M:%SZ') 99 except AttributeError: 100 pass 101 assert status_val == value, '%r != %r' % (getattr(status, key), value) 102 103 PR_EVENT_BODY = { 104 'repository': {'full_name': 'test/test'}, 105 'pull_request': { 106 'number': 123, 107 'head': {'sha': 'cafe'}, 108 'updated_at': '2016-07-07T02:03:12Z', 109 'state': 'open', 110 'user': {'login': 'rmmh'}, 111 'assignees': [{'login': 'spxtr'}], 112 'title': 'test pr', 113 }, 114 'action': 'opened', 115 } 116 117 def test_webhook_pr_open(self): 118 body = json.dumps(self.PR_EVENT_BODY) 119 self.get_response('pull_request', body) 120 digest = models.GHIssueDigest.get('test/test', 123) 121 self.assertTrue(digest.is_pr) 122 self.assertTrue(digest.is_open) 123 self.assertEqual(digest.involved, ['rmmh', 'spxtr']) 124 self.assertEqual(digest.payload['title'], 'test pr') 125 self.assertEqual(digest.payload['needs_rebase'], False) 126 127 def test_webhook_pr_open_and_status(self): 128 self.get_response('pull_request', self.PR_EVENT_BODY) 129 self.get_response('status', { 130 'repository': self.PR_EVENT_BODY['repository'], 131 'name': self.PR_EVENT_BODY['repository']['full_name'], 132 'sha': self.PR_EVENT_BODY['pull_request']['head']['sha'], 133 'context': 'test-ci', 134 'state': 'success', 135 'target_url': 'example.com', 136 'description': 'woop!', 137 'created_at': '2016-07-07T01:58:09Z', 138 'updated_at': '2016-07-07T02:03:15Z', 139 }) 140 digest = models.GHIssueDigest.get('test/test', 123) 141 self.assertEqual(digest.payload['status'], 142 {'test-ci': ['success', 'example.com', 'woop!']})