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.

67 lines
2.2 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(DISCORD_CLIENT_ID="t1", DISCORD_CLIENT_SECRET="s1")
  7. class AddDiscordTestCase(BaseTestCase):
  8. url = "/integrations/add_discord/"
  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 Discord", status_code=200)
  13. self.assertContains(r, "discordapp.com/api/oauth2/authorize")
  14. # There should now be a key in session
  15. self.assertTrue("discord" in self.client.session)
  16. @override_settings(DISCORD_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["discord"] = "foo"
  25. session.save()
  26. oauth_response = {
  27. "access_token": "test-token",
  28. "webhook": {
  29. "url": "foo",
  30. "id": "bar"
  31. }
  32. }
  33. mock_post.return_value.text = json.dumps(oauth_response)
  34. mock_post.return_value.json.return_value = oauth_response
  35. url = self.url + "?code=12345678&state=foo"
  36. self.client.login(username="[email protected]", password="password")
  37. r = self.client.get(url, follow=True)
  38. self.assertRedirects(r, "/integrations/")
  39. self.assertContains(r, "The Discord integration has been added!")
  40. ch = Channel.objects.get()
  41. self.assertEqual(ch.discord_webhook_url, "foo")
  42. # Session should now be clean
  43. self.assertFalse("discord" in self.client.session)
  44. def test_it_avoids_csrf(self):
  45. session = self.client.session
  46. session["discord"] = "foo"
  47. session.save()
  48. url = self.url + "?code=12345678&state=bar"
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.get(url)
  51. self.assertEqual(r.status_code, 400)