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 general_equal_assert(expected_objs, actual_objs, *exclude_attrs): """Perform general equal assert for deepcopy converted to list expected and actual objects according to '*exclude_attrs' tuple of excluding attributes' names (compare objects' collections w/ attributes' values set to None). """ expected_objs_wo_excluded_attrs, actual_objs_wo_excluded_attrs = ( Representation.extract_objs( help_utils.convert_to_list(expected_objs), help_utils.convert_to_list(actual_objs), *exclude_attrs)) assert (expected_objs_wo_excluded_attrs == actual_objs_wo_excluded_attrs), ( messages.AssertionMessages.format_err_msg_equal( expected_objs_wo_excluded_attrs, actual_objs_wo_excluded_attrs))
def general_contain_assert(expected_obj, actual_objs, *exclude_attrs): """Perform general contain assert for deepcopy converted expected object and actual objects according to '*exclude_attrs' tuple of excluding attributes' names (compare objects' collections w/ attributes' values set to None). """ expected_obj_wo_excluded_attrs = Entity.extract_objs_wo_excluded_attrs( help_utils.convert_to_list(expected_obj), *exclude_attrs)[0] actual_objs_wo_excluded_attrs = Entity.extract_objs_wo_excluded_attrs( help_utils.convert_to_list(actual_objs), *exclude_attrs) assert (expected_obj_wo_excluded_attrs in actual_objs_wo_excluded_attrs), ( messages.AssertionMessages.format_err_msg_contains( expected_obj_wo_excluded_attrs, actual_objs_wo_excluded_attrs))
def generate(self, mapped_objects, audit, asmt_tmpl=None): """Generate Assessment objects' instances under 'audit' based on info about 'mapped_objects' and 'asmt_tmpl' use generation logic accordingly. """ mapped_objects = help_utils.convert_to_list(mapped_objects) asmts_creators = PeopleFactory.extract_people_emails(self.admins) asmts_assignees = audit.audit_captains asmts_verifiers = ( audit.auditors if audit.auditors else audit.audit_captains) asmts_cas_def = None asmts_type = ( mapped_objects[0].type if all(getattr(mapped_obj, "type") for mapped_obj in mapped_objects) else None) if asmt_tmpl: if asmts_type != asmt_tmpl.template_object_type: raise ValueError( "Mapped objects' type: {} have to be the same with Assessment " "Template's type: {}".format( asmts_type, asmt_tmpl.template_object_type)) # if assignees or verifiers are ids (int not str related attrs) # todo add logic to converts ids (int) repr to users' names if any(all(isinstance(user, int) for user in asmts_users) for asmts_users in [asmts_users for asmts_users in asmts_assignees, asmts_verifiers if isinstance(asmts_users, list)]): raise NotImplementedError if asmt_tmpl.assignees == unicode(roles.AUDITORS): asmts_assignees = audit.auditors if asmt_tmpl.verifiers != unicode(roles.AUDITORS): asmts_verifiers = audit.audit_captains if getattr(asmt_tmpl, "custom_attribute_definitions"): asmts_cas_def = asmt_tmpl.custom_attribute_definitions
def map_objs(self, src_obj, dest_objs, **attrs_for_template): """Create relationship from source to destination objects and return created. """ return [self.client.create_object( type=objects.get_singular(self.endpoint), source=src_obj.__dict__, destination=dest_obj.__dict__, **attrs_for_template) for dest_obj in help_utils.convert_to_list(dest_objs)]
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 xfail_equal_assert(expected_objs, actual_objs, issue_msg, *exclude_attrs): """Perform xfail equal assert based on deepcopy converted to list expected and actual objects according to 'issue_msg' string and '*exclude_attrs' tuple of excluding attributes' names (compare simple' collections based on excluding attributes (attributes' names and values, if 'False' then rise pytest's xfail, else pytest's fail. """ expected_excluded_attrs, actual_excluded_attrs = ( Representation.extract_simple_collections( help_utils.convert_to_list(expected_objs), help_utils.convert_to_list(actual_objs), *exclude_attrs)) assert_msg = messages.AssertionMessages.format_err_msg_equal( expected_excluded_attrs, actual_excluded_attrs) is_list_excluded_attrs_equal = ( Representation.is_list_of_attrs_equal( expected_excluded_attrs, actual_excluded_attrs)) Test.check_xfail_or_fail( is_condition=is_list_excluded_attrs_equal, issue_msg=issue_msg, assert_msg=assert_msg)
def update_objs(self, objs, factory_params=None, **attrs_for_template): """Update existing objects via REST API and return list of updated objects with filtered attributes. """ list_objs = self.update_list_objs( entity_factory=self.entities_factory_cls(), list_objs_to_update=help_utils.convert_to_list(objs), attrs_to_factory=factory_params, **attrs_for_template) return Entity.filter_objs_attrs( obj_or_objs=list_objs, attrs_to_include=self.entities_factory_cls().obj_attrs_names)
def test_mapping_objectives_to_control_via_rest( self, new_control_rest, dynamic_objects, dynamic_relationships, is_map_again, selenium ): """Check if Objectives can be mapped to Control via REST.""" # pylint: disable=too-many-arguments expected_status_code = RestClient.STATUS_CODES["OK"] dynamic_relationships = help_utils.convert_to_list(dynamic_relationships) assert all(expected_status_code == relationship.status_code for relationship in dynamic_relationships) assert ( (rest_service.RelationshipsService().map_objs( src_obj=new_control_rest, dest_objs=dynamic_objects))[0].status_code == expected_status_code) if is_map_again else True actual_objectives_tab_count = ( webui_service.ObjectivesService(selenium).get_count_objs_from_tab( src_obj=new_control_rest)) assert (len(help_utils.convert_to_list(dynamic_objects)) == actual_objectives_tab_count)
def get_acl_members(role_id, people): """Return ACL the same members as list of dicts: [{ac_role_id: *, person: {id: *}, ...] """ def get_acl_member(role_id, person): """Return ACL member as dict: {ac_role_id: *, person: {id: *}.""" if isinstance(person, entity.PersonEntity): return {"ac_role_id": role_id, "person": person.repr_min_dict()} else: raise ValueError(messages.CommonMessages.err_common.format( entity.PersonEntity, person)) return [get_acl_member(role_id, person) for person in help_utils.convert_to_list(people)]
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 get_acl_members(role_id, people): """Return ACL the same members as list of dicts: [{ac_role_id: *, person: {id: *}, ...] """ def get_acl_member(role_id, person): """Return ACL member as dict: {ac_role_id: *, person: {id: *}.""" if isinstance(person, PersonEntity): return {"ac_role_id": role_id, "person": person.repr_min_dict()} else: raise ValueError(messages.CommonMessages.err_common.format( PersonEntity, person)) return [get_acl_member(role_id, person) for person in help_utils.convert_to_list(people)]
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 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", "assessor", "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 = string_utils.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 update_obj_attrs_values(obj, is_replace_attrs_values, is_allow_none_values, **attrs): """Update object's attributes values.""" if "review" in attrs and isinstance(attrs["review"], ReviewEntity): obj.update_review(attrs.pop("review")) 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 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 test_mapping_objectives_to_control_via_rest( self, control, obj, is_map_again, selenium ): """Check if Objectives can be mapped to Control via REST.""" # pylint: disable=too-many-arguments expected_status_code = RestClient.STATUS_CODES["OK"] mapping_responses = rest_facade.map_objs(control, obj) assert all(expected_status_code == response.status_code for response in mapping_responses) assert (rest_facade.map_objs(control, obj)[0].status_code == expected_status_code) if is_map_again else True actual_objectives_tab_count = ( webui_service.ObjectivesService(selenium).get_count_objs_from_tab( src_obj=control)) assert (len(help_utils.convert_to_list(obj)) == actual_objectives_tab_count)
def generate(self, mapped_objects, audit, asmt_tmpl=None): """Generate Assessment objects' instances under 'audit' based on info about 'mapped_objects' and 'asmt_tmpl' use generation logic accordingly. """ mapped_objects = help_utils.convert_to_list(mapped_objects) asmts_creators = PeopleFactory.extract_people_emails(self.admins) asmts_assignees = audit.audit_captains asmts_verifiers = (audit.auditors if audit.auditors else audit.audit_captains) asmts_cas_def = None asmts_type = (mapped_objects[0].type if all( getattr(mapped_obj, "type") for mapped_obj in mapped_objects) else None) if asmt_tmpl: if asmts_type != asmt_tmpl.template_object_type: raise ValueError( "Mapped objects' type: {} have to be the same with Assessment " "Template's type: {}".format( asmts_type, asmt_tmpl.template_object_type)) # if assignees or verifiers are ids (int not str related attrs) # todo add logic to converts ids (int) repr to users' names if any( all(isinstance(user, int) for user in asmts_users) for asmts_users in [ asmts_users for asmts_users in asmts_assignees, asmts_verifiers if isinstance(asmts_users, list) ]): raise NotImplementedError if asmt_tmpl.assignees == unicode(roles.AUDITORS): asmts_assignees = audit.auditors if asmt_tmpl.verifiers != unicode(roles.AUDITORS): asmts_verifiers = audit.audit_captains if getattr(asmt_tmpl, "custom_attribute_definitions"): asmts_cas_def = asmt_tmpl.custom_attribute_definitions
def extract_people_emails(people): """Extract values for person's email attributes.""" return [ person.email for person in help_utils.convert_to_list(people) if isinstance(person, PersonEntity) ]
def delete_objs(self, objs): """Delete existing objects via REST API.""" return [self.client.delete_object(href=obj.href) for obj in help_utils.convert_to_list(objs)]
def assign_owner_to_objs(self, objs, owner=ObjectPersonsFactory().default()): """Assign of an owner to objects.""" return [self.client.create_object( type=objects.get_singular(self.endpoint), ownable=obj.__dict__, person=owner.__dict__) for obj in help_utils.convert_to_list(objs)]
def extract_people_emails(people): """Extract values for person's email attributes.""" return [ person.email for person in help_utils.convert_to_list(people) if isinstance(person, PersonEntity)]
def _extend_list_all_scopes(self, headers, values): """Extend 'list all scopes' by headers' text and values' text.""" self.list_all_headers_txt.extend(help_utils.convert_to_list(headers)) self.list_all_values_txt.extend(help_utils.convert_to_list(values))
def delete_objs(self, objs): """Delete existing objects via REST API.""" return [ self.client.delete_object(href=obj.href) for obj in help_utils.convert_to_list(objs) ]
def _extend_list_all_scopes(self, headers, values): """Extend 'list all scopes' by headers' text and values' text.""" self.list_all_headers_txt.extend(help_utils.convert_to_list(headers)) self.list_all_values_txt.extend(help_utils.convert_to_list(values))
def assign_owner_to_objs(self, objs, owner=ObjectPersonsFactory().default()): """Assign of an owner to objects.""" return [self.client.create_object( type=objects.get_singular(self.endpoint), ownable=obj.__dict__, person=owner.__dict__) for obj in help_utils.convert_to_list(objs)]