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.

70 lines
2.5 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 AddSlackTestCase(BaseTestCase):
  7. @override_settings(SLACK_CLIENT_ID=None)
  8. def test_webhook_instructions_work(self):
  9. self.client.login(username="[email protected]", password="password")
  10. r = self.client.get("/integrations/add_slack/")
  11. self.assertContains(r, "Integration Settings", status_code=200)
  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. @override_settings(SLACK_CLIENT_ID="foo")
  18. def test_landing_page(self):
  19. r = self.client.get("/integrations/add_slack/")
  20. self.assertContains(r, "Before adding Slack integration",
  21. status_code=200)
  22. @patch("hc.front.views.requests.post")
  23. def test_it_handles_oauth_response(self, mock_post):
  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"
  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. @patch("hc.front.views.requests.post")
  44. def test_it_handles_oauth_error(self, mock_post):
  45. oauth_response = {
  46. "ok": False,
  47. "error": "something went wrong"
  48. }
  49. mock_post.return_value.text = json.dumps(oauth_response)
  50. mock_post.return_value.json.return_value = oauth_response
  51. url = "/integrations/add_slack_btn/?code=12345678"
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(url, follow=True)
  54. self.assertRedirects(r, "/integrations/")
  55. self.assertContains(r, "something went wrong")