Browse Source

Improve up/down flag validation

In SMS, Signal and WhatsApp forms, reject the form if
user unchecks both "alert when check goes DOWN" and
"alert when check goes UP".
pull/563/head
Pēteris Caune 3 years ago
parent
commit
6f5a22fd98
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
10 changed files with 73 additions and 66 deletions
  1. +9
    -0
      hc/front/forms.py
  2. +17
    -11
      hc/front/tests/test_add_signal.py
  3. +16
    -13
      hc/front/tests/test_add_sms.py
  4. +17
    -15
      hc/front/tests/test_add_whatsapp.py
  5. +0
    -11
      hc/front/tests/test_edit_signal.py
  6. +0
    -11
      hc/front/tests/test_edit_whatsapp.py
  7. +1
    -1
      templates/integrations/email_form.html
  8. +6
    -1
      templates/integrations/signal_form.html
  9. +1
    -2
      templates/integrations/sms_form.html
  10. +6
    -1
      templates/integrations/whatsapp_form.html

+ 9
- 0
hc/front/forms.py View File

@ -232,6 +232,15 @@ class PhoneUpDownForm(PhoneNumberForm):
up = forms.BooleanField(required=False, initial=True)
down = forms.BooleanField(required=False, initial=True)
def clean(self):
super().clean()
down = self.cleaned_data.get("down")
up = self.cleaned_data.get("up")
if not down and not up:
self.add_error("down", "Please select at least one.")
def get_json(self):
return json.dumps(
{


+ 17
- 11
hc/front/tests/test_add_signal.py View File

@ -39,17 +39,6 @@ class AddSignalTestCase(BaseTestCase):
# 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"}
self.client.login(username="[email protected]", password="password")
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)
@override_settings(SIGNAL_CLI_ENABLED=False)
def test_it_handles_disabled_integration(self):
self.client.login(username="[email protected]", password="password")
@ -63,3 +52,20 @@ class AddSignalTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
def test_it_handles_down_false_up_true(self):
form = {"phone": "+1234567890", "up": True}
self.client.login(username="[email protected]", password="password")
self.client.post(self.url, form)
c = Channel.objects.get()
self.assertFalse(c.signal_notify_down)
self.assertTrue(c.signal_notify_up)
def test_it_rejects_unchecked_up_and_down(self):
form = {"phone": "+1234567890"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Please select at least one.")

+ 16
- 13
hc/front/tests/test_add_sms.py View File

@ -44,14 +44,14 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(c.checks.count(), 1)
def test_it_rejects_bad_number(self):
for v in ["not a phone number address", False, 15, "+123456789A"]:
for v in ["not a phone number", False, 15, "+123456789A"]:
form = {"phone": v}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Invalid phone number format.")
def test_it_trims_whitespace(self):
form = {"phone": " +1234567890 "}
form = {"phone": " +1234567890 ", "down": True}
self.client.login(username="[email protected]", password="password")
self.client.post(self.url, form)
@ -74,7 +74,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(r.status_code, 403)
def test_it_strips_invisible_formatting_characters(self):
form = {"label": "My Phone", "phone": "\u202c+1234567890\u202c"}
form = {"phone": "\u202c+1234567890\u202c", "down": True}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
@ -84,7 +84,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_hyphens(self):
form = {"label": "My Phone", "phone": "+123-4567890"}
form = {"phone": "+123-4567890", "down": True}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
@ -94,7 +94,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_spaces(self):
form = {"label": "My Phone", "phone": "+123 45 678 90"}
form = {"phone": "+123 45 678 90", "down": True}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
@ -103,16 +103,19 @@ class AddSmsTestCase(BaseTestCase):
c = Channel.objects.get()
self.assertEqual(c.phone_number, "+1234567890")
def test_it_obeys_up_down_flags(self):
form = {"label": "My Phone", "phone": "+1234567890"}
def test_it_handles_down_false_up_true(self):
form = {"phone": "+1234567890", "up": True}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
self.client.post(self.url, form)
c = Channel.objects.get()
self.assertEqual(c.kind, "sms")
self.assertEqual(c.phone_number, "+1234567890")
self.assertEqual(c.name, "My Phone")
self.assertFalse(c.sms_notify_down)
self.assertFalse(c.sms_notify_up)
self.assertTrue(c.sms_notify_up)
def test_it_rejects_unchecked_up_and_down(self):
form = {"phone": "+1234567890"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Please select at least one.")

+ 17
- 15
hc/front/tests/test_add_whatsapp.py View File

@ -55,21 +55,6 @@ class AddWhatsAppTestCase(BaseTestCase):
# 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"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.kind, "whatsapp")
self.assertEqual(c.phone_number, "+1234567890")
self.assertEqual(c.name, "My Phone")
self.assertFalse(c.whatsapp_notify_down)
self.assertFalse(c.whatsapp_notify_up)
self.assertEqual(c.project, self.project)
@override_settings(TWILIO_USE_WHATSAPP=False)
def test_it_obeys_use_whatsapp_flag(self):
self.client.login(username="[email protected]", password="password")
@ -83,3 +68,20 @@ class AddWhatsAppTestCase(BaseTestCase):
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertEqual(r.status_code, 403)
def test_it_handles_down_false_up_true(self):
form = {"phone": "+1234567890", "up": True}
self.client.login(username="[email protected]", password="password")
self.client.post(self.url, form)
c = Channel.objects.get()
self.assertFalse(c.whatsapp_notify_down)
self.assertTrue(c.whatsapp_notify_up)
def test_it_rejects_unchecked_up_and_down(self):
form = {"phone": "+1234567890"}
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form)
self.assertContains(r, "Please select at least one.")

+ 0
- 11
hc/front/tests/test_edit_signal.py View File

@ -47,17 +47,6 @@ class EditSignalTestCase(BaseTestCase):
# 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.signal_notify_down)
self.assertFalse(self.channel.signal_notify_up)
@override_settings(SIGNAL_CLI_ENABLED=False)
def test_it_handles_disabled_integration(self):
self.client.login(username="[email protected]", password="password")


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

@ -54,17 +54,6 @@ class EditWhatsAppTestCase(BaseTestCase):
# 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")


+ 1
- 1
templates/integrations/email_form.html View File

@ -48,7 +48,7 @@ Email Settings - {{ site_name }}
</div>
</div>
<div id="add-email-notify-group" class="form-group {{ form.down.css_classes }}">
<div class="form-group {{ form.down.css_classes }}">
<label class="col-sm-2 control-label">Notify When</label>
<div class="col-sm-10">
<label class="checkbox-container">


+ 6
- 1
templates/integrations/signal_form.html View File

@ -69,7 +69,7 @@ Signal Settings - {% site_name %}
</div>
</div>
<div id="add-email-notify-group" class="form-group">
<div class="form-group {{ form.down.css_classes }}">
<label class="col-sm-2 control-label">Notify When</label>
<div class="col-sm-10">
<label class="checkbox-container">
@ -89,6 +89,11 @@ Signal Settings - {% site_name %}
{% if form.up.value %} checked {% endif %}>
<span class="checkmark"></span>
A check goes <strong>up</strong>
{% if form.down.errors %}
<div class="help-block">
{{ form.down.errors|join:"" }}
</div>
{% endif %}
</label>
</div>
</div>


+ 1
- 2
templates/integrations/sms_form.html View File

@ -79,7 +79,7 @@ SMS Settings - {% site_name %}
{% endif %}
</div>
</div>
<div id="add-email-notify-group" class="form-group {{ form.down.css_classes }}">
<div class="form-group {{ form.down.css_classes }}">
<label class="col-sm-2 control-label">Notify When</label>
<div class="col-sm-10">
<label class="checkbox-container">
@ -99,7 +99,6 @@ SMS Settings - {% site_name %}
{% if form.up.value %} checked {% endif %}>
<span class="checkmark"></span>
A check goes <strong>up</strong>
</span>
{% if form.down.errors %}
<div class="help-block">
{{ form.down.errors|join:"" }}


+ 6
- 1
templates/integrations/whatsapp_form.html View File

@ -80,7 +80,7 @@ WhatsApp Settings - {% site_name %}
</div>
</div>
<div id="add-email-notify-group" class="form-group">
<div class="form-group {{ form.down.css_classes }}">
<label class="col-sm-2 control-label">Notify When</label>
<div class="col-sm-10">
<label class="checkbox-container">
@ -100,6 +100,11 @@ WhatsApp Settings - {% site_name %}
{% if form.up.value %} checked {% endif %}>
<span class="checkmark"></span>
A check goes <strong>up</strong>
{% if form.down.errors %}
<div class="help-block">
{{ form.down.errors|join:"" }}
</div>
{% endif %}
</label>
</div>
</div>


Loading…
Cancel
Save