source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/utils/testConfigFileParsers.py @ 4131

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/utils/testConfigFileParsers.py@4131
Revision 4131, 4.5 KB checked in by cbyrom, 11 years ago (diff)

Create new utility package with class, ConfigFileParsers? - a utility
class with parsers for XML and INI style config files. This takes
a filename, together with an optional dictionary of valid keys (to
check for invalid config inputs) + optional section list (to restrict
parsing of INI files to particular sections) and returns a
dictionary of read in properties. NB, if valid keys are specified
and not featured in the prop file, default values are set up in the
returned property dict.
Implemented use of the ConfigFileParsers? in the AttAuthority? service.
Added new testsuite (together with noseTests class to drive tests) to
exercise the new parsers in the context of the AttAuthority? section +
added test config files.

Line 
1'''
2A test class for the Utilities module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest, os
8from ndg.security.common.utils.ConfigFileParsers import readProperties
9from ndg.security.server.AttAuthority import AttAuthority
10from ConfigParser import MissingSectionHeaderError
11
12class testConfigFileParsers(unittest.TestCase):
13   
14    BASE_DIR_ENV_VAR = '$PYTHON_TEST_DIR'
15    BASE_DIR = os.path.expandvars(BASE_DIR_ENV_VAR)
16    ATT_AUTH_INI_FILE = BASE_DIR + "/siteAAttAuthority.cfg"
17    ATT_AUTH_XML_FILE = BASE_DIR + "/siteAAttAuthorityProperties.xml"
18    INVALID_INI_FILE = BASE_DIR + "/invalidSiteAAttAuthority.cfg"
19    INVALID_XML_FILE = BASE_DIR + "/invalidSiteAAttAuthorityProperties.xml"
20    MISSING_INI_FILE = "blah"
21    MISSING_XML_FILE = "blah.xml"
22   
23    VALID_ROLES_MOD_NAME = 'siteAUserRoles' 
24    def setUp(self):
25        '''
26        set up data used in the tests.
27        '''
28        if self.BASE_DIR == self.BASE_DIR_ENV_VAR:
29            raise SystemError("The '$PYTHON_TEST_DIR' environment variable is not set up\
30                - please set this before running the tests")
31
32    def testMissingINIFile(self):
33        self.assertRaises(ValueError,readProperties, self.MISSING_INI_FILE, AttAuthority._AttAuthority__validKeys)
34
35    def testMissingXMLFile(self):
36        self.assertRaises(ValueError,readProperties, self.MISSING_XML_FILE, AttAuthority._AttAuthority__validKeys)
37
38    def testInvalidINIFile(self):
39        self.assertRaises(MissingSectionHeaderError,readProperties, self.INVALID_INI_FILE, AttAuthority._AttAuthority__validKeys)
40
41    def testInvalidXMLFile(self):
42        self.assertRaises(SyntaxError,readProperties, self.INVALID_XML_FILE, AttAuthority._AttAuthority__validKeys)
43           
44    def testBasicINIInput(self):
45       
46        cfg = readProperties(self.ATT_AUTH_INI_FILE, AttAuthority._AttAuthority__validKeys)
47        # do some basic checks of what is returned
48        self.assertNotEqual(cfg, None)
49        self.assertEqual(type(cfg), dict)
50        self.assertEqual(cfg['useSSL'], False)
51        self.assertEqual(cfg['userRolesModName'], self.VALID_ROLES_MOD_NAME)
52        self.assertEqual(type(cfg['caCertFileList']), list)
53        self.assertEqual(len(cfg['caCertFileList']), 2)
54        self.assertEqual(cfg['caCertFileList'][0], '$NDGSEC_AACLNT_UNITTEST_DIR/ca/ndg-test-ca.crt')
55
56
57    def testBasicXMLInput(self):
58       
59        cfg = readProperties(self.ATT_AUTH_XML_FILE, AttAuthority._AttAuthority__validKeys)
60        # do some basic checks of what is returned
61        self.assertNotEqual(cfg, None)
62        self.assertEqual(type(cfg), dict)
63        self.assertEqual(cfg['useSSL'], False)
64        self.assertEqual(cfg['userRolesModName'], self.VALID_ROLES_MOD_NAME)
65        self.assertEqual(type(cfg['caCertFileList']), list)
66        self.assertEqual(len(cfg['caCertFileList']), 2)
67        self.assertEqual(cfg['caCertFileList'][0], '$NDGSEC_AACLNT_UNITTEST_DIR/ca/ndg-test-ca.crt')
68
69
70    def testINIandXMLInput(self):
71        cfgINI = readProperties(self.ATT_AUTH_INI_FILE, AttAuthority._AttAuthority__validKeys)
72        cfgXML = readProperties(self.ATT_AUTH_XML_FILE, AttAuthority._AttAuthority__validKeys)
73        self.assertEqual(cfgINI, cfgXML)
74       
75
76    def testAttAuthorityINIInput(self):
77       
78        cfg = readProperties(self.ATT_AUTH_INI_FILE, AttAuthority._AttAuthority__validKeys)
79        # do some basic checks of what is returned
80        self.assertNotEqual(cfg, None)
81        self.assertEqual(type(cfg), dict)
82        self.assertEqual(cfg['useSSL'], False)
83        self.assertEqual(cfg['userRolesModName'], self.VALID_ROLES_MOD_NAME)
84        self.assertEqual(type(cfg['caCertFileList']), list)
85        self.assertEqual(len(cfg['caCertFileList']), 2)
86        self.assertEqual(cfg['caCertFileList'][0], '$NDGSEC_AACLNT_UNITTEST_DIR/ca/ndg-test-ca.crt')
87
88
89    def testAttAuthorityXMLInput(self):
90       
91        cfg = readProperties(self.ATT_AUTH_XML_FILE, AttAuthority._AttAuthority__validKeys)
92        # do some basic checks of what is returned
93        self.assertNotEqual(cfg, None)
94        self.assertEqual(type(cfg), dict)
95        self.assertEqual(cfg['useSSL'], False)
96        self.assertEqual(cfg['userRolesModName'], self.VALID_ROLES_MOD_NAME)
97        self.assertEqual(type(cfg['caCertFileList']), list)
98        self.assertEqual(len(cfg['caCertFileList']), 2)
99        self.assertEqual(cfg['caCertFileList'][0], '$NDGSEC_AACLNT_UNITTEST_DIR/ca/ndg-test-ca.crt')
100                         
101       
102       
103
104if __name__ == '__main__':
105
106    unittest.main()
Note: See TracBrowser for help on using the repository browser.