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

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

Added fix for unittests under Python2.5.

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        kw['withSSL'] = True
46        super(SamlSslSoapBindingTestCase, self).__init__(*arg, **kw)
47               
48    def test01M2CryptoInstalled(self):
49        # Force error for M2Crypto not present
50        _support = AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT
51        AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT = False
52        try:
53            self.assertRaises(ImportError, AttributeQuerySslSOAPBinding)
54        finally:
55            AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT = _support
56       
57        # Try again to really test all is well
58        try:
59            AttributeQuerySslSOAPBinding()
60        except ImportError, e:
61            self.fail('Import error with AttributeQuerySslSoapBinding: %s' % e)
62     
63    def test02SendQuery(self):
64        attributeQuery = AttributeQuerySslSOAPBinding()
65       
66        attributeQuery.subjectID = self.__class__.SUBJECT
67        attributeQuery.subjectIdFormat = self.__class__.SUBJECT_FORMAT
68        attributeQuery.clockSkewTolerance = 2.
69        attributeQuery.issuerName = '/O=Site A/CN=Authorisation Service'
70       
71        attribute = Attribute()
72        attribute.name = 'urn:ndg:saml:emailaddress'
73        attribute.friendlyName = 'emailAddress'
74        attribute.nameFormat = 'http://www.w3.org/2001/XMLSchema'
75       
76        attributeQuery.queryAttributes.append(attribute)
77       
78        attributeQuery.sslCACertDir = self.__class__.CLIENT_CACERT_DIR
79        attributeQuery.sslCertFilePath = self.__class__.CLIENT_CERT_FILEPATH
80        attributeQuery.sslPriKeyFilePath = self.__class__.CLIENT_PRIKEY_FILEPATH
81        attributeQuery.sslValidDNs = self.__class__.VALID_DNS
82       
83        response = attributeQuery.send(uri=self.__class__.SERVICE_URI)
84       
85        # Convert back to ElementTree instance read for string output
86        samlResponseElem = ResponseElementTree.toXML(response)
87       
88        print("SAML Response ...")
89        print(ElementTree.tostring(samlResponseElem))
90        print("Pretty print SAML Response ...")
91        print(prettyPrint(samlResponseElem))
92       
93        self.assert_(response.status.statusCode.value==StatusCode.SUCCESS_URI)
94 
95 
96if __name__ == "__main__":
97    unittest.main()       
Note: See TracBrowser for help on using the repository browser.