diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index 88940e5..485d3ba 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -10,7 +10,7 @@ from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import DumplicatedKey -from ldapcherry.exceptions import MissingAttributesFile, MissingKey +from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType from sets import Set import yaml @@ -32,6 +32,9 @@ for attrid in self.attributes: self._mandatory_check(attrid) + attr = self.attributes[attrid] + if not attr['type'] in types: + raise WrongAttributeType(attr['type'], attrid, attributes_file) def _mandatory_check(self, attr): for m in ['description', 'display_name', 'type', 'backend-attributes']: diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index 124e54d..7034c68 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -43,3 +43,12 @@ def __init__(self, attributesfile): self.attributesfile = attributesfile self.log = "fail to open attributes file <%(attributesfile)s>" % { 'attributesfile' : attributesfile} + +class WrongAttributeType(Exception): + def __init__(self, key, section, ymlfile): + self.key = key + self.section = section + self.ymlfile = ymlfile + self.log = "wrong attribute type <%(key)s> in section <%(section)s> inside file <%(ymlfile)s>" % {'key': key, 'section': section, 'ymlfile': ymlfile } + + diff --git a/tests/cfg/attributes_wrong_type.yml b/tests/cfg/attributes_wrong_type.yml new file mode 100644 index 0000000..2f27399 --- /dev/null +++ b/tests/cfg/attributes_wrong_type.yml @@ -0,0 +1,20 @@ +cn: + description: "Firt Name and Display Name" + display_name: "Display Name" + type: notatype + autofill: + function: cn + args: + - $first-name + - $name + backend-attributes: + ldap: cn + ad: CN + +first-name: + description: "First name of the user" + display_name: "First Name" + type: string + backend-attributes: + ldap: givenName + ad: givenName diff --git a/tests/test_Attributes.py b/tests/test_Attributes.py index 5d3599d..b3b8122 100644 --- a/tests/test_Attributes.py +++ b/tests/test_Attributes.py @@ -8,7 +8,7 @@ import sys from sets import Set from ldapcherry.attributes import Attributes -from ldapcherry.exceptions import MissingAttributesFile, MissingKey +from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError class TestError(object): @@ -33,27 +33,16 @@ else: raise AssertionError("expected an exception") -# def testAttrKeyDuplication(self): -# try: -# inv = Attributes('./tests/cfg/attributes_key_dup.yml') -# except DumplicateAttrKey: -# return -# else: -# raise AssertionError("expected an exception") -# + def testWrongType(self): + try: + inv = Attributes('./tests/cfg/attributes_wrong_type.yml') + except WrongAttributeType: + return + else: + raise AssertionError("expected an exception") -# def testGetDisplayNameMissingAttr(self): -# inv = Attributes('./tests/cfg/attributes.yml') -# try: -# res = inv.get_display_name('notarole') -# except MissingAttr: -# return -# else: -# raise AssertionError("expected an exception") -# # def testGetDisplayName(self): # inv = Attributes('./tests/cfg/attributes.yml') # res = inv.get_display_name('users') # expected = 'Simple Users' # assert res == expected -#