source: TI12-security/trunk/ndg_xacml/ndg/xacml/test/__init__.py @ 7666

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

Started testing PDP with custom Group/Role? Attribute Value type. Custom match and Bag functions are needed to enable evaluation of rules using the custom type.

  • Property svn:keywords set to Id
Line 
1"""NDG XACML unit test package
2
3NERC DataGrid
4"""
5__author__ = "P J Kershaw"
6__date__ = "16/03/10"
7__copyright__ = "(C) 2010 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id$"
12from os import path
13
14from ndg.xacml.core.attributevalue import (AttributeValueClassFactory, 
15                                           AttributeValue)
16from ndg.xacml.parsers import XMLParseError
17from ndg.xacml.parsers.etree import QName
18from ndg.xacml.parsers.etree.attributevaluereader import (
19                                                DataTypeReaderClassFactory,
20                                                ETreeDataTypeReaderBase)
21
22THIS_DIR = path.dirname(__file__)
23XACML_NDGTEST1_FILENAME = "ndg1.xml"
24XACML_NDGTEST1_FILEPATH = path.join(THIS_DIR, XACML_NDGTEST1_FILENAME)
25XACML_ESGFTEST1_FILENAME = "esgf1.xml"
26XACML_ESGFTEST1_FILEPATH = path.join(THIS_DIR, XACML_ESGFTEST1_FILENAME)
27
28
29class GroupRoleAttributeValue(AttributeValue):
30    """Example Custom Attribute Value"""
31    IDENTIFIER = 'urn:grouprole'
32    TYPE = dict
33    GROUPROLE_ELEMENT_LOCAL_NAME = 'groupRole'
34    GROUP_ELEMENT_LOCAL_NAME = 'group'
35    ROLE_ELEMENT_LOCAL_NAME = 'role'
36    ROLE_DEFAULT_VALUE = 'default'
37   
38    __slots__ = ('group', 'role')
39   
40    def __init__(self):
41        super(GroupRoleAttributeValue, self).__init__()
42        self.group = None
43        self.role = self.__class__.ROLE_DEFAULT_VALUE
44       
45    @property
46    def value(self):
47        """Override default value property to give custom result.  Also,
48        'value' becomes a read-only property"""
49        return self.group, self.value
50   
51   
52class ETreeGroupRoleDataTypeReader(ETreeDataTypeReaderBase):
53    """Example custom parser to read custom attribute value data type"""
54   
55    @classmethod
56    def parse(cls, elem, attributeValue):
57        """Parse ESG Group/Role type object
58
59        @param obj: input object to parse
60        @type obj: ElementTree Element, or stream object
61        @return: ElementTree element
62        @rtype: xml.etree.Element
63        """
64        if len(elem) != 1:
65            raise XMLParseError("Expecting single groupRole child element but " 
66                                "found only %d element(s)" % len(elem))
67                     
68        groupRoleElem = elem[0]
69       
70        if (QName.getLocalPart(groupRoleElem.tag) != 
71            attributeValue.__class__.GROUPROLE_ELEMENT_LOCAL_NAME):
72            raise XMLParseError("%r element found, expecting \"%s\" element " 
73                        "instead" % 
74                        attributeValue.__class__.GROUPROLE_ELEMENT_LOCAL_NAME)
75       
76        # Allow for any of the defined Expression sub-types in the child
77        # elements
78        for subElem in groupRoleElem:
79            localName = QName.getLocalPart(subElem.tag)
80            if localName == attributeValue.__class__.ROLE_ELEMENT_LOCAL_NAME:
81                attributeValue.role = subElem.text
82            elif localName == attributeValue.__class__.GROUP_ELEMENT_LOCAL_NAME:
83                attributeValue.group = subElem.text
84            else:
85                raise XMLParseError('%r ESG Group/Role sub-element not '
86                                    'recognised' % localName) 
Note: See TracBrowser for help on using the repository browser.