From 74ed8fa0d4a3a1a6cce6bc6112ffda032a995db5 Mon Sep 17 00:00:00 2001 From: kakwa Date: Fri, 22 May 2015 09:49:50 +0200 Subject: [PATCH] adding method and unit tests to get all the attributes of one backend --- ldapcherry/attributes.py | 11 ++++++++++- tests/test_Attributes.py | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index 8b945b5..cfea209 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -10,7 +10,7 @@ import sys from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import DumplicatedKey -from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType +from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType, WrongBackend from sets import Set import yaml @@ -22,6 +22,7 @@ class Attributes: self.attributes_file = attributes_file self.backends = Set([]) self.self_attributes = Set([]) + self.backend_attributes = {} try: stream = open(attributes_file, 'r') except: @@ -40,6 +41,9 @@ class Attributes: self.self_attributes.add(attrid) for b in attr['backends']: 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): for m in ['description', 'display_name', 'type', 'backends']: @@ -54,6 +58,11 @@ class Attributes: """return the list of backends in roles file""" 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): """get the list of groups from roles""" return self.self_attributes diff --git a/tests/test_Attributes.py b/tests/test_Attributes.py index 8814765..1de80b5 100644 --- a/tests/test_Attributes.py +++ b/tests/test_Attributes.py @@ -8,7 +8,7 @@ import pytest import sys from sets import Set 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 class TestError(object): @@ -30,6 +30,22 @@ class TestError(object): expected = Set(['ldap', 'ad']) 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): try: inv = Attributes('./tests/cfg/dontexist')