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.

68 lines
2.5 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(ZENDESK_CLIENT_ID="t1", ZENDESK_CLIENT_SECRET="s1")
  7. class AddZendeskTestCase(BaseTestCase):
  8. url = "/integrations/add_zendesk/"
  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, "Connect Zendesk Support", status_code=200)
  13. def test_post_works(self):
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.post(self.url, {"subdomain": "foo"})
  16. self.assertEqual(r.status_code, 302)
  17. self.assertTrue("foo.zendesk.com" in r["Location"])
  18. # There should now be a key in session
  19. self.assertTrue("zendesk" in self.client.session)
  20. @override_settings(ZENDESK_CLIENT_ID=None)
  21. def test_it_requires_client_id(self):
  22. self.client.login(username="[email protected]", password="password")
  23. r = self.client.get(self.url)
  24. self.assertEqual(r.status_code, 404)
  25. @patch("hc.front.views.requests.post")
  26. def test_it_handles_oauth_response(self, mock_post):
  27. session = self.client.session
  28. session["zendesk"] = "foo"
  29. session["subdomain"] = "foodomain"
  30. session.save()
  31. oauth_response = {"access_token": "test-token"}
  32. mock_post.return_value.text = json.dumps(oauth_response)
  33. mock_post.return_value.json.return_value = oauth_response
  34. url = self.url + "?code=12345678&state=foo"
  35. self.client.login(username="[email protected]", password="password")
  36. r = self.client.get(url, follow=True)
  37. self.assertRedirects(r, "/integrations/")
  38. self.assertContains(r, "The Zendesk integration has been added!")
  39. ch = Channel.objects.get()
  40. self.assertEqual(ch.zendesk_token, "test-token")
  41. self.assertEqual(ch.zendesk_subdomain, "foodomain")
  42. # Session should now be clean
  43. self.assertFalse("zendesk" in self.client.session)
  44. self.assertFalse("subdomain" in self.client.session)
  45. def test_it_avoids_csrf(self):
  46. session = self.client.session
  47. session["zendesk"] = "foo"
  48. session.save()
  49. url = self.url + "?code=12345678&state=bar"
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get(url)
  52. self.assertEqual(r.status_code, 400)