Browse Source

Add ability to edit existing WhatsApp integrations

pull/563/head
Pēteris Caune 3 years ago
parent
commit
8541ec59ca
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
9 changed files with 127 additions and 20 deletions
  1. +1
    -1
      CHANGELOG.md
  2. +1
    -1
      hc/api/models.py
  3. +7
    -2
      hc/front/tests/test_add_whatsapp.py
  4. +4
    -4
      hc/front/tests/test_edit_signal.py
  5. +1
    -1
      hc/front/tests/test_edit_sms.py
  6. +80
    -0
      hc/front/tests/test_edit_whatsapp.py
  7. +1
    -1
      hc/front/urls.py
  8. +25
    -9
      hc/front/views.py
  9. +7
    -1
      templates/integrations/whatsapp_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, Signal, SMS integrations
- Add ability to edit existing email, Signal, SMS, WhatsApp 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", "sms", "signal")
return self.kind in ("email", "webhook", "sms", "signal", "whatsapp")
def assign_all_checks(self):
checks = Check.objects.filter(project=self.project)


+ 7
- 2
hc/front/tests/test_add_whatsapp.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
TEST_CREDENTIALS = {
@ -14,11 +14,13 @@ TEST_CREDENTIALS = {
class AddWhatsAppTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.url = "/projects/%s/add_whatsapp/" % self.project.code
self.check = Check.objects.create(project=self.project)
self.url = f"/projects/{self.project.code}/add_whatsapp/"
def test_instructions_work(self):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "Add WhatsApp Integration")
self.assertContains(r, "Get a WhatsApp message")
@override_settings(USE_PAYMENTS=True)
@ -50,6 +52,9 @@ class AddWhatsAppTestCase(BaseTestCase):
self.assertTrue(c.whatsapp_notify_up)
self.assertEqual(c.project, self.project)
# Make sure it calls assign_all_checks
self.assertEqual(c.checks.count(), 1)
def test_it_obeys_up_down_flags(self):
form = {"label": "My Phone", "phone": "+1234567890"}


+ 4
- 4
hc/front/tests/test_edit_signal.py View File

@ -6,7 +6,7 @@ from hc.test import BaseTestCase
@override_settings(SIGNAL_CLI_ENABLED=True)
class AddSignalTestCase(BaseTestCase):
class EditSignalTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.check = Check.objects.create(project=self.project)
@ -54,9 +54,9 @@ class AddSignalTestCase(BaseTestCase):
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertFalse(c.signal_notify_down)
self.assertFalse(c.signal_notify_up)
self.channel.refresh_from_db()
self.assertFalse(self.channel.signal_notify_down)
self.assertFalse(self.channel.signal_notify_up)
@override_settings(SIGNAL_CLI_ENABLED=False)
def test_it_handles_disabled_integration(self):


+ 1
- 1
hc/front/tests/test_edit_sms.py View File

@ -6,7 +6,7 @@ from hc.test import BaseTestCase
@override_settings(TWILIO_ACCOUNT="foo", TWILIO_AUTH="foo", TWILIO_FROM="123")
class AddSmsTestCase(BaseTestCase):
class EditSmsTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.check = Check.objects.create(project=self.project)


+ 80
- 0
hc/front/tests/test_edit_whatsapp.py View File

@ -0,0 +1,80 @@
import json
from django.test.utils import override_settings
from hc.api.models import Channel, Check
from hc.test import BaseTestCase
TEST_CREDENTIALS = {
"TWILIO_ACCOUNT": "foo",
"TWILIO_AUTH": "foo",
"TWILIO_FROM": "123",
"TWILIO_USE_WHATSAPP": True,
}
@override_settings(**TEST_CREDENTIALS)
class EditWhatsAppTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.check = Check.objects.create(project=self.project)
self.channel = Channel(project=self.project, kind="whatsapp")
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, "WhatsApp Settings")
self.assertContains(r, "Get a WhatsApp message")
self.assertContains(r, "+12345678")
def test_it_updates_channel(self):
form = {
"label": "My Phone",
"phone": "+1234567890",
"down": "true",
"up": "false",
}
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.whatsapp_notify_down)
self.assertFalse(self.channel.whatsapp_notify_up)
# Make sure it does not call assign_all_checks
self.assertFalse(self.channel.checks.exists())
def test_it_obeys_up_down_flags(self):
form = {"label": "My Phone", "phone": "+1234567890"}
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.assertFalse(self.channel.whatsapp_notify_down)
self.assertFalse(self.channel.whatsapp_notify_up)
@override_settings(TWILIO_USE_WHATSAPP=False)
def test_it_obeys_use_whatsapp_flag(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
- 1
hc/front/urls.py View File

@ -75,7 +75,7 @@ project_urls = [
path("add_trello/", views.add_trello, name="hc-add-trello"),
path("add_victorops/", views.add_victorops, name="hc-add-victorops"),
path("add_webhook/", views.webhook_form, name="hc-add-webhook"),
path("add_whatsapp/", views.add_whatsapp, name="hc-add-whatsapp"),
path("add_whatsapp/", views.whatsapp_form, name="hc-add-whatsapp"),
path("add_zulip/", views.add_zulip, name="hc-add-zulip"),
path("badges/", views.badges, name="hc-badges"),
path("checks/", views.my_checks, name="hc-checks"),


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

@ -1016,6 +1016,8 @@ def edit_channel(request, code):
return sms_form(request, channel=channel)
if channel.kind == "signal":
return signal_form(request, channel=channel)
if channel.kind == "whatsapp":
return whatsapp_form(request, channel=channel)
return HttpResponseBadRequest()
@ -1696,28 +1698,42 @@ def add_call(request, code):
@require_setting("TWILIO_USE_WHATSAPP")
@login_required
def add_whatsapp(request, code):
project = _get_rw_project_for_user(request, code)
def whatsapp_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="whatsapp")
if request.method == "POST":
form = forms.PhoneUpDownForm(request.POST)
if form.is_valid():
channel = Channel(project=project, kind="whatsapp")
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()
else:
form = forms.PhoneUpDownForm(
{
"label": channel.name,
"phone": channel.phone_number,
"up": channel.whatsapp_notify_up,
"down": channel.whatsapp_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_whatsapp.html", ctx)
return render(request, "integrations/whatsapp_form.html", ctx)
@require_setting("SIGNAL_CLI_ENABLED")


templates/integrations/add_whatsapp.html → templates/integrations/whatsapp_form.html View File


Loading…
Cancel
Save