1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-22 17:34:21 +01:00

adding method and unit tests to get all the attributes of one backend

This commit is contained in:
kakwa 2015-05-22 09:49:50 +02:00
parent 5b03596ed7
commit 74ed8fa0d4
2 changed files with 27 additions and 2 deletions

View File

@ -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, WrongAttributeType from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType, WrongBackend
from sets import Set from sets import Set
import yaml import yaml
@ -22,6 +22,7 @@ class Attributes:
self.attributes_file = attributes_file self.attributes_file = attributes_file
self.backends = Set([]) self.backends = Set([])
self.self_attributes = Set([]) self.self_attributes = Set([])
self.backend_attributes = {}
try: try:
stream = open(attributes_file, 'r') stream = open(attributes_file, 'r')
except: except:
@ -40,6 +41,9 @@ class Attributes:
self.self_attributes.add(attrid) self.self_attributes.add(attrid)
for b in attr['backends']: for b in attr['backends']:
self.backends.add(b) self.backends.add(b)
if b not in self.backend_attributes:
self.backend_attributes[b] = []
self.backend_attributes[b].append(attr['backends'][b])
def _mandatory_check(self, attr): def _mandatory_check(self, attr):
for m in ['description', 'display_name', 'type', 'backends']: for m in ['description', 'display_name', 'type', 'backends']:
@ -54,6 +58,11 @@ class Attributes:
"""return the list of backends in roles file""" """return the list of backends in roles file"""
return self.backends return self.backends
def get_backend_attributes(self, backend):
if backend not in self.backends:
raise WrongBackend(backend)
return self.backend_attributes[backend]
def get_attributes(self): def get_attributes(self):
"""get the list of groups from roles""" """get the list of groups from roles"""
return self.self_attributes return self.self_attributes

View File

@ -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, WrongAttributeType from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType, WrongBackend
from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError
class TestError(object): class TestError(object):
@ -30,6 +30,22 @@ class TestError(object):
expected = Set(['ldap', 'ad']) expected = Set(['ldap', 'ad'])
assert ret == expected assert ret == expected
def testGetBackendAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_backend_attributes('ldap')
expected = ['shell', 'cn', 'uid', 'uidNumber', 'gidNumber', 'home', 'userPassword', 'givenName', 'email', 'sn']
assert ret == expected
def testWrongGetBackendAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
try:
ret = inv.get_backend_attributes('notabackend')
except WrongBackend:
return
else:
raise AssertionError("expected an exception")
def testNoFile(self): def testNoFile(self):
try: try:
inv = Attributes('./tests/cfg/dontexist') inv = Attributes('./tests/cfg/dontexist')