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

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

Working and tested version with functionality for adding custom attribute value types and functions.

  • 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.core.functions.v1.bag import BagBase
17from ndg.xacml.core.functions.v1.at_least_one_member_of import \
18    AtLeastOneMemberOfBase
19   
20from ndg.xacml.parsers import XMLParseError
21from ndg.xacml.parsers.etree import QName
22from ndg.xacml.parsers.etree.attributevaluereader import (
23                                                DataTypeReaderClassFactory,
24                                                ETreeDataTypeReaderBase)
25
26THIS_DIR = path.dirname(__file__)
27XACML_NDGTEST1_FILENAME = "ndg1.xml"
28XACML_NDGTEST1_FILEPATH = path.join(THIS_DIR, XACML_NDGTEST1_FILENAME)
29XACML_ESGFTEST1_FILENAME = "esgf1.xml"
30XACML_ESGFTEST1_FILEPATH = path.join(THIS_DIR, XACML_ESGFTEST1_FILENAME)
31
32
33class GroupRoleAttributeValue(AttributeValue):
34    """Example Custom Attribute Value"""
35    IDENTIFIER = 'urn:grouprole'
36    TYPE = dict
37    GROUPROLE_ELEMENT_LOCAL_NAME = 'groupRole'
38    GROUP_ELEMENT_LOCAL_NAME = 'group'
39    ROLE_ELEMENT_LOCAL_NAME = 'role'
40    ROLE_DEFAULT_VALUE = 'default'
41   
42    __slots__ = ('group', 'role')
43   
44    def __init__(self):
45        super(GroupRoleAttributeValue, self).__init__()
46        self.group = None
47        self.role = self.__class__.ROLE_DEFAULT_VALUE
48       
49    @property
50    def value(self):
51        """Override default value property to give custom result.  Also,
52        'value' becomes a read-only property.  Making this change is critical
53        to the function of the GroupRoleAtLeastOneMemberOf class below - it
54        relies on being able to make comparison of the value attribute of
55        different GroupRoleAttributeValue instances.  Defined this way,
56        comparison is by group,role to group,role tuple
57        """
58        return self.group, self.role
59   
60
61class GroupRoleBag(BagBase):
62    """Bag function for Group/Role custom attribute value type"""
63    TYPE = GroupRoleAttributeValue
64    FUNCTION_NS = 'urn:grouprole-bag'
65
66 
67class GroupRoleAtLeastOneMemberOf(AtLeastOneMemberOfBase):
68    """At least one member of function for Group/Role custom attribute value
69    type"""
70    TYPE = GroupRoleAttributeValue
71    FUNCTION_NS = 'urn:grouprole-bag'
72
73   
74class ETreeGroupRoleDataTypeReader(ETreeDataTypeReaderBase):
75    """Example custom parser to read custom attribute value data type"""
76   
77    @classmethod
78    def parse(cls, elem, attributeValue):
79        """Parse ESG Group/Role type object
80
81        @param obj: input object to parse
82        @type obj: ElementTree Element, or stream object
83        @return: ElementTree element
84        @rtype: xml.etree.Element
85        """
86        if len(elem) != 1:
87            raise XMLParseError("Expecting single groupRole child element but " 
88                                "found only %d element(s)" % len(elem))
89                     
90        groupRoleElem = elem[0]
91       
92        if (QName.getLocalPart(groupRoleElem.tag) != 
93            attributeValue.__class__.GROUPROLE_ELEMENT_LOCAL_NAME):
94            raise XMLParseError("%r element found, expecting \"%s\" element " 
95                        "instead" % 
96                        attributeValue.__class__.GROUPROLE_ELEMENT_LOCAL_NAME)
97       
98        # Allow for any of the defined Expression sub-types in the child
99        # elements
100        for subElem in groupRoleElem:
101            localName = QName.getLocalPart(subElem.tag)
102            if localName == attributeValue.__class__.ROLE_ELEMENT_LOCAL_NAME:
103                attributeValue.role = subElem.text
104            elif localName == attributeValue.__class__.GROUP_ELEMENT_LOCAL_NAME:
105                attributeValue.group = subElem.text
106            else:
107                raise XMLParseError('%r ESG Group/Role sub-element not '
108                                    'recognised' % localName) 
Note: See TracBrowser for help on using the repository browser.