Package openid :: Package test :: Module trustroot
[frames] | no frames]

Source Code for Module openid.test.trustroot

 1  import os 
 2  import unittest 
 3  from openid.server.trustroot import TrustRoot 
 4   
5 -class _ParseTest(unittest.TestCase):
6 - def __init__(self, sanity, desc, case):
7 unittest.TestCase.__init__(self) 8 self.desc = desc + ': ' + repr(case) 9 self.case = case 10 self.sanity = sanity
11
12 - def shortDescription(self):
13 return self.desc
14
15 - def runTest(self):
16 tr = TrustRoot.parse(self.case) 17 if self.sanity == 'sane': 18 assert tr.isSane(), self.case 19 elif self.sanity == 'insane': 20 assert not tr.isSane(), self.case 21 else: 22 assert tr is None, tr
23
24 -class _MatchTest(unittest.TestCase):
25 - def __init__(self, match, desc, line):
26 unittest.TestCase.__init__(self) 27 tr, rt = line.split() 28 self.desc = desc + ': ' + repr(tr) + ' ' + repr(rt) 29 self.tr = tr 30 self.rt = rt 31 self.match = match
32
33 - def shortDescription(self):
34 return self.desc
35
36 - def runTest(self):
37 tr = TrustRoot.parse(self.tr) 38 self.failIf(tr is None, self.tr) 39 40 match = tr.validateURL(self.rt) 41 if self.match: 42 assert match 43 else: 44 assert not match
45
46 -def getTests(t, grps, head, dat):
47 tests = [] 48 top = head.strip() 49 gdat = map(str.strip, dat.split('-' * 40 + '\n')) 50 assert not gdat[0] 51 assert len(gdat) == (len(grps) * 2 + 1), (gdat, grps) 52 i = 1 53 for x in grps: 54 n, desc = gdat[i].split(': ') 55 cases = gdat[i + 1].split('\n') 56 assert len(cases) == int(n) 57 for case in cases: 58 tests.append(t(x, top + ' - ' + desc, case)) 59 i += 2 60 return tests
61
62 -def parseTests(data):
63 parts = map(str.strip, data.split('=' * 40 + '\n')) 64 assert not parts[0] 65 _, ph, pdat, mh, mdat = parts 66 67 tests = [] 68 tests.extend(getTests(_ParseTest, ['bad', 'insane', 'sane'], ph, pdat)) 69 tests.extend(getTests(_MatchTest, [1, 0], mh, mdat)) 70 return tests
71
72 -def pyUnitTests():
73 here = os.path.dirname(os.path.abspath(__file__)) 74 test_data_file_name = os.path.join(here, 'data', 'trustroot.txt') 75 test_data_file = file(test_data_file_name) 76 test_data = test_data_file.read() 77 test_data_file.close() 78 79 tests = parseTests(test_data) 80 return unittest.TestSuite(tests)
81 82 if __name__ == '__main__': 83 suite = pyUnitTests() 84 runner = unittest.TextTestRunner() 85 runner.run(suite) 86