Browse Source

Add ability to edit existing SMS integrations

pull/563/head
Pēteris Caune 3 years ago
parent
commit
3807c200ce
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
8 changed files with 100 additions and 17 deletions
  1. +1
    -1
      CHANGELOG.md
  2. +1
    -1
      hc/api/models.py
  3. +7
    -2
      hc/front/tests/test_add_sms.py
  4. +57
    -0
      hc/front/tests/test_edit_sms.py
  5. +1
    -2
      hc/front/tests/test_edit_webhook.py
  6. +1
    -1
      hc/front/urls.py
  7. +25
    -9
      hc/front/views.py
  8. +7
    -1
      templates/integrations/sms_form.html

+ 1
- 1
CHANGELOG.md View File

@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
### Improvements
- Add /api/v1/badges/ endpoint (#552)
- Add ability to edit existing email integrations
- Add ability to edit existing email, SMS integrations
### Bug Fixes
- Add handling for non-latin-1 characters in webhook headers


+ 1
- 1
hc/api/models.py View File

@ -416,7 +416,7 @@ class Channel(models.Model):
return {"id": str(self.code), "name": self.name, "kind": self.kind}
def is_editable(self):
return self.kind in ("email", "webhook")
return self.kind in ("email", "webhook", "sms")
def assign_all_checks(self):
checks = Check.objects.filter(project=self.project)


+ 7
- 2
hc/front/tests/test_add_sms.py View File

@ -1,5 +1,5 @@
from django.test.utils import override_settings
from hc.api.models import Channel
from hc.api.models import Channel, Check
from hc.test import BaseTestCase
@ -7,11 +7,13 @@ from hc.test import BaseTestCase
class AddSmsTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.url = "/projects/%s/add_sms/" % self.project.code
self.check = Check.objects.create(project=self.project)
self.url = f"/projects/{self.project.code}/add_sms/"
def test_instructions_work(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "Add SMS Integration")
self.assertContains(r, "Get a SMS message")
@override_settings(USE_PAYMENTS=True)
@ -38,6 +40,9 @@ class AddSmsTestCase(BaseTestCase):
self.assertFalse(c.sms_notify_up)
self.assertEqual(c.project, self.project)
# Make sure it calls assign_all_checks
self.assertEqual(c.checks.count(), 1)
def test_it_rejects_bad_number(self):
for v in ["not a phone number address", False, 15, "+123456789A"]:
form = {"phone": v}


+ 57
- 0
hc/front/tests/test_edit_sms.py View File

@ -0,0 +1,57 @@
import json
from django.test.utils import override_settings
from hc.api.models import Channel, Check
from hc.test import BaseTestCase
@override_settings(TWILIO_ACCOUNT="foo", TWILIO_AUTH="foo", TWILIO_FROM="123")
class AddSmsTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.check = Check.objects.create(project=self.project)
self.channel = Channel(project=self.project, kind="sms")
self.channel.value = json.dumps(
{"value": "+12345678", "up": True, "down": True}
)
self.channel.save()
self.url = f"/integrations/{self.channel.code}/edit/"
def test_instructions_work(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "SMS Settings")
self.assertContains(r, "Get a SMS message")
self.assertContains(r, "+12345678")
def test_it_updates_channel(self):
form = {"label": "My Phone", "phone": "+1234567890", "down": True}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
self.channel.refresh_from_db()
self.assertEqual(self.channel.phone_number, "+1234567890")
self.assertEqual(self.channel.name, "My Phone")
self.assertTrue(self.channel.sms_notify_down)
self.assertFalse(self.channel.sms_notify_up)
# Make sure it does not call assign_all_checks
self.assertFalse(self.channel.checks.exists())
@override_settings(TWILIO_AUTH=None)
def test_it_requires_credentials(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 404)
def test_it_requires_rw_access(self):
self.bobs_membership.role = "r"
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)

+ 1
- 2
hc/front/tests/test_edit_webhook.py View File

@ -80,8 +80,7 @@ class EditWebhookTestCase(BaseTestCase):
self.assertFalse(self.channel.checks.exists())
def test_it_requires_kind_webhook(self):
self.channel.kind = "sms"
self.channel.value = "[email protected]"
self.channel.kind = "shell"
self.channel.save()
self.client.login(username="[email protected]", password="password")


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

@ -70,7 +70,7 @@ project_urls = [
path("add_signal/", views.add_signal, name="hc-add-signal"),
path("add_slack/", views.add_slack, name="hc-add-slack"),
path("add_slack_btn/", views.add_slack_btn, name="hc-add-slack-btn"),
path("add_sms/", views.add_sms, name="hc-add-sms"),
path("add_sms/", views.sms_form, name="hc-add-sms"),
path("add_spike/", views.add_spike, name="hc-add-spike"),
path("add_trello/", views.add_trello, name="hc-add-trello"),
path("add_victorops/", views.add_victorops, name="hc-add-victorops"),


+ 25
- 9
hc/front/views.py View File

@ -1012,6 +1012,8 @@ def edit_channel(request, code):
return email_form(request, channel=channel)
if channel.kind == "webhook":
return webhook_form(request, channel=channel)
if channel.kind == "sms":
return sms_form(request, channel=channel)
return HttpResponseBadRequest()
@ -1626,28 +1628,42 @@ def add_telegram(request):
@require_setting("TWILIO_AUTH")
@login_required
def add_sms(request, code):
project = _get_rw_project_for_user(request, code)
def sms_form(request, channel=None, code=None):
is_new = channel is None
if is_new:
project = _get_rw_project_for_user(request, code)
channel = Channel(project=project, kind="sms")
if request.method == "POST":
form = forms.PhoneUpDownForm(request.POST)
if form.is_valid():
channel = Channel(project=project, kind="sms")
channel.name = form.cleaned_data["label"]
channel.value = form.get_json()
channel.save()
channel.assign_all_checks()
return redirect("hc-channels", project.code)
else:
if is_new:
channel.assign_all_checks()
return redirect("hc-channels", channel.project.code)
elif is_new:
form = forms.PhoneUpDownForm(initial={"up": False})
else:
form = forms.PhoneUpDownForm(
{
"label": channel.name,
"phone": channel.phone_number,
"up": channel.sms_notify_up,
"down": channel.sms_notify_down,
}
)
ctx = {
"page": "channels",
"project": project,
"project": channel.project,
"form": form,
"profile": project.owner_profile,
"profile": channel.project.owner_profile,
"is_new": is_new,
}
return render(request, "integrations/add_sms.html", ctx)
return render(request, "integrations/sms_form.html", ctx)
@require_setting("TWILIO_AUTH")


templates/integrations/add_sms.html → templates/integrations/sms_form.html View File


Loading…
Cancel
Save