source: TI12-security/trunk/python/ndg_security_test/ndg/security/test/unit/openid/relyingparty/validation/test_validation.py @ 5779

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg_security_test/ndg/security/test/unit/openid/relyingparty/validation/test_validation.py@5779
Revision 5779, 3.7 KB checked in by pjkersha, 11 years ago (diff)

Integrated automated start-up and shutdown of Paste http servers for unit tests.

Line 
1"""OpenID IdP Validation unit test package
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "16/07/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id$'
11import logging
12logging.basicConfig(level=logging.DEBUG)
13
14import os
15import unittest
16from ndg.security.test.unit import BaseTestCase, mkDataDirPath
17from ndg.security.server.wsgi.openid.relyingparty.validation import \
18    IdPValidator, IdPValidationDriver, IdPInvalidException, \
19    SSLIdPValidationDriver, SSLClientAuthNValidator
20   
21class ProviderWhitelistValidator(IdPValidator):
22    """Test stub for Whitelist validator"""
23    def __init__(self):
24        pass
25   
26    def initialize(self, **parameters):
27        '''@raise ConfigException:''' 
28        assert('config-file' in parameters)
29       
30    def validate(self, idpEndpoint, idpIdentity):
31        '''@raise IdPInvalidException:
32        @raise ConfigException:''' 
33        pass
34
35
36class ProviderIdentifierTestValidator(IdPValidator):
37    """Test stub for identifier validator - fixed to reject all IdPs"""
38    def __init__(self):
39        pass
40
41    def initialize(self, **parameters):
42        '''@raise ConfigException:''' 
43        assert('config-file' in parameters)
44       
45    def validate(self, idpEndpoint, idpIdentity):
46        '''Test method hard wired to raise an invalid IdP exception
47        @raise IdPInvalidException:
48        @raise ConfigException:''' 
49        raise IdPInvalidException("%s is invalid" % idpEndpoint)
50
51
52class DiscoveryInfoPlaceHolder(object):
53    getOPEndpoint = lambda self: 'https://localhost/openid/provider'
54
55 
56class IdentifierPlaceHolder(object):
57    getIdentifier = lambda self: 'myid'
58
59from M2Crypto import X509
60
61class X509StoreCtxPlaceHolder(object):
62    x509CertFilePath = mkDataDirPath(os.path.join('pki', 'localhost.crt'))
63   
64    def get_current_cert(self):
65        return X509.load_cert(X509StoreCtxPlaceHolder.x509CertFilePath)
66   
67class IdPValidationTestCase(BaseTestCase):
68    thisDir = os.path.dirname(os.path.abspath(__file__))
69    idpConfigFilePath = os.path.join(thisDir, 'idpvalidator.xml')
70    os.environ['NDGSEC_UNITTEST_IDPVALIDATION_DIR'] = thisDir
71   
72    def test01IdPConfigFileEnvVarNotSet(self):
73        identifier = IdentifierPlaceHolder()
74        discoveries = [DiscoveryInfoPlaceHolder()]
75       
76        idPValidationDriver = IdPValidationDriver()
77        validDiscoveries = idPValidationDriver.performIdPValidation(identifier,
78                                                                discoveries)
79        # Expect no discoveries returned because the IDP_CONFIG_FILE
80        # environment variable is not set
81        self.assert_(len(validDiscoveries) == 1)
82       
83    def test02WithIdPConfigFile(self):
84        identifier = IdentifierPlaceHolder()
85        discoveries = [DiscoveryInfoPlaceHolder()]
86       
87        os.environ['IDP_CONFIG_FILE'] = IdPValidationTestCase.idpConfigFilePath
88        idPValidationDriver = IdPValidationDriver()
89        validDiscoveries = idPValidationDriver.performIdPValidation(identifier,
90                                                                discoveries)
91        self.assert_(len(validDiscoveries) == 1)
92       
93    def test03SSLValidation(self):
94        idpConfigFilePath = os.path.join(IdPValidationTestCase.thisDir, 
95                                         'ssl-idp-validator.xml')
96        idPValidationDriver = SSLIdPValidationDriver(
97                                        idpConfigFilePath=idpConfigFilePath)
98       
99        # preVerifyOK set to 1 to indicate all is otherwise OK with
100        # verification
101        idPValidationDriver(1, X509StoreCtxPlaceHolder())
102       
103if __name__ == "__main__":
104    unittest.main()       
Note: See TracBrowser for help on using the repository browser.