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.

51 lines
1.9 KiB

  1. from json import JSONDecodeError
  2. from unittest.mock import patch
  3. from django.test.utils import override_settings
  4. from hc.api.models import Channel
  5. from hc.test import BaseTestCase
  6. @override_settings(MATRIX_ACCESS_TOKEN="foo")
  7. @override_settings(MATRIX_HOMESERVER="fake-homeserver")
  8. class AddMatrixTestCase(BaseTestCase):
  9. def setUp(self):
  10. super(AddMatrixTestCase, self).setUp()
  11. self.url = "/projects/%s/add_matrix/" % self.project.code
  12. @override_settings(MATRIX_ACCESS_TOKEN="foo")
  13. def test_instructions_work(self):
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.get(self.url)
  16. self.assertContains(r, "Integration Settings", status_code=200)
  17. @patch("hc.front.forms.requests.post")
  18. def test_it_works(self, mock_post):
  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())