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 _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 test_destructive_search_unified_mapper_and_check_mapping( self, create_audit_with_control_and_update_control, control_for_mapper, control_for_tree_view, dynamic_objects, dynamic_relationships, expected_map_statuses, expected_is_found, selenium): """Check searching of shapshotable and snapshoted objects via Unified Mapper modal and check their correct mapping. """ audit_with_one_control = create_audit_with_control_and_update_control source_obj = dynamic_objects expected_control_from_mapper = ( audit_with_one_control[control_for_mapper][0].repr_ui()) expected_control_from_tree_view = ( (expected_control_from_mapper if control_for_mapper == control_for_tree_view else audit_with_one_control[control_for_tree_view][0].repr_ui()) if control_for_tree_view else None) expected_map_status = MappingStatusAttrs( expected_control_from_mapper.title, *expected_map_statuses) controls_ui_service = webui_service.ControlsService(selenium) actual_controls_from_mapper, actual_map_status = ( controls_ui_service.get_list_objs_from_mapper( src_obj=source_obj, dest_objs=[expected_control_from_mapper])) actual_controls_from_tree_view = ( controls_ui_service.get_list_objs_from_tree_view( src_obj=source_obj)) # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_controls_from_mapper, actual_controls_from_mapper = ( Representation.extract_objs( [expected_control_from_mapper], actual_controls_from_mapper, *Representation.tree_view_attrs_to_exclude)) expected_controls_from_tree_view = [] if expected_control_from_tree_view: expected_controls_from_tree_view, actual_controls_from_tree_view = ( Representation.extract_objs( [expected_control_from_tree_view], actual_controls_from_tree_view, *Representation.tree_view_attrs_to_exclude)) assert (expected_is_found is (expected_controls_from_mapper[0] in actual_controls_from_mapper) is (expected_map_status in actual_map_status)) == ( (expected_controls_from_tree_view[0] in actual_controls_from_tree_view) if expected_control_from_tree_view else expected_controls_from_tree_view == actual_controls_from_tree_view), ( messages.AssertionMessages.format_err_msg_equal( messages.AssertionMessages.format_err_msg_contains( expected_controls_from_mapper[0], actual_controls_from_mapper), messages.AssertionMessages.format_err_msg_contains( expected_controls_from_tree_view[0], actual_controls_from_tree_view) if expected_control_from_tree_view else messages.AssertionMessages.format_err_msg_equal( expected_controls_from_tree_view, actual_controls_from_tree_view)))
def create_objs(self, count, factory_params=None, **attrs_for_template): """Create new objects via REST API and return list of created objects with filtered attributes. """ list_objs = self.create_list_objs( entity_factory=self.entities_factory_cls, count=count, attrs_to_factory=factory_params, **attrs_for_template) return Representation.filter_objs_attrs( objs=list_objs, attrs_to_include=Representation.all_attrs_names())
def test_destructive_search_unified_mapper_and_check_mapping( self, create_audit_with_control_and_update_control, control_for_mapper, control_for_tree_view, dynamic_objects, dynamic_relationships, expected_map_statuses, expected_is_found, selenium ): """Check searching of shapshotable and snapshoted objects via Unified Mapper modal and check their correct mapping. """ audit_with_one_control = create_audit_with_control_and_update_control source_obj = dynamic_objects expected_control_from_mapper = ( audit_with_one_control[control_for_mapper][0].repr_ui()) expected_control_from_tree_view = ( (expected_control_from_mapper if control_for_mapper == control_for_tree_view else audit_with_one_control[control_for_tree_view][0].repr_ui()) if control_for_tree_view else None) expected_map_status = MappingStatusAttrs( expected_control_from_mapper.title, *expected_map_statuses) controls_ui_service = webui_service.ControlsService(selenium) actual_controls_from_mapper, actual_map_status = ( controls_ui_service.get_list_objs_from_mapper( src_obj=source_obj, dest_objs=[expected_control_from_mapper])) actual_controls_from_tree_view = ( controls_ui_service.get_list_objs_from_tree_view(src_obj=source_obj)) # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_controls_from_mapper, actual_controls_from_mapper = ( Representation.extract_objs( [expected_control_from_mapper], actual_controls_from_mapper, *Representation.tree_view_attrs_to_exclude)) expected_controls_from_tree_view = [] if expected_control_from_tree_view: expected_controls_from_tree_view, actual_controls_from_tree_view = ( Representation.extract_objs( [expected_control_from_tree_view], actual_controls_from_tree_view, *Representation.tree_view_attrs_to_exclude)) assert ( expected_is_found is (expected_controls_from_mapper[0] in actual_controls_from_mapper) is (expected_map_status in actual_map_status)) == ( (expected_controls_from_tree_view[0] in actual_controls_from_tree_view) if expected_control_from_tree_view else expected_controls_from_tree_view == actual_controls_from_tree_view), ( messages.AssertionMessages.format_err_msg_equal( messages.AssertionMessages.format_err_msg_contains( expected_controls_from_mapper[0], actual_controls_from_mapper), messages.AssertionMessages.format_err_msg_contains( expected_controls_from_tree_view[0], actual_controls_from_tree_view) if expected_control_from_tree_view else messages.AssertionMessages.format_err_msg_equal( expected_controls_from_tree_view, actual_controls_from_tree_view)))
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 Representation.filter_objs_attrs( objs=list_objs, attrs_to_include=Representation.all_attrs_names())
def test_destructive_asmts_gcas_filtering( self, new_program_rest, new_audit_rest, new_cas_for_assessments_rest, new_assessments_rest, operator, selenium ): """Test for checking filtering of Assessment by Global Custom Attributes in audit scope. Preconditions: - Program created via REST API. - Audit created via REST API. - Global Custom Attributes for Assessment created via REST API. - Assessments created via REST API. """ custom_attr_values = ( CustomAttributeDefinitionsFactory().generate_ca_values( list_ca_def_objs=new_cas_for_assessments_rest)) checkbox_id = Representation.filter_objs_by_attrs( objs=new_cas_for_assessments_rest, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX).id expected_asmt = new_assessments_rest[0] asmts_rest_service = rest_service.AssessmentsService() asmts_rest_service.update_obj( obj=expected_asmt, custom_attributes=custom_attr_values) asmts_rest_service.update_obj( obj=new_assessments_rest[1], custom_attributes={checkbox_id: not custom_attr_values[checkbox_id]}) self._check_assessments_filtration(expected_asmt, custom_attr_values, operator, new_audit_rest, selenium)
def get_obj(self, obj): """Get and return object according to 'obj.type' and 'obj.id'.""" obj_dict = (BaseRestService.get_items_from_resp(self.client.create_object( type=self.endpoint, object_name=unicode(obj.type), filters=query.Query.expression_get_obj_by_id(obj.id))).get( "values")[0]) return Representation.repr_dict_to_obj(obj_dict)
def get_snapshoted_obj(self, origin_obj, paren_obj): """Get and return snapshoted object according to 'origin_obj' and 'paren_obj'. """ def get_response(): """Get response from query.""" return self.client.create_object( type=self.endpoint, object_name=objects.get_obj_type(objects.SNAPSHOTS), filters=query.Query.expression_get_snapshoted_obj( obj_type=origin_obj.type, obj_id=origin_obj.id, parent_type=paren_obj.type, parent_id=paren_obj.id)) def get_response_values(): """Get values fom responese.""" return json.loads(get_response().text, encoding="utf-8")[0]["Snapshot"]["values"] test_utils.wait_for(get_response_values, constants.ux.MAX_USER_WAIT_SECONDS * 2) snapshoted_obj_dict = (BaseRestService.get_items_from_resp( get_response()).get("values")[0]) return Representation.repr_dict_to_obj(snapshoted_obj_dict)
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 = ( Representation.extract_objs_wo_excluded_attrs( help_utils.convert_to_list(expected_obj), *exclude_attrs)[0]) actual_objs_wo_excluded_attrs = ( Representation.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 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 test_asmts_gcas_filtering( self, new_program_rest, new_audit_rest, new_cas_for_assessments_rest, new_assessments_rest, operator, selenium ): """Test for checking filtering of Assessment by Global Custom Attributes in audit scope. Preconditions: - Program created via REST API. - Audit created via REST API. - Global Custom Attributes for Assessment created via REST API. - Assessments created via REST API. """ custom_attr_values = ( CustomAttributeDefinitionsFactory().generate_ca_values( list_ca_def_objs=new_cas_for_assessments_rest)) checkbox_id = Representation.filter_objs_by_attrs( objs=new_cas_for_assessments_rest, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX).id expected_asmt = new_assessments_rest[0] asmts_rest_service = rest_service.AssessmentsService() asmts_rest_service.update_obj( obj=expected_asmt, custom_attributes=custom_attr_values) asmts_rest_service.update_obj( obj=new_assessments_rest[1], custom_attributes={checkbox_id: not custom_attr_values[checkbox_id]}) self._check_assessments_filtration(expected_asmt, custom_attr_values, operator, new_audit_rest, selenium)
def test_destructive_mapping_control_to_existing_audit( self, new_program_rest, new_audit_rest, new_control_rest, selenium ): """Check if Control can be mapped to existing Audit and mapping between Control and Program of this Audit automatically created. Preconditions: - Audit and program, and different control created via REST API """ # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_control = Representation.extract_objs_wo_excluded_attrs( [new_control_rest.repr_ui()], *Representation.tree_view_attrs_to_exclude)[0] controls_ui_service = webui_service.ControlsService(selenium) controls_ui_service.map_objs_via_tree_view( src_obj=new_audit_rest, dest_objs=[new_control_rest]) actual_controls_count_in_tab_audit = ( controls_ui_service.get_count_objs_from_tab(src_obj=new_audit_rest)) actual_controls_in_audit = ( controls_ui_service.get_list_objs_from_tree_view( src_obj=new_audit_rest)) actual_controls_count_in_tab_program = ( controls_ui_service.get_count_objs_from_tab(src_obj=new_program_rest)) actual_controls_in_program = ( controls_ui_service.get_list_objs_from_tree_view( src_obj=new_program_rest)) assert (len([expected_control]) == actual_controls_count_in_tab_audit == actual_controls_count_in_tab_program) assert ([expected_control] == actual_controls_in_audit == actual_controls_in_program), ( messages.AssertionMessages.format_err_msg_equal( messages.AssertionMessages.format_err_msg_equal( [expected_control], actual_controls_in_audit), messages.AssertionMessages.format_err_msg_equal( [expected_control], actual_controls_in_program)))
def test_destructive_mapping_control_to_existing_audit( self, program, audit, control, selenium): """Check if Control can be mapped to existing Audit and mapping between Control and Program of this Audit automatically created. Preconditions: - Audit and program, and different control created via REST API """ # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_control = Representation.extract_objs_wo_excluded_attrs( [control.repr_ui()], *Representation.tree_view_attrs_to_exclude)[0] controls_ui_service = webui_service.ControlsService(selenium) controls_ui_service.map_objs_via_tree_view(src_obj=audit, dest_objs=[control]) actual_controls_count_in_tab_audit = ( controls_ui_service.get_count_objs_from_tab(src_obj=audit)) actual_controls_in_audit = ( controls_ui_service.get_list_objs_from_tree_view(src_obj=audit)) actual_controls_count_in_tab_program = ( controls_ui_service.get_count_objs_from_tab(src_obj=program)) actual_controls_in_program = ( controls_ui_service.get_list_objs_from_tree_view(src_obj=program)) assert (len([expected_control]) == actual_controls_count_in_tab_audit == actual_controls_count_in_tab_program) assert ([expected_control] == actual_controls_in_audit == actual_controls_in_program), ( messages.AssertionMessages.format_err_msg_equal( messages.AssertionMessages.format_err_msg_equal( [expected_control], actual_controls_in_audit), messages.AssertionMessages.format_err_msg_equal( [expected_control], actual_controls_in_program)))
def test_fill_asmt_lcas(self, control_mapped_to_program, audit, attr_type, selenium): """ 1. Create a program via REST 2. Create a control within a program via REST 3. Create an audit within a program via REST 4. Create an assessment template with each type of local custom attribute within audit via REST 5. Generate an assessment from assessment template and control snapshot via REST 6. Open this assessment in UI 7. Fill local custom attribute 8. Reload page and check that object built from the page looks as expected """ asmt_template = rest_facade.create_asmt_template( audit=audit, assessment_type="Control", cad_type=attr_type) cas = CustomAttributeDefinitionsFactory().generate_custom_attributes([ Representation.repr_dict_to_obj(cad) for cad in asmt_template.custom_attribute_definitions]) exp_asmt = rest_facade.create_asmt_from_template( audit, asmt_template, control_mapped_to_program) asmts_ui_service = webui_service.AssessmentsService(selenium) asmts_ui_service.fill_asmt_lcas(exp_asmt, cas) selenium.refresh() act_asmt = self.info_service().get_obj(obj=exp_asmt) exp_asmt.update_attrs(updated_at=act_asmt.updated_at, status=unicode(object_states.IN_PROGRESS), mapped_objects=[control_mapped_to_program], custom_attributes=cas) _assert_asmt(asmts_ui_service, exp_asmt)
def create_asmt_from_template(audit, asmt_template, obj_to_map): """Create an assessment from template""" snapshots = [ Representation.convert_repr_to_snapshot(objs=obj_to_map, parent_obj=audit) ] return rest_service.AssessmentsFromTemplateService().create_assessments( audit=audit, template=asmt_template, snapshots=snapshots)[0]
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 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 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 get_obj(self, obj): """Get and return object according to 'obj.type' and 'obj.id'.""" @test_utils.wait_for def get_obj_response_values(): """Get values fom response.""" obj_type = obj.type if hasattr(obj, "type") else obj.obj_type() obj_id = obj.id if hasattr(obj, "id") else obj.obj_id filters = query.Query.expression_get_obj_by_id(obj_id) return self.get_obj_dict(obj_type, filters=filters) return Representation.repr_dict_to_obj(get_obj_response_values[0])
def create_asmt_from_template_rest(audit, control, asmt_template): """Create new Assessment based on Assessment Template via REST API. Return: lib.entities.entity.AssessmentEntity """ control_snapshots = [ Representation.convert_repr_to_snapshot(objs=control, parent_obj=audit) ] assessments_service = rest_service.AssessmentsFromTemplateService() assessments = assessments_service.create_assessments( audit=audit, template=asmt_template, control_snapshots=control_snapshots) return assessments[0]
def get_snapshoted_obj(self, origin_obj, paren_obj): """Get and return snapshoted object according to 'origin_obj' and 'paren_obj'. """ snapshoted_obj_dict = ( BaseRestService.get_items_from_resp(self.client.create_object( type=self.endpoint, object_name=objects.get_obj_type(objects.SNAPSHOTS), filters=query.Query.expression_get_snapshoted_obj( obj_type=origin_obj.type, obj_id=origin_obj.id, parent_type=paren_obj.type, parent_id=paren_obj.id))).get("values")[0]) return Representation.repr_dict_to_obj(snapshoted_obj_dict)
def get_obj(self, obj): """Get and return object according to 'obj.type' and 'obj.id'.""" @test_utils.wait_for def get_obj_response_values(): """Get values fom response.""" obj_type = obj.type if hasattr(obj, "type") else obj.obj_type() obj_id = obj.id if hasattr(obj, "id") else obj.obj_id filters = query.Query.expression_get_obj_by_id(obj_id) obj_resp = self.get_obj_dict(obj_type, filters=filters) # Update object href that used in entities. obj_resp[0]["href"] = obj_resp[0]["selfLink"] return obj_resp return Representation.repr_dict_to_obj(get_obj_response_values[0])
def get_fixture_from_dict_fixtures(fixture): """Get value of fixture by key (fixture name) from dictionary of executed fixtures.""" global dict_executed_fixtures # extend executed fixtures using exist fixture in snapshot representation if fixture.endswith("_snapshot"): origin_obj = get_fixture_from_dict_fixtures( fixture.replace("_snapshot", "")) parent_obj = get_fixture_from_dict_fixtures("new_audit_rest")[0] dict_executed_fixtures.update( {fixture: Representation.convert_repr_to_snapshot( objs=origin_obj, parent_obj=parent_obj)}) return {k: v for k, v in dict_executed_fixtures.iteritems() if k == fixture}[fixture]
def get_comment_obj(self, paren_obj, comment_description): """Get and return comment object according to 'paren_obj' type) and comment_description 'paren_obj'. As default 'is_sort_by_created_at' and if even comments have the same descriptions query return selection w/ latest created datetime. """ comment_obj_dict = ( BaseRestService.get_items_from_resp(self.client.create_object( type=self.endpoint, object_name=objects.get_obj_type(objects.COMMENTS), filters=query.Query.expression_get_comment_by_desc( parent_type=paren_obj.type, parent_id=paren_obj.id, comment_desc=comment_description), order_by=[{"name": "created_at", "desc": True}])).get("values")[0]) return Representation.repr_dict_to_obj(comment_obj_dict)
def create_asmt_from_template_rest( audit, control, asmt_template ): """Create new Assessment based on Assessment Template via REST API. Return: lib.entities.entity.AssessmentEntity """ control_snapshots = [Representation.convert_repr_to_snapshot( objs=control, parent_obj=audit)] assessments_service = rest_service.AssessmentsFromTemplateService() assessments = assessments_service.create_assessments( audit=audit, template=asmt_template, control_snapshots=control_snapshots ) return assessments[0]
def get_snapshoted_obj(self, origin_obj, paren_obj): """Get and return snapshoted object according to 'origin_obj' and 'paren_obj'. """ @test_utils.wait_for def get_snapshoted_obj_response_values(): """Get values fom response.""" # pylint: disable=invalid-name filters = query.Query.expression_get_snapshoted_obj( obj_type=origin_obj.type, obj_id=origin_obj.id, parent_type=paren_obj.type, parent_id=paren_obj.id) return self.get_obj_dict( objects.get_obj_type(objects.SNAPSHOTS), filters=filters) return Representation.repr_dict_to_obj( get_snapshoted_obj_response_values[0])
def _set_values_for_assessment(assessment, cads, only_checkbox, checkbox_value): """Set CA values for assessment""" checkbox_cad = Representation.filter_objs_by_attrs( objs=cads, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX) if only_checkbox: cavs = [CustomAttributeDefinitionsFactory.generate_ca_value( checkbox_cad, checkbox_value)] else: cavs = CustomAttributeDefinitionsFactory.generate_ca_values(cads) for cav in cavs: if cav.custom_attribute_id == checkbox_cad.id: cav.attribute_value = checkbox_value rest_service.AssessmentsService().update_obj( obj=assessment, custom_attribute_values=[cav.__dict__ for cav in cavs]) return cavs
def get_comment_obj(self, paren_obj, comment_description): """Get and return comment object according to 'paren_obj' type) and comment_description 'paren_obj'. As default 'is_sort_by_created_at' and if even comments have the same descriptions query return selection w/ latest created datetime. """ @test_utils.wait_for def get_obj_comment_response_values(): """Get values fom response.""" # pylint: disable=invalid-name filters = query.Query.expression_get_comment_by_desc( parent_type=paren_obj.type, parent_id=paren_obj.id, comment_desc=comment_description) order_by = [{"name": "created_at", "desc": True}] return self.get_obj_dict(objects.get_obj_type(objects.COMMENTS), filters=filters, order_by=order_by) return Representation.repr_dict_to_obj(get_obj_comment_response_values[0])
def set_values_for_assessment(assessment, only_checkbox, checkbox_value): """Set LCA values for assessment""" custom_attr_definitions = [ CustomAttributeDefinitionsFactory().create(**definition) for definition in assessment.cads_from_template()] checkbox_id = Representation.filter_objs_by_attrs( objs=custom_attr_definitions, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX).id if only_checkbox: attr_values = {} else: attr_values = CustomAttributeDefinitionsFactory().generate_ca_values( list_ca_def_objs=custom_attr_definitions) attr_values[checkbox_id] = checkbox_value rest_service.AssessmentsService().update_obj( obj=assessment, custom_attributes=attr_values) return attr_values
def get_snapshoted_obj(self, origin_obj, paren_obj): """Get and return snapshoted object according to 'origin_obj' and 'paren_obj'. """ @test_utils.wait_for def get_snapshoted_obj_response_values(): """Get values fom response.""" # pylint: disable=invalid-name filters = query.Query.expression_get_snapshoted_obj( obj_type=origin_obj.type, obj_id=origin_obj.id, parent_type=paren_obj.type, parent_id=paren_obj.id) return self.get_obj_dict(objects.get_obj_type(objects.SNAPSHOTS), filters=filters) return Representation.repr_dict_to_obj( get_snapshoted_obj_response_values[0])
def _set_values_for_assessment(assessment, cads, only_checkbox, checkbox_value): """Set CA values for assessment""" checkbox_cad = Representation.filter_objs_by_attrs( objs=cads, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX) if only_checkbox: cavs = [CustomAttributeDefinitionsFactory.generate_cav( checkbox_cad, checkbox_value)] else: cavs = CustomAttributeDefinitionsFactory.generate_cavs(cads) for cav in cavs: if cav.custom_attribute_id == checkbox_cad.id: cav.attribute_value = checkbox_value rest_service.AssessmentsService().update_obj( obj=assessment, custom_attribute_values=[cav.__dict__ for cav in cavs]) return cavs
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 get_comment_obj(self, paren_obj, comment_description): """Get and return comment object according to 'paren_obj' type) and comment_description 'paren_obj'. As default 'is_sort_by_created_at' and if even comments have the same descriptions query return selection w/ latest created datetime. """ @test_utils.wait_for def get_obj_comment_response_values(): """Get values fom response.""" # pylint: disable=invalid-name filters = query.Query.expression_get_comment_by_desc( parent_type=paren_obj.type, parent_id=paren_obj.id, comment_desc=comment_description) order_by = [{"name": "created_at", "desc": True}] return self.get_obj_dict(objects.get_obj_type(objects.COMMENTS), filters=filters, order_by=order_by) return Representation.repr_dict_to_obj( get_obj_comment_response_values[0])
def test_filter_of_snapshotable_control( self, create_audit_with_control_and_update_control, version_of_ctrl, is_found, selenium ): """Check via UI that filtering work for snapshoted version of Control only, filtering by actual values returns no items in scope of Audit page. """ audit_with_one_control = create_audit_with_control_and_update_control audit = audit_with_one_control["new_audit_rest"][0] expected_control = audit_with_one_control[version_of_ctrl][0].repr_ui() filter_exp = FilterUtils.get_filter_exp_by_title(expected_control.title) actual_controls = (webui_service.ControlsService(selenium). filter_and_get_list_objs_from_tree_view( src_obj=audit, filter_exp=filter_exp)) # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_controls, actual_controls = Representation.extract_objs( [expected_control], actual_controls, *Representation.tree_view_attrs_to_exclude) expected_control = expected_controls[0] assert is_found is (expected_control in actual_controls), ( messages.AssertionMessages.format_err_msg_contains( expected_control, actual_controls))
def test_filter_of_snapshotable_control( self, create_audit_with_control_and_update_control, version_of_ctrl, is_found, selenium): """Check via UI that filtering work for snapshoted version of Control only, filtering by actual values returns no items in scope of Audit page. """ audit_with_one_control = create_audit_with_control_and_update_control audit = audit_with_one_control["new_audit_rest"][0] expected_control = audit_with_one_control[version_of_ctrl][0].repr_ui() filter_exp = FilterUtils.get_filter_exp_by_title( expected_control.title) actual_controls = (webui_service.ControlsService( selenium).filter_and_get_list_objs_from_tree_view( src_obj=audit, filter_exp=filter_exp)) # 'actual_controls': created_at, updated_at, custom_attributes (None) expected_controls, actual_controls = Representation.extract_objs( [expected_control], actual_controls, *Representation.tree_view_attrs_to_exclude) expected_control = expected_controls[0] assert is_found is (expected_control in actual_controls), ( messages.AssertionMessages.format_err_msg_contains( expected_control, actual_controls))
def convert_cntrl_to_snapshot(audit, obj): """Convert control to snapshot.""" return Representation.convert_repr_to_snapshot( objs=obj, parent_obj=audit)
def convert_obj_to_snapshot(audit, obj): """Convert object to snapshot.""" return Representation.convert_repr_to_snapshot( obj=obj, parent_obj=audit)
def convert_obj_to_snapshot(audit, obj): """Convert object to snapshot.""" return Representation.convert_repr_to_snapshot(obj=obj, parent_obj=audit)
def test_asmts_gcas_filtering(self, new_program_rest, new_audit_rest, new_cas_for_assessments_rest, new_assessments_rest, operator, selenium): """Test for checking filtering of Assessment by Custom Attributes in audit scope. Preconditions: - Program created via REST API. - Audit created via REST API. - Assessments created via REST API. - Global Custom Attributes for Assessment created via REST API. - Set revers value of GCA with Checkbox type for second Assessment. """ # pylint: disable=too-many-locals custom_attr_values = ( CustomAttributeDefinitionsFactory().generate_ca_values( list_ca_def_objs=new_cas_for_assessments_rest)) checkbox_id = Representation.filter_objs_by_attrs( objs=new_cas_for_assessments_rest, attribute_type=element.AdminWidgetCustomAttributes.CHECKBOX).id expected_asmt = new_assessments_rest[0] asmts_rest_service = rest_service.AssessmentsService() asmts_rest_service.update_obj(obj=expected_asmt, custom_attributes=custom_attr_values) asmts_rest_service.update_obj(obj=new_assessments_rest[1], custom_attributes={ checkbox_id: not custom_attr_values[checkbox_id] }) filter_exprs = FilterUtils().get_filter_exprs_by_cas( expected_asmt.custom_attribute_definitions, custom_attr_values, operator) # 'expected_asmt': updated_at (outdated) # 'actual_asmts': created_at, updated_at, custom_attributes, audit # assessment_type, modified_by (None) expected_asmt = Representation.extract_objs_wo_excluded_attrs( [expected_asmt.repr_ui()], *(Representation.tree_view_attrs_to_exclude + ("audit", "assessment_type", "modified_by")))[0] expected_results = [{ "filter": filter_expr, "objs": [expected_asmt] } for filter_expr in filter_exprs] actual_results = [{ "filter": filter_expr, "objs": webui_service.AssessmentsService( selenium).filter_and_get_list_objs_from_tree_view( new_audit_rest, filter_expr) } for filter_expr in filter_exprs] assert expected_results == actual_results, ( 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])))