Browse Source

Split "Add Pagerduty Connect" in three views for clarity.

pull/340/head
Pēteris Caune 5 years ago
parent
commit
b5b5c58d77
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
6 changed files with 93 additions and 60 deletions
  1. +2
    -15
      hc/front/tests/test_add_pdc.py
  2. +26
    -0
      hc/front/tests/test_add_pdc_complete.py
  3. +21
    -0
      hc/front/tests/test_add_pdc_help.py
  4. +2
    -1
      hc/front/urls.py
  5. +40
    -24
      hc/front/views.py
  6. +2
    -20
      templates/integrations/add_pdc.html

+ 2
- 15
hc/front/tests/test_add_pdc.py View File

@ -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="[email protected]", 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="[email protected]", 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="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)

+ 26
- 0
hc/front/tests/test_add_pdc_complete.py View File

@ -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="[email protected]", 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="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)

+ 21
- 0
hc/front/tests/test_add_pdc_help.py View File

@ -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="[email protected]", 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)

+ 2
- 1
hc/front/urls.py View File

@ -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/<str:state>/", views.add_pdc, name="hc-add-pdc-state"),
path("add_pdc/<str:state>/", 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"),


+ 40
- 24
hc/front/views.py View File

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


+ 2
- 20
templates/integrations/add_pdc.html View File

@ -35,26 +35,8 @@
{% site_name %}:</p>
<div class="text-center">
<form class="form-inline" action="{% url 'hc-login' %}" method="post">
{% csrf_token %}
<div class="form-group">
<div class="input-group input-group-lg">
<div class="input-group-addon">@</div>
<input
type="email"
class="form-control"
name="email"
autocomplete="email"
placeholder="Email">
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-lg btn-primary pull-right">
Log In
</button>
</div>
</form>
<a href="{% url 'hc-login' %}"
class="btn btn-primary btn-lg">Sign In</a>
</div>
{% endif %}
</div>


Loading…
Cancel
Save