def update_objs(self, objs, factory_params=None, **attrs_for_template): """Update existing objects via REST API and return updated.""" return self.update_list_objs( entity_factory=self.entities_factory_cls(), list_objs_to_update=string_utils.convert_to_list(objs), attrs_to_factory=factory_params, **attrs_for_template)
def create(self, objs, owner=PersonsFactory().default()): """Assign of an owner to objects.""" return [ self.client.create_object(type=self._object_owner, ownable=obj.__dict__, person=owner.__dict__) for obj in string_utils.convert_to_list(objs) ]
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 string_utils. convert_to_list(origin_obj_attr_value) + string_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=string_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 assign_owner_to_objs(self, objs, owner=ObjectOwnersFactory().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 string_utils.convert_to_list(objs) ]
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 = (string_utils.convert_to_list(entity) if entity else Entity.all_entities_classes()) all_entities_attrs_names = string_utils.convert_list_elements_to_list( [entity_cls().__dict__.keys() for entity_cls in all_entities_cls]) return list(set(all_entities_attrs_names))
def test_mapping_objectives_to_control_via_rest(self, new_control_rest, dynamic_object, 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 = string_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_object))[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(string_utils.convert_to_list(dynamic_object)) == actual_objectives_tab_count)
def map_objs(self, src_obj, dest_objs): """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__) for dest_obj in string_utils.convert_to_list(dest_objs) ]
def create(self, src_obj, dest_objs): """Create relationship from source to destination objects and return created. """ return [ self.client.create_object(type=self._relationship, source=src_obj.__dict__, destination=dest_obj.__dict__) for dest_obj in string_utils.convert_to_list(dest_objs) ]
def assert_for_snapshotable_controls(expected_controls, actual_controls): """Specific assert method for snapshotable Controls due of issue in app GGRC-2344. """ expected_controls = string_utils.convert_to_list(expected_controls) actual_controls = string_utils.convert_to_list(actual_controls) expected_controls_wo_cas = [ expected_control.update_attrs(custom_attributes={None: None}) for expected_control in expected_controls] actual_controls_wo_cas = [ actual_control.update_attrs(custom_attributes={None: None}) for actual_control in actual_controls] assert expected_controls_wo_cas == actual_controls_wo_cas, ( messages.ERR_MSG_FORMAT.format( expected_controls_wo_cas, actual_controls_wo_cas)) assert (True if (all(string_utils.is_one_dict_is_subset_another_dict( exp_ctrl.custom_attributes, act_ctrl.custom_attributes) for exp_ctrl, act_ctrl in zip(expected_controls, actual_controls))) else pytest.xfail(reason="Issue in app GGRC-2344"))
def extract_excluding_attrs(cls, expected_objs, actual_objs, *exclude_attrs): """Extract dictionary which contains collections to compare according to exclude attributes. Where: 'exp_objs_wo_ex_attrs', 'act_objs_wo_ex_attrs' - list objects w/o excluding attributes; 'exp_ex_attrs', 'act_ex_attrs' - list dictionaries w/ excluding attributes (items which contain attributes' names and values); ''*exclude_attrs' - tuple of excluding attributes names. """ # pylint: disable=invalid-name expected_objs = string_utils.convert_to_list(expected_objs) actual_objs = string_utils.convert_to_list(actual_objs) expected_excluded_attrs = [ dict([(attr, getattr(expected_obj, attr)) for attr in exclude_attrs]) for expected_obj in expected_objs ] actual_excluded_attrs = [ dict([(attr, getattr(actual_obj, attr)) for attr in exclude_attrs]) for actual_obj in actual_objs ] expected_objs_wo_excluded_attrs = [ expected_obj.update_attrs(**dict([(attr, ({ None: None } if attr == "custom_attributes" else None)) for attr in exclude_attrs])) for expected_obj in expected_objs ] actual_objs_wo_excluded_attrs = [ actual_obj.update_attrs(**dict([(attr, ({ None: None } if attr == "custom_attributes" else None)) for attr in exclude_attrs])) for actual_obj in actual_objs ] return { "exp_objs_wo_ex_attrs": expected_objs_wo_excluded_attrs, "act_objs_wo_ex_attrs": actual_objs_wo_excluded_attrs, "exp_ex_attrs": expected_excluded_attrs, "act_ex_attrs": actual_excluded_attrs }
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=string_utils.convert_to_list(objs), attrs_to_factory=factory_params, **attrs_for_template) return self.entities_factory_cls().filter_objs_attrs( objs=list_objs, attrs_to_include=self.entities_factory_cls().obj_attrs_names)
def assert_for_snapshotable_controls(expected_controls, actual_controls): """Specific assert method for snapshotable Controls due of issue in app GGRC-2344. """ expected_controls = string_utils.convert_to_list(expected_controls) actual_controls = string_utils.convert_to_list(actual_controls) expected_controls_wo_cas = [ expected_control.update_attrs(custom_attributes={None: None}) for expected_control in expected_controls ] actual_controls_wo_cas = [ actual_control.update_attrs(custom_attributes={None: None}) for actual_control in actual_controls ] assert expected_controls_wo_cas == actual_controls_wo_cas, ( messages.ERR_MSG_FORMAT.format(expected_controls_wo_cas, actual_controls_wo_cas)) assert (True if (all( string_utils.is_one_dict_is_subset_another_dict( exp_ctrl.custom_attributes, act_ctrl.custom_attributes) for exp_ctrl, act_ctrl in zip(expected_controls, actual_controls))) else pytest.xfail(reason="Issue in app GGRC-2344"))
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_obj_repr_from_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 string_utils. convert_to_list(origin_obj_attr_value) + string_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) 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=string_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 assert_for_snapshotable_controls(expected_controls, actual_controls): """Specific assert method for snapshotable Controls due of issue in app GGRC-2344. """ expected_controls = string_utils.convert_to_list(expected_controls) actual_controls = string_utils.convert_to_list(actual_controls) expected_controls_wo_cas = [ copy.deepcopy(expected_control).update_attrs( custom_attributes={None: None}) for expected_control in expected_controls ] actual_controls_wo_cas = [ copy.deepcopy(actual_control).update_attrs( custom_attributes={None: None}) for actual_control in actual_controls ] assert expected_controls_wo_cas == actual_controls_wo_cas, ( messages.AssertionMessages.format_err_msg_equal( expected_controls_wo_cas, actual_controls_wo_cas)) assert (True if (all( entity.Entity.compare_cas(exp_ctrl.custom_attributes, act_ctrl.custom_attributes) for exp_ctrl, act_ctrl in zip(expected_controls, actual_controls))) else pytest.xfail(reason="Issue in app GGRC-2344"))
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 = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes ] list_scopes_remapped = string_utils.remap_keys_for_list_dicts( dict_of_transform_keys=Entity.items_of_remap_keys(), list_dicts=list_scopes_with_upper_keys) # add extra 'owners' attribute name and value if 'creator' in scopes list_extra_scopes = [ dict(scope_remapped.items() + [("owners", scope_remapped.get("creator"))]) if "creator" in scope_remapped.keys() else scope_remapped for scope_remapped in list_scopes_remapped ] # convert and represent values in scopes for scope in list_extra_scopes: for key, val in scope.iteritems(): # convert u'false' and u'true' to boolean scope[key] = string_utils.get_bool_from_string(val) # convert multiple values to list of strings if key in ["owners", "assessor", "creator"]: # convert CSV like u'*****@*****.**' to u'Example User' val = val if val != url.DEFAULT_USER_EMAIL else roles.DEFAULT_USER scope[key] = string_utils.convert_to_list(val) # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS and "*" in val): scope[key] = val.replace("*", "") return [ EntitiesFactory.update_objs_attrs_values_by_entered_data( objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_extra_scopes, list_factory_objs) ]
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 obj.__dict__.keys(): condition = (True if is_allow_none_values else arguments.get(obj_attr_name)) if condition: obj_attr_value = cls.convert_obj_repr_from_obj_to_dict( arguments.get(obj_attr_name)) 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 string_utils. convert_to_list(origin_obj_attr_value) + string_utils.convert_to_list(obj_attr_value)) setattr(obj, obj_attr_name, obj_attr_value) return obj
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 = [ string_utils.dict_keys_to_upper_case(scope) for scope in list_scopes] list_scopes_remapped = string_utils.remap_keys_for_list_dicts( dict_of_transform_keys=Entity.items_of_remap_keys(), list_dicts=list_scopes_with_upper_keys) # add extra 'owners' attribute name and value if 'creator' in scopes list_extra_scopes = [ dict(scope_remapped.items() + [("owners", scope_remapped.get("creator"))]) if "creator" in scope_remapped.keys() else scope_remapped for scope_remapped in list_scopes_remapped] # convert and represent values in scopes for scope in list_extra_scopes: for key, val in scope.iteritems(): # convert u'false' and u'true' to boolean scope[key] = string_utils.get_bool_from_string(val) # convert multiple values to list of strings if key in ["owners", "assessor", "creator"]: # convert CSV like u'*****@*****.**' to u'Example User' val = val if val != url.DEFAULT_USER_EMAIL else roles.DEFAULT_USER scope[key] = string_utils.convert_to_list(val) # convert 'slug' from CSV for snapshoted objects u'*23eb72ac-4d9d' if (key == "slug" and self.obj_name in objects.ALL_SNAPSHOTABLE_OBJS and "*" in val): scope[key] = val.replace("*", "") return [ EntitiesFactory.update_objs_attrs_values_by_entered_data( objs=factory_obj, is_allow_none_values=False, **scope) for scope, factory_obj in zip(list_extra_scopes, list_factory_objs)]
def delete(self, objs): """Delete existing Controls objects via REST API.""" return [ self.client.delete_object(href=obj.href) for obj in string_utils.convert_to_list(objs) ]
def update(self, objs): """Update existing Controls objects via REST API and return updated.""" return self.update_list_objs( list_old_objs=string_utils.convert_to_list(objs), factory=ControlsFactory())
def update_objs(self, objs): """Update existing objects via REST API and return updated.""" return self.update_list_objs( list_objs_to_update=string_utils.convert_to_list(objs), entity_factory=self.entities_factory_cls())