summaryrefslogtreecommitdiffstats
path: root/ipa-python
diff options
context:
space:
mode:
Diffstat (limited to 'ipa-python')
-rw-r--r--ipa-python/ipavalidate.py28
-rw-r--r--ipa-python/test/test_ipavalidate.py71
2 files changed, 62 insertions, 37 deletions
diff --git a/ipa-python/ipavalidate.py b/ipa-python/ipavalidate.py
index 918c34a62..1750b8397 100644
--- a/ipa-python/ipavalidate.py
+++ b/ipa-python/ipavalidate.py
@@ -19,7 +19,7 @@
import re
-def email(mail, notEmpty=True):
+def Email(mail, notEmpty=True):
"""Do some basic validation of an e-mail address.
Return 0 if ok
Return 1 if not
@@ -49,7 +49,7 @@ def email(mail, notEmpty=True):
return 0
-def plain(text, notEmpty=False):
+def Plain(text, notEmpty=False, allowSpaces=True):
"""Do some basic validation of a plain text field
Return 0 if ok
Return 1 if not
@@ -57,23 +57,33 @@ def plain(text, notEmpty=False):
If notEmpty is True the this will return an error if the field
is "" or None.
"""
- textRE = re.compile(r"^[a-zA-Z_\-0-9\'\ ]*$")
-
- if not text and notEmpty is True:
- return 1
-
- if text is None:
+ if (text is None) or (not text.strip()):
if notEmpty is True:
return 1
else:
return 0
+ if allowSpaces:
+ textRE = re.compile(r"^[a-zA-Z_\-0-9\'\ ]*$")
+ else:
+ textRE = re.compile(r"^[a-zA-Z_\-0-9\']*$")
if not textRE.search(text):
return 1
return 0
-def path(text, notEmpty=False):
+def String(text, notEmpty=False):
+ """A string type. This is much looser in what it allows than plain"""
+
+ if text is None or not text.strip():
+ if notEmpty is True:
+ return 1
+ else:
+ return 0
+
+ return 0
+
+def Path(text, notEmpty=False):
"""Do some basic validation of a path
Return 0 if ok
Return 1 if not
diff --git a/ipa-python/test/test_ipavalidate.py b/ipa-python/test/test_ipavalidate.py
index 507f7e022..677866d04 100644
--- a/ipa-python/test/test_ipavalidate.py
+++ b/ipa-python/test/test_ipavalidate.py
@@ -31,39 +31,54 @@ class TestValidate(unittest.TestCase):
def tearDown(self):
pass
- def test_validemail(self):
- self.assertEqual(0, ipavalidate.email("test@freeipa.org"))
- self.assertEqual(0, ipavalidate.email("", notEmpty=False))
+ def test_validEmail(self):
+ self.assertEqual(0, ipavalidate.Email("test@freeipa.org"))
+ self.assertEqual(0, ipavalidate.Email("", notEmpty=False))
- def test_invalidemail(self):
- self.assertEqual(1, ipavalidate.email("test"))
- self.assertEqual(1, ipavalidate.email("test@freeipa"))
- self.assertEqual(1, ipavalidate.email("test@.com"))
- self.assertEqual(1, ipavalidate.email(""))
- self.assertEqual(1, ipavalidate.email(None))
+ def test_invalidEmail(self):
+ self.assertEqual(1, ipavalidate.Email("test"))
+ self.assertEqual(1, ipavalidate.Email("test@freeipa"))
+ self.assertEqual(1, ipavalidate.Email("test@.com"))
+ self.assertEqual(1, ipavalidate.Email(""))
+ self.assertEqual(1, ipavalidate.Email(None))
- def test_validplain(self):
- self.assertEqual(0, ipavalidate.plain("Joe User"))
- self.assertEqual(0, ipavalidate.plain("Joe O'Malley"))
- self.assertEqual(0, ipavalidate.plain("", notEmpty=False))
- self.assertEqual(0, ipavalidate.plain(None, notEmpty=False))
+ def test_validPlain(self):
+ self.assertEqual(0, ipavalidate.Plain("Joe User"))
+ self.assertEqual(0, ipavalidate.Plain("Joe O'Malley"))
+ self.assertEqual(0, ipavalidate.Plain("", notEmpty=False))
+ self.assertEqual(0, ipavalidate.Plain(None, notEmpty=False))
+ self.assertEqual(0, ipavalidate.Plain("JoeUser", allowSpaces=False))
+ self.assertEqual(0, ipavalidate.Plain("JoeUser", allowSpaces=True))
- def test_invalidplain(self):
- self.assertEqual(1, ipavalidate.plain("Joe (User)"))
- self.assertEqual(1, ipavalidate.plain("", notEmpty=True))
- self.assertEqual(1, ipavalidate.plain(None, notEmpty=True))
+ def test_invalidPlain(self):
+ self.assertEqual(1, ipavalidate.Plain("Joe (User)"))
+ self.assertEqual(1, ipavalidate.Plain("Joe C. User"))
+ self.assertEqual(1, ipavalidate.Plain("", notEmpty=True))
+ self.assertEqual(1, ipavalidate.Plain(None, notEmpty=True))
+ self.assertEqual(1, ipavalidate.Plain("Joe User", allowSpaces=False))
- def test_validpath(self):
- self.assertEqual(0, ipavalidate.path("/"))
- self.assertEqual(0, ipavalidate.path("/home/user"))
- self.assertEqual(0, ipavalidate.path("../home/user"))
- self.assertEqual(0, ipavalidate.path("", notEmpty=False))
- self.assertEqual(0, ipavalidate.path(None, notEmpty=False))
+ def test_validString(self):
+ self.assertEqual(0, ipavalidate.String("Joe User"))
+ self.assertEqual(0, ipavalidate.String("Joe O'Malley"))
+ self.assertEqual(1, ipavalidate.Plain("Joe C. User"))
+ self.assertEqual(0, ipavalidate.String("", notEmpty=False))
+ self.assertEqual(0, ipavalidate.String(None, notEmpty=False))
- def test_invalidpath(self):
- self.assertEqual(1, ipavalidate.path("(foo)"))
- self.assertEqual(1, ipavalidate.path("", notEmpty=True))
- self.assertEqual(1, ipavalidate.path(None, notEmpty=True))
+ def test_invalidString(self):
+ self.assertEqual(1, ipavalidate.String("", notEmpty=True))
+ self.assertEqual(1, ipavalidate.String(None, notEmpty=True))
+
+ def test_validPath(self):
+ self.assertEqual(0, ipavalidate.Path("/"))
+ self.assertEqual(0, ipavalidate.Path("/home/user"))
+ self.assertEqual(0, ipavalidate.Path("../home/user"))
+ self.assertEqual(0, ipavalidate.Path("", notEmpty=False))
+ self.assertEqual(0, ipavalidate.Path(None, notEmpty=False))
+
+ def test_invalidPath(self):
+ self.assertEqual(1, ipavalidate.Path("(foo)"))
+ self.assertEqual(1, ipavalidate.Path("", notEmpty=True))
+ self.assertEqual(1, ipavalidate.Path(None, notEmpty=True))
if __name__ == '__main__':
unittest.main()