diff --git a/hc/front/tests/test_add_matrix.py b/hc/front/tests/test_add_matrix.py new file mode 100644 index 00000000..0927d7bc --- /dev/null +++ b/hc/front/tests/test_add_matrix.py @@ -0,0 +1,28 @@ +from hc.api.models import Channel +from hc.test import BaseTestCase +from mock import patch + + +class AddMatrixTestCase(BaseTestCase): + def setUp(self): + super(AddMatrixTestCase, self).setUp() + self.url = "/projects/%s/add_matrix/" % self.project.code + + def test_instructions_work(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "Integration Settings", status_code=200) + + @patch("hc.front.forms.requests.post") + def test_it_works(self, mock_post): + mock_post.return_value.json.return_value = {"room_id": "fake-room-id"} + + form = {"alias": "!foo:example.org"} + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.url, form) + self.assertRedirects(r, self.channels_url) + + c = Channel.objects.get() + self.assertEqual(c.kind, "matrix") + self.assertEqual(c.value, "fake-room-id") + self.assertEqual(c.project, self.project) diff --git a/hc/front/urls.py b/hc/front/urls.py index 6800bc18..a8589c4d 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -44,7 +44,6 @@ channel_urls = [ path("add_whatsapp/", views.add_whatsapp, name="hc-add-whatsapp"), path("add_trello/", views.add_trello, name="hc-add-trello"), path("add_trello/settings/", views.trello_settings, name="hc-trello-settings"), - path("add_matrix/", views.add_matrix, name="hc-add-matrix"), path("add_apprise/", views.add_apprise, name="hc-add-apprise"), path("add_msteams/", views.add_msteams, name="hc-add-msteams"), path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"), @@ -75,6 +74,7 @@ urlpatterns = [ path("integrations/", include(channel_urls)), path("projects//integrations/", views.channels, name="hc-p-channels"), path("projects//add_email/", views.add_email, name="hc-add-email"), + path("projects//add_matrix/", views.add_matrix, name="hc-add-matrix"), path("projects//add_webhook/", views.add_webhook, name="hc-add-webhook"), path("docs/", views.serve_doc, name="hc-docs"), path("docs/api/", views.docs_api, name="hc-docs-api"), diff --git a/hc/front/views.py b/hc/front/views.py index d5bf270a..c8b861d6 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -1457,14 +1457,15 @@ def add_trello(request): @login_required -def add_matrix(request): +def add_matrix(request, code): if settings.MATRIX_ACCESS_TOKEN is None: raise Http404("matrix integration is not available") + project = _get_project_for_user(request, code) if request.method == "POST": form = AddMatrixForm(request.POST) if form.is_valid(): - channel = Channel(project=request.project, kind="matrix") + channel = Channel(project=project, kind="matrix") channel.value = form.cleaned_data["room_id"] # If user supplied room alias instead of ID, use it as channel name @@ -1476,13 +1477,13 @@ def add_matrix(request): channel.assign_all_checks() messages.success(request, "The Matrix integration has been added!") - return redirect("hc-channels") + return redirect("hc-p-channels", project.code) else: form = AddMatrixForm() ctx = { "page": "channels", - "project": request.project, + "project": project, "form": form, "matrix_user_id": settings.MATRIX_USER_ID, } diff --git a/templates/front/channels.html b/templates/front/channels.html index 35df0379..55f60ae1 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -223,7 +223,7 @@

Matrix

Post notifications to a Matrix room.

- Add Integration + Add Integration {% endif %}