diff --git a/hc/front/tests/test_add_pdc.py b/hc/front/tests/test_add_pdc.py index 01e962b2..3480da36 100644 --- a/hc/front/tests/test_add_pdc.py +++ b/hc/front/tests/test_add_pdc.py @@ -5,7 +5,9 @@ from hc.test import BaseTestCase @override_settings(PD_VENDOR_KEY="foo") class AddPdConnectTestCase(BaseTestCase): - url = "/integrations/add_pdc/" + def setUp(self): + super(AddPdConnectTestCase, self).setUp() + self.url = "/projects/%s/add_pdc/" % self.project.code def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") @@ -19,8 +21,8 @@ class AddPdConnectTestCase(BaseTestCase): self.client.login(username="alice@example.org", password="password") url = self.url + "1234567890AB/?service_key=123" - r = self.client.get(url) - self.assertEqual(r.status_code, 302) + r = self.client.get(url, follow=True) + self.assertRedirects(r, self.channels_url) c = Channel.objects.get() self.assertEqual(c.kind, "pd") @@ -29,7 +31,7 @@ class AddPdConnectTestCase(BaseTestCase): def test_it_validates_code(self): session = self.client.session - session["pd"] = "1234567890AB" # 12 characters + session["pd"] = "1234567890AB" session.save() self.client.login(username="alice@example.org", password="password") diff --git a/hc/front/urls.py b/hc/front/urls.py index 28a30e9d..37150578 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -24,8 +24,6 @@ check_urls = [ channel_urls = [ path("", views.channels, name="hc-channels"), - path("add_pdc/", views.add_pdc, name="hc-add-pdc"), - path("add_pdc//", views.add_pdc, name="hc-add-pdc-state"), path("add_slack/", views.add_slack, name="hc-add-slack"), path("add_slack_btn/", views.add_slack_btn, name="hc-add-slack-btn"), path( @@ -64,6 +62,8 @@ project_urls = [ path("add_pagerteam/", views.add_pagerteam, name="hc-add-pagerteam"), path("add_pagertree/", views.add_pagertree, name="hc-add-pagertree"), path("add_pd/", views.add_pd, name="hc-add-pd"), + path("add_pdc/", views.add_pdc, name="hc-add-pdc"), + path("add_pdc//", views.add_pdc, name="hc-add-pdc-state"), path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"), path("add_pushbullet/", views.add_pushbullet, name="hc-add-pushbullet"), path("add_shell/", views.add_shell, name="hc-add-shell"), diff --git a/hc/front/views.py b/hc/front/views.py index 71548d65..f5463460 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -941,10 +941,12 @@ def add_pd(request, code): return render(request, "integrations/add_pd.html", ctx) -def add_pdc(request, state=None): +def add_pdc(request, code, state=None): if settings.PD_VENDOR_KEY is None: raise Http404("pagerduty integration is not available") + project = _get_project_for_user(request, code) + if state and request.user.is_authenticated: if "pd" not in request.session: return HttpResponseBadRequest() @@ -954,11 +956,10 @@ def add_pdc(request, state=None): return HttpResponseBadRequest() if request.GET.get("error") == "cancelled": - messages.warning(request, "PagerDuty setup was cancelled") - return redirect("hc-channels") + messages.warning(request, "PagerDuty setup was cancelled.") + return redirect("hc-p-channels", project.code) - channel = Channel(kind="pd", project=request.project) - channel.user = request.project.owner + channel = Channel(kind="pd", project=project) channel.value = json.dumps( { "service_key": request.GET.get("service_key"), @@ -968,15 +969,18 @@ def add_pdc(request, state=None): channel.save() channel.assign_all_checks() messages.success(request, "The PagerDuty integration has been added!") - return redirect("hc-channels") + return redirect("hc-p-channels", project.code) - state = _prepare_state(request, "pd") - callback = settings.SITE_ROOT + reverse("hc-add-pdc-state", args=[state]) + state = token_urlsafe() + callback = settings.SITE_ROOT + reverse( + "hc-add-pdc-state", args=[project.code, state] + ) connect_url = "https://connect.pagerduty.com/connect?" + urlencode( {"vendor": settings.PD_VENDOR_KEY, "callback": callback} ) - ctx = {"page": "channels", "project": request.project, "connect_url": connect_url} + ctx = {"page": "channels", "project": project, "connect_url": connect_url} + request.session["pd"] = state return render(request, "integrations/add_pdc.html", ctx) diff --git a/templates/front/channels.html b/templates/front/channels.html index 21faf5e1..2a55a7bd 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -257,7 +257,7 @@

On-call scheduling, alerting, and incident tracking.

{% if enable_pdc %} - Add Integration + Add Integration {% else %} Add Integration {% endif %}