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.

89 lines
3.0 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. class AddSlackBtnTestCase(BaseTestCase):
  7. @override_settings(SLACK_CLIENT_ID="foo")
  8. def test_instructions_work(self):
  9. r = self.client.get("/integrations/add_slack/")
  10. self.assertContains(r, "Before adding Slack integration",
  11. status_code=200)
  12. # There should now be a key in session
  13. self.assertTrue("slack" in self.client.session)
  14. @override_settings(SLACK_CLIENT_ID="foo")
  15. def test_slack_button(self):
  16. self.client.login(username="[email protected]", password="password")
  17. r = self.client.get("/integrations/add_slack/")
  18. self.assertContains(r, "slack.com/oauth/authorize", status_code=200)
  19. @patch("hc.front.views.requests.post")
  20. def test_it_handles_oauth_response(self, mock_post):
  21. session = self.client.session
  22. session["slack"] = "foo"
  23. session.save()
  24. oauth_response = {
  25. "ok": True,
  26. "team_name": "foo",
  27. "incoming_webhook": {
  28. "url": "http://example.org",
  29. "channel": "bar"
  30. }
  31. }
  32. mock_post.return_value.text = json.dumps(oauth_response)
  33. mock_post.return_value.json.return_value = oauth_response
  34. url = "/integrations/add_slack_btn/?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 Slack integration has been added!")
  39. ch = Channel.objects.get()
  40. self.assertEqual(ch.slack_team, "foo")
  41. self.assertEqual(ch.slack_channel, "bar")
  42. self.assertEqual(ch.slack_webhook_url, "http://example.org")
  43. # Session should now be clean
  44. self.assertFalse("slack" in self.client.session)
  45. def test_it_avoids_csrf(self):
  46. session = self.client.session
  47. session["slack"] = "foo"
  48. session.save()
  49. url = "/integrations/add_slack_btn/?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)
  53. @patch("hc.front.views.requests.post")
  54. def test_it_handles_oauth_error(self, mock_post):
  55. session = self.client.session
  56. session["slack"] = "foo"
  57. session.save()
  58. oauth_response = {
  59. "ok": False,
  60. "error": "something went wrong"
  61. }
  62. mock_post.return_value.text = json.dumps(oauth_response)
  63. mock_post.return_value.json.return_value = oauth_response
  64. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.get(url, follow=True)
  67. self.assertRedirects(r, "/integrations/")
  68. self.assertContains(r, "something went wrong")