mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
forms.py: Refactor MIT mailing list check into a modern style.
No change to behavior. non_mit_mailing_list never returned False, so it was never possible to reach the line "Otherwise, the user is an MIT mailing list, and .."
This commit is contained in:
@@ -58,21 +58,19 @@ def get_valid_realm(email):
|
|||||||
return None
|
return None
|
||||||
return realm
|
return realm
|
||||||
|
|
||||||
def not_mit_mailing_list(value):
|
def email_is_not_mit_mailing_list(email):
|
||||||
# type: (str) -> bool
|
# type: (text_type) -> None
|
||||||
"""Prevent MIT mailing lists from signing up for Zulip"""
|
"""Prevent MIT mailing lists from signing up for Zulip"""
|
||||||
if "@mit.edu" in value:
|
if "@mit.edu" in email:
|
||||||
username = value.rsplit("@", 1)[0]
|
username = email.rsplit("@", 1)[0]
|
||||||
# Check whether the user exists and can get mail.
|
# Check whether the user exists and can get mail.
|
||||||
try:
|
try:
|
||||||
DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT)
|
DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT)
|
||||||
return True
|
|
||||||
except DNS.Base.ServerError as e:
|
except DNS.Base.ServerError as e:
|
||||||
if e.rcode == DNS.Status.NXDOMAIN:
|
if e.rcode == DNS.Status.NXDOMAIN:
|
||||||
raise ValidationError(mark_safe(MIT_VALIDATION_ERROR))
|
raise ValidationError(mark_safe(MIT_VALIDATION_ERROR))
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
return True
|
|
||||||
|
|
||||||
class RegistrationForm(forms.Form):
|
class RegistrationForm(forms.Form):
|
||||||
full_name = forms.CharField(max_length=100)
|
full_name = forms.CharField(max_length=100)
|
||||||
@@ -160,18 +158,10 @@ class HomepageForm(forms.Form):
|
|||||||
if realm is None:
|
if realm is None:
|
||||||
raise ValidationError(mark_safe(SIGNUP_STRING))
|
raise ValidationError(mark_safe(SIGNUP_STRING))
|
||||||
|
|
||||||
# If it's a clear realm not used for Zephyr mirroring, pass
|
if realm.is_zephyr_mirror_realm:
|
||||||
if not realm.is_zephyr_mirror_realm:
|
email_is_not_mit_mailing_list(email)
|
||||||
return email
|
|
||||||
|
|
||||||
# At this point, the user is trying to join a Zephyr mirroring
|
return email
|
||||||
# realm. We confirm that they are a real account (not a
|
|
||||||
# mailing list), and if so, let them in.
|
|
||||||
if not_mit_mailing_list(email):
|
|
||||||
return email
|
|
||||||
|
|
||||||
# Otherwise, the user is an MIT mailing list, and we return failure
|
|
||||||
raise ValidationError(mark_safe(SIGNUP_STRING))
|
|
||||||
|
|
||||||
def email_is_not_disposable(email):
|
def email_is_not_disposable(email):
|
||||||
# type: (text_type) -> None
|
# type: (text_type) -> None
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from zerver.forms import not_mit_mailing_list
|
from zerver.forms import email_is_not_mit_mailing_list
|
||||||
|
|
||||||
from zerver.lib.rate_limiter import (
|
from zerver.lib.rate_limiter import (
|
||||||
add_ratelimit_rule,
|
add_ratelimit_rule,
|
||||||
@@ -45,13 +45,13 @@ class MITNameTest(TestCase):
|
|||||||
def test_mailinglist(self):
|
def test_mailinglist(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
||||||
self.assertRaises(ValidationError, not_mit_mailing_list, "1234567890@mit.edu")
|
self.assertRaises(ValidationError, email_is_not_mit_mailing_list, "1234567890@mit.edu")
|
||||||
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
||||||
self.assertRaises(ValidationError, not_mit_mailing_list, "ec-discuss@mit.edu")
|
self.assertRaises(ValidationError, email_is_not_mit_mailing_list, "ec-discuss@mit.edu")
|
||||||
def test_notmailinglist(self):
|
def test_notmailinglist(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
with mock.patch('DNS.dnslookup', return_value=[['POP IMAP.EXCHANGE.MIT.EDU starnine']]):
|
with mock.patch('DNS.dnslookup', return_value=[['POP IMAP.EXCHANGE.MIT.EDU starnine']]):
|
||||||
self.assertTrue(not_mit_mailing_list("sipbexch@mit.edu"))
|
email_is_not_mit_mailing_list("sipbexch@mit.edu")
|
||||||
|
|
||||||
class RateLimitTests(ZulipTestCase):
|
class RateLimitTests(ZulipTestCase):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user