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.

65 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": {"url": "foo", "id": "bar"},
  29. }
  30. mock_post.return_value.text = json.dumps(oauth_response)
  31. mock_post.return_value.json.return_value = oauth_response
  32. url = self.url + "?code=12345678&state=foo"
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.get(url, follow=True)
  35. self.assertRedirects(r, "/integrations/")
  36. self.assertContains(r, "The Discord integration has been added!")
  37. ch = Channel.objects.get()
  38. self.assertEqual(ch.discord_webhook_url, "foo")
  39. self.assertEqual(ch.project, self.project)
  40. # Session should now be clean
  41. self.assertFalse("discord" in self.client.session)
  42. def test_it_avoids_csrf(self):
  43. session = self.client.session
  44. session["discord"] = "foo"
  45. session.save()
  46. url = self.url + "?code=12345678&state=bar"
  47. self.client.login(username="[email protected]", password="password")
  48. r = self.client.get(url)
  49. self.assertEqual(r.status_code, 400)