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.

58 lines
2.2 KiB

  1. from unittest.mock import patch
  2. from django.test.utils import override_settings
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. @override_settings(MATRIX_ACCESS_TOKEN="foo")
  6. @override_settings(MATRIX_HOMESERVER="fake-homeserver")
  7. class AddMatrixTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.url = "/projects/%s/add_matrix/" % self.project.code
  11. @override_settings(MATRIX_ACCESS_TOKEN="foo")
  12. def test_instructions_work(self):
  13. self.client.login(username="[email protected]", password="password")
  14. r = self.client.get(self.url)
  15. self.assertContains(r, "Integration Settings", status_code=200)
  16. @patch("hc.front.forms.requests.post")
  17. def test_it_works(self, mock_post):
  18. mock_post.return_value.json.return_value = {"room_id": "fake-room-id"}
  19. form = {"alias": "!foo:example.org"}
  20. self.client.login(username="[email protected]", password="password")
  21. r = self.client.post(self.url, form)
  22. self.assertRedirects(r, self.channels_url)
  23. c = Channel.objects.get()
  24. self.assertEqual(c.kind, "matrix")
  25. self.assertEqual(c.value, "fake-room-id")
  26. self.assertEqual(c.project, self.project)
  27. @override_settings(MATRIX_ACCESS_TOKEN=None)
  28. def test_it_requires_access_token(self):
  29. self.client.login(username="[email protected]", password="password")
  30. r = self.client.get(self.url)
  31. self.assertEqual(r.status_code, 404)
  32. @patch("hc.front.forms.requests.post")
  33. def test_it_handles_429(self, mock_post):
  34. mock_post.return_value.status_code = 429
  35. form = {"alias": "!foo:example.org"}
  36. self.client.login(username="[email protected]", password="password")
  37. r = self.client.post(self.url, form)
  38. self.assertContains(r, "Matrix server returned status code 429")
  39. self.assertFalse(Channel.objects.exists())
  40. def test_it_requires_rw_access(self):
  41. self.bobs_membership.role = "r"
  42. self.bobs_membership.save()
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.get(self.url)
  45. self.assertEqual(r.status_code, 403)