forked from GithubBackups/healthchecks
Project code in URL for the "Add Matrix" page. cc: #336
This commit is contained in:
parent
59f5b7a5f5
commit
f8758e39ea
28
hc/front/tests/test_add_matrix.py
Normal file
28
hc/front/tests/test_add_matrix.py
Normal 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)
|
@ -44,7 +44,6 @@ channel_urls = [
|
|||||||
path("add_whatsapp/", views.add_whatsapp, name="hc-add-whatsapp"),
|
path("add_whatsapp/", views.add_whatsapp, name="hc-add-whatsapp"),
|
||||||
path("add_trello/", views.add_trello, name="hc-add-trello"),
|
path("add_trello/", views.add_trello, name="hc-add-trello"),
|
||||||
path("add_trello/settings/", views.trello_settings, name="hc-trello-settings"),
|
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_apprise/", views.add_apprise, name="hc-add-apprise"),
|
||||||
path("add_msteams/", views.add_msteams, name="hc-add-msteams"),
|
path("add_msteams/", views.add_msteams, name="hc-add-msteams"),
|
||||||
path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"),
|
path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"),
|
||||||
@ -75,6 +74,7 @@ urlpatterns = [
|
|||||||
path("integrations/", include(channel_urls)),
|
path("integrations/", include(channel_urls)),
|
||||||
path("projects/<uuid:code>/integrations/", views.channels, name="hc-p-channels"),
|
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_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("projects/<uuid:code>/add_webhook/", views.add_webhook, name="hc-add-webhook"),
|
||||||
path("docs/", views.serve_doc, name="hc-docs"),
|
path("docs/", views.serve_doc, name="hc-docs"),
|
||||||
path("docs/api/", views.docs_api, name="hc-docs-api"),
|
path("docs/api/", views.docs_api, name="hc-docs-api"),
|
||||||
|
@ -1457,14 +1457,15 @@ def add_trello(request):
|
|||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def add_matrix(request):
|
def add_matrix(request, code):
|
||||||
if settings.MATRIX_ACCESS_TOKEN is None:
|
if settings.MATRIX_ACCESS_TOKEN is None:
|
||||||
raise Http404("matrix integration is not available")
|
raise Http404("matrix integration is not available")
|
||||||
|
|
||||||
|
project = _get_project_for_user(request, code)
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = AddMatrixForm(request.POST)
|
form = AddMatrixForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
channel = Channel(project=request.project, kind="matrix")
|
channel = Channel(project=project, kind="matrix")
|
||||||
channel.value = form.cleaned_data["room_id"]
|
channel.value = form.cleaned_data["room_id"]
|
||||||
|
|
||||||
# If user supplied room alias instead of ID, use it as channel name
|
# 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()
|
channel.assign_all_checks()
|
||||||
messages.success(request, "The Matrix integration has been added!")
|
messages.success(request, "The Matrix integration has been added!")
|
||||||
return redirect("hc-channels")
|
return redirect("hc-p-channels", project.code)
|
||||||
else:
|
else:
|
||||||
form = AddMatrixForm()
|
form = AddMatrixForm()
|
||||||
|
|
||||||
ctx = {
|
ctx = {
|
||||||
"page": "channels",
|
"page": "channels",
|
||||||
"project": request.project,
|
"project": project,
|
||||||
"form": form,
|
"form": form,
|
||||||
"matrix_user_id": settings.MATRIX_USER_ID,
|
"matrix_user_id": settings.MATRIX_USER_ID,
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,7 @@
|
|||||||
<h2>Matrix</h2>
|
<h2>Matrix</h2>
|
||||||
<p>Post notifications to a Matrix room.</p>
|
<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>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user