From b5b5c58d778b938b88e673f69d12cc93db66a838 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Thu, 27 Feb 2020 10:28:14 +0200
Subject: [PATCH] Split "Add Pagerduty Connect" in three views for clarity.
---
hc/front/tests/test_add_pdc.py | 17 +------
hc/front/tests/test_add_pdc_complete.py | 26 ++++++++++
hc/front/tests/test_add_pdc_help.py | 21 ++++++++
hc/front/urls.py | 3 +-
hc/front/views.py | 64 +++++++++++++++----------
templates/integrations/add_pdc.html | 22 +--------
6 files changed, 93 insertions(+), 60 deletions(-)
create mode 100644 hc/front/tests/test_add_pdc_complete.py
create mode 100644 hc/front/tests/test_add_pdc_help.py
diff --git a/hc/front/tests/test_add_pdc.py b/hc/front/tests/test_add_pdc.py
index 3480da36..0e19c761 100644
--- a/hc/front/tests/test_add_pdc.py
+++ b/hc/front/tests/test_add_pdc.py
@@ -9,11 +9,6 @@ class AddPdConnectTestCase(BaseTestCase):
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")
- r = self.client.get(self.url)
- self.assertContains(r, "If your team uses")
-
def test_it_works(self):
session = self.client.session
session["pd"] = "1234567890AB" # 12 characters
@@ -29,17 +24,9 @@ class AddPdConnectTestCase(BaseTestCase):
self.assertEqual(c.pd_service_key, "123")
self.assertEqual(c.project, self.project)
- def test_it_validates_code(self):
- session = self.client.session
- session["pd"] = "1234567890AB"
- session.save()
-
- self.client.login(username="alice@example.org", password="password")
- url = self.url + "XXXXXXXXXXXX/?service_key=123"
- r = self.client.get(url)
- self.assertEqual(r.status_code, 400)
-
@override_settings(PD_VENDOR_KEY=None)
def test_it_requires_vendor_key(self):
+ self.client.login(username="alice@example.org", password="password")
+
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)
diff --git a/hc/front/tests/test_add_pdc_complete.py b/hc/front/tests/test_add_pdc_complete.py
new file mode 100644
index 00000000..fffc40ab
--- /dev/null
+++ b/hc/front/tests/test_add_pdc_complete.py
@@ -0,0 +1,26 @@
+from django.test.utils import override_settings
+from hc.test import BaseTestCase
+
+
+@override_settings(PD_VENDOR_KEY="foo")
+class AddPdcCompleteTestCase(BaseTestCase):
+ def setUp(self):
+ super(AddPdcCompleteTestCase, self).setUp()
+ self.url = "/projects/%s/add_pdc/" % self.project.code
+ self.url += "XXXXXXXXXXXX/?service_key=123"
+
+ def test_it_validates_code(self):
+ session = self.client.session
+ session["pd"] = "1234567890AB"
+ session.save()
+
+ self.client.login(username="alice@example.org", password="password")
+ r = self.client.get(self.url)
+ self.assertEqual(r.status_code, 400)
+
+ @override_settings(PD_VENDOR_KEY=None)
+ def test_it_requires_vendor_key(self):
+ self.client.login(username="alice@example.org", password="password")
+
+ r = self.client.get(self.url)
+ self.assertEqual(r.status_code, 404)
diff --git a/hc/front/tests/test_add_pdc_help.py b/hc/front/tests/test_add_pdc_help.py
new file mode 100644
index 00000000..9ac98975
--- /dev/null
+++ b/hc/front/tests/test_add_pdc_help.py
@@ -0,0 +1,21 @@
+from django.test.utils import override_settings
+from hc.test import BaseTestCase
+
+
+@override_settings(PD_VENDOR_KEY="foo")
+class AddPdcHelpTestCase(BaseTestCase):
+ url = "/integrations/add_pdc/"
+
+ def test_instructions_work_when_not_logged_in(self):
+ r = self.client.get(self.url)
+ self.assertContains(r, "Before adding PagerDuty integration, please log")
+
+ def test_instructions_work(self):
+ self.client.login(username="alice@example.org", password="password")
+ r = self.client.get(self.url)
+ self.assertContains(r, "If your team uses")
+
+ @override_settings(PD_VENDOR_KEY=None)
+ def test_it_requires_vendor_key(self):
+ r = self.client.get(self.url)
+ self.assertEqual(r.status_code, 404)
diff --git a/hc/front/urls.py b/hc/front/urls.py
index c1970f10..6cd7e229 100644
--- a/hc/front/urls.py
+++ b/hc/front/urls.py
@@ -31,6 +31,7 @@ channel_urls = [
path("add_discord/", views.add_discord_complete, name="hc-add-discord-complete"),
path("add_pushover/", views.add_pushover_help),
path("telegram/bot/", views.telegram_bot, name="hc-telegram-webhook"),
+ path("add_pdc/", views.add_pdc_help),
path("add_slack/", views.add_slack_help),
path("add_slack_btn/", views.add_slack_complete),
path("add_telegram/", views.add_telegram, name="hc-add-telegram"),
@@ -61,7 +62,7 @@ project_urls = [
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_pdc//", views.add_pdc_complete, name="hc-add-pdc-complete"),
path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"),
path("add_pushbullet/", views.add_pushbullet, name="hc-add-pushbullet"),
path("add_pushover/", views.add_pushover, name="hc-add-pushover"),
diff --git a/hc/front/views.py b/hc/front/views.py
index 636210aa..81a505bb 100644
--- a/hc/front/views.py
+++ b/hc/front/views.py
@@ -925,39 +925,24 @@ def add_pd(request, code):
return render(request, "integrations/add_pd.html", ctx)
-def add_pdc(request, code, state=None):
+def add_pdc_help(request):
if settings.PD_VENDOR_KEY is None:
raise Http404("pagerduty integration is not available")
- project = _get_project_for_user(request, code)
+ ctx = {"page": "channels"}
+ return render(request, "integrations/add_pdc.html", ctx)
- if state and request.user.is_authenticated:
- if "pd" not in request.session:
- return HttpResponseBadRequest()
- session_state = request.session.pop("pd")
- if session_state != state:
- return HttpResponseBadRequest()
-
- if request.GET.get("error") == "cancelled":
- messages.warning(request, "PagerDuty setup was cancelled.")
- return redirect("hc-p-channels", project.code)
+@login_required
+def add_pdc(request, code):
+ if settings.PD_VENDOR_KEY is None:
+ raise Http404("pagerduty integration is not available")
- channel = Channel(kind="pd", project=project)
- channel.value = json.dumps(
- {
- "service_key": request.GET.get("service_key"),
- "account": request.GET.get("account"),
- }
- )
- channel.save()
- channel.assign_all_checks()
- messages.success(request, "The PagerDuty integration has been added!")
- return redirect("hc-p-channels", project.code)
+ project = _get_project_for_user(request, code)
state = token_urlsafe()
callback = settings.SITE_ROOT + reverse(
- "hc-add-pdc-state", args=[project.code, state]
+ "hc-add-pdc-complete", args=[project.code, state]
)
connect_url = "https://connect.pagerduty.com/connect?" + urlencode(
{"vendor": settings.PD_VENDOR_KEY, "callback": callback}
@@ -968,6 +953,37 @@ def add_pdc(request, code, state=None):
return render(request, "integrations/add_pdc.html", ctx)
+@login_required
+def add_pdc_complete(request, code, state):
+ if settings.PD_VENDOR_KEY is None:
+ raise Http404("pagerduty integration is not available")
+
+ if "pd" not in request.session:
+ return HttpResponseBadRequest()
+
+ project = _get_project_for_user(request, code)
+
+ session_state = request.session.pop("pd")
+ if session_state != state:
+ return HttpResponseBadRequest()
+
+ if request.GET.get("error") == "cancelled":
+ messages.warning(request, "PagerDuty setup was cancelled.")
+ return redirect("hc-p-channels", project.code)
+
+ channel = Channel(kind="pd", project=project)
+ channel.value = json.dumps(
+ {
+ "service_key": request.GET.get("service_key"),
+ "account": request.GET.get("account"),
+ }
+ )
+ channel.save()
+ channel.assign_all_checks()
+ messages.success(request, "The PagerDuty integration has been added!")
+ return redirect("hc-p-channels", project.code)
+
+
@login_required
def add_pagertree(request, code):
project = _get_project_for_user(request, code)
diff --git a/templates/integrations/add_pdc.html b/templates/integrations/add_pdc.html
index c0d5be69..5feefeaa 100644
--- a/templates/integrations/add_pdc.html
+++ b/templates/integrations/add_pdc.html
@@ -35,26 +35,8 @@
{% site_name %}:
{% endif %}