def test_invalid_child_types(self): """Test child_type property with invalid snapshots list.""" with factories.single_commit(): snapshots = self._create_snapshots( factories.AuditFactory(), [factories.ControlFactory(), factories.PolicyFactory()], ) converter = mock.MagicMock() ids = [s.id for s in snapshots] block = SnapshotBlockConverter(converter, ids) with self.assertRaises(AssertionError): block.child_type = block.child_type
def test_cycle_task_objects(self): """Test cycle task and various objects.""" with freeze_time("2017-03-07"): workflow = wf_factories.WorkflowFactory() task_group = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=task_group) wf_factories.TaskGroupTaskFactory(task_group=task_group) policy = factories.PolicyFactory() policy_slug = policy.slug factories.RelationshipFactory(source=task_group, destination=policy) self.wf_generator.generate_cycle(workflow) self.wf_generator.activate_workflow(workflow) data = [ { "object_name": "CycleTaskGroupObjectTask", "filters": { "expression": { "op": { "name": "relevant" }, "object_name": "Policy", "slugs": [policy_slug], }, }, "fields": "all", }, { "object_name": "Policy", "filters": { "expression": { "op": { "name": "relevant" }, "object_name": "__previous__", "ids": ["0"], }, }, "fields": ["slug", "title"], }, ] response = self.export_csv(data) self.assert200(response) response_data = response.data self.assertEqual(2, response_data.count("CYCLETASK-")) self.assertEqual(3, response_data.count(policy_slug))
def test_wf_indirect_relevant_filters(self): # pylint: disable=invalid-name """Test related filter for indirect relationships on wf objects.""" with freeze_time("2017-03-07"): workflow = wf_factories.WorkflowFactory(title="workflow-1") task_group1 = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=task_group1) wf_factories.TaskGroupTaskFactory(task_group=task_group1) task_group2 = wf_factories.TaskGroupFactory(workflow=workflow) wf_factories.TaskGroupTaskFactory(task_group=task_group2) policy = factories.PolicyFactory() policy_slug = policy.slug factories.RelationshipFactory(source=task_group1, destination=policy) self.wf_generator.generate_cycle(workflow) self.wf_generator.activate_workflow(workflow) def block(obj): return { "object_name": obj, "fields": ["slug"], "filters": { "expression": { "object_name": "Policy", "op": { "name": "relevant" }, "slugs": [policy_slug], }, }, } data = [ block("Workflow"), block("Cycle"), block("CycleTaskGroup"), block("CycleTaskGroupObjectTask"), ] response = self.export_csv(data) self.assert200(response) response_data = response.data wf1 = Workflow.query.filter_by(title="workflow-1").first() cycle = wf1.cycles[0] cycle_tasks = [] for cycle_task in cycle.cycle_task_group_object_tasks: for related_object in cycle_task.related_objects(): if related_object.slug == policy_slug: cycle_tasks.append(cycle_task) break cycle_task_groups = list( {cycle_task.cycle_task_group for cycle_task in cycle_tasks}) self.assertEqual(1, response_data.count("WORKFLOW-")) self.assertRegexpMatches(response_data, ",{}[,\r\n]".format(wf1.slug)) self.assertEqual(1, response_data.count("CYCLE-")) self.assertRegexpMatches(response_data, ",{}[,\r\n]".format(cycle.slug)) self.assertEqual(1, response_data.count("CYCLEGROUP-")) self.assertEqual(1, len(cycle_task_groups)) self.assertRegexpMatches( response_data, ",{}[,\r\n]".format(cycle_task_groups[0].slug)) self.assertEqual(2, response_data.count("CYCLETASK-")) self.assertEqual(2, len(cycle_tasks)) for cycle_task in cycle_tasks: self.assertRegexpMatches(response_data, ",{}[,\r\n]".format(cycle_task.slug)) destinations = [ ("Workflow", wf1.slug, 1), ("Cycle", cycle.slug, 1), ("CycleTaskGroupObjectTask", cycle_tasks[0].slug, 1), ("CycleTaskGroupObjectTask", cycle_tasks[1].slug, 1), ] for object_name, slug, count in destinations: data = [{ "object_name": "Policy", "fields": ["slug"], "filters": { "expression": { "object_name": object_name, "op": { "name": "relevant" }, "slugs": [slug], }, }, }] response = self.export_csv(data) self.assert200(response) response_data = response.data self.assertEqual(count, response_data.count(",POLICY-"), "Count for " + object_name) self.assertIn("," + policy_slug, response_data)
def test_requirement_policy_relevant_query(self): """Test requirement policy relevant query""" with factories.single_commit(): policies = [factories.PolicyFactory(title="pol-{}".format(i)) for i in range(1, 3)] standards = [factories.StandardFactory(title="stand-{}".format(i)) for i in range(1, 3)] regulations = [factories.RegulationFactory(title="reg-{}".format(i)) for i in range(1, 3)] requirements = [factories.RequirementFactory(title="req-{}".format(i)) for i in range(1, 4)] policy_slugs = [policy.slug for policy in policies] standard_slugs = [standard.slug for standard in standards] regulation_slugs = [regulation.slug for regulation in regulations] requirement_slugs = [requirement.slug for requirement in requirements] policy_map_data = [ get_object_data("Policy", policy_slugs[0], requirement=requirement_slugs[0]), ] self.import_data(*policy_map_data) standard_map_data = [ get_object_data("Standard", standard_slugs[0], requirement=requirement_slugs[1]), ] self.import_data(*standard_map_data) regulation_map_data = [ get_object_data("Regulation", regulation_slugs[0], requirement=requirement_slugs[2]), ] self.import_data(*regulation_map_data) data = [ get_related_objects("Policy", "Requirement", policy_slugs[:1]), get_related_objects("Requirement", "Policy", requirement_slugs[:1]), get_related_objects("Standard", "Requirement", standard_slugs[:1]), get_related_objects("Requirement", "Standard", requirement_slugs[1:2]), get_related_objects("Regulation", "Requirement", regulation_slugs[:1]), get_related_objects("Requirement", "Regulation", requirement_slugs[2:3]) ] response = self.export_csv(data) titles = [",req-{},".format(i) for i in range(1, 4)] titles.extend([",pol-1,", ",pol-2,", ",stand-1,", ",stand-2,", ",reg-1,", ",reg-2,"]) expected = set([ ",req-1,", ",req-2,", ",req-3,", ",pol-1,", ",stand-1,", ",reg-1,", ]) for title in titles: if title in expected: self.assertIn(title, response.data, "'{}' not found".format(title)) else: self.assertNotIn(title, response.data, "'{}' was found".format(title))