Download:
child 228:f45f894eb706
parent 226:0e10087ef94f
227:b6b7b5f902ba
Anton Shestakov <av6@dwimlabs.net>, Thu, 25 Aug 2016 11:22:09 +0800
tests: test hook handlers in hooks-queue.py

2 файлов изменено, 102 вставок(+), 0 удалений(-) [+]
bitbucket-pipelines.yml file | annotate | diff | comparison | revisions
tests/test_hooks_queue.py file | annotate | diff | comparison | revisions
--- a/bitbucket-pipelines.yml Thu Aug 25 11:20:06 2016 +0800
+++ b/bitbucket-pipelines.yml Thu Aug 25 11:22:09 2016 +0800
@@ -11,3 +11,4 @@
--cov=incoming.py
--cov=viewer.py
--cov=checker.py
+ --cov=hooks-queue.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_hooks_queue.py Thu Aug 25 11:22:09 2016 +0800
@@ -0,0 +1,101 @@
+from mock import Mock
+from tornado.escape import json_encode
+from tornado.testing import AsyncHTTPTestCase
+
+
+hooksmod = __import__('hooks-queue')
+rconn = Mock()
+
+
+class HookTestCase(AsyncHTTPTestCase):
+ def get_app(self):
+ return hooksmod.CandolintHooks(rconn, debug=True)
+
+ def post_json(self, url, data, extra_headers):
+ body = json_encode(data)
+ headers = {'Content-Type': 'application/json'}
+ headers.update(extra_headers)
+ return self.fetch(url, method='POST', body=body, headers=headers)
+
+
+class BitbucketHookTestCase(HookTestCase):
+ url = '/bitbucket'
+
+ def test_invalid_payload(self):
+ response = self.fetch(self.url, method='POST', body='invalid')
+ assert response.code == 400
+
+ def test_push_hg(self):
+ data = {
+ 'repository': {
+ 'scm': 'hg',
+ 'name': 'test',
+ 'links': {
+ 'html': {
+ 'href': 'https://bitbucket.org/example/test'
+ }
+ }
+ },
+ 'push': {
+ 'changes': [{
+ 'new': {
+ 'heads': [
+ {'hash': 'foo'},
+ {'hash': 'bar'},
+ {'hash': 'baz'}
+ ],
+ 'target': {'hash': 'bar'}
+ }
+ }]
+ }
+ }
+ response = self.post_json(self.url, data, {'X-Event-Key': 'repo:push'})
+ assert response.code == 200
+ assert '"queued": 3' in response.body
+
+ def test_push_git(self):
+ data = {
+ 'repository': {
+ 'scm': 'git',
+ 'name': 'test',
+ 'links': {
+ 'html': {
+ 'href': 'https://bitbucket.org/example/test'
+ }
+ }
+ },
+ 'push': {
+ 'changes': [{
+ 'new': {'type': 'branch', 'name': 'master'}
+ }]
+ }
+ }
+ response = self.post_json(self.url, data, {'X-Event-Key': 'repo:push'})
+ assert response.code == 200
+ assert '"queued": 1' in response.body
+
+
+class GithubHookTestCase(HookTestCase):
+ url = '/github'
+
+ def test_no_event(self):
+ response = self.post_json(self.url, {}, {})
+ assert response.code == 200
+ assert response.body == '{"result": "unknown event"}'
+
+ def test_ping(self):
+ response = self.post_json(self.url, {}, {'X-GitHub-Event': 'ping'})
+ assert response.code == 200
+ assert response.body == '{"result": "pong"}'
+
+ def test_push(self):
+ data = {
+ 'repository': {
+ 'name': 'test',
+ 'html_url': 'https://github.com/example/test'
+ },
+ 'ref': 'refs/heads/master'
+ }
+ response = self.post_json(self.url, data, {'X-GitHub-Event': 'push'})
+ assert response.code == 200
+ assert '"queued": 1' in response.body