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.

89 lines
3.3 KiB

  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. def _get_payload(**kwargs):
  5. payload = {
  6. "bot_email": "[email protected]",
  7. "api_key": "fake-key",
  8. "site": "https://example.org",
  9. "mtype": "stream",
  10. "to": "general",
  11. }
  12. payload.update(kwargs)
  13. return payload
  14. class AddZulipTestCase(BaseTestCase):
  15. def setUp(self):
  16. super().setUp()
  17. self.url = "/projects/%s/add_zulip/" % self.project.code
  18. def test_instructions_work(self):
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.get(self.url)
  21. self.assertContains(r, "open-source group chat app")
  22. def test_it_works(self):
  23. self.client.login(username="[email protected]", password="password")
  24. r = self.client.post(self.url, _get_payload())
  25. self.assertRedirects(r, self.channels_url)
  26. c = Channel.objects.get()
  27. self.assertEqual(c.kind, "zulip")
  28. self.assertEqual(c.zulip_bot_email, "[email protected]")
  29. self.assertEqual(c.zulip_api_key, "fake-key")
  30. self.assertEqual(c.zulip_type, "stream")
  31. self.assertEqual(c.zulip_to, "general")
  32. def test_it_rejects_bad_email(self):
  33. payload = _get_payload(bot_email="not@an@email")
  34. self.client.login(username="[email protected]", password="password")
  35. r = self.client.post(self.url, payload)
  36. self.assertContains(r, "Invalid file format.")
  37. def test_it_rejects_missing_api_key(self):
  38. payload = _get_payload(api_key="")
  39. self.client.login(username="[email protected]", password="password")
  40. r = self.client.post(self.url, payload)
  41. self.assertContains(r, "Invalid file format.")
  42. def test_it_rejects_missing_site(self):
  43. payload = _get_payload(site="")
  44. self.client.login(username="[email protected]", password="password")
  45. r = self.client.post(self.url, payload)
  46. self.assertContains(r, "Invalid file format.")
  47. def test_it_rejects_malformed_site(self):
  48. payload = _get_payload(site="not-an-url")
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.post(self.url, payload)
  51. self.assertContains(r, "Invalid file format.")
  52. def test_it_rejects_bad_mtype(self):
  53. payload = _get_payload(mtype="this-should-not-work")
  54. self.client.login(username="[email protected]", password="password")
  55. r = self.client.post(self.url, payload)
  56. self.assertEqual(r.status_code, 200)
  57. def test_it_rejects_missing_stream_name(self):
  58. payload = _get_payload(to="")
  59. self.client.login(username="[email protected]", password="password")
  60. r = self.client.post(self.url, payload)
  61. self.assertContains(r, "This field is required.")
  62. def test_it_requires_rw_access(self):
  63. self.bobs_membership.rw = False
  64. self.bobs_membership.save()
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.get(self.url)
  67. self.assertEqual(r.status_code, 403)
  68. @override_settings(ZULIP_ENABLED=False)
  69. def test_it_handles_disabled_integration(self):
  70. self.client.login(username="[email protected]", password="password")
  71. r = self.client.get(self.url)
  72. self.assertEqual(r.status_code, 404)