Skip to content

Commit

Permalink
Improved email validator by performing a basic sanity check
Browse files Browse the repository at this point in the history
instead of a more restrictive check.

fixes #17
  • Loading branch information
jaimegildesagredo committed Jan 28, 2014
1 parent 8673049 commit 38003e3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

0.5.1 (Not released yet)
------------------------

* The `Email` validator now only performs a basic sanity check instead of the more restrictive previous check. See `issue 17<https://github.com/jaimegildesagredo/booby/issues/17>`_.

0.5.0 (Jan 4, 2014)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion booby/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Email(String):
def __init__(self):
super(Email, self).__init__()

self.pattern = re.compile('^\w+\@\w+\.[a-z]{2,3}$')
self.pattern = re.compile('^[^@]+\@[^@]+$')

@nullable
def validate(self, value):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_model_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_when_token_key_is_not_a_string_then_raises_validation_error(self):
user.validate()

def test_when_email_is_an_invalid_email_then_raises_validation_error(self):
user = User(login='root', email='root@localhost')
user = User(login='root', email='@localhost')

with assert_raises_regexp(errors.ValidationError, 'should be a valid email'):
user.validate()
Expand Down
1 change: 1 addition & 0 deletions tests/unit/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
14 changes: 14 additions & 0 deletions tests/unit/validators/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-

from expects import expect

from booby import errors


class String(object):
def test_should_pass_if_value_is_none(self):
self.validator(None)

def test_should_fail_if_value_is_not_a_string(self):
expect(lambda: self.validator(1)).to.raise_error(
errors.ValidationError, 'should be a string')
37 changes: 37 additions & 0 deletions tests/unit/validators/test_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from expects import expect
from . import mixins

from booby import validators, errors


class TestEmail(mixins.String):
def test_should_pass_if_value_is_valid_email(self):
self.validator('[email protected]')

def test_should_pass_if_value_contains_plus_sign(self):
self.validator('[email protected]')

def test_should_pass_if_value_contains_minus_sign(self):
self.validator('[email protected]')

def test_should_pass_if_domain_is_tld(self):
self.validator('foo@example')

def test_should_fail_if_nothing_before_at_sign(self):
expect(lambda: self.validator('@example')).to.raise_error(
errors.ValidationError, 'should be a valid email')

def test_should_fail_if_value_doesnt_have_at_sign(self):
expect(lambda: self.validator('foo%example.com')).to.raise_error(
errors.ValidationError, 'should be a valid email')

def test_should_fail_if_empty_string(self):
expect(lambda: self.validator('')).to.raise_error(
errors.ValidationError, 'should be a valid email')

def setup(self):
self.validator = validators.Email()
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from __future__ import unicode_literals

from expects import expect
from . import mixins
from .._helpers import stub_validator

from booby import validators, fields, models, errors

from ._helpers import stub_validator


class TestRequired(object):
def test_when_value_is_none_then_raises_validation_error(self):
Expand All @@ -33,16 +33,7 @@ def setup(self):
self.validator = validators.In(['foo', 'bar'])


class StringMixin(object):
def test_when_value_is_not_string_then_raises_validation_error(self):
expect(lambda: self.validator(1)).to.raise_error(
errors.ValidationError, 'should be a string')

def test_when_value_is_none_then_does_not_raise(self):
self.validator(None)


class TestString(StringMixin):
class TestString(mixins.String):
def test_when_value_is_a_string_then_does_not_raise(self):
self.validator('foo')

Expand Down Expand Up @@ -147,21 +138,5 @@ def setup(self):
self.validator = validators.List()


class TestEmail(StringMixin):
def test_when_value_doesnt_match_email_pattern_then_raises_validation_error(self):
expect(lambda: self.validator('foo@example')).to.raise_error(
errors.ValidationError, 'should be a valid email')

def test_when_value_doesnt_have_at_sign_then_raises_validation_error(self):
expect(lambda: self.validator('foo%example.com')).to.raise_error(
errors.ValidationError, 'should be a valid email')

def test_when_value_is_a_valid_email_then_does_not_raise(self):
self.validator('[email protected]')

def setup(self):
self.validator = validators.Email()


class User(models.Model):
name = fields.String()

0 comments on commit 38003e3

Please sign in to comment.