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.

70 lines
2.6 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.status_code = 200
  19. mock_post.return_value.json.return_value = {"room_id": "fake-room-id"}
  20. form = {"alias": "!foo:example.org"}
  21. self.client.login(username="[email protected]", password="password")
  22. r = self.client.post(self.url, form)
  23. self.assertRedirects(r, self.channels_url)
  24. c = Channel.objects.get()
  25. self.assertEqual(c.kind, "matrix")
  26. self.assertEqual(c.value, "fake-room-id")
  27. self.assertEqual(c.project, self.project)
  28. @override_settings(MATRIX_ACCESS_TOKEN=None)
  29. def test_it_requires_access_token(self):
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.get(self.url)
  32. self.assertEqual(r.status_code, 404)
  33. @patch("hc.front.forms.requests.post")
  34. def test_it_handles_429(self, mock_post):
  35. mock_post.return_value.status_code = 429
  36. form = {"alias": "!foo:example.org"}
  37. self.client.login(username="[email protected]", password="password")
  38. r = self.client.post(self.url, form)
  39. self.assertContains(r, "Matrix server returned status code 429")
  40. self.assertFalse(Channel.objects.exists())
  41. @patch("hc.front.forms.requests.post")
  42. def test_it_handles_502(self, mock_post):
  43. mock_post.return_value.status_code = 502
  44. form = {"alias": "!foo:example.org"}
  45. self.client.login(username="[email protected]", password="password")
  46. r = self.client.post(self.url, form)
  47. self.assertContains(r, "Matrix server returned status code 502")
  48. self.assertFalse(Channel.objects.exists())
  49. def test_it_requires_rw_access(self):
  50. self.bobs_membership.role = "r"
  51. self.bobs_membership.save()
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(self.url)
  54. self.assertEqual(r.status_code, 403)