1 from datetime import datetime
2 import logging
3 import unittest
4 import uuid
5
6 from ndg.saml import importElementTree
7 ET = importElementTree()
8
9 from ndg.saml.saml2.core import (SAMLVersion, Issuer)
10
11 try:
12 from ndg.saml.saml2.xacml_profile import XACMLAuthzDecisionQuery
13 from ndg.saml.xml.etree_xacml_profile import \
14 XACMLAuthzDecisionQueryElementTree
15 _xacml_support = True
16
17 except ImportError, e:
18 from warnings import warn
19 warn('Error importing XACML packages - skipping XACML profile unit ' + \
20 'tests module. (Error is: %s)' % e)
21 _xacml_support = False
22
23
24 if _xacml_support:
25
26 from ndg.xacml.core.context.action import Action
27 from ndg.xacml.core.context.environment import Environment
28 from ndg.xacml.core.context.request import Request
29 from ndg.xacml.core.context.resource import Resource
30 from ndg.xacml.core.context.subject import Subject
31
32 logging.basicConfig(level=logging.DEBUG)
33
35 """Test XML handling for XACML profile elements"""
36 ISSUER_DN = "/O=NDG/OU=BADC/CN=attributeauthority.badc.rl.ac.uk"
37
38 - def _getSingleElementText(self, contextElem, path):
39 elems = contextElem.findall(path)
40 self.assertEquals(len(elems), 1, "Single element not selected")
41 return elems[0].text
42
44
45 rcContentsStr = '''<wps:GetCapabilities
46 xmlns:ows="http://www.opengis.net/ows/1.1"
47 xmlns:wps="http://www.opengis.net/wps/1.0.0"
48 xmlns:xlink="http://www.w3.org/1999/xlink"
49 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
50 xsi:schemaLocation="http://schemas.opengis.net/wps/1.0.0/wpsGetCapabilities_request.xsd"
51 language="en-CA" service="WPS">
52 <wps:AcceptVersions>
53 <ows:Version>1.0.0</ows:Version>
54 </wps:AcceptVersions>
55 </wps:GetCapabilities>
56 '''
57
58 rcContentsElem = ET.XML(rcContentsStr)
59 resourceContent = ET.Element(
60 "{urn:oasis:names:tc:xacml:2.0:context:schema:os}ResourceContent")
61 resourceContent.append(rcContentsElem)
62 resourceContent.set('TestAttribute', 'Test Value')
63
64 resource = Resource()
65 resource.resourceContent = resourceContent
66
67 request = Request()
68 request.subjects.append(Subject())
69 request.resources.append(resource)
70 request.action = Action()
71 request.environment = Environment()
72
73 query = XACMLAuthzDecisionQuery()
74 query.xacmlContextRequest = request
75
76 query.version = SAMLVersion(SAMLVersion.VERSION_20)
77 query.id = str(uuid.uuid4())
78 query.issueInstant = datetime.utcnow()
79
80 query.issuer = Issuer()
81 query.issuer.format = Issuer.X509_SUBJECT
82 query.issuer.value = self.ISSUER_DN
83
84
85 queryElem = XACMLAuthzDecisionQueryElementTree.toXML(query)
86 print ET.tostring(queryElem)
87
88
89 self.assertEqual(queryElem.get("Version"), "2.0")
90
91 self.assertEqual(self._getSingleElementText(queryElem,
92 "{urn:oasis:names:tc:SAML:2.0:assertion}Issuer"),
93 self.ISSUER_DN)
94
95 self.assertEqual(self._getSingleElementText(queryElem,
96 "{urn:oasis:names:tc:xacml:2.0:context:schema:os}Request/"
97 "{urn:oasis:names:tc:xacml:2.0:context:schema:os}Resource/"
98 "{urn:oasis:names:tc:xacml:2.0:context:schema:os}ResourceContent/"
99 "{http://www.opengis.net/wps/1.0.0}GetCapabilities/"
100 "{http://www.opengis.net/wps/1.0.0}AcceptVersions/"
101 "{http://www.opengis.net/ows/1.1}Version"), "1.0.0")
102
103
162
163 if __name__ == "__main__":
164 unittest.main()
165