def _check_assessments_filtration(assessment, cavs, operator, audit, selenium): """Check that filtration of assessments works.""" cads = [Representation.repr_dict_to_obj(cad) for cad in assessment.custom_attribute_definitions] filter_exprs = FilterUtils().get_filter_exprs_by_cavs( cads, cavs, operator) assessment = Representation.extract_objs_wo_excluded_attrs( [assessment.repr_ui()], *(Representation.tree_view_attrs_to_exclude + ( "audit", "assessment_type", "modified_by")) )[0] expected_results = [{"filter": filter_expr, "objs": [assessment]} for filter_expr in filter_exprs] actual_results = [] for filter_expr in filter_exprs: result = { "filter": filter_expr, "objs": webui_service.AssessmentsService(selenium) .filter_and_get_list_objs_from_tree_view(audit, filter_expr) } actual_results.append(result) error_message = messages.AssertionMessages.format_err_msg_equal( [{exp_res["filter"]: [exp_obj.title for exp_obj in exp_res["objs"]]} for exp_res in expected_results], [{act_res["filter"]: [act_obj.title for act_obj in act_res["objs"]]} for act_res in actual_results] ) + messages.AssertionMessages.format_err_msg_equal( StringMethods.convert_list_elements_to_list( [exp_res["objs"] for exp_res in expected_results]), StringMethods.convert_list_elements_to_list( [act_res["objs"] for act_res in actual_results])) assert expected_results == actual_results, error_message
def get_filter_exprs_by_ca(self, ca_title, ca_val, ca_type, operator): """Return all possible filter expressions for CA according to CA type""" if ca_type == AdminWidgetCustomAttributes.CHECKBOX: values_to_filter = (StringMethods.get_list_of_all_cases( alias.YES_VAL) if StringMethods.get_bool_value_from_arg(ca_val) else StringMethods.get_list_of_all_cases( alias.NO_VAL)) elif ca_type == AdminWidgetCustomAttributes.PERSON: from lib.service import rest_service person = rest_service.ObjectsInfoService().get_obj( obj=Representation.repr_dict_to_obj( dict(zip(["type", "id"], ca_val.split(":"))))) values_to_filter = [person.name, person.email] elif ca_type == AdminWidgetCustomAttributes.DATE: date_formats = ["%m/%d/%Y", "%m/%Y", "%Y-%m-%d", "%Y-%m", "%Y"] date = parser.parse(ca_val).date() values_to_filter = [ date.strftime(_format) for _format in date_formats ] else: values_to_filter = [ca_val] return [ self.get_filter_exp(ca_title, operator, [val]) for val in values_to_filter ]
def _check_assessments_filtration(assessment, cavs, operator, audit, selenium): """Check that filtration of assessments works.""" # pylint: disable=too-many-locals cads = [Representation.repr_dict_to_obj(cad) for cad in assessment.custom_attribute_definitions] filter_exprs = FilterUtils().get_filter_exprs_by_cavs( cads, cavs, operator) assessment = Representation.extract_objs_wo_excluded_attrs( [assessment.repr_ui()], *(Representation.tree_view_attrs_to_exclude + ( "audit", "assessment_type", "modified_by")) )[0] expected_results = [{"filter": filter_expr, "objs": [assessment]} for filter_expr in filter_exprs] actual_results = [] for filter_expr in filter_exprs: result = { "filter": filter_expr, "objs": webui_service.AssessmentsService( selenium).filter_and_get_list_objs_from_tree_view(audit, filter_expr) } actual_results.append(result) error_message = messages.AssertionMessages.format_err_msg_equal( [{exp_res["filter"]: [exp_obj.title for exp_obj in exp_res["objs"]]} for exp_res in expected_results], [{act_res["filter"]: [act_obj.title for act_obj in act_res["objs"]]} for act_res in actual_results] ) + messages.AssertionMessages.format_err_msg_equal( StringMethods.convert_list_elements_to_list( [exp_res["objs"] for exp_res in expected_results]), StringMethods.convert_list_elements_to_list( [act_res["objs"] for act_res in actual_results])) assert expected_results == actual_results, error_message
def generate_string(cls, first_part): """Generate string in unicode format according object type and random data. """ return unicode("{first_part}_{uuid}_{rand_str}".format( first_part=first_part, uuid=StringMethods.random_uuid(), rand_str=StringMethods.random_string()))
def _create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory.create_empty() for _ in xrange(len(list_scopes))] list_scopes_with_upper_keys = [ StringMethods.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_to_convert = StringMethods.exchange_dicts_items( transform_dict=Entity.items_of_remap_keys(), dicts=list_scopes_with_upper_keys, is_keys_not_values=True) # convert and represent values in scopes for scope in list_scopes_to_convert: # convert u'None', u'No person' to None type StringMethods.update_dicts_values(scope, ["None", "No person"], None) for key, val in scope.iteritems(): if val: if key in ["mandatory", "verified"]: # convert u'false', u'true' like to Boolean scope[key] = StringMethods.get_bool_value_from_arg(val) if key in ["updated_at", "created_at"]: # UI like u'08/20/2017' to date=2017-08-20, timetz=00:00:00 datetime_val = parser.parse(val) if str(datetime_val.time()) != "00:00:00": # UI like u'08/20/2017 07:30:45 AM +03:00' to date=2017-08-20, # timetz=04:30:45+00:00 if 'tzinfo', else: # CSV like u'08-20-2017 04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 datetime_val = ( datetime_val.astimezone(tz=tz.tzutc()) if datetime_val.tzinfo else datetime_val.replace(tzinfo=tz.tzutc())) scope[key] = datetime_val if (key == "comments" and isinstance(val, list) and all(isinstance(comment, dict) for comment in val)): # extract datetime from u'(Creator) 08/20/2017 07:30:45 AM +03:00' scope[key] = [ {k: (parser.parse(re.sub(regex.TEXT_W_PARENTHESES, Symbols.BLANK, v) ).astimezone(tz=tz.tzutc()) if k == "created_at" else v) for k, v in comment.iteritems()} for comment in val] # convert multiple values to list of strings and split if need it if (key in ["owners", "assignee", "creator", "verifier"] and not isinstance(val, list)): # split Tree View values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F'] # Info Widget values will be represent by internal methods scope[key] = val.split(", ") # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and (self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and Symbols.STAR in val): scope[key] = val.replace(Symbols.STAR, Symbols.BLANK) return [ Entity.update_objs_attrs_values_by_entered_data( obj_or_objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs)]
def generate_string(cls, first_part, allowed_chars=StringMethods.ALLOWED_CHARS): """Generate random string in unicode format according to object type. Symbols allowed in random part may be specified by `allowed_chars` argument. """ return unicode("{first_part}_{uuid}_{rand_str}".format( first_part=first_part, uuid=StringMethods.random_uuid(), rand_str=StringMethods.random_string(chars=allowed_chars)))
def _create_list_objs(self, entity_factory, list_scopes): """Create and return list of objects used entity factory and UI data (list of scopes UI text elements {"header": "item", ...} remapped to list of dicts {"attr": "value", ...}). Return list of created objects. """ list_factory_objs = [ entity_factory().obj_inst() for _ in xrange(len(list_scopes))] list_scopes_with_upper_keys = [ StringMethods.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_to_convert = StringMethods.exchange_dicts_items( transform_dict=Representation.remap_collection(), dicts=list_scopes_with_upper_keys, is_keys_not_values=True) # convert and represent values in scopes for scope in list_scopes_to_convert: # convert u'None', u'No person' to None type StringMethods.update_dicts_values(scope, ["None", "No person"], None) for key, val in scope.iteritems(): if val: if key in ["mandatory", "verified"]: # convert u'false', u'true' like to Boolean scope[key] = StringMethods.get_bool_value_from_arg(val) if key in ["updated_at", "created_at"]: # UI like u'08/20/2017' to date=2017-08-20, timetz=00:00:00 datetime_val = parser.parse(val) if str(datetime_val.time()) != "00:00:00": # UI like u'08/20/2017 07:30:45 AM +03:00' to date=2017-08-20, # timetz=04:30:45+00:00 if 'tzinfo', else: # CSV like u'08-20-2017 04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 datetime_val = ( datetime_val.astimezone(tz=tz.tzutc()) if datetime_val.tzinfo else datetime_val.replace(tzinfo=tz.tzutc())) scope[key] = datetime_val if (key == "comments" and isinstance(val, list) and all(isinstance(comment, dict) for comment in val)): # extract datetime from u'(Creator) 08/20/2017 07:30:45 AM +03:00' scope[key] = [ {k: (parser.parse(re.sub(regex.TEXT_W_PARENTHESES, Symbols.BLANK, v) ).astimezone(tz=tz.tzutc()) if k == "created_at" else v) for k, v in comment.iteritems()} for comment in val] # convert multiple values to list of strings and split if need it if (key in Representation.people_attrs_names and not isinstance(val, list)): # split Tree View values if need 'Ex1, Ex2 F' to ['Ex1', 'Ex2 F'] # Info Widget values will be represent by internal methods scope[key] = val.split(", ") # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and (self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS) and Symbols.STAR in val): scope[key] = val.replace(Symbols.STAR, Symbols.BLANK) return [ factory_obj.update_attrs(is_allow_none=True, **scope) for scope, factory_obj in zip(list_scopes_to_convert, list_factory_objs)]
def generate_string(cls, first_part, allowed_chars=StringMethods.ALLOWED_CHARS): """Generate random string in unicode format according to object type. Symbols allowed in random part may be specified by `allowed_chars` argument. """ return unicode("{first_part}_{uuid}_{rand_str}".format( first_part=first_part, uuid=StringMethods.random_uuid(), rand_str=StringMethods.random_string(chars=allowed_chars)))
def test_dashboard_gca(self, new_control_rest, selenium): # pylint: disable=anomalous-backslash-in-string """Check Dashboard Tab is exist if 'Dashboard' GCA filled with right value. Possible values match to regexp r"^https?://[^\s]+$". Steps: - Create 'Dashboard' gcas for object. - Fill with values - Check if 'Dashboard' tab exist. - Navigate to 'Dashboard' tab. - Check only GCAs filled with right values displayed on the tab. """ urls = ["https://gmail.by/", "https://www.google.com/", environment.app_url, StringMethods.random_string(), "ftp://something.com/"] cads_rest_service = rest_service.CustomAttributeDefinitionsService() gca_defs = (cads_rest_service.create_dashboard_gcas( new_control_rest.type, count=len(urls))) control_rest_service = rest_service.ControlsService() control_rest_service.update_obj( obj=new_control_rest, custom_attributes=dict( zip([gca_def.id for gca_def in gca_defs], urls))) expected_dashboards_items = dict(zip( [gca_def.title.replace(aliases.DASHBOARD + "_", "") for gca_def in gca_defs], urls[:3])) controls_ui_service = webui_service.ControlsService(selenium) is_dashboard_tab_exist = ( controls_ui_service.is_dashboard_tab_exist(new_control_rest)) assert is_dashboard_tab_exist actual_dashboards_items = ( controls_ui_service.get_items_from_dashboard_widget(new_control_rest)) assert expected_dashboards_items == actual_dashboards_items cads_rest_service.delete_objs(gca_defs)
def create(self, is_add_rest_attrs=False, **attrs): """Create random Custom Attribute object's instance, if 'is_add_rest_attrs' then add attributes for REST, if 'attrs' then update attributes accordingly. """ attrs = copy.deepcopy(attrs) attrs.setdefault( "attribute_type", random.choice(AdminWidgetCustomAttributes.ALL_CA_TYPES)) attrs.setdefault( "definition_type", objects.get_singular(random.choice(objects.ALL_CA_OBJS))) attrs.setdefault("title", self.generate_ca_title(attrs["attribute_type"])) if attrs["attribute_type"] == AdminWidgetCustomAttributes.DROPDOWN: attrs.setdefault("multi_choice_options", StringMethods.random_list_strings()) else: attrs["multi_choice_options"] = None attrs.setdefault("mandatory", False) obj = self.obj_inst() obj.update_attrs(is_allow_none=False, **attrs) if is_add_rest_attrs: obj.modal_title = "Add Attribute to type {}".format( obj.definition_type.title()) return obj
def _update_ca_attrs_values(self, obj, **attrs): """Update CA's (obj) attributes values according to dictionary of arguments (key = value). Restrictions: 'multi_choice_options' is a mandatory attribute for Dropdown CA and 'placeholder' is a attribute that exists only for Text and Rich Text CA. Generated data - 'obj', entered data - '**arguments'. """ # fix generated data if attrs.get("attribute_type"): obj.title = self.generate_ca_title(attrs["attribute_type"]) if (obj.multi_choice_options and obj.attribute_type == AdminWidgetCustomAttributes.DROPDOWN and attrs.get("attribute_type") != AdminWidgetCustomAttributes.DROPDOWN): obj.multi_choice_options = None # fix entered data if (attrs.get("multi_choice_options") and attrs.get("attribute_type") != AdminWidgetCustomAttributes.DROPDOWN): attrs["multi_choice_options"] = None if (attrs.get("placeholder") and attrs.get("attribute_type") not in (AdminWidgetCustomAttributes.TEXT, AdminWidgetCustomAttributes.RICH_TEXT)): attrs["placeholder"] = None # extend entered data if (attrs.get("attribute_type") == AdminWidgetCustomAttributes.DROPDOWN and not obj.multi_choice_options): obj.multi_choice_options = StringMethods.random_list_strings() return obj.update_attrs(**attrs)
def test_asmt_from_template_w_dropdown_url(self, program, control_mapped_to_program, audit, selenium): """Check evidence url could be filled in via Assessment dropdown. Objects structure: Program. -> Control mapped to program. -> Audit. -> Asmt template with evidence url dropdown. -> Autogenerated asmt. """ asmt_template_w_dropdown = rest_facade.create_asmt_template_w_dropdown( audit, ["url"]) expected_asmt = rest_facade.create_asmt_from_template_rest( audit, control_mapped_to_program, asmt_template_w_dropdown) dropdown = CustomAttributeDefinitionsFactory().create( **expected_asmt.cads_from_template()[0]) asmt_service = webui_service.AssessmentsService(selenium) exp_url = StringMethods.random_string( size=StringMethods.RANDOM_STR_LENGTH) expected_asmt = asmt_service.choose_and_fill_dropdown_lca( expected_asmt, dropdown, url=exp_url) expected_asmt_urls = [exp_url] expected_asmt.update_attrs( updated_at=self.info_service().get_obj( obj=expected_asmt).updated_at, evidence_urls=expected_asmt_urls, mapped_objects=[control_mapped_to_program.title], status=object_states.IN_PROGRESS).repr_ui() actual_asmt = asmt_service.get_obj_from_info_page(obj=expected_asmt) self.general_equal_assert(expected_asmt, actual_asmt, "audit")
def remap_collection(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "admins", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.PROGRAM_MANAGERS: "managers", els.VERIFIED: "verified", els.STATUS: "status", els.LAUNCH_STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAINS: "audit_captains", els.AUDITORS: "auditors", "MAPPED_OBJECTS": "mapped_objects", els.ASSIGNEES: "assignees", els.CREATORS: "creators", "VERIFIERS": "verifiers", "COMMENTS": "comments", "CREATED_AT": "created_at", els.MODIFIED_BY: "modified_by", "LAST_UPDATED_BY": "modified_by", "UPDATED_AT": "updated_at", "ASSESSMENT_TYPE": "assessment_type", "IS_VERIFIED": "verified", "CUSTOM_ATTRIBUTES": "custom_attributes", "DESCRIPTION": "description", "EVIDENCE_URLS": "evidence_urls", "ASSERTIONS": "assertions", "EXTERNAL_SLUG": "external_slug", "EXTERNAL_ID": "external_id", "REVIEW_STATUS": "review_status", "REVIEW_STATUS_DISPLAY_NAME": "review_status_display_name", "PRIMARY_CONTACTS": "primary_contacts", "CONTROL_OPERATORS": "control_operators", "CONTROL_OWNERS": "control_owners", "URL": "url", "ID": "id", "RISK_TYPE": "risk_type", "REVIEW": "review" } csv_remap_items = { csv.REVISION_DATE: "updated_at", "REVIEW STATUS": "review_status", "REVIEW_STATUS_DISPLAY_NAME": "review_status_display_name" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return StringMethods.dict_keys_to_upper_case(result_remap_items)
def update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **attrs): """Update object's attributes values.""" for obj_attr_name in attrs: obj_attr_value = None if obj_attr_name in Representation.all_attrs_names(): _obj_attr_value = attrs.get(obj_attr_name) if not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.repr_obj_to_dict(_obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else help_utils.convert_to_list(origin_obj_attr_value) + help_utils.convert_to_list(obj_attr_value)) if is_replace_values_of_dicts and isinstance(_obj_attr_value, dict): obj_attr_value = StringMethods.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=help_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = ( obj_attr_value if isinstance(getattr(obj, obj_attr_name), list) else obj_attr_value[0]) if (is_allow_none_values is True or (is_allow_none_values is False and obj_attr_value is not None)): setattr(obj, obj_attr_name, obj_attr_value) return obj
def create_asmt_template_w_dropdown(audit, dropdown_types_list): """Create assessment template with dropdown custom attribute.""" multi_choice_mandatory = { "file": "2", "url": "4", "comment": "1", "file_url": "6", "url_comment": "5", "file_comment": "3", "file_url_comment": "7", "nothing": "0" } ca_definitions_factory = CustomAttributeDefinitionsFactory() custom_attribute_definitions = [ ca_definitions_factory.create( title=(ca_definitions_factory.generate_ca_title( AdminWidgetCustomAttributes.DROPDOWN)), attribute_type=AdminWidgetCustomAttributes.DROPDOWN, definition_type=AdminWidgetCustomAttributes.DROPDOWN, multi_choice_mandatory=(",".join( multi_choice_mandatory[dropdown_type] for dropdown_type in dropdown_types_list)), multi_choice_options=(StringMethods.random_list_strings( list_len=len(dropdown_types_list)))) ] custom_attribute_definitions = ( ca_definitions_factory.generate_ca_defenitions_for_asmt_tmpls( custom_attribute_definitions)) return create_asmt_template( audit, custom_attribute_definitions=custom_attribute_definitions)
def _update_ca_attrs_values(cls, obj, **attrs): """Update CA's (obj) attributes values according to dictionary of arguments (key = value). Restrictions: 'multi_choice_options' is a mandatory attribute for Dropdown CA and 'placeholder' is a attribute that exists only for Text and Rich Text CA. Generated data - 'obj', entered data - '**arguments'. """ # fix generated data if attrs.get("attribute_type"): obj.title = cls.generate_string(attrs["attribute_type"]) if (obj.multi_choice_options and obj.attribute_type == AdminWidgetCustomAttributes.DROPDOWN and attrs.get("attribute_type") != AdminWidgetCustomAttributes.DROPDOWN): obj.multi_choice_options = None # fix entered data if (attrs.get("multi_choice_options") and attrs.get("attribute_type") != AdminWidgetCustomAttributes.DROPDOWN): attrs["multi_choice_options"] = None if (attrs.get("placeholder") and attrs.get("attribute_type") not in (AdminWidgetCustomAttributes.TEXT, AdminWidgetCustomAttributes.RICH_TEXT)): attrs["placeholder"] = None # extend entered data if (attrs.get("attribute_type") == AdminWidgetCustomAttributes.DROPDOWN and not obj.multi_choice_options): obj.multi_choice_options = StringMethods.random_list_strings() return Entity.update_objs_attrs_values_by_entered_data(obj_or_objs=obj, **attrs)
def remap_collection(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "admins", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.MANAGER: "managers", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAINS: "audit_captains", els.CAS: "custom_attributes", els.MAPPED_OBJECTS: "mapped_objects", els.ASSIGNEES: "assignees", els.CREATORS: "creators", els.VERIFIERS: "verifiers", els.COMMENTS_HEADER: "comments", els.CREATED_AT: "created_at", els.MODIFIED_BY: "modified_by", els.LAST_UPDATED_BY: "modified_by", els.UPDATED_AT: "updated_at", els.ASMT_TYPE: "assessment_type" } csv_remap_items = { csv.REVISION_DATE: "updated_at" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return StringMethods.dict_keys_to_upper_case(result_remap_items)
def update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **attrs): """Update object's attributes values.""" for obj_attr_name in attrs: obj_attr_value = None if (obj_attr_name in Representation.all_attrs_names()): _obj_attr_value = attrs.get(obj_attr_name) if not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.repr_obj_to_dict(_obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else help_utils.convert_to_list(origin_obj_attr_value) + help_utils.convert_to_list(obj_attr_value)) if is_replace_values_of_dicts and isinstance(_obj_attr_value, dict): obj_attr_value = StringMethods.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=help_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = ( obj_attr_value if isinstance(getattr(obj, obj_attr_name), list) else obj_attr_value[0]) if (is_allow_none_values is True or (is_allow_none_values is False and obj_attr_value is not None)): setattr(obj, obj_attr_name, obj_attr_value) return obj
def items_of_remap_keys(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "owners", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.MANAGER: "manager", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAIN: "contact", els.CAS: "custom_attributes", els.MAPPED_OBJECTS: "objects_under_assessment", els.ASSIGNEES: "assignee", els.CREATORS: "creator", els.VERIFIERS: "verifier", element.AssessmentInfoWidget.COMMENTS_HEADER: "comments", els.PRIMARY_CONTACTS: "contact", els.CREATED_AT: "created_at", els.MODIFIED_BY: "modified_by", els.LAST_UPDATED_BY: "modified_by", els.UPDATED_AT: "updated_at" } csv_remap_items = { csv.REVISION_DATE: "updated_at" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return StringMethods.dict_keys_to_upper_case(result_remap_items)
def test_asmt_from_template_w_dropdown_url( self, program, control_mapped_to_program, audit, selenium ): """Check evidence url could be filled in via Assessment dropdown. Objects structure: Program. -> Control mapped to program. -> Audit. -> Asmt template with evidence url dropdown. -> Autogenerated asmt. """ asmt_template_w_dropdown = rest_facade.create_asmt_template_w_dropdown( audit, ["url"]) expected_asmt = rest_facade.create_asmt_from_template_rest( audit, control_mapped_to_program, asmt_template_w_dropdown) dropdown = CustomAttributeDefinitionsFactory().create( **expected_asmt.cads_from_template()[0]) asmt_service = webui_service.AssessmentsService(selenium) exp_url = StringMethods.random_string( size=StringMethods.RANDOM_STR_LENGTH) expected_asmt = asmt_service.choose_and_fill_dropdown_lca( expected_asmt, dropdown, url=exp_url) expected_asmt_urls = [exp_url] expected_asmt.update_attrs( updated_at=self.info_service().get_obj(obj=expected_asmt).updated_at, evidence_urls=expected_asmt_urls, mapped_objects=[control_mapped_to_program.title], status=object_states.IN_PROGRESS).repr_ui() actual_asmt = asmt_service.get_obj_from_info_page(obj=expected_asmt) self.general_equal_assert(expected_asmt, actual_asmt, "audit")
def test_dashboard_gca(self, new_control_rest, selenium): # pylint: disable=anomalous-backslash-in-string """Check Dashboard Tab is exist if 'Dashboard' GCA filled with right value. Possible values match to regexp r"^https?://[^\s]+$". Steps: - Create 'Dashboard' gcas for object. - Fill with values - Check if 'Dashboard' tab exist. - Navigate to 'Dashboard' tab. - Check only GCAs filled with right values displayed on the tab. """ urls = ["https://gmail.by/", "https://www.google.com/", environment.app_url, StringMethods.random_string(), "ftp://something.com/"] cads_rest_service = rest_service.CustomAttributeDefinitionsService() gca_defs = (cads_rest_service.create_dashboard_gcas( new_control_rest.type, count=len(urls))) control_rest_service = rest_service.ControlsService() control_rest_service.update_obj( obj=new_control_rest, custom_attributes=dict( zip([gca_def.id for gca_def in gca_defs], urls))) expected_dashboards_items = dict(zip( [gca_def.title.replace(aliases.DASHBOARD + "_", "") for gca_def in gca_defs], urls[:3])) controls_ui_service = webui_service.ControlsService(selenium) is_dashboard_tab_exist = ( controls_ui_service.is_dashboard_tab_exist(new_control_rest)) assert is_dashboard_tab_exist actual_dashboards_items = ( controls_ui_service.get_items_from_dashboard_widget(new_control_rest)) assert expected_dashboards_items == actual_dashboards_items cads_rest_service.delete_objs(gca_defs)
def compare_cas(self_cas, other_cas): """Compare entities' 'custom_attributes' attributes.""" if isinstance(self_cas and other_cas, dict): return StringMethods.is_subset_of_dicts(self_cas, other_cas) else: Representation.attrs_values_types_error( self_attr=self_cas, other_attr=other_cas, expected_types=dict.__name__)
def get_attrs_names(cls, entity=None): """Get list unique entities attributes' names. If 'entity' then get attributes of one entered entity, else get attributes of all entities. """ all_entities_cls = (help_utils.convert_to_list(entity) if entity else list(Entity.all_entities_classes())) all_entities_attrs_names = StringMethods.convert_list_elements_to_list( [entity_cls().__dict__.keys() for entity_cls in all_entities_cls]) return list(set(all_entities_attrs_names))
def get_attrs_names_for_entities(cls, entity=None): """Get list unique attributes names for entities. If 'entity' then get attributes of one entered entity, else get attributes of all entities. """ all_entities_cls = (help_utils.convert_to_list(entity) if entity else list(Entity.all_entities_classes())) all_entities_attrs_names = StringMethods.convert_list_elements_to_list( [entity_cls().__dict__.keys() for entity_cls in all_entities_cls]) return list(set(all_entities_attrs_names))
def get_filter_exprs_by_ca(self, cad, cav, operator): """Return all possible filter expressions for CA according to CA type""" ca_type = cad.attribute_type if ca_type == AdminWidgetCustomAttributes.CHECKBOX: value = alias.YES_VAL if StringMethods.get_bool_value_from_arg( cav.attribute_value) else alias.NO_VAL values_to_filter = StringMethods.get_list_of_all_cases(value) elif ca_type == AdminWidgetCustomAttributes.PERSON: from lib.service import rest_service person = rest_service.ObjectsInfoService().get_obj( obj=Representation.repr_dict_to_obj(cav.attribute_object)) values_to_filter = [person.name, person.email] elif ca_type == AdminWidgetCustomAttributes.DATE: date_formats = ["%m/%d/%Y", "%m/%Y", "%Y-%m-%d", "%Y-%m", "%Y"] date = parser.parse(cav.attribute_value).date() values_to_filter = [date.strftime(_format) for _format in date_formats] else: values_to_filter = [cav.attribute_value] return [self.get_filter_exp(cad.title, operator, [val]) for val in values_to_filter]
def filter_objs_by_attrs(objs, **attrs): """Filter objects by attributes' items and return matched according to plurality. 'objs' - object or list objects; '**attrs' - items of attributes' names and values. """ list_objs = help_utils.convert_to_list(objs) matched_objs = [ obj for obj in list_objs if isinstance(obj, Entity.all_entities_classes()) and StringMethods.is_subset_of_dicts(dict(**attrs), obj.__dict__)] return (help_utils.get_single_obj(matched_objs) if not help_utils.is_multiple_objs(matched_objs) else matched_objs)
def filter_objs_by_attrs(objs, **attrs): """Filter objects by attributes' items and return matched according to plurality. 'objs' - object or list objects; '**attrs' - items of attributes' names and values. """ list_objs = help_utils.convert_to_list(objs) matched_objs = [ obj for obj in list_objs if isinstance(obj, Entity.all_entities_classes()) and StringMethods.is_subset_of_dicts(dict(**attrs), obj.__dict__)] return (help_utils.get_single_obj(matched_objs) if not help_utils.is_multiple_objs(matched_objs) else matched_objs)
def _create_random_ca(cls): """Create CustomAttribute entity with randomly filled fields.""" random_ca = CustomAttributeEntity() random_ca.type = cls.obj_ca random_ca.attribute_type = unicode( random.choice(AdminWidgetCustomAttributes.ALL_CA_TYPES)) random_ca.title = cls.generate_string(random_ca.attribute_type) if random_ca.attribute_type == AdminWidgetCustomAttributes.DROPDOWN: random_ca.multi_choice_options = StringMethods.random_list_strings( ) random_ca.definition_type = unicode( objects.get_singular(random.choice(objects.ALL_CA_OBJS))) random_ca.mandatory = False return random_ca
def _set_custom_attributes_list(self): """Set custom attributes list with Custom Attribute objects from current opened content item. """ for row in selenium_utils.get_when_all_visible(self._driver, self._locators.ROW_CSS): attrs = [i.text for i in row.find_elements( *self._locators.CELL_IN_ROW_CSS)] # todo: add PO and getting 'multi_choice_options' via 'Edit' btn self.custom_attributes_list.append( CustomAttributeDefinitionsFactory().create( title=attrs[0], attribute_type=attrs[1], mandatory=StringMethods.get_bool_value_from_arg(attrs[2]), definition_type=self._item_name, multi_choice_options=None))
def compare_cas(self_cas, other_cas): """Compare entities' 'custom_attributes' attributes.""" if (isinstance(self_cas, (dict, type(None))) and isinstance(other_cas, (dict, type(None)))): is_equal = False if (isinstance(self_cas, dict) and isinstance(other_cas, dict)): is_equal = StringMethods.is_subset_of_dicts(self_cas, other_cas) else: is_equal = self_cas == other_cas return is_equal else: Representation.attrs_values_types_error( self_attr=self_cas, other_attr=other_cas, expected_types=(dict.__name__, type(None).__name__))
def _set_custom_attributes_list(self): """Set custom attributes list with Custom Attribute objects from current opened content item. """ for row in selenium_utils.get_when_all_visible(self._driver, self._locators.ROW_CSS): attrs = [i.text for i in row.find_elements( *self._locators.CELL_IN_ROW_CSS)] # todo: add PO and getting 'multi_choice_options' via 'Edit' btn self.custom_attributes_list.append( CustomAttributeDefinitionsFactory().create( title=attrs[0], attribute_type=attrs[1], mandatory=StringMethods.get_bool_value_from_arg(attrs[2]), definition_type=self._item_name, multi_choice_options=None))
def compare_cas(self_cas, other_cas): """Compare entities' 'custom_attributes' attributes.""" if (isinstance(self_cas, (dict, type(None))) and isinstance(other_cas, (dict, type(None)))): is_equal = False if (isinstance(self_cas, dict) and isinstance(other_cas, dict)): is_equal = StringMethods.is_subset_of_dicts(self_cas, other_cas) else: is_equal = self_cas == other_cas return is_equal else: Representation.attrs_values_types_error( self_attr=self_cas, other_attr=other_cas, expected_types=(dict.__name__, type(None).__name__))
def _set_custom_attributes_list(self): """Set custom attributes list with Custom Attribute objects from current opened content item. """ for row in selenium_utils.get_when_all_visible(self._driver, self._locators.ROW_CSS): attrs = [i.text for i in row.find_elements( *self._locators.CELL_IN_ROW_CSS)] self.custom_attributes_list.append( CustomAttributeEntity( title=attrs[0], type=objects.get_singular(objects.CUSTOM_ATTRIBUTES), attribute_type=attrs[1], mandatory=StringMethods.get_bool_value_from_arg(attrs[2]), definition_type=self._item_name))
def generate_snapshots_fixtures(fixture): """Generate, run and return of results for snapshots dynamic fixtures according to tuple of fixture name. Example: 'create_audit_with_control__risk_and_update_control' 'create_audit_with_controls' 'create_audit_with_controls_and_update_control' 'create_audit_with_control__risk_and_update_control__risk """ global dict_executed_fixtures if isinstance(fixture, str) and fixture.startswith("create_audit_with_"): _creation_params = None _action_params = None updating_params = [] deleting_params = [] fixture_params = fixture.replace("create_audit_with_", "") if "_and_" in fixture_params: _creation_params, _action_params = fixture_params.split("_and_") if "_and_" not in fixture_params: _creation_params = fixture_params creation_params = StringMethods.convert_list_elements_to_list([ "new_{}_rest".format(param) if "_with_cas" not in param else [ "new_cas_for_{}_rest".format( objects.get_plural(param.split("_")[0])), "new_{}_rest".format(param) ] for param in _creation_params.split("__") ]) mapping_params = [ "map_new_program_rest_to_new_{}_rest".format(param) for param in _creation_params.split("__") ] creation_part = (["new_program_rest"] + creation_params + mapping_params + ["new_audit_rest"]) if _action_params: if "update" in _action_params: updating_params = [ "update_{}_rest".format(param) for param in _action_params.replace("update_", "").split("__") ] if "delete" in _action_params: deleting_params = [ "delete_{}_rest".format(param) for param in _action_params.replace("delete_", "").split("__") ] action_part = (updating_params + deleting_params) all_manipulations = creation_part + action_part generate_common_fixtures(*all_manipulations) executed_snapshots_fixtures = copy.deepcopy(dict_executed_fixtures) return executed_snapshots_fixtures
def remap_collection(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "admins", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.STATE: "status" } ui_remap_items = { els.PROGRAM_MANAGERS: "managers", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAINS: "audit_captains", els.AUDITORS: "auditors", "MAPPED_OBJECTS": "mapped_objects", els.ASSIGNEES: "assignees", els.CREATORS: "creators", "VERIFIERS": "verifiers", "COMMENTS": "comments", "CREATED_AT": "created_at", els.MODIFIED_BY: "modified_by", "LAST_UPDATED_BY": "modified_by", "UPDATED_AT": "updated_at", "ASSESSMENT_TYPE": "assessment_type", "IS_VERIFIED": "verified", "CUSTOM_ATTRIBUTES": "custom_attributes", "DESCRIPTION": "description", "EVIDENCE_URLS": "evidence_urls", "ASSERTIONS": "assertions", "EXTERNAL_SLUG": "external_slug", "EXTERNAL_ID": "external_id", "REVIEW_STATUS": "review_status", "REVIEW_STATUS_DISPLAY_NAME": "review_status_display_name", "PRIMARY_CONTACTS": "primary_contacts", "CONTROL_OPERATORS": "control_operators", "CONTROL_OWNERS": "control_owners", "URL": "url", "ID": "id", "RISK_TYPE": "risk_type", "REVIEW": "review" } csv_remap_items = { csv.REVISION_DATE: "updated_at", "REVIEW STATUS": "review_status", "REVIEW_STATUS_DISPLAY_NAME": "review_status_display_name" } result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return StringMethods.dict_keys_to_upper_case(result_remap_items)
def remap_collection(): """Get transformation dictionary {'OLD KEY': 'NEW KEY'}, where 'OLD KEY' - UI elements and CSV fields correspond to 'NEW KEY' - objects attributes. """ from lib.constants import element, files els = element.TransformationElements csv = files.TransformationCSVFields # common for UI and CSV result_remap_items = { els.TITLE: "title", els.ADMIN: "admins", els.CODE: "slug", els.REVIEW_STATE: "os_state", els.OBJECT_REVIEW: "os_state", els.OBJECT_REVIEW_FULL: "object_review_txt", els.STATE: "status" } ui_remap_items = { els.PROGRAM_MANAGERS: "managers", els.VERIFIED: "verified", els.STATUS: "status", els.LAST_UPDATED: "updated_at", els.AUDIT_CAPTAINS: "audit_captains", els.CAS: "custom_attributes", els.AUDITORS: "auditors", els.MAPPED_OBJECTS: "mapped_objects", els.ASSIGNEES: "assignees", els.CREATORS: "creators", els.VERIFIERS: "verifiers", els.COMMENTS_HEADER: "comments", els.CREATED_AT: "created_at", els.MODIFIED_BY: "modified_by", els.LAST_UPDATED_BY: "modified_by", els.UPDATED_AT: "updated_at", els.ASMT_TYPE: "assessment_type", els.LCAS: "custom_attribute_definitions", "EVIDENCE_URLS": "evidence_urls", "ASSERTIONS": "assertions", "PRIMARY_CONTACTS": "primary_contacts" } csv_remap_items = {csv.REVISION_DATE: "updated_at"} result_remap_items.update(ui_remap_items) result_remap_items.update(csv_remap_items) return StringMethods.dict_keys_to_upper_case(result_remap_items)
def update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **arguments): """Update object's attributes values.""" for obj_attr_name in arguments: if (obj_attr_name in Entity.get_attrs_names_for_entities(obj.__class__)): _obj_attr_value = arguments.get(obj_attr_name) condition = (True if is_allow_none_values else _obj_attr_value) if condition and not is_replace_values_of_dicts: # convert repr from objects to dicts exclude datetime objects obj_attr_value = ( cls.convert_objs_repr_to_dict(_obj_attr_value) if not isinstance(_obj_attr_value, datetime) else _obj_attr_value) if not is_replace_attrs_values: origin_obj_attr_value = getattr(obj, obj_attr_name) obj_attr_value = ( dict(origin_obj_attr_value.items() + obj_attr_value.items()) if obj_attr_name == "custom_attributes" else help_utils. convert_to_list(origin_obj_attr_value) + help_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) if obj_attr_name in [ "creator", "assignee", "verifier" ]: from lib.entities.entities_factory import ObjectPersonsFactory if not isinstance(obj.assignees, dict): obj.assignees = dict() obj.assignees[obj_attr_name.capitalize()] = ([ ObjectPersonsFactory().default().__dict__ ]) if is_replace_values_of_dicts and isinstance( _obj_attr_value, dict): obj_attr_value = StringMethods.exchange_dicts_items( transform_dict=_obj_attr_value, dicts=help_utils.convert_to_list( getattr(obj, obj_attr_name)), is_keys_not_values=False) obj_attr_value = (obj_attr_value if isinstance( getattr(obj, obj_attr_name), list) else obj_attr_value[0]) setattr(obj, obj_attr_name, obj_attr_value) return obj
def _create_random_obj(self, is_add_rest_attrs): """Create Custom Attribute entity with randomly and predictably filled fields, if 'is_add_rest_attrs' then add attributes for REST.""" ca_obj_attr_type = unicode( random.choice(AdminWidgetCustomAttributes.ALL_CA_TYPES)) ca_obj = self.obj_inst().update_attrs( title=self.generate_ca_title(ca_obj_attr_type), attribute_type=ca_obj_attr_type, multi_choice_options=( StringMethods.random_list_strings() if ca_obj_attr_type == AdminWidgetCustomAttributes.DROPDOWN else None), definition_type=unicode( objects.get_singular(random.choice(objects.ALL_CA_OBJS))), mandatory=False) if is_add_rest_attrs: ca_obj.update_attrs(modal_title="Add Attribute to type {}".format( ca_obj.definition_type.title())) return ca_obj
def get_list_objs_from_csv(self, path_to_exported_file): """Get and return list of objects from CSV file of exported objects in test's temporary directory 'path_to_export_dir'. """ # pylint: disable=invalid-name dict_list_objs_scopes = file_utils.get_list_objs_scopes_from_csv( path_to_csv=path_to_exported_file) dict_key = dict_list_objs_scopes.iterkeys().next() # 'Control' to 'controls', 'Control Snapshot' to 'controls' obj_name_from_dict = objects.get_plural( StringMethods.get_first_word_from_str(dict_key)) if self.obj_name == obj_name_from_dict: return self._create_list_objs( entity_factory=self.entities_factory_cls, list_scopes=dict_list_objs_scopes[dict_key]) else: raise ValueError(messages.ExceptionsMessages.err_csv_format. format(dict_list_objs_scopes))
def generate_cad(cls, **attrs): """Creates multi-choice dropdown CAD for asmt template.""" multi_choice_opts = {"file": "2", "url": "4", "comment": "1", "file_url": "6", "url_comment": "5", "file_comment": "3", "file_url_comment": "7", "nothing": "0"} dropdown_types_list = attrs["dropdown_types_list"] cad_factory = CustomAttributeDefinitionsFactory() cad = cad_factory.create( attribute_type=AdminWidgetCustomAttributes.DROPDOWN, definition_type="", multi_choice_mandatory=(",".join( multi_choice_opts[dropdown_type] for dropdown_type in dropdown_types_list)), multi_choice_options=( StringMethods.random_list_strings( list_len=len(dropdown_types_list)))) return cad_factory.generate_cads_for_asmt_tmpls([cad])[0]
def generate_cad(cls, **attrs): """Creates multi-choice dropdown CAD for asmt template.""" multi_choice_opts = {"file": "2", "url": "4", "comment": "1", "file_url": "6", "url_comment": "5", "file_comment": "3", "file_url_comment": "7", "nothing": "0"} dropdown_types_list = attrs["dropdown_types_list"] cad_factory = CustomAttributeDefinitionsFactory() cad = cad_factory.create( attribute_type=AdminWidgetCustomAttributes.DROPDOWN, definition_type="", multi_choice_mandatory=(",".join( multi_choice_opts[dropdown_type] for dropdown_type in dropdown_types_list)), multi_choice_options=( StringMethods.random_list_strings( list_len=len(dropdown_types_list)))) return cad_factory.generate_cads_for_asmt_tmpls([cad])[0]
def get_list_objs_from_csv(self, path_to_exported_file): """Get and return list of objects from CSV file of exported objects in test's temporary directory 'path_to_export_dir'. """ # pylint: disable=invalid-name dict_list_objs_scopes = file_utils.get_list_objs_scopes_from_csv( path_to_csv=path_to_exported_file) dict_key = dict_list_objs_scopes.iterkeys().next() # 'Control' to 'controls', 'Control Snapshot' to 'controls' obj_name_from_dict = objects.get_plural( StringMethods.get_first_word_from_str(dict_key)) if self.obj_name == obj_name_from_dict: return self._create_list_objs( entity_factory=self.entities_factory_cls, list_scopes=dict_list_objs_scopes[dict_key]) else: raise ValueError(messages.ExceptionsMessages.err_csv_format. format(dict_list_objs_scopes))
def _create_random_obj(self, is_add_rest_attrs): """Create Custom Attribute entity with randomly and predictably filled fields, if 'is_add_rest_attrs' then add attributes for REST.""" ca_obj_attr_type = unicode(random.choice( AdminWidgetCustomAttributes.ALL_CA_TYPES)) ca_obj = self.obj_inst().update_attrs( title=self.generate_ca_title(ca_obj_attr_type), attribute_type=ca_obj_attr_type, multi_choice_options=( StringMethods.random_list_strings() if ca_obj_attr_type == AdminWidgetCustomAttributes.DROPDOWN else None), definition_type=unicode(objects.get_singular( random.choice(objects.ALL_CA_OBJS))), mandatory=False) if is_add_rest_attrs: ca_obj.update_attrs( modal_title="Add Attribute to type {}".format( ca_obj.definition_type.title())) return ca_obj
def test_set_url_for_dropdown_lca(self, program, control_mapped_to_program, login_role, obj, obj_role, selenium): """Check evidence url could be filled in via Assessment dropdown. Objects structure: Program. -> Control mapped to program. -> Audit. -> Asmt template with evidence url dropdown. -> Autogenerated asmt. """ user = rest_facade.create_user_with_role(login_role) obj_args = {obj_role: [user]} audit = rest_facade.create_audit(program, **obj_args if obj == "audit" else {}) url = StringMethods.random_string() asmt_template_w_dropdown = rest_facade.create_asmt_template( audit, cad_type=AdminWidgetCustomAttributes.DROPDOWN, dropdown_types_list=["url"], **obj_args if obj == "assessment" else {}) expected_asmt = rest_facade.create_asmt_from_template( audit, asmt_template_w_dropdown, [control_mapped_to_program]) dropdown = CustomAttributeDefinitionsFactory().create( **expected_asmt.cads_from_template()[0]) users.set_current_user(user) asmt_service = webui_service.AssessmentsService(selenium) asmt_service.choose_and_fill_dropdown_lca(expected_asmt, dropdown, url=url) rest_asmt_obj = self.info_service().get_obj(expected_asmt) expected_asmt.update_attrs( custom_attributes={ dropdown.title.upper(): dropdown.multi_choice_options }, updated_at=rest_asmt_obj.updated_at, modified_by=rest_asmt_obj.modified_by, evidence_urls=[url], mapped_objects=[control_mapped_to_program.title], status=object_states.IN_PROGRESS) _assert_asmt(asmt_service, expected_asmt)
def generate_snapshots_fixtures(fixture): """Generate, run and return of results for snapshots dynamic fixtures according to tuple of fixture name. Example: 'create_audit_with_control__risk_and_update_control' 'create_audit_with_controls' 'create_audit_with_controls_and_update_control' 'create_audit_with_control__risk_and_update_control__risk """ global dict_executed_fixtures if isinstance(fixture, str) and fixture.startswith("create_audit_with_"): _creation_params = None _action_params = None updating_params = [] deleting_params = [] fixture_params = fixture.replace("create_audit_with_", "") if "_and_" in fixture_params: _creation_params, _action_params = fixture_params.split("_and_") if "_and_" not in fixture_params: _creation_params = fixture_params creation_params = StringMethods.convert_list_elements_to_list([ "new_{}_rest".format(param) if "_with_cas" not in param else ["new_cas_for_{}_rest".format(objects.get_plural(param.split("_")[0])), "new_{}_rest".format(param)] for param in _creation_params.split("__")]) mapping_params = [ "map_new_program_rest_to_new_{}_rest".format(param) for param in _creation_params.split("__")] creation_part = (["new_program_rest"] + creation_params + mapping_params + ["new_audit_rest"]) if _action_params: if "update" in _action_params: updating_params = ["update_{}_rest".format(param) for param in _action_params.replace("update_", "").split("__")] if "delete" in _action_params: deleting_params = ["delete_{}_rest".format(param) for param in _action_params.replace("delete_", "").split("__")] action_part = (updating_params + deleting_params) all_manipulations = creation_part + action_part generate_common_fixtures(*all_manipulations) executed_snapshots_fixtures = copy.deepcopy(dict_executed_fixtures) return executed_snapshots_fixtures
def compare_comments(self_comments, other_comments): """Compare entities' 'comments' attributes due to specific dictionaries' format values in list comments. """ # pylint: disable=no-else-return if help_utils.is_multiple_objs( StringMethods.convert_list_elements_to_list( [self_comments, other_comments]), (dict, type(None))): if self_comments and other_comments: is_comments_equal_list = [] for self_comment, other_comment in zip(self_comments, other_comments): is_comments_equal = False if self_comment and other_comment: is_comments_equal = (all( (Representation.compare_datetime( self_comment.get("created_at"), other_comment.get("created_at")) if ( isinstance(_self, datetime) and isinstance(_other, datetime) ) else _self == _other) for _self, _other in zip(self_comment.iteritems( ), other_comment.iteritems()))) # convert datetime to unicode in order to get visible repr if self_comment.get("created_at"): self_comment["created_at"] = unicode( self_comment.get("created_at")) if other_comment.get("created_at"): other_comment["created_at"] = unicode( other_comment.get("created_at")) else: is_comments_equal = self_comment == other_comment is_comments_equal_list.append(is_comments_equal) return all(is_equal for is_equal in is_comments_equal_list) else: return self_comments == other_comments else: Representation.attrs_values_types_error( self_attr=self_comments, other_attr=other_comments, expected_types=(list.__name__, type(None).__name__))
def compare_comments(self_comments, other_comments): """Compare entities' 'comments' attributes due to specific dictionaries' format values in list comments. """ # pylint: disable=no-else-return if help_utils.is_multiple_objs( StringMethods.convert_list_elements_to_list( [self_comments, other_comments]), (dict, type(None))): if self_comments and other_comments: is_comments_equal_list = [] for self_comment, other_comment in zip(self_comments, other_comments): is_comments_equal = False if self_comment and other_comment: is_comments_equal = ( all((Representation.compare_datetime( self_comment.get("created_at"), other_comment.get("created_at") ) if (isinstance(_self, datetime) and isinstance(_other, datetime))else _self == _other) for _self, _other in zip( self_comment.iteritems(), other_comment.iteritems()))) # convert datetime to unicode in order to get visible repr if self_comment.get("created_at"): self_comment["created_at"] = unicode( self_comment.get("created_at")) if other_comment.get("created_at"): other_comment["created_at"] = unicode( other_comment.get("created_at")) else: is_comments_equal = self_comment == other_comment is_comments_equal_list.append(is_comments_equal) return all(is_equal for is_equal in is_comments_equal_list) else: return self_comments == other_comments else: Representation.attrs_values_types_error( self_attr=self_comments, other_attr=other_comments, expected_types=(list.__name__, type(None).__name__))
def create_asmt_template_w_dropdown(audit, dropdown_types_list): """Create assessment template with dropdown custom attribute.""" multi_choice_mandatory = {"file": "2", "url": "4", "comment": "1", "file_url": "6", "url_comment": "5", "file_comment": "3", "file_url_comment": "7", "nothing": "0"} ca_definitions_factory = CustomAttributeDefinitionsFactory() custom_attribute_definitions = [ca_definitions_factory.create( title=(ca_definitions_factory.generate_ca_title( AdminWidgetCustomAttributes.DROPDOWN)), attribute_type=AdminWidgetCustomAttributes.DROPDOWN, definition_type=AdminWidgetCustomAttributes.DROPDOWN, multi_choice_mandatory=(",".join( multi_choice_mandatory[dropdown_type] for dropdown_type in dropdown_types_list)), multi_choice_options=( StringMethods.random_list_strings( list_len=len(dropdown_types_list))))] custom_attribute_definitions = (ca_definitions_factory. generate_ca_defenitions_for_asmt_tmpls( custom_attribute_definitions)) return create_asmt_template( audit, custom_attribute_definitions=custom_attribute_definitions)
def create(self, is_add_rest_attrs=False, **attrs): """Create random Custom Attribute object's instance, if 'is_add_rest_attrs' then add attributes for REST, if 'attrs' then update attributes accordingly. """ attrs = copy.deepcopy(attrs) attrs.setdefault("attribute_type", random.choice(AdminWidgetCustomAttributes.ALL_CA_TYPES)) attrs.setdefault("definition_type", objects.get_singular(random.choice(objects.ALL_CA_OBJS))) attrs.setdefault("title", self.generate_ca_title(attrs["attribute_type"])) if attrs["attribute_type"] == AdminWidgetCustomAttributes.DROPDOWN: attrs.setdefault("multi_choice_options", StringMethods.random_list_strings()) else: attrs["multi_choice_options"] = None attrs.setdefault("mandatory", False) obj = self.obj_inst() obj.update_attrs(is_allow_none=False, **attrs) if is_add_rest_attrs: obj.modal_title = "Add Attribute to type {}".format( obj.definition_type.title()) return obj
def generate_slug(cls): """Generate slug in unicode format according str part and random data.""" return unicode("{slug}".format(slug=StringMethods.random_uuid()))
def generate_email(cls, domain=users.DEFAULT_EMAIL_DOMAIN): """Generate email in unicode format according to domain.""" return unicode("{mail_name}@{domain}".format( mail_name=StringMethods.random_uuid(), domain=domain))
def generate_slug(cls): """Generate slug in unicode format according str part and random data.""" return unicode("{slug}".format(slug=StringMethods.random_uuid()))
def generate_email(cls, domain=users.DEFAULT_EMAIL_DOMAIN): """Generate email in unicode format according to domain.""" return unicode("{mail_name}@{domain}".format( mail_name=StringMethods.random_uuid(), domain=domain))
def convert_repr_rest_to_ui(obj): """Convert object's attributes from REST to UI like representation.""" def convert_attr_val_repr_dict_to_unicode(attr_name, attr_value): """Convert attribute value from dictionary to unicode representation (get value by key from dictionary 'attr_value' where key determine according to 'attr_name'). """ if isinstance(attr_value, dict): converted_attr_value = attr_value if attr_name in Representation.people_attrs_names + [ "created_by", "modified_by" ]: converted_attr_value = unicode(attr_value.get("email")) if attr_name in ["custom_attribute_definitions", "program", "audit", "mapped_objects"]: converted_attr_value = ( unicode(attr_value.get("title")) if attr_name != "custom_attribute_definitions" else {attr_value.get("id"): attr_value.get("title").upper()} ) if attr_name in ["custom_attribute_values"]: converted_attr_value = {attr_value.get("custom_attribute_id"): attr_value.get("attribute_value")} if obj_attr_name == "comments": converted_attr_value = { k: (parser.parse(v).replace(tzinfo=tz.tzutc()) if k == "created_at" and isinstance(v, unicode) else v) for k, v in attr_value.iteritems() if k in ["modified_by", "created_at", "description"]} if attr_name == "assertions": for name, assertion_id in ControlEntity.ASSERTIONS.iteritems(): if assertion_id == attr_value["id"]: converted_attr_value = name return converted_attr_value origin_obj = copy.deepcopy(obj) for obj_attr_name in obj.__dict__.keys(): # 'Ex', u'Ex', 1, None to 'Ex', u'Ex', 1, None obj_attr_value = getattr(obj, obj_attr_name) # REST like u'08-20-2017T04:30:45' to date=2017-08-20, # timetz=04:30:45+00:00 if (obj_attr_name in ["updated_at", "created_at"] and isinstance(obj_attr_value, unicode)): obj_attr_value = (parser.parse(obj_attr_value). replace(tzinfo=tz.tzutc())) if isinstance(obj_attr_value, dict) and obj_attr_value: # "modified_by" {"type": "Person", "id": x} to u'*****@*****.**' # todo: deprecated? if obj_attr_name == "modified_by": from lib.service import rest_service obj_attr_value = getattr(rest_service.ObjectsInfoService().get_obj( obj=Representation.repr_dict_to_obj(obj_attr_value)), "email") # {'name': u'Ex1', 'type': u'Ex2', ...} to u'Ex1' else: obj_attr_value = convert_attr_val_repr_dict_to_unicode( obj_attr_name, obj_attr_value) # [el1, el2, ...] or [{item1}, {item2}, ...] to [u'Ex1, u'Ex2', ...] if (isinstance(obj_attr_value, list) and all(isinstance(item, dict) for item in obj_attr_value)): obj_attr_value = [ convert_attr_val_repr_dict_to_unicode(obj_attr_name, item) for item in obj_attr_value] setattr(obj, obj_attr_name, obj_attr_value) # merge "custom_attribute_definitions" and "custom_attribute_values" obj_cas_attrs_names = [ "custom_attributes", "custom_attribute_definitions", "custom_attribute_values"] if set(obj_cas_attrs_names).issubset(obj.__dict__.keys()): cas_def = obj.custom_attribute_definitions cas_val = obj.custom_attribute_values # form CAs values of CAs definitions exist but CAs values not, or CAs # definitions have different then CAs values lengths if (cas_def and (not cas_val or (isinstance(cas_def and cas_val, list)) and len(cas_def) != len(cas_val))): from lib.entities.entities_factory import ( CustomAttributeDefinitionsFactory) cas_val_dicts_keys = ([_.keys()[0] for _ in cas_val] if isinstance(cas_val, list) else [None]) _cas_val = [ {k: v} for k, v in CustomAttributeDefinitionsFactory.generate_ca_title_id( [Representation.repr_dict_to_obj(cad) for cad in origin_obj.custom_attribute_definitions] ).iteritems() if k not in cas_val_dicts_keys] cas_val = _cas_val if not cas_val else cas_val + _cas_val cas_def_dict = ( dict([_def.iteritems().next() for _def in cas_def]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else None) cas_val_dict = ( dict([_val.iteritems().next() for _val in cas_val]) if (isinstance(cas_def, list) and all(isinstance(_def, dict) for _def in cas_def)) else None) cas = StringMethods.merge_dicts_by_same_key(cas_def_dict, cas_val_dict) if obj.custom_attributes: cas.update(obj.custom_attributes) if cas in [{None: None}, {}]: cas = None setattr(obj, "custom_attributes", cas) return obj