diff --git a/api/tests.py b/api/tests.py index cb56ba96..337abe4b 100755 --- a/api/tests.py +++ b/api/tests.py @@ -188,3 +188,45 @@ class AllowedRepoTests(TestCase): self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/master/metadata.yaml')[0]) self.assertFalse(apimodels.repo_allowed('https://github.com/test/test/raw/master/metadata.json')[0]) +class WebHookTests(TestCase): + def test_travisci_webhook(self): + """ + test of api.views.travisci_webhook + """ + + payload = json.dumps({ + "repository":{ + "id":4651401, + "name":"Adventures-of-Huckleberry-Finn_76", + "owner_name":"GITenberg", + "url":"http://GITenberg.github.com/" + }, + "status_message": "Passed", + "type": "push" + }) + + invalid_payload = json.dumps({ + "repository":{ + "id":4651401, + "name":"", + "url":"http://GITenberg.github.com/" + }, + "status_message": "Passed", + "type": "push" + }) + + url = "/api/travisci/webhook" + + # 200 if a simple get + r = self.client.get(url) + self.assertEqual(r.status_code, 200) + + # 200 when we actually load a valid repo (should we use 201?) + r = self.client.post(url, data={'payload':payload}, headers={}, allow_redirects=True) + self.assertEqual(r.status_code, 200) + + # 400 error if we get exception when trying to load a book + r = self.client.post(url, data={'payload':invalid_payload}, headers={}, allow_redirects=True) + self.assertEqual(r.status_code, 400) + + diff --git a/api/views.py b/api/views.py index 6533f5fe..f37b4e75 100755 --- a/api/views.py +++ b/api/views.py @@ -14,6 +14,7 @@ from django.views.generic.base import View, TemplateView from django.http import ( HttpResponse, HttpResponseNotFound, + HttpResponseBadRequest, HttpResponseRedirect, Http404, ) @@ -95,45 +96,31 @@ def travisci_webhook(request): if request.method == "POST": - #repo_header = request.META.get('HTTP_TRAVIS_REPO_SLUG', '') - - data = json.loads(request.POST.get('payload')) - - # [python - How can I get all the request headers in Django? - Stack Overflow](https://stackoverflow.com/questions/3889769/how-can-i-get-all-the-request-headers-in-django) - import re - regex = re.compile('^HTTP_') - all_headers = dict((regex.sub('', header), value) for (header, value) - in request.META.items() if header.startswith('HTTP_')) + try: + + data = json.loads(request.POST.get('payload')) - # what's the URL to feed to load - # https://github.com/GITenberg/Adventures-of-Huckleberry-Finn_76/raw/master/metadata.yaml - - if data['status_message'] == 'Passed' and data['type'] == 'push': - - logger.info("data.keys():{}".format(data.keys())) - logger.info("data['repository']: {}".format(data['repository'])) - repo_url = "https://github.com/{}/{}/raw/master/metadata.yaml".format(data['repository']['owner_name'], - data['repository']['name']) - try: + # example of URL to feed to yaml loader: + # https://github.com/GITenberg/Adventures-of-Huckleberry-Finn_76/raw/master/metadata.yaml + + if data['status_message'] == 'Passed' and data['type'] == 'push': + + # another way to get owner_name / name would be request.META.get('HTTP_TRAVIS_REPO_SLUG', '') + repo_url = "https://github.com/{}/{}/raw/master/metadata.yaml".format(data['repository']['owner_name'], + data['repository']['name']) + work_id = load_from_yaml(repo_url) - logger.info("work_id: {}".format(work_id)) - return HttpResponse('successful: {}'.format(work_id)) - except Exception as e: - logger.info("exception: {}".format(unicode(e))) - return HttpResponse('unsuccessful: {}'.format(unicode(e))) + return HttpResponse('Successful. work_id: {}'.format(work_id)) + + except Exception as e: + return HttpResponseBadRequest('Unsuccessful. Exception: {}'.format(unicode(e))) else: - return HttpResponse('travisci webhook POST owner_name:{} repo_name: {} type:{} state:{} result:{} status_message:{} commit:{} '.format( - data['repository']['owner_name'], - data['repository']['name'], - data.get('repository', {}).get('name'), - data.get('type'), - data['state'], - data['result'], - data.get('status_message'), - data.get('commit') - )) + return HttpResponse('No action') + + else: + return HttpResponse('No action')