def resolve_credentials_for_rest(credentials, element, datafile=None, system_name=None, variable_config="variable_config", var_sub="var_sub"): if element == "expected_response": credentials[element] = resolve_exp_resp_string_as_list( credentials[element]) if element == "allow_redirects": credentials[element] = resolve_value_of_redirects(credentials[element]) if element == "data": credentials[element] = resolve_value_of_data(credentials[element], datafile) if element == "timeout": credentials[element] = resolve_value_of_timeout(credentials[element]) if element == "json": credentials[element] = resolve_value_of_json( credentials[element], datafile, system_name, credentials[variable_config], credentials[var_sub]) if element == "cookies": credentials[element] = resolve_value_of_cookies(credentials[element]) if element == "files": credentials[element] = resolve_value_of_files(credentials[element]) if element == "verify": credentials[element] = resolve_value_of_verify(credentials[element]) if element == "stream": credentials[element] = resolve_value_of_stream(credentials[element]) if element == "cert": credentials[element] = resolve_value_of_cert(credentials[element]) if element == "proxies" or element == "headers" or element == "params": credentials[element] = convert_string_to_dict(credentials[element]) return credentials
def resolve_value_of_cookies(element): """ This function evaluates user input for cookies. If a file path is given, then a cookiejar object is created and the contents of the file are loaded into the object. This object is then returned. Else, a dictionary would be created out of the string given input = "foo=foo1; bar=bar1; ; =foobar; barfoo=" return value = {'foo': 'foo1', 'bar': 'bar1'} If the dictionary is empty at the end of the function, None is retuened. """ if element is not None and element is not False and element != "": abs_path = file_Utils.getAbsPath(element, sys.path[0]) if os.path.exists(abs_path): element = http.cookiejar.LWPCookieJar(element) try: element.load() except http.cookiejar.LoadError: pNote("Cookies could not be loaded from {}.".format(element), "error") element = None except Exception as e: pNote("An Error Occurred: {0}".format(e), "error") else: element = convert_string_to_dict(element) else: element = None return element
def resolve_value_of_data(element, datafile): """ This function validates the user input for data. If the input is a file path, then a file object is returned. if the input is in bytes, then the input is retuned. else, dictionary is returned. input = "foo=foo1; bar=bar1; ; =foobar; barfoo=" return value = {'foo': 'foo1', 'bar': 'bar1'} If the dict is empty at the end of the function, None is returned """ if element is not None and element is not False and element != "": element = check_ext_get_abspath(element, os.path.dirname(datafile)) if os.path.exists(element): final_dict = open(element, 'rb') elif isinstance(element, str): final_dict = element else: final_dict = convert_string_to_dict(element) else: final_dict = None return final_dict
def dict_in_tuple(element): """ This function evaluates the value of the files tag and returned validated data. input: string (path/to/file5.txt;content_type;(header1=value;header2=value2)), (path/to/file1.txt;content_type;(header3=value3;header4=value4)) output: dict { file5.txt: (file5.txt, open file5.txt (in binary mode), content_type, { header1: value, header2: value2 }), file1.txt: (file1.txt, open file5.txt (in binary mode), content_type, { header3: value3, header4: value4 }) } """ final_dict = {} temp_list = [] element = element.strip("))") element = element.strip("(") if element is not None and element is not False and element != "": element = element.split(";(") if element[0] is None or element[0] is False: pNote("File path cannot be {0}!".format(element), "error") elif element[0] == "": pNote("File path cannot be empty!", "error") else: element[0] = element[0].split(";") for j in range(0, len(element[0])): element[0][j] = element[0][j].strip() if element[0][0] is None or element[0][0] is False: pNote("File path cannot be {0}!".format(element), "error") elif element[0][0] == "": pNote("File path cannot be empty!", "error") else: abs_path = file_Utils.getAbsPath(element[0][0], sys.path[0]) if os.path.exists(abs_path): temp_list.append( os.path.basename(os.path.normpath(element[0][0]))) temp_list.append(open(abs_path, 'rb')) else: pNote("{0} doesn't exist!".format(element[0]), "error") if len(element[0]) > 1: if element[0][1] is not None and element[0][1] is not False \ and element[0][1] != "": temp_list.append(element[0][1]) if len(element) > 1: if element[1] is not None and element[1] is not False \ and element[1] != "": temp_list.append(convert_string_to_dict( element[1])) else: if len(element) > 1: if element[1] is not None and element[1] is not False \ and element[1] != "": temp_list.append("") temp_list.append(convert_string_to_dict(element[1])) if temp_list != []: if len(temp_list) < 3: final_dict[temp_list[0]] = (temp_list[0], temp_list[1]) elif len(temp_list) > 3: final_dict[temp_list[0]] = (temp_list[0], temp_list[1], temp_list[2], temp_list[3]) elif len(temp_list) > 2: final_dict[temp_list[0]] = (temp_list[0], temp_list[1], temp_list[2]) else: if element == "": pNote("File path cannot be empty!", "error") else: pNote("File path cannot be {0}!".format(element), "error") return final_dict