Skip to content

Commit

Permalink
adding key and fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kakwa committed May 31, 2015
1 parent 8d8f4ff commit 2860f5a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ldapcherry/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ldapcherry.pyyamlwrapper import loadNoDump
from ldapcherry.pyyamlwrapper import DumplicatedKey
from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType, WrongBackend
from ldapcherry.exceptions import MissingAttributesFile, MissingKey, WrongAttributeType, WrongBackend, DumplicateUserKey, MissingUserKey
from sets import Set
import yaml

Expand All @@ -23,6 +23,8 @@ def __init__(self, attributes_file):
self.backends = Set([])
self.self_attributes = Set([])
self.backend_attributes = {}
self.displayed_attributes = []
self.key = None
try:
stream = open(attributes_file, 'r')
except:
Expand All @@ -39,11 +41,26 @@ def __init__(self, attributes_file):
raise WrongAttributeType(attr['type'], attrid, attributes_file)
if 'self' in attr and attr['self']:
self.self_attributes.add(attrid)
if 'key' in attr and attr['key']:
if not self.key is None:
raise DumplicateUserKey(attrid, self.key)
self.key = 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])
if 'search_displayed' in attr and attr['search_displayed']:
self.displayed_attributes.append(attrid)

if self.key is None:
raise MissingUserKey()

def get_search_attributes(self):
return self.displayed_attributes

def get_key(self):
return self.key

def _mandatory_check(self, attr):
for m in ['description', 'display_name', 'type', 'backends']:
Expand Down
10 changes: 10 additions & 0 deletions ldapcherry/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def __init__(self, param, section, possible_values):
possible_values_str = string.join(possible_values, ', ')
self.log = "wrong value for param <%(param)s> in section <%(section)s>, possible values are [%(values)s]" % {'param': param, 'section': section, 'values': possible_values_str}

class DumplicateUserKey(Exception):
def __init__(self, attrid1, attrid2):
self.attrid1 = attrid1
self.attrid2 = attrid2
self.log = "duplicate key in <%(attrid1)s> and <%(attrid2)s>" % {'attrid1': attrid1, 'attrid2': attrid2}

class MissingUserKey(Exception):
def __init__(self):
self.log = "missing key"

class WrongAttributeType(Exception):
def __init__(self, key, section, ymlfile):
self.key = key
Expand Down
6 changes: 6 additions & 0 deletions tests/cfg/attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cn:
description: "Firt Name and Display Name"
display_name: "Display Name"
type: string
search_displayed: True
autofill:
function: cn
args:
Expand All @@ -13,20 +14,23 @@ cn:
first-name:
description: "First name of the user"
display_name: "First Name"
search_displayed: True
type: string
backends:
ldap: givenName
ad: givenName
name:
description: "Family name of the user"
display_name: "Name"
search_displayed: True
type: string
backends:
ldap: sn
ad: sn
email:
description: "Email of the user"
display_name: "Name"
search_displayed: True
type: email
autofill:
function: email
Expand All @@ -40,6 +44,8 @@ email:
uid:
description: "UID of the user"
display_name: "UID"
search_displayed: True
key: True
type: string
autofill:
function: uid
Expand Down
1 change: 1 addition & 0 deletions tests/cfg/attributes_missing_mandatory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ email:
uid:
display_name: "UID"
type: string
key: True
autofill:
function: uid
args:
Expand Down
1 change: 1 addition & 0 deletions tests/cfg/attributes_wrong_type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cn:
description: "Firt Name and Display Name"
display_name: "Display Name"
type: notatype
key: True
autofill:
function: cn
args:
Expand Down

0 comments on commit 2860f5a

Please sign in to comment.