source: TI12-security/trunk/NDG_XACML/ndg/xacml/test/test_context.py @ 6792

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/test/test_context.py@6792
Revision 6792, 5.5 KB checked in by pjkersha, 10 years ago (diff)

Made ndg.xacml.core.expression.Expression abstract: derived classes must implement the evaluate method used to evaluate a result in a rule condition.

Line 
1#!/usr/bin/env python
2"""NDG XACML Context unit test package
3
4NERC DataGrid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "26/03/10"
8__copyright__ = "(C) 2010 Science and Technology Facilities Council"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__license__ = "BSD - see LICENSE file in top-level directory"
11__contact__ = "Philip.Kershaw@stfc.ac.uk"
12__revision__ = "$Id: $"
13import unittest
14from os import path
15import logging
16logging.basicConfig(level=logging.DEBUG)
17
18from ndg.xacml.test import XACML_NDGTEST1_FILEPATH
19from ndg.xacml.parsers.etree.factory import ReaderFactory
20from ndg.xacml.core.context.pdpinterface import PDPInterface
21from ndg.xacml.core.context.pdp import PDP
22from ndg.xacml.core.context.handler import AbstractContextHandler
23from ndg.xacml.core.attribute import Attribute
24from ndg.xacml.core.attributevalue import AttributeValue
25from ndg.xacml.core.context.request import Request
26from ndg.xacml.core.context.response import Response
27from ndg.xacml.core.context.result import Result, Decision
28from ndg.xacml.core.context.subject import Subject
29from ndg.xacml.core.context.resource import Resource
30from ndg.xacml.core.context.action import Action
31
32               
33class TestContextHandler(AbstractContextHandler):
34    """Test implementation of Context Handler"""
35   
36    def __init__(self):
37        super(TestContextHandler, self).__init__()
38        self.pip = None       
39       
40    def handlePEPRequest(self, myRequest):
41       
42        # Convert myRequest to XACML context request
43        request = myRequest
44       
45        if self.pdp is None:
46            raise TypeError('No "pdp" attribute set')
47       
48        response = self.pdp.evaluate(request)
49       
50        # Convert XACML context response to domain specific request
51        myResponse = response
52       
53        return myResponse
54
55
56class XACMLContextTestCase(unittest.TestCase):
57    """Test PDP, PAP, PIP and Context handler"""
58   
59    def _createRequestCtx(self):
60        request = Request()
61        subject = Subject()
62       
63        openidSubjectAttribute = Attribute()
64        roleAttribute = Attribute()
65       
66        openidSubjectAttribute.attributeId = "urn:esg:openid"
67        openidSubjectAttribute.dataType = \
68                                    'http://www.w3.org/2001/XMLSchema#anyURI'
69        openidSubjectAttribute.attributeValues.append(AttributeValue())
70        openidSubjectAttribute.attributeValues[-1].dataType = \
71                                    'http://www.w3.org/2001/XMLSchema#anyURI'
72        openidSubjectAttribute.attributeValues[-1].value = \
73                                    'https://my.name.somewhere.ac.uk'
74       
75        subject.attributes.append(openidSubjectAttribute)
76
77        roleAttribute.attributeId = "urn:ndg:security:authz:1.0:attr"
78        roleAttribute.dataType = 'http://www.w3.org/2001/XMLSchema#string'
79        roleAttribute.attributeValues.append(AttributeValue())
80        roleAttribute.attributeValues[-1].dataType = \
81                                    'http://www.w3.org/2001/XMLSchema#string'
82        roleAttribute.attributeValues[-1].value = 'staff' 
83       
84        subject.attributes.append(roleAttribute)
85                                 
86        request.subjects.append(subject)
87       
88        resource = Resource()
89        resourceAttribute = Attribute()
90        resource.attributes.append(resourceAttribute)
91       
92        resourceAttribute.attributeId = \
93                            "urn:oasis:names:tc:xacml:1.0:resource:resource-id"
94                           
95        resourceAttribute.dataType = "http://www.w3.org/2001/XMLSchema#anyURI"
96        resourceAttribute.attributeValues.append(AttributeValue())
97        resourceAttribute.attributeValues[-1].value = \
98                                        'http://www.localhost/test_securedURI'
99
100        request.resources.append(resource)
101       
102        request.action = Action()
103        actionAttribute = Attribute()
104        request.action.attributes.append(actionAttribute)
105       
106        actionAttribute.attributeId = \
107                                "urn:oasis:names:tc:xacml:1.0:action:action-id"
108        actionAttribute.dataType = "http://www.w3.org/2001/XMLSchema#string"
109        actionAttribute.attributeValues.append(AttributeValue())
110        actionAttribute.attributeValues[-1].value = 'read'
111       
112        return request
113   
114    def test01CreateRequest(self):
115        requestCtx = self._createRequestCtx()
116        self.assert_(requestCtx)
117       
118    def test02CreateResponse(self):
119        response = Response()
120        result = Result()
121        response.results.append(result)
122        result.decision = Decision()
123        result.decision.value = Decision.NOT_APPLICABLE
124       
125    def test03AbstractCtxHandler(self):
126        self.assertRaises(TypeError, AbstractContextHandler)
127       
128    def test04CreateCtxHandler(self):
129        ctxHandler = TestContextHandler()
130       
131    def test04PDPInterface(self):
132        self.assertRaises(TypeError, PDPInterface)
133       
134    def test05CreatePDP(self):
135        pdp = PDP()
136        self.assert_(pdp)
137       
138    def _createPDPfromPolicy(self):
139        pdp = PDP.fromPolicySource(XACML_NDGTEST1_FILEPATH, ReaderFactory)
140        return pdp
141       
142    def test06CreatePDPfromPolicy(self):
143        pdp = self._createPDPfromPolicy()
144        self.assert_(pdp)
145       
146    def test07EvaluatePDP(self):
147        request = self._createRequestCtx()
148        pdp = self._createPDPfromPolicy()
149        response = pdp.evaluate(request)
150        self.assert_(response)
151
152       
153if __name__ == "__main__":
154    unittest.main()
Note: See TracBrowser for help on using the repository browser.