1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-24 18:24:31 +01:00
ldapcherry/tests/test_Attributes.py

114 lines
4.9 KiB
Python
Raw Normal View History

2015-05-17 12:24:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
from __future__ import unicode_literals
import pytest
import sys
from sets import Set
from ldapcherry.attributes import Attributes
from ldapcherry.exceptions import *
2015-05-17 12:24:29 +02:00
from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError
class TestError(object):
def testNominal(self):
inv = Attributes('./tests/cfg/attributes.yml')
2015-05-18 15:04:36 +02:00
inv.get_attributes()
2015-05-17 12:24:29 +02:00
return True
def testGetSelfAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_selfattributes()
expected = {'password': {'backends': {'ad': 'unicodePwd', 'ldap': 'userPassword'}, 'display_name': 'Password', 'description': 'Password of the user', 'weight': 31, 'self': True, 'type': 'password'}, 'shell': {'backends': {'ad': 'SHELL', 'ldap': 'shell'}, 'display_name': 'Shell', 'description': 'Shell of the user', 'weight': 80, 'values': ['/bin/bash', '/bin/zsh', '/bin/sh'], 'self': True, 'type': 'stringlist'}}
assert ret == expected
def testGetSelfAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_backends()
expected = Set(['ldap', 'ad'])
assert ret == expected
2015-05-31 19:38:31 +02:00
def testGetSearchAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_search_attributes()
expected = {'first-name': {'backends': {'ad': 'givenName', 'ldap': 'givenName'}, 'display_name': 'First Name', 'description': 'First name of the user', 'weight': 20, 'search_displayed': True, 'type': 'string'}, 'cn': {'autofill': {'function': 'cn', 'args': ['$first-name', '$name']}, 'backends': {'ad': 'cn', 'ldap': 'cn'}, 'display_name': 'Display Name', 'description': 'Firt Name and Display Name', 'weight': 30, 'search_displayed': True, 'type': 'string'}, 'name': {'backends': {'ad': 'sn', 'ldap': 'sn'}, 'display_name': 'Name', 'description': 'Family name of the user', 'weight': 10, 'search_displayed': True, 'type': 'string'}, 'uid': {'display_name': 'UID', 'description': 'UID of the user', 'weight': 50, 'autofill': {'function': 'uid', 'args': ['$first-name', '$last-name']}, 'backends': {'ad': 'UID', 'ldap': 'uid'}, 'key': True, 'search_displayed': True, 'type': 'string'}}
2015-05-31 19:38:31 +02:00
assert ret == expected
def testGetBackendAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_backend_attributes('ldap')
expected = ['shell', 'cn', 'userPassword', 'uidNumber', 'gidNumber', 'sn', 'home', 'givenName', 'email', 'uid']
assert ret == expected
2015-05-31 19:38:31 +02:00
def testGetKey(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_key()
2015-07-05 22:48:24 +02:00
expected = 'uid'
2015-05-31 19:38:31 +02:00
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")
2015-05-17 12:24:29 +02:00
def testNoFile(self):
try:
inv = Attributes('./tests/cfg/dontexist')
except MissingAttributesFile:
return
else:
raise AssertionError("expected an exception")
def testMissingMandatory(self):
try:
inv = Attributes('./tests/cfg/attributes_missing_mandatory.yml')
except MissingKey:
return
else:
raise AssertionError("expected an exception")
2015-05-17 19:49:13 +02:00
def testWrongType(self):
try:
inv = Attributes('./tests/cfg/attributes_wrong_type.yml')
except WrongAttributeType:
return
else:
raise AssertionError("expected an exception")
2015-05-17 12:24:29 +02:00
def testDuplicatePassword(self):
try:
inv = Attributes('./tests/cfg/attribute_pwderror.yml')
except PasswordAttributesCollision:
return
else:
raise AssertionError("expected an exception")
def testValidate(self):
inv = Attributes('./tests/cfg/attributes.yml')
attrs = {'cn': 'test', 'email': 'test@example.org', 'uidNumber': 4242, 'shell': '/bin/bash', 'logscript': 'login1.bat'}
for attrid in attrs:
inv.check_attr(attrid, attrs[attrid])
def testValidateError(self):
inv = Attributes('./tests/cfg/attributes.yml')
attrs = {'email': 'notamail', 'uidNumber': 'not an integer', 'shell': '/bin/not in list', 'logscript': 'not fixed'}
for attrid in attrs:
try:
inv.check_attr(attrid, attrs[attrid])
except WrongAttrValue:
pass
else:
raise AssertionError("expected an exception")
2015-05-17 12:24:29 +02:00
# def testGetDisplayName(self):
# inv = Attributes('./tests/cfg/attributes.yml')
# res = inv.get_display_name('users')
# expected = 'Simple Users'
# assert res == expected