source: TI12-security/trunk/ndg_saml/ndg/saml/test/binding/soap/test_attributeservice_paster.py @ 7154

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_saml/ndg/saml/test/binding/soap/test_attributeservice_paster.py@7154
Revision 7154, 3.8 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

  • added test_attributeservice_paster - tests an attribute service over an SSL connection
Line 
1"""SAML SOAP Binding Query/Response Interface with service hosted in
2Paste paster web server
3
4NERC DataGrid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "01/07/10"
8__copyright__ = "(C) 2010 Science and Technology Facilities Council"
9__license__ = "http://www.apache.org/licenses/LICENSE-2.0"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id:$'
12import logging
13logging.basicConfig(level=logging.DEBUG)
14
15import unittest
16from os import path
17from xml.etree import ElementTree
18
19from ndg.soap.utils.etree import prettyPrint
20
21from ndg.saml.saml2.core import Attribute, StatusCode
22from ndg.saml.xml.etree import ResponseElementTree
23from ndg.saml.saml2.binding.soap.client.attributequery import \
24    AttributeQuerySslSOAPBinding
25from ndg.saml.test.binding.soap import WithPasterBaseTestCase   
26   
27   
28class SamlSslSoapBindingTestCase(WithPasterBaseTestCase):
29    """Test SAML SOAP Binding with SSL"""
30    SERVICE_URI = 'https://localhost:5443/attributeauthority'
31    SUBJECT = "https://openid.localhost/philip.kershaw"
32    SUBJECT_FORMAT = "urn:ndg:saml:openid"
33    CONFIG_FILENAME = 'attribute-interface.ini'
34   
35    CLIENT_CERT_FILEPATH = path.join(WithPasterBaseTestCase.THIS_DIR, 
36                                     'test.crt')
37    CLIENT_PRIKEY_FILEPATH = path.join(WithPasterBaseTestCase.THIS_DIR, 
38                                       'test.key')
39    CLIENT_CACERT_DIR = path.join(WithPasterBaseTestCase.THIS_DIR, 'ca')
40    VALID_DNS = [
41        '/O=NDG/OU=Security/CN=localhost', 
42    ]
43   
44    def __init__(self, *arg, **kw):
45        super(SamlSslSoapBindingTestCase, self).__init__(*arg, 
46                                                         withSSL=True, 
47                                                         **kw)
48               
49    def test01M2CryptoInstalled(self):
50        # Force error for M2Crypto not present
51        _support = AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT
52        AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT = False
53        try:
54            self.assertRaises(ImportError, AttributeQuerySslSOAPBinding)
55        finally:
56            AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT = _support
57       
58        # Try again to really test all is well
59        try:
60            AttributeQuerySslSOAPBinding()
61        except ImportError, e:
62            self.fail('Import error with AttributeQuerySslSoapBinding: %s' % e)
63     
64    def test02SendQuery(self):
65        attributeQuery = AttributeQuerySslSOAPBinding()
66       
67        attributeQuery.subjectID = self.__class__.SUBJECT
68        attributeQuery.subjectIdFormat = self.__class__.SUBJECT_FORMAT
69        attributeQuery.clockSkewTolerance = 2.
70        attributeQuery.issuerName = '/O=Site A/CN=Authorisation Service'
71       
72        attribute = Attribute()
73        attribute.name = 'urn:ndg:saml:emailaddress'
74        attribute.friendlyName = 'emailAddress'
75        attribute.nameFormat = 'http://www.w3.org/2001/XMLSchema'
76       
77        attributeQuery.queryAttributes.append(attribute)
78       
79        attributeQuery.sslCACertDir = self.__class__.CLIENT_CACERT_DIR
80        attributeQuery.sslCertFilePath = self.__class__.CLIENT_CERT_FILEPATH
81        attributeQuery.sslPriKeyFilePath = self.__class__.CLIENT_PRIKEY_FILEPATH
82        attributeQuery.sslValidDNs = self.__class__.VALID_DNS
83       
84        response = attributeQuery.send(uri=self.__class__.SERVICE_URI)
85       
86        # Convert back to ElementTree instance read for string output
87        samlResponseElem = ResponseElementTree.toXML(response)
88       
89        print("SAML Response ...")
90        print(ElementTree.tostring(samlResponseElem))
91        print("Pretty print SAML Response ...")
92        print(prettyPrint(samlResponseElem))
93       
94        self.assert_(response.status.statusCode.value==StatusCode.SUCCESS_URI)
95 
96 
97if __name__ == "__main__":
98    unittest.main()       
Note: See TracBrowser for help on using the repository browser.