Skip to content

Commit 7607f78

Browse files
committed
Carve out mig.shared.safeinput commonname checks as formal test cases.
1 parent 0ab5cac commit 7607f78

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
@@ -2276,20 +2276,6 @@ def __str__(self):
22762276
def main(_print=print):
22772277
print = _print # workaround print as reserved word on PY2
22782278

2279-
for test_cn in ('Firstname Lastname', 'Test Æøå', 'Test Überh4x0r',
2280-
'Harry S. Truman', u'Unicode æøå', "Invalid D'Angelo",
2281-
'Test Maybe Invalid Źacãŕ', 'Test Invalid ?',
2282-
'Test HTML Invalid <code/>'):
2283-
try:
2284-
print('Testing valid_commonname: %s' % test_cn)
2285-
print('Filtered commonname: %s' % filter_commonname(test_cn))
2286-
# print 'DEBUG %s only in %s' % ([test_cn],
2287-
# [VALID_NAME_CHARACTERS])
2288-
valid_commonname(test_cn)
2289-
print('Accepted raw commonname!')
2290-
except Exception as exc:
2291-
print('Rejected raw commonname %r: %s' % (test_cn, exc))
2292-
22932279
for test_org in ('UCPH', 'Some University, Some Dept.', 'Green Shoes Ltd.',
22942280
u'Unicode Org', "Invalid R+D", "Invalid R/D",
22952281
"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
@@ -1,12 +1,30 @@
11
# -*- coding: utf-8 -*-
22

3+
import codecs
34
import importlib
45
import os
56
import sys
7+
from past.builtins import basestring
68

79
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
810

911
from support import MigTestCase, testmain
12+
from mig.shared.safeinput import \
13+
filter_commonname, valid_commonname
14+
15+
PY2 = sys.version_info[0] == 2
16+
17+
18+
def as_string_of_unicode(value):
19+
assert isinstance(value, basestring)
20+
if not is_string_of_unicode(value):
21+
assert PY2, "unreachable unless Python 2"
22+
return unicode(codecs.decode(value, 'utf8'))
23+
return value
24+
25+
26+
def is_string_of_unicode(value):
27+
return type(value) == type(u'')
1028

1129

1230
class MigSharedSafeinput(MigTestCase):
@@ -18,6 +36,47 @@ def test_existing_main(self):
1836
safeimport = importlib.import_module("mig.shared.safeinput")
1937
safeimport.main(_print=lambda _: None)
2038

39+
COMMONNAME_PERMITTED = (
40+
'Firstname Lastname',
41+
'Test Æøå',
42+
'Test Überh4x0r',
43+
'Harry S. Truman',
44+
u'Unicode æøå')
45+
46+
COMMONNAME_PROHIBITED = (
47+
"Invalid D'Angelo",
48+
'Test Maybe Invalid Źacãŕ',
49+
'Test Invalid ?',
50+
'Test HTML Invalid <code/>')
51+
52+
def test_commonname_valid(self):
53+
for test_cn in self.COMMONNAME_PERMITTED:
54+
saw_raise = False
55+
try:
56+
valid_commonname(test_cn)
57+
except Exception:
58+
saw_raise = True
59+
self.assertFalse(saw_raise)
60+
61+
for test_cn in self.COMMONNAME_PROHIBITED:
62+
saw_raise = False
63+
try:
64+
valid_commonname(test_cn)
65+
except Exception:
66+
saw_raise = True
67+
self.assertTrue(saw_raise)
68+
69+
def test_commonname_filter(self):
70+
for test_cn in self.COMMONNAME_PERMITTED:
71+
test_cn_unicode = as_string_of_unicode(test_cn)
72+
filtered_cn = filter_commonname(test_cn)
73+
self.assertEqual(filtered_cn, test_cn_unicode)
74+
75+
for test_cn in self.COMMONNAME_PROHIBITED:
76+
test_cn_unicode = as_string_of_unicode(test_cn)
77+
filtered_cn = filter_commonname(test_cn)
78+
self.assertNotEqual(filtered_cn, test_cn_unicode)
79+
2180

2281
if __name__ == '__main__':
2382
testmain()

0 commit comments

Comments
 (0)