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.

75 lines
2.8 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. @override_settings(SLACK_CLIENT_ID="fake-client-id")
  7. class AddSlackCompleteTestCase(BaseTestCase):
  8. @patch("hc.front.views.requests.post")
  9. def test_it_handles_oauth_response(self, mock_post):
  10. session = self.client.session
  11. session["add_slack"] = ("foo", str(self.project.code))
  12. session.save()
  13. oauth_response = {
  14. "ok": True,
  15. "team_name": "foo",
  16. "incoming_webhook": {"url": "http://example.org", "channel": "bar"},
  17. }
  18. mock_post.return_value.text = json.dumps(oauth_response)
  19. mock_post.return_value.json.return_value = oauth_response
  20. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  21. self.client.login(username="[email protected]", password="password")
  22. r = self.client.get(url, follow=True)
  23. self.assertRedirects(r, self.channels_url)
  24. self.assertContains(r, "The Slack integration has been added!")
  25. ch = Channel.objects.get()
  26. self.assertEqual(ch.slack_team, "foo")
  27. self.assertEqual(ch.slack_channel, "bar")
  28. self.assertEqual(ch.slack_webhook_url, "http://example.org")
  29. self.assertEqual(ch.project, self.project)
  30. # Session should now be clean
  31. self.assertFalse("add_slack" in self.client.session)
  32. def test_it_avoids_csrf(self):
  33. session = self.client.session
  34. session["add_slack"] = ("foo", str(self.project.code))
  35. session.save()
  36. url = "/integrations/add_slack_btn/?code=12345678&state=bar"
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.get(url)
  39. self.assertEqual(r.status_code, 403)
  40. @patch("hc.front.views.requests.post")
  41. def test_it_handles_oauth_error(self, mock_post):
  42. session = self.client.session
  43. session["add_slack"] = ("foo", str(self.project.code))
  44. session.save()
  45. oauth_response = {"ok": False, "error": "something went wrong"}
  46. mock_post.return_value.text = json.dumps(oauth_response)
  47. mock_post.return_value.json.return_value = oauth_response
  48. url = "/integrations/add_slack_btn/?code=12345678&state=foo"
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.get(url, follow=True)
  51. self.assertRedirects(r, self.channels_url)
  52. self.assertContains(r, "something went wrong")
  53. @override_settings(SLACK_CLIENT_ID=None)
  54. def test_it_requires_client_id(self):
  55. self.client.login(username="[email protected]", password="password")
  56. r = self.client.get("/integrations/add_slack_btn/?code=12345678&state=foo")
  57. self.assertEqual(r.status_code, 404)