From 7f1f177a554e0992b6dff0b2db29ee89bf2672c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 11 Dec 2015 18:34:40 +0200 Subject: [PATCH] Get rid of leading/trailing whitespace in channel values. This is a typical problem when copy-pasting Slack URLs. --- hc/front/forms.py | 4 ++++ hc/front/tests/test_add_channel.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/hc/front/forms.py b/hc/front/forms.py index 328caeeb..5cea3fc8 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -12,3 +12,7 @@ class AddChannelForm(forms.ModelForm): class Meta: model = Channel fields = ['kind', 'value'] + + def clean_value(self): + value = self.cleaned_data["value"] + return value.strip() diff --git a/hc/front/tests/test_add_channel.py b/hc/front/tests/test_add_channel.py index 3944fb54..3d194cde 100644 --- a/hc/front/tests/test_add_channel.py +++ b/hc/front/tests/test_add_channel.py @@ -24,6 +24,18 @@ class AddChannelTestCase(TestCase): assert r.status_code == 302 assert Channel.objects.count() == 1 + def test_it_trims_whitespace(self): + """ Leading and trailing whitespace should get trimmed. """ + + url = "/integrations/add/" + form = {"kind": "email", "value": " alice@example.org "} + + self.client.login(username="alice", password="password") + self.client.post(url, form) + + q = Channel.objects.filter(value="alice@example.org") + self.assertEqual(q.count(), 1) + def test_it_rejects_bad_kind(self): url = "/integrations/add/" form = {"kind": "dog", "value": "Lassie"}