source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/unit/attributeauthority/saml/test_samlinterface.py @ 5550

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/unit/attributeauthority/saml/test_samlinterface.py@5550
Revision 5550, 4.9 KB checked in by pjkersha, 11 years ago (diff)

ndg.security.common.utils: fixes to TypedList? and prettyPrint classes. prettyPrint now correctly substitutes namespace prefixes from ElementTree._namespace_map

Line 
1"""Attribute Authority SAML Interface unit test package
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "21/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)
13import unittest
14
15from datetime import datetime
16import base64 
17import os
18from uuid import uuid4
19import paste.fixture
20
21from ndg.security.common.saml import Assertion, Attribute, AttributeValue, \
22    AttributeStatement, SAMLVersion, Subject, NameID, Issuer, AttributeQuery, \
23    XSStringAttributeValue
24from ndg.security.common.saml.xml import XMLConstants
25from ndg.security.common.saml.xml.etree import AssertionETreeObject, \
26    AttributeQueryETreeObject
27   
28from ndg.security.common.soap.etree import SOAPEnvelope
29
30
31class SamlSoapBindingMiddleware(object):
32    def __call__(self, environ, start_response):
33        soapRequestStream = environ['wsgi.input']
34        soapRequest = SOAPEnvelope()
35        soapRequest.parse(soapRequestStream)
36        print "Received request from client:\n"
37        print soapRequest.prettyPrint()
38       
39        soapResponse = SOAPEnvelope()
40        soapResponse.create()
41        response = soapResponse.serialize()
42        start_response("200 OK",
43                       [('Content-length', str(len(response))),
44                        ('Content-type', 'text/xml')])
45        return [response]
46
47       
48class SamlAttributeAuthorityInterfaceTestCase(unittest.TestCase):
49    """TODO: test SAML Attribute Authority interface"""
50    thisDir = os.path.dirname(os.path.abspath(__file__))
51
52    def __init__(self, *args, **kwargs):
53        wsgiApp = SamlSoapBindingMiddleware()
54        self.app = paste.fixture.TestApp(wsgiApp)
55         
56        unittest.TestCase.__init__(self, *args, **kwargs)
57       
58
59    def test01AttributeQuery(self):
60        attributeQuery = AttributeQuery()
61        attributeQuery.version = SAMLVersion(SAMLVersion.VERSION_20)
62        attributeQuery.id = str(uuid4())
63        attributeQuery.issueInstant = datetime.utcnow()
64       
65        attributeQuery.issuer = Issuer()
66        attributeQuery.issuer.format = Issuer.X509_SUBJECT
67        attributeQuery.issuer.value = \
68                        "/O=NDG/OU=BADC/CN=attributeauthority.badc.rl.ac.uk"
69                       
70                       
71        attributeQuery.subject = Subject() 
72        attributeQuery.subject.nameID = NameID()
73        attributeQuery.subject.nameID.format = "urn:esg:openid"
74        attributeQuery.subject.nameID.value = \
75                                    "https://openid.localhost/philip.kershaw"
76       
77        # special case handling for 'FirstName' attribute
78        fnAttribute = Attribute()
79        fnAttribute.name = "urn:esg:first:name"
80        fnAttribute.nameFormat = "http://www.w3.org/2001/XMLSchema#string"
81        fnAttribute.friendlyName = "FirstName"
82
83        attributeQuery.attributes.append(fnAttribute)
84   
85        # special case handling for 'LastName' attribute
86        lnAttribute = Attribute()
87        lnAttribute.name = "urn:esg:last:name"
88        lnAttribute.nameFormat = "http://www.w3.org/2001/XMLSchema#string"
89        lnAttribute.friendlyName = "LastName"
90
91        attributeQuery.attributes.append(lnAttribute)
92   
93        # special case handling for 'LastName' attribute
94        emailAddressAttribute = Attribute()
95        emailAddressAttribute.name = "urn:esg:email:address"
96        emailAddressAttribute.nameFormat = XMLConstants.XSD_NS+"#"+\
97                                    XSStringAttributeValue.TYPE_LOCAL_NAME
98        emailAddressAttribute.friendlyName = "emailAddress"
99
100        attributeQuery.attributes.append(emailAddressAttribute)                                   
101       
102        attributeQueryETreeObject = AttributeQueryETreeObject()
103        elem = attributeQueryETreeObject.create(attributeQuery)
104        query = attributeQueryETreeObject.serialize()
105        soapRequest = SOAPEnvelope()
106        soapRequest.create()
107        soapRequest.body.elem.append(elem)
108       
109        request = soapRequest.serialize()
110       
111        header = {
112            'soapAction': "http://www.oasis-open.org/committees/security",
113            'Content-length': str(len(request)),
114            'Content-type': 'text/xml'
115        }
116        response = self.app.post('/attributeauthority', 
117                                 params=request, 
118                                 headers=header, 
119                                 status=200)
120        print response.status
121
122        from ndg.security.common.utils import prettyPrint
123        soapResponse = SOAPEnvelope()
124        from cStringIO import StringIO
125        responseStream = StringIO()
126        responseStream.write(response.body)
127        responseStream.seek(0)
128        soapResponse.parse(responseStream)
129        print "Pretty print..."
130        print prettyPrint(soapResponse.elem)
131       
132
133if __name__ == "__main__":
134    unittest.main()       
135
Note: See TracBrowser for help on using the repository browser.