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.

84 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_it_prepares_login_link(self):
  9. r = self.client.get("/integrations/add_slack/")
  10. self.assertContains(r, "Before adding Slack integration", status_code=200)
  11. self.assertContains(r, "?next=/integrations/add_slack/")
  12. @override_settings(SLACK_CLIENT_ID="foo")
  13. def test_slack_button(self):
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.get("/integrations/add_slack/")
  16. self.assertContains(r, "slack.com/oauth/authorize", status_code=200)
  17. # There should now be a key in session
  18. self.assertTrue("slack" in self.client.session)
  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": {"url": "http://example.org", "channel": "bar"},
  28. }
  29. mock_post.return_value.text = json.dumps(oauth_response)
  30. mock_post.return_value.json.return_value = oauth_response
  31. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  32. self.client.login(username="[email protected]", password="password")
  33. r = self.client.get(url, follow=True)
  34. self.assertRedirects(r, "/integrations/")
  35. self.assertContains(r, "The Slack integration has been added!")
  36. ch = Channel.objects.get()
  37. self.assertEqual(ch.slack_team, "foo")
  38. self.assertEqual(ch.slack_channel, "bar")
  39. self.assertEqual(ch.slack_webhook_url, "http://example.org")
  40. self.assertEqual(ch.project, self.project)
  41. # Session should now be clean
  42. self.assertFalse("slack" in self.client.session)
  43. def test_it_avoids_csrf(self):
  44. session = self.client.session
  45. session["slack"] = "foo"
  46. session.save()
  47. url = "/integrations/add_slack_btn/?code=12345678&state=bar"
  48. self.client.login(username="[email protected]", password="password")
  49. r = self.client.get(url)
  50. self.assertEqual(r.status_code, 400)
  51. @patch("hc.front.views.requests.post")
  52. def test_it_handles_oauth_error(self, mock_post):
  53. session = self.client.session
  54. session["slack"] = "foo"
  55. session.save()
  56. oauth_response = {"ok": False, "error": "something went wrong"}
  57. mock_post.return_value.text = json.dumps(oauth_response)
  58. mock_post.return_value.json.return_value = oauth_response
  59. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  60. self.client.login(username="[email protected]", password="password")
  61. r = self.client.get(url, follow=True)
  62. self.assertRedirects(r, "/integrations/")
  63. self.assertContains(r, "something went wrong")