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.

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