def save(group): """adds a new posix group to the LDAP directory""" if ("members" not in group or type(group['members']) is not list or len(group['members']) == 0): raise ValueError("You must give atleast one group member") if 'gid_number' not in group: raise ValueError("You must give a gid number") if 'name' not in group: raise ValueError("You must give a name") unfixed_group = dict(group) # I don't want to be editing what I'm given unfixed_group['members'] = list(group['members']) for i, member_name in enumerate(unfixed_group["members"]): error_msg = "{} is not in the directory".format(member_name) if not users.find_one(member_name): logger.error(error_msg) raise ValueError(error_msg) fixed_group = convert(unfixed_group, TO_LDAP_MAP) dn = "cn=" + fixed_group["cn"] + "," + BASEDN existing_group = manager.find_one(fixed_group, base=BASEDN, filter_key="cn") if existing_group: manager.update(dn, fixed_group) else: fixed_group["objectClass"] = ["posixGroup"] fixed_group["cn"] = str(fixed_group["cn"]) fixed_group['memberUid'] = [ str(member) for member in fixed_group['memberUid'] ] if 'dn' in fixed_group: del fixed_group['dn'] manager.create(dn, fixed_group)
def test_add_host_user_has_no_hosts(self): new_host = "testing.ug.eie.wits.ac.za" host_less = { "username": "******", "first_name": "Host", "last_name": "Less", "email": ["*****@*****.**"], "password": "******", "yos": "1" } expected = { 'username': '******', 'gid_number': '1000', 'login_shell': '/bin/bash', 'first_name': 'Host', 'last_name': 'Less', 'hosts': ['testing.ug.eie.wits.ac.za'], 'home_directory': '/home/ug/hostless', 'uid_number': '1000', 'yos': '1', 'email': ['*****@*****.**'] } users.delete(host_less['username']) users.add(host_less) users.add_host(host_less['username'], new_host) user = users.find_one(host_less['username']) self.assertEquals(user, expected)
def test_remove_host_not_there(self): fake_host = 'aslkajs' users.remove_host(self.existing_user['username'], fake_host) user = users.find_one(self.existing_user['username']) self.existing_user['uid_number'] = "4001" del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def test_update(self): self.existing_user['first_name'] = 'Jane' users.update(self.existing_user) user = users.find_one(self.existing_user['username']) self.existing_user['uid_number'] = '4002' # This is a problem del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def test_add_host_already_added(self): dummy = self.existing_user['hosts'][0] users.add_host(self.existing_user['username'], dummy) user = users.find_one(self.existing_user['username']) self.existing_user['uid_number'] = "4001" del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def save(group): """adds a new posix group to the LDAP directory""" if ("members" not in group or type(group['members']) is not list or len(group['members']) == 0): raise ValueError("You must give atleast one group member") if 'gid_number' not in group: raise ValueError("You must give a gid number") if 'name' not in group: raise ValueError("You must give a name") unfixed_group = dict(group) # I don't want to be editing what I'm given unfixed_group['members'] = list(group['members']) for i, member_name in enumerate(unfixed_group["members"]): error_msg = "{} is not in the directory".format(member_name) if not users.find_one(member_name): logger.error(error_msg) raise ValueError(error_msg) fixed_group = convert(unfixed_group, TO_LDAP_MAP) dn = "cn=" + fixed_group["cn"] + "," + BASEDN existing_group = manager.find_one(fixed_group, base=BASEDN, filter_key="cn") if existing_group: manager.update(dn, fixed_group) else: fixed_group["objectClass"] = ["posixGroup"] fixed_group["cn"] = str(fixed_group["cn"]) fixed_group['memberUid'] = [str(member) for member in fixed_group['memberUid']] if 'dn' in fixed_group: del fixed_group['dn'] manager.create(dn, fixed_group)
def test_remove_host_last_one(self): only_host = self.existing_user['hosts'][0] users.remove_host(self.existing_user['username'], only_host) user = users.find_one(self.existing_user['username']) self.existing_user['hosts'].remove(only_host) self.existing_user['uid_number'] = "4002" del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def test_remove_host(self): new_host = "testing.ug.eie.wits.ac.za" users.add_host(self.existing_user['username'], new_host) users.remove_host(self.existing_user['username'], new_host) user = users.find_one(self.existing_user['username']) self.existing_user['uid_number'] = "4001" del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def test_update_username(self): '''you should are not allowed to change username''' username = self.existing_user['username'] self.existing_user['username'] = '******' users.update(self.existing_user) self.existing_user['username'] = username user = users.find_one(username) del self.existing_user['password'] self.assertEquals(user, self.existing_user)
def add_member(group_name, member_username): """ should check it the member is the ldap then add them""" group = find_one(group_name) if not group: raise ValueError(str(group_name) + " does not exists") user = users.find_one(member_username) if not user: error_msg = "trying to add {0} to {1} but {0} is not in the directory".format(member_username, group_name) logger.error(error_msg) raise ValueError(error_msg) if user['username'] not in group['members']: group['members'].append(user['username']) save(group)
def add_member(group_name, member_username): """ should check it the member is the ldap then add them""" group = find_one(group_name) if not group: raise ValueError(str(group_name) + " does not exists") user = users.find_one(member_username) if not user: error_msg = "trying to add {0} to {1} but {0} is not in the directory".format( member_username, group_name) logger.error(error_msg) raise ValueError(error_msg) if user['username'] not in group['members']: group['members'].append(user['username']) save(group)
def test_find_one_username_not_given(self): user = users.find_one() self.assertEquals(user, None)
def test_find_one(self): user = users.find_one(self.existing_user['username']) del self.existing_user['password'] self.assertEquals(user, self.existing_user)