Skip to content

Commit

Permalink
added helper function for public email domain check
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibhas committed Mar 20, 2018
1 parent 47bfa25 commit 136876e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions baseframe/forms/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from .validators import IsPublicEmailDomain, ValidationError

__all__ = ['is_public_email_domain']


class DummyField(object):
# Validators need a field param to work, we're going to pass a dummy
# https://github.com/wtforms/wtforms/blob/master/tests/validators.py#L14
def __init__(self, data):
self.data = data


def is_public_email_domain(email_or_domain):
"""
Checks if the given email address or domain belongs to a public email domain
"""
try:
validator = IsPublicEmailDomain()
# custom validators need to accept a form and field to work
# as our validator doesn't do anything with the form, can just pass a dummy object
validator(dict(), DummyField(email_or_domain))
except ValidationError:
return False
return True
10 changes: 10 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import warnings
import urllib3
from baseframe.forms.helper import is_public_email_domain
from .fixtures import TestCaseBaseframe, UrlFormTest, AllUrlsFormTest, OptionalIfFormTest, OptionalIfNotFormTest, PublicEmailDomainFormTest


Expand Down Expand Up @@ -72,6 +73,15 @@ def test_public_email_domain(self):
self.assertIn('webmail_domain', self.webmail_form.errors)
self.assertNotIn('not_webmail_domain', self.webmail_form.errors)

def test_public_email_domain_helper(self):
with self.app.test_request_context('/'):
self.assertEqual(is_public_email_domain(u'gmail.com'), True)
self.assertEqual(is_public_email_domain(u'i❤.ws'), False)
# this domain lookup fails, so the helper should return False
self.assertEqual(
is_public_email_domain(u'www.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijks.com'),
False)

def test_url_without_protocol(self):
with self.app.test_request_context('/'):
url = 'hasgeek.com'
Expand Down

0 comments on commit 136876e

Please sign in to comment.