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.

64 lines
2.3 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. @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. @patch("hc.front.views.requests.post")
  18. def test_it_handles_oauth_response(self, mock_post):
  19. oauth_response = {
  20. "ok": True,
  21. "team_name": "foo",
  22. "incoming_webhook": {
  23. "url": "http://example.org",
  24. "channel": "bar"
  25. }
  26. }
  27. mock_post.return_value.text = json.dumps(oauth_response)
  28. mock_post.return_value.json.return_value = oauth_response
  29. url = "/integrations/add_slack_btn/?code=12345678"
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.get(url, follow=True)
  32. self.assertRedirects(r, "/integrations/")
  33. self.assertContains(r, "The Slack integration has been added!")
  34. ch = Channel.objects.get()
  35. self.assertEqual(ch.slack_team, "foo")
  36. self.assertEqual(ch.slack_channel, "bar")
  37. self.assertEqual(ch.slack_webhook_url, "http://example.org")
  38. @patch("hc.front.views.requests.post")
  39. def test_it_handles_oauth_error(self, mock_post):
  40. oauth_response = {
  41. "ok": False,
  42. "error": "something went wrong"
  43. }
  44. mock_post.return_value.text = json.dumps(oauth_response)
  45. mock_post.return_value.json.return_value = oauth_response
  46. url = "/integrations/add_slack_btn/?code=12345678"
  47. self.client.login(username="[email protected]", password="password")
  48. r = self.client.get(url, follow=True)
  49. self.assertRedirects(r, "/integrations/")
  50. self.assertContains(r, "something went wrong")