source: TI12-security/branches/ndg-security-1.5.x/ndg_security_test/ndg/security/test/unit/openid/relyingparty/validation/test_validation.py @ 6633

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

Merging in changes from 6557

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   
21   
22class ProviderWhitelistValidator(IdPValidator):
23    """Test stub for Whitelist validator"""
24    def __init__(self):
25        pass
26   
27    def initialize(self, **parameters):
28        '''@raise ConfigException:''' 
29        assert('config-file' in parameters)
30       
31    def validate(self, idpEndpoint, idpIdentity):
32        '''@raise IdPInvalidException:
33        @raise ConfigException:''' 
34        pass
35
36
37class ProviderIdentifierTestValidator(IdPValidator):
38    """Test stub for identifier validator - fixed to reject all IdPs"""
39    def __init__(self):
40        pass
41
42    def initialize(self, **parameters):
43        '''@raise ConfigException:''' 
44        assert('config-file' in parameters)
45       
46    def validate(self, idpEndpoint, idpIdentity):
47        '''Test method hard wired to raise an invalid IdP exception
48        @raise IdPInvalidException:
49        @raise ConfigException:''' 
50        raise IdPInvalidException("%s is invalid" % idpEndpoint)
51
52
53class DiscoveryInfoPlaceHolder(object):
54    getOPEndpoint = lambda self: 'https://localhost/openid/provider'
55
56 
57class IdentifierPlaceHolder(object):
58    getIdentifier = lambda self: 'myid'
59
60from M2Crypto import X509
61
62class X509StoreCtxPlaceHolder(object):
63    x509CertFilePath = mkDataDirPath(os.path.join('pki', 'localhost.crt'))
64   
65    def get1_chain(self):
66        return [X509.load_cert(X509StoreCtxPlaceHolder.x509CertFilePath)]
67   
68class IdPValidationTestCase(BaseTestCase):
69    thisDir = os.path.dirname(os.path.abspath(__file__))
70    IDP_CONFIG_FILEPATH = os.path.join(thisDir, 'idpvalidator.xml')
71    os.environ['NDGSEC_UNITTEST_IDPVALIDATION_DIR'] = thisDir
72   
73    def test01IdPConfigFileEnvVarNotSet(self):
74        identifier = IdentifierPlaceHolder()
75        discoveries = [DiscoveryInfoPlaceHolder()]
76       
77        idPValidationDriver = IdPValidationDriver()
78        validDiscoveries = idPValidationDriver.performIdPValidation(identifier,
79                                                                discoveries)
80        # Expect no discoveries returned because the IDP_CONFIG_FILE
81        # environment variable is not set
82        self.assert_(len(validDiscoveries) == 1)
83       
84    def test02WithIdPConfigFile(self):
85        identifier = 'https://pjk.badc.rl.ac.uk'
86       
87        os.environ[IdPValidationDriver.IDP_CONFIG_FILEPATH_ENV_VARNAME
88            ] = IdPValidationTestCase.IDP_CONFIG_FILEPATH
89           
90        idPValidationDriver = IdPValidationDriver()
91        validDiscoveries = idPValidationDriver.performIdPValidation(identifier)
92        self.assert_(len(validDiscoveries) == 2)
93       
94    def test03SSLValidation(self):
95        idpConfigFilePath = os.path.join(IdPValidationTestCase.thisDir, 
96                                         'ssl-idp-validator.xml')
97        idPValidationDriver = SSLIdPValidationDriver(
98                                        idpConfigFilePath=idpConfigFilePath)
99       
100        # preVerifyOK set to 1 to indicate all is otherwise OK with
101        # verification
102        idPValidationDriver(1, X509StoreCtxPlaceHolder())
103       
104       
105if __name__ == "__main__":
106    unittest.main()       
Note: See TracBrowser for help on using the repository browser.