Project code in URL for the "Add Matrix" page. cc: #336

This commit is contained in:
Pēteris Caune 2020-02-21 14:44:44 +02:00
parent 59f5b7a5f5
commit f8758e39ea
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
4 changed files with 35 additions and 6 deletions

View File

@ -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)

View File

@ -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/<uuid:code>/integrations/", views.channels, name="hc-p-channels"),
path("projects/<uuid:code>/add_email/", views.add_email, name="hc-add-email"),
path("projects/<uuid:code>/add_matrix/", views.add_matrix, name="hc-add-matrix"),
path("projects/<uuid:code>/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"),

View File

@ -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,
}

View File

@ -223,7 +223,7 @@
<h2>Matrix</h2>
<p>Post notifications to a Matrix room.</p>
<a href="{% url 'hc-add-matrix' %}" class="btn btn-primary">Add Integration</a>
<a href="{% url 'hc-add-matrix' project.code %}" class="btn btn-primary">Add Integration</a>
</li>
{% endif %}