2024-03-12 23:59:46 +01:00
#!/usr/bin/env python3
2015-05-17 12:24:29 +02:00
# -*- coding: utf-8 -*-
from __future__ import with_statement
from __future__ import unicode_literals
import pytest
import sys
from ldapcherry . attributes import Attributes
2015-07-13 09:10:36 +02:00
from ldapcherry . exceptions import *
2015-05-17 12:24:29 +02:00
from ldapcherry . pyyamlwrapper import DumplicatedKey , RelationError
2019-02-07 20:55:50 +01:00
if sys . version < ' 3 ' :
from sets import Set as set
2015-05-17 12:24:29 +02:00
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
2015-05-18 12:30:51 +02:00
def testGetSelfAttributes ( self ) :
inv = Attributes ( ' ./tests/cfg/attributes.yml ' )
ret = inv . get_selfattributes ( )
2016-06-17 07:52:49 +02:00
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 ' } }
2015-05-18 12:30:51 +02:00
assert ret == expected
def testGetSelfAttributes ( self ) :
inv = Attributes ( ' ./tests/cfg/attributes.yml ' )
ret = inv . get_backends ( )
2019-02-07 20:55:50 +01:00
expected = set ( [ ' ldap ' , ' ad ' ] )
2015-05-18 12:30:51 +02:00
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 ( )
2016-06-17 07:52:49 +02:00
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
2015-05-22 09:49:50 +02:00
def testGetBackendAttributes ( self ) :
inv = Attributes ( ' ./tests/cfg/attributes.yml ' )
ret = inv . get_backend_attributes ( ' ldap ' )
2015-06-15 23:03:47 +02:00
expected = [ ' shell ' , ' cn ' , ' userPassword ' , ' uidNumber ' , ' gidNumber ' , ' sn ' , ' home ' , ' givenName ' , ' email ' , ' uid ' ]
2019-02-09 16:22:42 +01:00
expected . sort ( )
2015-05-22 09:49:50 +02:00
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
2015-05-22 09:49:50 +02:00
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 " )
2015-05-17 19:22:11 +02:00
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
2015-07-13 09:10:36 +02:00
def testDuplicatePassword ( self ) :
try :
inv = Attributes ( ' ./tests/cfg/attribute_pwderror.yml ' )
except PasswordAttributesCollision :
return
else :
raise AssertionError ( " expected an exception " )
2015-07-14 11:21:33 +02:00
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