Package ndg :: Package saml :: Package test :: Package xml :: Module test_xacml_profile
[hide private]

Source Code for Module ndg.saml.test.xml.test_xacml_profile

  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       
34 - class XacmlProfileTestCase(unittest.TestCase):
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
43 - def test01(self):
44 # Construct a ResourceContent element. 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 # Construct XACMLAuthzDecisionQuery including resource content XML. 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 # Convert to element tree. 85 queryElem = XACMLAuthzDecisionQueryElementTree.toXML(query) 86 print ET.tostring(queryElem) 87 88 # Check some values from query and resource content XML. 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
104 - def test02(self):
105 # Construct a ResourceContent element. 106 rcContentsStr = '''<wps:GetCapabilities 107 xmlns:ows="http://www.opengis.net/ows/1.1" 108 xmlns:wps="http://www.opengis.net/wps/1.0.0" 109 xmlns:xlink="http://www.w3.org/1999/xlink" 110 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 111 xsi:schemaLocation="http://schemas.opengis.net/wps/1.0.0/wpsGetCapabilities_request.xsd" 112 language="en-CA" service="WPS"> 113 <wps:AcceptVersions> 114 <ows:Version>1.0.0</ows:Version> 115 </wps:AcceptVersions> 116 </wps:GetCapabilities> 117 ''' 118 # Construct XACMLAuthzDecisionQuery including resource content XML. 119 rcContentsElem = ET.XML(rcContentsStr) 120 resourceContent = ET.Element( 121 "{urn:oasis:names:tc:xacml:2.0:context:schema:os}ResourceContent") 122 resourceContent.append(rcContentsElem) 123 resourceContent.set('TestAttribute', 'Test Value') 124 125 resource = Resource() 126 resource.resourceContent = resourceContent 127 128 request = Request() 129 request.subjects.append(Subject()) 130 request.resources.append(resource) 131 request.action = Action() 132 request.environment = Environment() 133 134 query = XACMLAuthzDecisionQuery() 135 query.xacmlContextRequest = request 136 137 query.version = SAMLVersion(SAMLVersion.VERSION_20) 138 query.id = str(uuid.uuid4()) 139 query.issueInstant = datetime.utcnow() 140 141 query.issuer = Issuer() 142 query.issuer.format = Issuer.X509_SUBJECT 143 query.issuer.value = self.ISSUER_DN 144 145 # Convert to element tree. 146 queryElem = XACMLAuthzDecisionQueryElementTree.toXML(query) 147 148 # Convert back to object tree. 149 query2 = XACMLAuthzDecisionQueryElementTree.fromXML(queryElem) 150 151 # Check some values from the query and the resource content XML. 152 self.assertEqual(query2.version, SAMLVersion(SAMLVersion.VERSION_20)) 153 self.assertEqual(query2.issuer.value, self.ISSUER_DN) 154 self.assertEqual(len(query2.xacmlContextRequest.resources), 1) 155 156 rcContentsElem2 = query2.xacmlContextRequest.resources[0 157 ].resourceContent 158 self.assertEqual(self._getSingleElementText(rcContentsElem2, 159 "{http://www.opengis.net/wps/1.0.0}GetCapabilities/" 160 "{http://www.opengis.net/wps/1.0.0}AcceptVersions/" 161 "{http://www.opengis.net/ows/1.1}Version"), "1.0.0")
162 163 if __name__ == "__main__": 164 unittest.main() 165