You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.6 KiB

  1. import json
  2. from django.test.utils import override_settings
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. from mock import patch
  6. @override_settings(PUSHBULLET_CLIENT_ID="t1", PUSHBULLET_CLIENT_SECRET="s1")
  7. class AddPushbulletTestCase(BaseTestCase):
  8. url = "/integrations/add_pushbullet/"
  9. @patch("hc.front.views.requests.post")
  10. def test_it_handles_oauth_response(self, mock_post):
  11. session = self.client.session
  12. session["add_pushbullet"] = ("foo", str(self.project.code))
  13. session.save()
  14. oauth_response = {"access_token": "test-token"}
  15. mock_post.return_value.text = json.dumps(oauth_response)
  16. mock_post.return_value.json.return_value = oauth_response
  17. url = self.url + "?code=12345678&state=foo&project=%s" % self.project.code
  18. self.client.login(username="[email protected]", password="password")
  19. r = self.client.get(url, follow=True)
  20. self.assertRedirects(r, self.channels_url)
  21. self.assertContains(r, "The Pushbullet integration has been added!")
  22. ch = Channel.objects.get()
  23. self.assertEqual(ch.value, "test-token")
  24. self.assertEqual(ch.project, self.project)
  25. # Session should now be clean
  26. self.assertFalse("add_pushbullet" in self.client.session)
  27. def test_it_avoids_csrf(self):
  28. session = self.client.session
  29. session["pushbullet"] = ("foo", str(self.project.code))
  30. session.save()
  31. url = self.url + "?code=12345678&state=bar&project=%s" % self.project.code
  32. self.client.login(username="[email protected]", password="password")
  33. r = self.client.get(url)
  34. self.assertEqual(r.status_code, 403)
  35. @patch("hc.front.views.requests.post")
  36. def test_it_handles_denial(self, mock_post):
  37. session = self.client.session
  38. session["add_pushbullet"] = ("foo", str(self.project.code))
  39. session.save()
  40. self.client.login(username="[email protected]", password="password")
  41. r = self.client.get(self.url + "?error=access_denied", follow=True)
  42. self.assertRedirects(r, self.channels_url)
  43. self.assertContains(r, "Pushbullet setup was cancelled")
  44. self.assertEqual(Channel.objects.count(), 0)
  45. # Session should now be clean
  46. self.assertFalse("add_pushbullet" in self.client.session)
  47. @override_settings(PUSHBULLET_CLIENT_ID=None)
  48. def test_it_requires_client_id(self):
  49. url = self.url + "?code=12345678&state=bar&project=%s" % self.project.code
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get(url)
  52. self.assertEqual(r.status_code, 404)