mirror of
https://github.com/kakwa/ldapcherry
synced 2024-11-25 18:54:29 +01:00
better checks for attributes
This commit is contained in:
parent
679c2bba85
commit
a71093038c
@ -10,7 +10,7 @@ import sys
|
|||||||
|
|
||||||
from ldapcherry.pyyamlwrapper import loadNoDump
|
from ldapcherry.pyyamlwrapper import loadNoDump
|
||||||
from ldapcherry.pyyamlwrapper import DumplicatedKey
|
from ldapcherry.pyyamlwrapper import DumplicatedKey
|
||||||
from ldapcherry.exceptions import MissingAttributesFile, MissingKey
|
from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType
|
||||||
from sets import Set
|
from sets import Set
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@ -32,6 +32,9 @@ class Attributes:
|
|||||||
|
|
||||||
for attrid in self.attributes:
|
for attrid in self.attributes:
|
||||||
self._mandatory_check(attrid)
|
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):
|
def _mandatory_check(self, attr):
|
||||||
for m in ['description', 'display_name', 'type', 'backend-attributes']:
|
for m in ['description', 'display_name', 'type', 'backend-attributes']:
|
||||||
|
@ -43,3 +43,12 @@ class MissingAttributesFile(Exception):
|
|||||||
def __init__(self, attributesfile):
|
def __init__(self, attributesfile):
|
||||||
self.attributesfile = attributesfile
|
self.attributesfile = attributesfile
|
||||||
self.log = "fail to open attributes file <%(attributesfile)s>" % { '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 }
|
||||||
|
|
||||||
|
|
||||||
|
20
tests/cfg/attributes_wrong_type.yml
Normal file
20
tests/cfg/attributes_wrong_type.yml
Normal file
@ -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
|
@ -8,7 +8,7 @@ import pytest
|
|||||||
import sys
|
import sys
|
||||||
from sets import Set
|
from sets import Set
|
||||||
from ldapcherry.attributes import Attributes
|
from ldapcherry.attributes import Attributes
|
||||||
from ldapcherry.exceptions import MissingAttributesFile, MissingKey
|
from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType
|
||||||
from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError
|
from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError
|
||||||
|
|
||||||
class TestError(object):
|
class TestError(object):
|
||||||
@ -33,27 +33,16 @@ class TestError(object):
|
|||||||
else:
|
else:
|
||||||
raise AssertionError("expected an exception")
|
raise AssertionError("expected an exception")
|
||||||
|
|
||||||
# def testAttrKeyDuplication(self):
|
def testWrongType(self):
|
||||||
# try:
|
try:
|
||||||
# inv = Attributes('./tests/cfg/attributes_key_dup.yml')
|
inv = Attributes('./tests/cfg/attributes_wrong_type.yml')
|
||||||
# except DumplicateAttrKey:
|
except WrongAttributeType:
|
||||||
# return
|
return
|
||||||
# else:
|
else:
|
||||||
# raise AssertionError("expected an exception")
|
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):
|
# def testGetDisplayName(self):
|
||||||
# inv = Attributes('./tests/cfg/attributes.yml')
|
# inv = Attributes('./tests/cfg/attributes.yml')
|
||||||
# res = inv.get_display_name('users')
|
# res = inv.get_display_name('users')
|
||||||
# expected = 'Simple Users'
|
# expected = 'Simple Users'
|
||||||
# assert res == expected
|
# assert res == expected
|
||||||
#
|
|
||||||
|
Loading…
Reference in New Issue
Block a user