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.

92 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. self.assertEqual(ch.project, self.project)
  45. # Session should now be clean
  46. self.assertFalse("slack" in self.client.session)
  47. def test_it_avoids_csrf(self):
  48. session = self.client.session
  49. session["slack"] = "foo"
  50. session.save()
  51. url = "/integrations/add_slack_btn/?code=12345678&state=bar"
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(url)
  54. self.assertEqual(r.status_code, 400)
  55. @patch("hc.front.views.requests.post")
  56. def test_it_handles_oauth_error(self, mock_post):
  57. session = self.client.session
  58. session["slack"] = "foo"
  59. session.save()
  60. oauth_response = {
  61. "ok": False,
  62. "error": "something went wrong"
  63. }
  64. mock_post.return_value.text = json.dumps(oauth_response)
  65. mock_post.return_value.json.return_value = oauth_response
  66. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  67. self.client.login(username="[email protected]", password="password")
  68. r = self.client.get(url, follow=True)
  69. self.assertRedirects(r, "/integrations/")
  70. self.assertContains(r, "something went wrong")