Skip to content

Commit

Permalink
Merge fdd9b26 into 8eee1df
Browse files Browse the repository at this point in the history
  • Loading branch information
qurbat committed Jan 23, 2020
2 parents 8eee1df + fdd9b26 commit b0a0cfe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions baseframe/forms/validators.py
Expand Up @@ -26,6 +26,7 @@
from lxml import html
from pyisemail import is_email
import dns.resolver
import emoji
import requests

from coaster.utils import deobfuscate_email, make_name
Expand Down Expand Up @@ -299,6 +300,19 @@ class NotEqualTo(_Comparison):
def compare(self, value, other):
return value != other

class IsEmoji(object):
"""
Validate field to contain a single emoji.
:param message:
Error message to raise in case of a validation error.
"""
def __init__(self, message=None):
self.message = message or _(u"This is not a valid emoji.")

def __call__(self, form, field):
if field.data not in emoji.UNICODE_EMOJI:
raise ValidationError(self.message)

class IsPublicEmailDomain(object):
"""
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -23,6 +23,7 @@
'pytz',
'pyIsEmail',
'dnspython',
'emoji',
'WTForms>=2.2',
'Flask>=1.0',
'Flask-Assets',
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures.py
Expand Up @@ -57,6 +57,9 @@ class UrlFormTest(forms.Form):
filters=[forms.filters.strip()],
)

class EmojiFormTest(forms.Form):
emoji = forms.StringField(__("Emoji"), validators=[forms.validators.IsEmoji()])


class AllUrlsFormTest(forms.Form):
content_with_urls = forms.TextAreaField(
Expand Down
14 changes: 14 additions & 0 deletions tests/test_validators.py
Expand Up @@ -12,6 +12,7 @@

from .fixtures import (
AllUrlsFormTest,
EmojiFormTest,
PublicEmailDomainFormTest,
TestCaseBaseframe,
UrlFormTest,
Expand All @@ -23,6 +24,7 @@ def setUp(self):
super(TestValidators, self).setUp()
with self.app.test_request_context('/'):
self.form = UrlFormTest(meta={'csrf': False})
self.emoji_form = EmojiFormTest(meta={'csrf': False})
self.all_urls_form = AllUrlsFormTest(meta={'csrf': False})
self.webmail_form = PublicEmailDomainFormTest(meta={'csrf': False})
urllib3.disable_warnings()
Expand All @@ -42,6 +44,18 @@ def test_invalid_url(self):
url = 'https://hasgeek'
self.form.process(url=url)
assert not self.form.validate()

def test_valid_emoji(self):
with self.app.test_request_context('/'):
dat = u'👍'
self.emoji_form.process(emoji=dat)
assert self.emoji_form.validate() is True

def test_invalid_emoji(self):
with self.app.test_request_context('/'):
dat = 'eviltext'
self.emoji_form.process(emoji=dat)
assert self.emoji_form.validate() is False

def test_public_email_domain(self):
with self.app.test_request_context('/'):
Expand Down

0 comments on commit b0a0cfe

Please sign in to comment.