daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest May 31st, 2015 272 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # VIEW
  2.  
  3. import json
  4.  
  5. from django.shortcuts import get_object_or_404
  6. from django.http import HttpResponse
  7.  
  8. from repos.models import Repo
  9. from repos import tasks
  10.  
  11. def github_webhook(request):
  12.     event_type = request.META['X-GITHUB-EVENT']
  13.     payload = json.loads(request.body)
  14.     pr_number = payload['number']
  15.     name = payload['pull_request']['base']['repo']['full_name']
  16.  
  17.     gh_repo = get_object_or_404(Repo, name=name, enabled=True, deleted=False)
  18.     print 'in the view: %s' % tasks.lintRepo.delay
  19.     tasks.lintRepo.delay(name, pr_number, gh_repo.linters)
  20.     return HttpResponse(status=201)
  21.  
  22.  
  23. # TESTS
  24.  
  25. import os.path
  26.  
  27. from django.test import TestCase, Client
  28. from django.core.urlresolvers import reverse
  29. from mock import patch
  30.  
  31. from repos.factories import RepoFactory
  32.  
  33. class GitHubWebhookTests(TestCase):
  34.     @classmethod
  35.     def setUpClass(cls):
  36.         super(GitHubWebhookTests, cls).setUpClass()
  37.         with open(os.path.join(os.path.dirname(__file__), 'fixtures/github_pr.json')) as f:
  38.             cls.github_pr_json = f.read()
  39.  
  40.  
  41.     def test_sends_relevant_info_to_task(self):
  42.         print ""
  43.         RepoFactory.create(name='baxterthehacker/public-repo')
  44.         c = Client()
  45.  
  46.         with patch('repos.tasks') as mock_tasks:
  47.             print 'sends relevent before post: %s' % mock_tasks.lintRepo.delay
  48.             c.post(
  49.                 reverse('github_webhook'),
  50.                 content_type='application/json',
  51.                 data=self.github_pr_json, **{
  52.                     'X-GITHUB-EVENT': 'pull_request'
  53.                 }
  54.             )
  55.             print 'sends relevent after post: %s' % mock_tasks.lintRepo.delay
  56.  
  57.             self.assertTrue(mock_tasks.lintRepo.delay.called)
  58.             mock_tasks.lintRepo.delay.assert_called_with(
  59.                 'baxterthehacker/public-repo', 1, '[]')
  60.            
  61.  
  62.     def test_404s_if_repo_doesnt_exist(self):
  63.         print ""
  64.         c = Client()
  65.         with patch('repos.tasks') as mock_tasks:
  66.             print '404 before post: %s' % mock_tasks.lintRepo.delay.id
  67.             resp = c.post(
  68.                 reverse('github_webhook'),
  69.                 content_type='application/json',
  70.                 data=self.github_pr_json, **{
  71.                     'X-GITHUB-EVENT': 'pull_request'
  72.                 }
  73.             )
  74.             print '404 after post: %s' % mock_tasks.lintRepo.delay.id
  75.             self.assertEqual(resp.status_code, 404)
  76.    
  77.  
  78. # ERRORS
  79.  
  80. -*- mode: compilation; default-directory: "~/src/justin.abrah.ms/betterdiff-django/" -*-
  81. Compilation started at Sun May 31 00:03:43
  82.  
  83. make test
  84. cd betterdiff && ./manage.py test --pattern="*_test.py" --verbosity=1
  85. Creating test database for alias 'default'...
  86. .
  87. 404 before post: <MagicMock name='tasks.lintRepo.delay' id='139714623964624'>
  88. 404 after post: <MagicMock name='tasks.lintRepo.delay' id='139714623964624'>
  89. .
  90. sends relevent before post: <MagicMock name='tasks.lintRepo.delay' id='139714623744272'>
  91. in the view: <MagicMock name='tasks.lintRepo.delay' id='139714623964624'>
  92. sends relevent after post: <MagicMock name='tasks.lintRepo.delay' id='139714623744272'>
  93. F
  94. ======================================================================
  95. FAIL: test_sends_relevant_info_to_task (repos.views_test.GitHubWebhookTests)
  96. ----------------------------------------------------------------------
  97. Traceback (most recent call last):
  98.   File "/home/jabrahms/src/justin.abrah.ms/betterdiff-django/betterdiff/repos/views_test.py", line 33, in test_sends_relevant_info_to_task
  99.     self.assertTrue(mock_tasks.lintRepo.delay.called)
  100. AssertionError: False is not true
  101.  
  102. ----------------------------------------------------------------------
  103. Ran 3 tests in 0.017s
  104.  
  105. FAILED (failures=1)
  106. Destroying test database for alias 'default'...
  107. Makefile:2: recipe for target 'test' failed
  108. make: *** [test] Error 1
  109.  
  110. Compilation exited abnormally with code 2 at Sun May 31 00:03:43
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top