Commits

Andriy Kornatskyy committed cdfde13

Added validation unittests for demo template.

Comments (0)

Files changed (3)

demos/template/setup.py

     'wheezy.http>=0.1.287',
     'wheezy.routing>=0.1.145',
     'wheezy.security>=0.1.46',
-    'wheezy.validation>=0.1.91',
+    'wheezy.validation>=0.1.104',
     'wheezy.web>=0.1.373',
 ]
 

demos/template/src/membership/validation.py

 
 from wheezy.validation import Validator
 from wheezy.validation.rules import compare
+from wheezy.validation.rules import email
 from wheezy.validation.rules import length
 from wheezy.validation.rules import one_of
 from wheezy.validation.rules import range
 _ = lambda s: s
 
 credential_validator = Validator({
-    'username': [required, length(min=2, max=20)],
+    'username': [required, length(min=2), length(max=20)],
     'password': [required, length(min=8, max=12)]
 })
 
 account_validator = Validator({
-    'email': [required, length(min=6, max=30)],
+    'email': [required, length(min=6), length(max=30), email],
     'display_name': [required, length(max=30)],
     'account_type': [required, one_of(('user', 'business'))]
 })

demos/template/src/membership/web/tests/test_validation.py

+
+""" ``test_validation`` module.
+"""
+
+import unittest
+
+from wheezy.validation.checker import Checker
+
+
+class CredentialValidatorTestCase(unittest.TestCase):
+
+    def setUp(self):
+        from membership.validation import credential_validator
+        self.c = Checker(gettext=lambda t: str(t))
+        self.c.use(credential_validator)
+
+    def test_username(self):
+        assert not self.c.error(username='johh')
+
+        e = 'Required field cannot be left blank.'
+        assert e == self.c.error(username='')
+        e = 'Required to be a minimum of 2 characters in length.'
+        assert e == self.c.error(username='x')
+        e = 'Exceeds maximum length of 20.'
+        assert e == self.c.error(username='x' * 21)
+
+    def test_password(self):
+        assert not self.c.error(password='P@ssw0rd')
+
+        e = 'Required field cannot be left blank.'
+        assert e == self.c.error(password='')
+        e = 'The length must fall within the range 8 - 12 characters.'
+        assert e == self.c.error(password='x' * 7)
+        assert e == self.c.error(password='x' * 13)
+
+
+class AccountValidatorTestCase(unittest.TestCase):
+
+    def setUp(self):
+        from membership.validation import account_validator
+        self.c = Checker(gettext=lambda t: str(t))
+        self.c.use(account_validator)
+
+    def test_email(self):
+        assert not self.c.error(email='johh@somewhere.net')
+
+        e = 'Required field cannot be left blank.'
+        assert e == self.c.error(email='')
+        e = 'Required to be a minimum of 6 characters in length.'
+        assert e == self.c.error(email='x' * 5)
+        e = 'Exceeds maximum length of 30.'
+        assert e == self.c.error(email='x' * 31)
+        e = 'Required to be a valid email address.'
+        assert e == self.c.error(email='x@somewhere')
+
+    def test_account_type(self):
+        for account_type in ('user', 'business'):
+            assert not self.c.error(account_type=account_type)
+
+        e = 'Required field cannot be left blank.'
+        assert e == self.c.error(account_type='')
+        e = 'The value does not belong to the list of known items.'
+        assert e == self.c.error(account_type='x')
+
+
+class PasswordMatchValidatorTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.c = Checker(gettext=lambda t: str(t))
+        self.c.use('membership.validation.password_match_validator')
+
+    def test_password(self):
+        assert not self.c.error(password='x', confirm_password='x')
+
+        e = 'Passwords do not match.'
+        assert e == self.c.error(password='x', confirm_password='')