From d02a539a2182dc1b12d0b1f9fa26939bfdf0cf6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 2 May 2019 14:38:55 +0300 Subject: [PATCH] Skip the verification step if user is setting up email notifications to their own email address. --- hc/front/tests/test_add_email.py | 22 +++++++++++++++++++--- hc/front/views.py | 15 ++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/hc/front/tests/test_add_email.py b/hc/front/tests/test_add_email.py index 9bba4430..3431ad42 100644 --- a/hc/front/tests/test_add_email.py +++ b/hc/front/tests/test_add_email.py @@ -17,7 +17,7 @@ class AddEmailTestCase(BaseTestCase): self.assertContains(r, "Requires confirmation") def test_it_creates_channel(self): - form = {"value": "alice@example.org"} + form = {"value": "dan@example.org"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -26,7 +26,7 @@ class AddEmailTestCase(BaseTestCase): c = Channel.objects.get() doc = json.loads(c.value) self.assertEqual(c.kind, "email") - self.assertEqual(doc["value"], "alice@example.org") + self.assertEqual(doc["value"], "dan@example.org") self.assertFalse(c.email_verified) self.assertEqual(c.project, self.project) @@ -36,7 +36,7 @@ class AddEmailTestCase(BaseTestCase): email = mail.outbox[0] self.assertTrue(email.subject.startswith("Verify email address on")) # Make sure we're sending to an email address, not a JSON string: - self.assertEqual(email.to[0], "alice@example.org") + self.assertEqual(email.to[0], "dan@example.org") def test_team_access_works(self): form = {"value": "bob@example.org"} @@ -73,6 +73,22 @@ class AddEmailTestCase(BaseTestCase): @override_settings(EMAIL_USE_VERIFICATION=False) def test_it_auto_verifies_email(self): + form = {"value": "dan@example.org"} + + self.client.login(username="alice@example.org", password="password") + r = self.client.post(self.url, form) + self.assertRedirects(r, "/integrations/") + + c = Channel.objects.get() + doc = json.loads(c.value) + self.assertEqual(c.kind, "email") + self.assertEqual(doc["value"], "dan@example.org") + self.assertTrue(c.email_verified) + + # Email should *not* have been sent + self.assertEqual(len(mail.outbox), 0) + + def test_it_auto_verifies_own_email(self): form = {"value": "alice@example.org"} self.client.login(username="alice@example.org", password="password") diff --git a/hc/front/views.py b/hc/front/views.py index 32aa9d31..0f41c44b 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -711,13 +711,18 @@ def add_email(request): channel.assign_all_checks() - if settings.EMAIL_USE_VERIFICATION: - channel.send_verify_link() - else: - # In self-hosted setting, administator has the option to - # disable the email verification step. + is_own_email = form.cleaned_data["value"] == request.user.email + if is_own_email or not settings.EMAIL_USE_VERIFICATION: + # If user is subscribing *their own* address + # we can skip the verification step. + + # Additionally, in self-hosted setting, administator has the + # option to disable the email verification step altogether. + channel.email_verified = True channel.save() + else: + channel.send_verify_link() return redirect("hc-channels") else: