Skip to content

Commit 0603956

Browse files
committed
manually merge PR65 to add more unit tests for safeinput.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6059 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent 8ad48ab commit 0603956

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

mig/shared/safeinput.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,20 +2284,6 @@ def __str__(self):
22842284
def main(_print=print):
22852285
print = _print # workaround print as reserved word on PY2
22862286

2287-
for test_cn in ('Firstname Lastname', 'Test Æøå', 'Test Überh4x0r',
2288-
'Harry S. Truman', u'Unicode æøå', "Invalid D'Angelo",
2289-
'Test Maybe Invalid Źacãŕ', 'Test Invalid ?',
2290-
'Test HTML Invalid <code/>'):
2291-
try:
2292-
print('Testing valid_commonname: %s' % test_cn)
2293-
print('Filtered commonname: %s' % filter_commonname(test_cn))
2294-
# print 'DEBUG %s only in %s' % ([test_cn],
2295-
# [VALID_NAME_CHARACTERS])
2296-
valid_commonname(test_cn)
2297-
print('Accepted raw commonname!')
2298-
except Exception as exc:
2299-
print('Rejected raw commonname %r: %s' % (test_cn, exc))
2300-
23012287
for test_org in ('UCPH', 'Some University, Some Dept.', 'Green Shoes Ltd.',
23022288
u'Unicode Org', "Invalid R+D", "Invalid R/D",
23032289
"Invalid R@D", 'Test HTML Invalid <code/>'):

tests/test_mig_shared_safeinput.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,31 @@
2727

2828
"""Unit tests for the migrid module pointed to in the filename"""
2929

30+
import codecs
3031
import importlib
3132
import os
3233
import sys
34+
from past.builtins import basestring
3335

3436
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
3537

3638
from support import MigTestCase, testmain
39+
from mig.shared.safeinput import \
40+
filter_commonname, valid_commonname
41+
42+
PY2 = sys.version_info[0] == 2
43+
44+
45+
def as_string_of_unicode(value):
46+
assert isinstance(value, basestring)
47+
if not is_string_of_unicode(value):
48+
assert PY2, "unreachable unless Python 2"
49+
return unicode(codecs.decode(value, 'utf8'))
50+
return value
51+
52+
53+
def is_string_of_unicode(value):
54+
return type(value) == type(u'')
3755

3856

3957
class MigSharedSafeinput(MigTestCase):
@@ -45,6 +63,47 @@ def test_existing_main(self):
4563
safeimport = importlib.import_module("mig.shared.safeinput")
4664
safeimport.main(_print=lambda _: None)
4765

66+
COMMONNAME_PERMITTED = (
67+
'Firstname Lastname',
68+
'Test Æøå',
69+
'Test Überh4x0r',
70+
'Harry S. Truman',
71+
u'Unicode æøå')
72+
73+
COMMONNAME_PROHIBITED = (
74+
"Invalid D'Angelo",
75+
'Test Maybe Invalid Źacãŕ',
76+
'Test Invalid ?',
77+
'Test HTML Invalid <code/>')
78+
79+
def test_commonname_valid(self):
80+
for test_cn in self.COMMONNAME_PERMITTED:
81+
saw_raise = False
82+
try:
83+
valid_commonname(test_cn)
84+
except Exception:
85+
saw_raise = True
86+
self.assertFalse(saw_raise)
87+
88+
for test_cn in self.COMMONNAME_PROHIBITED:
89+
saw_raise = False
90+
try:
91+
valid_commonname(test_cn)
92+
except Exception:
93+
saw_raise = True
94+
self.assertTrue(saw_raise)
95+
96+
def test_commonname_filter(self):
97+
for test_cn in self.COMMONNAME_PERMITTED:
98+
test_cn_unicode = as_string_of_unicode(test_cn)
99+
filtered_cn = filter_commonname(test_cn)
100+
self.assertEqual(filtered_cn, test_cn_unicode)
101+
102+
for test_cn in self.COMMONNAME_PROHIBITED:
103+
test_cn_unicode = as_string_of_unicode(test_cn)
104+
filtered_cn = filter_commonname(test_cn)
105+
self.assertNotEqual(filtered_cn, test_cn_unicode)
106+
48107

49108
if __name__ == '__main__':
50109
testmain()

0 commit comments

Comments
 (0)