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.

61 lines
2.1 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. def test_instructions_work(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertContains(r, "www.pushbullet.com/authorize", status_code=200)
  13. self.assertContains(r, "Connect Pushbullet")
  14. # There should now be a key in session
  15. self.assertTrue("pushbullet" in self.client.session)
  16. @override_settings(PUSHBULLET_CLIENT_ID=None)
  17. def test_it_requires_client_id(self):
  18. self.client.login(username="[email protected]", password="password")
  19. r = self.client.get(self.url)
  20. self.assertEqual(r.status_code, 404)
  21. @patch("hc.front.views.requests.post")
  22. def test_it_handles_oauth_response(self, mock_post):
  23. session = self.client.session
  24. session["pushbullet"] = "foo"
  25. session.save()
  26. oauth_response = {"access_token": "test-token"}
  27. mock_post.return_value.text = json.dumps(oauth_response)
  28. mock_post.return_value.json.return_value = oauth_response
  29. url = self.url + "?code=12345678&state=foo"
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.get(url, follow=True)
  32. self.assertRedirects(r, "/integrations/")
  33. self.assertContains(r, "The Pushbullet integration has been added!")
  34. ch = Channel.objects.get()
  35. self.assertEqual(ch.value, "test-token")
  36. # Session should now be clean
  37. self.assertFalse("pushbullet" in self.client.session)
  38. def test_it_avoids_csrf(self):
  39. session = self.client.session
  40. session["pushbullet"] = "foo"
  41. session.save()
  42. url = self.url + "?code=12345678&state=bar"
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.get(url)
  45. self.assertEqual(r.status_code, 400)