summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2016-05-13 10:45:50 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2016-05-13 10:46:00 +0200
commit284dab73ba7b3be17d3c073e807ef42c8a3462e5 (patch)
tree9628433072821e555904b2724b9cb960285d6bf6
parent7c9eed0e662d8dc7f2e1996af631457a307a8a7a (diff)
downloadpykolab-284dab73ba7b3be17d3c073e807ef42c8a3462e5.tar.gz
Trim spaces in surname/givenname/sn when generating mail/alias (#5360)
Reviewers: vanmeeuwen, #pykolab_developers Reviewed By: vanmeeuwen, #pykolab_developers Subscribers: vanmeeuwen Differential Revision: https://git.kolab.org/D136
-rw-r--r--.gitignore2
-rw-r--r--pykolab/utils.py23
-rw-r--r--tests/unit/test-022-utils.py20
-rw-r--r--tests/unit/test-030-recipientpolicy.py30
4 files changed, 66 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index e8d60b3..b18752f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,4 +25,4 @@ po/POTFILES
pykolab/constants.py
pykolab-[0-9]*.*/
src/
-test-*
+./test-*
diff --git a/pykolab/utils.py b/pykolab/utils.py
index 7fca3fe..29a0e95 100644
--- a/pykolab/utils.py
+++ b/pykolab/utils.py
@@ -353,22 +353,29 @@ def normalize(_object):
return result
elif type(_object) == dict:
- for key in _object.keys():
+ def _strip(value):
+ try:
+ return value.strip()
+ except:
+ return value
+
+ for key in _object:
if type(_object[key]) == list:
- if _object[key] == None:
+ if _object[key] is None:
continue
- if len(_object[key]) == 1:
- result[key.lower()] = ''.join(_object[key])
+ val = map(_strip, _object[key])
+
+ if len(val) == 1:
+ result[key.lower()] = ''.join(val)
else:
- result[key.lower()] = _object[key]
+ result[key.lower()] = val
else:
- if _object[key] == None:
+ if _object[key] is None:
continue
- # What the heck?
- result[key.lower()] = _object[key]
+ result[key.lower()] = _strip(_object[key])
if result.has_key('objectsid') and not result['objectsid'][0] == "S":
result['objectsid'] = sid_to_string(result['objectsid'])
diff --git a/tests/unit/test-022-utils.py b/tests/unit/test-022-utils.py
new file mode 100644
index 0000000..ad65aea
--- /dev/null
+++ b/tests/unit/test-022-utils.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+
+from pykolab import utils
+
+
+class TestTranslate(unittest.TestCase):
+
+ def test_001_normalize(self):
+ attr = {"test1": " trim ", "test2": [" trim1 ", " trim2 "]}
+ result = utils.normalize(attr)
+
+ self.assertEqual(result['test1'], "trim")
+ self.assertEqual(result['test2'][0], "trim1")
+ self.assertEqual(result['test2'][1], "trim2")
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/unit/test-030-recipientpolicy.py b/tests/unit/test-030-recipientpolicy.py
new file mode 100644
index 0000000..0f50747
--- /dev/null
+++ b/tests/unit/test-030-recipientpolicy.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+
+from pykolab.plugins.recipientpolicy import KolabRecipientpolicy
+
+policy = KolabRecipientpolicy()
+
+
+class TestRecipientPolicy(unittest.TestCase):
+ def test_001_primary_mail(self):
+ """
+ The spaces in attributes used for mail generation.
+ """
+
+ entry = {
+ 'surname': ' sn ',
+ 'givenname': ' gn ',
+ }
+
+ mail = policy.set_primary_mail(
+ primary_mail='%(givenname)s.%(surname)s@%(domain)s',
+ primary_domain='example.org',
+ entry=entry
+ )
+
+ self.assertEqual('gn.sn@example.org', mail)
+
+if __name__ == '__main__':
+ unittest.main()