def step8(self): # creates the merge request sys.argv[1] = 'setrole' del sys.argv[2:] # project setrole sys.argv.append('-p') # ARG role sys.argv.append(self.__class__.user_role) # ARG user sys.argv.append(self.__class__.user_ldap_member) # ARG project sys.argv.append(self.__class__.project_2) gitutils.main() time.sleep(2) project_id = gitlab_utils.get_project_id('hax_l', self.__class__.project_2) members = gitlab_utils.get_project(project_id).members.all(all=True) for i in members: if i.attributes['name'] == self.__class__.user_ldap_member: found = True self.assertEqual( i.attributes['access_level'], gitlab_utils.check_role(self.__class__.user_role)) self.assertTrue(found) time.sleep(2) ################################# ##### CLEANUP OF TEST STUFF ##### ################################# # removal of personal project gitlab_utils.remove_project(project_id) # removal of test group gitlab_utils.remove_group( gitlab_utils.get_group_id(self.__class__.group_name))
def step5(self): # CMD sys.argv[1] = 'addldap' # deletes previous args del sys.argv[2:] # group sys.argv.append(self.__class__.group_name) # ldap user sys.argv.append(self.__class__.user_ldap) # role sys.argv.append(self.__class__.user_role) # calls main to fork gitutils.main() time.sleep(2) # gets members of group members = gitlab_utils.get_group( gitlab_utils.get_group_id( self.__class__.group_name)).members.all(all=True) found = False # add ldap adds several users that are member of that ldap group # we'll be checking for a member of that ldap group : user_ldap_member for i in members: if i.attributes['name'] == self.__class__.user_ldap_member: found = True self.assertEqual( i.attributes['access_level'], gitlab_utils.check_role(self.__class__.user_role)) self.assertTrue(found) time.sleep(2)
def test_get_group_id(self): """ Test get_group_id """ group_id = \ gitlab_utils.get_group_id('controls_highlevel_applications') self.assertEqual(group_id, 84)
def create_projects(group_name, project_names=[]): """ It create groups based on the list provided. : param group_name : group name in which the projects will be created : type group_name : str : param project_names : List of name(s) of group to be created : type project_names : list """ total_proj_names = len(project_names) group_id = 0 # checks if group exists and create if it doesn't exists yet if not gitlab_utils.group_exists(group_name): # if not, creates the new group print(const.CREATEPROJECT_NOGROUP % (group_name, group_name)) print(const.CREATEGROUP_CREATING % (1, group_name), end="") gitlab_utils.create_group(group_name, "-") time.sleep(1) group_id = gitlab_utils.get_group_id(group_name) if group_id != -1: print(const.CREATEGROUP_ID % group_id) # if group existed before if group_id == 0: group_id = gitlab_utils.get_group_id(group_name) # gets all projects from this group group_projects = gitlab_utils.get_group_projects(group_name) # iterates over all entries of new projects count = 1 print(const.CREATEPROJECT_START % (const.bcolors.BOLD, group_name, group_id, const.bcolors.ENDC)) for project in project_names: skip_proj = False for existing_project in group_projects: if existing_project['name'] == project: print(const.CREATEPROJECT_TAKEN % (count, group_name, project)) skip_proj = True if not skip_proj: print(const.CREATEPROJECT_CREATING % (count, project), end="") new_proj = gitlab_utils.create_project(group_id, project) project_id = gitlab_utils.get_project_id(group_name, project) if project_id != -1: print(const.CREATEGROUP_ID % project_id) count += 1 print(const.CREATEPROJECT_END % (count - 1, total_proj_names))
def step1(self): # CMD sys.argv[1] = 'creategroups' # deletes previous args del sys.argv[2:] # ARG sys.argv.append(self.__class__.group_name) # calls main to create group gitutils.main() # verifies if the group exists self.assertTrue(gitlab_utils.group_exists(self.__class__.group_name)) # gets the id of the newly created group self.assertNotEqual( gitlab_utils.get_group_id(self.__class__.group_name), -1) time.sleep(2)
def add_ldap(git_group, ldap_cn, role): """ Assign ldap group sync to a git group :param ldap_cn: LDAP USER CN (common name) :type ldap_cn: str :param git_group: Git group that the ldap will be added :type git_group: str :param role: Role that will be given to the user :type role: str :return: """ print(const.ADDLDAP_INIT_MSG % ( const.bcolors.BOLD, ldap_cn, role, const.bcolors.ENDC, const.bcolors.BOLD, git_group, const.bcolors.ENDC, )) # verify if ldap_cn is a real ldap group list_of_ldap_cns = gitlab_utils.get_ldap_groups(ldap_cn) found = False # verifies if the ldap group is actually a ldap group for i in list_of_ldap_cns: if ldap_cn == i.attributes['cn']: found = True # gets group id group_id = -1 try: group_id = gitlab_utils.get_group_id(git_group) except Exception as ex: raise gitutils_exception.GitutilsError(ex) if found is True and group_id != -1: gitlab_utils.addldapgroup(git_group, group_id, ldap_cn, role) else: gitutils_exception.GitutilsError(const.ADDLDAP_LDAP_NAME_PROBLEM)
def create_groups(group_names=[]): """ It create groups based on the list provided. : param group_name : List of name(s) of group to be created : type group_name : list """ total_group_names = len(group_names) if total_group_names < 1: gitutils_exception.GitutilsError(const.PROBLEM_CREATEGROUP_EMPTY) count = 1 print(const.CREATEGROUP_START) for group_name in group_names: # check if group exists if not gitlab_utils.group_exists(group_name): print(const.CREATEGROUP_CREATING % (count, group_name), end="") gitlab_utils.create_group(group_name, "-") time.sleep(1) group_id = gitlab_utils.get_group_id(group_name) if group_id != -1: print(const.CREATEGROUP_ID % group_id) count += 1 else: print(const.CREATEGROUP_TAKEN % group_name) print(const.CREATEGROUP_END % (count - 1, total_group_names))
def set_role(role, username, git_groups, project_flag): """ Sets a role to a specified user in a specified group or project :param role: Role that will be given to the user. :type role: int :param username: Username :type username: str :param git_group: Group or project names :type git_group: list :param project_flag: Flag to indicate project-level role. :type project_flag: bool :return: """ # gets user_id user_id = gitlab_utils.get_user_id(username) if user_id == -1: raise gitutils_exception.GitutilsError( const.ROLE_SETROLE_PROBLEM_USERID) ################# # GROUP SETROLE # ################# if not project_flag: for git_group in git_groups: group_id = gitlab_utils.get_group_id(git_group) print(const.SETROLE_INIT_MSG % ( const.bcolors.BOLD, role, const.bcolors.ENDC, const.bcolors.BOLD, username, user_id, const.bcolors.ENDC, const.bcolors.BOLD, git_group, group_id, const.bcolors.ENDC, ), end="") # gets group group = gitlab_utils.get_group(git_group) # adds member with the desired access level to the group group.members.create({'user_id': user_id, 'access_level': role}) # verification members = group.members.all(all=True) found = any(member.attributes['username'] == username for member in members) if not found: print(' ⨯') raise gitutils_exception.GitutilsError( const.SETROLE_PROJECT_VALIDATION_FAILS) print(' ✓') else: ################### # PROJECT SETROLE # ################### # git_groups has projects for project_name in git_groups: print(const.SETROLE_P_INIT_MSG % ( const.bcolors.BOLD, username, role, const.bcolors.ENDC, const.bcolors.BOLD, project_name, const.bcolors.ENDC, ), end="") # gets group name group_name = gitlab_utils.get_project_group_simplified( project_name) # gets project id project_id = gitlab_utils.get_project_id(group_name, project_name) # gets the project project = gitlab_utils.get_project(project_id) # adds member with the desired role to the project project.members.create({'user_id': user_id, 'access_level': role}) # verification members = project.members.all(all=True) found = any(member.attributes['username'] == username for member in members) if not found: print(' ⨯') raise gitutils_exception.GitutilsError( const.SETROLE_PROJECT_VALIDATION_FAILS) print(' ✓')