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.

82 lines
3.0 KiB

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