2020-10-05 14:51:16 +02:00
|
|
|
from phi.ldap.user import get_user_by_uid, get_all_users, \
|
|
|
|
add_user, delete_user_by_uid, delete_user
|
2017-12-23 17:10:15 +01:00
|
|
|
|
2020-10-10 11:25:14 +02:00
|
|
|
from phi.ldap.group import add_group_member, get_group_by_cn, get_all_groups
|
2020-10-04 23:01:38 +02:00
|
|
|
|
2017-12-23 17:10:15 +01:00
|
|
|
|
|
|
|
def test_connection(ldap_client):
|
|
|
|
assert ldap_client.connection.closed is False
|
|
|
|
assert ldap_client.connection.bound is True
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_user_by_id(ldap_client):
|
2020-10-04 17:45:09 +02:00
|
|
|
user = get_user_by_uid(ldap_client, 'conte_mascetti')
|
|
|
|
assert user['uid'] == 'conte_mascetti'
|
|
|
|
assert user['mail'] == 'rmascetti@autistici.org'
|
2020-09-30 18:42:28 +02:00
|
|
|
|
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
def test_get_all_users(ldap_client):
|
2020-10-05 14:51:16 +02:00
|
|
|
users = get_all_users(ldap_client)
|
2020-10-10 12:54:28 +02:00
|
|
|
# print(users)
|
2020-09-30 18:42:28 +02:00
|
|
|
assert 'conte_mascetti' in [u['uid'] for u in users]
|
2020-10-03 18:42:18 +02:00
|
|
|
|
|
|
|
|
2020-10-04 16:47:33 +02:00
|
|
|
def test_add_delete_user(ldap_client):
|
2020-10-04 17:45:09 +02:00
|
|
|
uid = 'rosa_rossi'
|
2020-10-03 18:42:18 +02:00
|
|
|
cn = 'Rosa'
|
|
|
|
sn = 'Rossi'
|
|
|
|
mail = 'foo@autistici.org'
|
2020-10-10 12:54:28 +02:00
|
|
|
password = 'changeme'
|
2020-10-04 17:45:09 +02:00
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
add_user(ldap_client, uid, cn, sn, mail, password)
|
2020-10-04 17:45:09 +02:00
|
|
|
|
|
|
|
user = get_user_by_uid(ldap_client, uid)
|
|
|
|
assert user['uid'] == uid
|
|
|
|
assert user['mail'] == mail
|
|
|
|
|
2020-10-05 14:51:16 +02:00
|
|
|
delete_user(ldap_client, user)
|
2020-10-10 12:54:28 +02:00
|
|
|
# print(user)
|
2020-10-04 17:45:09 +02:00
|
|
|
|
|
|
|
user = get_user_by_uid(ldap_client, uid)
|
|
|
|
assert user is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_failing_add_user(ldap_client):
|
|
|
|
uid = 'conte_mascetti'
|
|
|
|
|
|
|
|
try:
|
2020-10-10 12:54:28 +02:00
|
|
|
add_user(ldap_client, uid, 'name', 'surname', 'mail', 'pass')
|
2020-10-04 17:45:09 +02:00
|
|
|
except: # User alrady existing
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
|
|
|
def test_failing_delete_user(ldap_client):
|
2020-10-03 18:42:18 +02:00
|
|
|
uid = 'rosa_rossi'
|
|
|
|
|
2020-10-04 17:45:09 +02:00
|
|
|
try:
|
2020-10-05 14:51:16 +02:00
|
|
|
delete_user_by_uid(ldap_client, uid)
|
2020-10-04 17:45:09 +02:00
|
|
|
except: # User already not existing
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False
|
2020-10-04 23:01:38 +02:00
|
|
|
|
|
|
|
|
2020-10-10 11:25:14 +02:00
|
|
|
def test_get_all_groups(ldap_client):
|
|
|
|
groups = get_all_groups(ldap_client)
|
|
|
|
|
|
|
|
cns = [g['cn'] for g in groups]
|
|
|
|
assert 'WikiUsers' in cns
|
|
|
|
|
|
|
|
|
2020-10-04 23:01:38 +02:00
|
|
|
def test_add_to_group(ldap_client):
|
|
|
|
client = ldap_client
|
|
|
|
|
|
|
|
group_cn = 'WikiUsers'
|
|
|
|
member_uid = 'rosa_rossi'
|
2020-10-10 12:54:28 +02:00
|
|
|
add_user(client, member_uid, 'name', 'surname', 'mail', 'pass')
|
2020-10-04 23:01:38 +02:00
|
|
|
|
2020-10-05 14:51:16 +02:00
|
|
|
user = get_user_by_uid(client, member_uid)
|
2020-10-10 12:54:28 +02:00
|
|
|
# print(user)
|
2020-10-05 14:51:16 +02:00
|
|
|
|
|
|
|
group = get_group_by_cn(client, group_cn)
|
2020-10-10 12:54:28 +02:00
|
|
|
group_members = group['members']
|
2020-10-05 14:51:16 +02:00
|
|
|
|
2020-10-04 23:01:38 +02:00
|
|
|
assert len(group_members) == 1
|
|
|
|
# print(group_members)
|
|
|
|
|
2020-10-05 14:51:16 +02:00
|
|
|
add_group_member(client, group, user)
|
|
|
|
|
|
|
|
group = get_group_by_cn(client, group_cn)
|
2020-10-10 12:54:28 +02:00
|
|
|
group_members = group['members']
|
2020-10-04 23:01:38 +02:00
|
|
|
|
|
|
|
assert len(group_members) == 2
|
2020-10-10 12:54:28 +02:00
|
|
|
assert user['uid'] in group_members
|
2020-10-10 12:59:41 +02:00
|
|
|
|
2020-10-04 23:01:38 +02:00
|
|
|
# print(group_members)
|
2020-10-10 12:59:41 +02:00
|
|
|
# print(user)
|
2020-10-04 23:01:38 +02:00
|
|
|
|
2020-10-05 14:51:16 +02:00
|
|
|
delete_user(client, user)
|