27
27
28
28
"""Unit tests for the migrid module pointed to in the filename"""
29
29
30
+ import codecs
30
31
import importlib
31
32
import os
32
33
import sys
34
+ from past .builtins import basestring
33
35
34
36
sys .path .append (os .path .realpath (os .path .join (os .path .dirname (__file__ ), "." )))
35
37
36
38
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'' )
37
55
38
56
39
57
class MigSharedSafeinput (MigTestCase ):
@@ -45,6 +63,47 @@ def test_existing_main(self):
45
63
safeimport = importlib .import_module ("mig.shared.safeinput" )
46
64
safeimport .main (_print = lambda _ : None )
47
65
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
+
48
107
49
108
if __name__ == '__main__' :
50
109
testmain ()
0 commit comments