Пример #1
0
  def test_simple_multi_export(self):
    match = 1
    programs = [factories.ProgramFactory().title for i in range(3)]
    regulations = [factories.RegulationFactory().title for i in range(3)]

    data = [{
        "object_name": "Program",  # prog-1
        "filters": {
            "expression": {
                "left": "title",
                "op": {"name": "="},
                "right": programs[match]
            },
        },
        "fields": "all",
    }, {
        "object_name": "Regulation",  # regulation-9000
        "filters": {
            "expression": {
                "left": "title",
                "op": {"name": "="},
                "right": regulations[match]
            },
        },
        "fields": "all",
    }]
    response = self.export_csv(data)
    for i in range(3):
      if i == match:
        self.assertIn(programs[i], response.data)
        self.assertIn(regulations[i], response.data)
      else:
        self.assertNotIn(programs[i], response.data)
        self.assertNotIn(regulations[i], response.data)
Пример #2
0
  def test_get_identifier_mappings(self):
    """Test _get_identifier_mappings function."""
    count = 3

    with factories.single_commit():
      regulation = factories.RegulationFactory()
      requirements = [factories.RequirementFactory() for _ in range(count)]
      controls = [factories.ControlFactory() for _ in range(count)]
      expected_id_map = {
          "Requirement": {o.id: o.slug for o in requirements},
          "Control": {o.id: o.slug for o in controls},
      }

      relationships = []
      for i in range(count):
        relationships.append(factories.RelationshipFactory(
            source=regulation if i % 2 == 0 else requirements[i],
            destination=regulation if i % 2 == 1 else requirements[i],
        ))
        relationships.append(factories.RelationshipFactory(
            source=regulation if i % 2 == 0 else controls[i],
            destination=regulation if i % 2 == 1 else controls[i],
        ))

    block = base_block.ExportBlockConverter(
        mock.MagicMock(),
        object_class=models.Regulation,
        fields="all",
        object_ids=[regulation.id],
        class_name=models.Regulation.__name__,
    )
    id_map = block._get_identifier_mappings(relationships)
    self.assertEqual(expected_id_map, id_map)
Пример #3
0
    def test_populated_automapping(self):
        """Test automapping content in revision"""
        with factories.single_commit():
            program_a = factories.ProgramFactory()
            program_b = factories.ProgramFactory()
            factories.RelationshipFactory(source=program_b,
                                          destination=program_a)
            regulation_a = factories.RegulationFactory()

        program_a_id = program_a.id
        program_b_id = program_b.id
        regulation_a_id = regulation_a.id
        self.gen.generate_relationship(regulation_a, program_a)
        rel_1 = all_models.Relationship.query.filter_by(
            source_type="Regulation",
            source_id=regulation_a_id,
            destination_type="Program",
            destination_id=program_b_id).first()
        rel_2 = all_models.Relationship.query.filter_by(
            source_type="Program",
            source_id=program_b_id,
            destination_type="Regulation",
            destination_id=regulation_a_id).first()
        relationship = rel_1 or rel_2
        revision = all_models.Revision.query.filter_by(
            resource_type="Relationship", resource_id=relationship.id).first()
        automapping = revision.content["automapping"]
        nodes = {
            automapping["source_type"]: automapping["source_id"],
            automapping["destination_type"]: automapping["destination_id"]
        }
        self.assertTrue(program_a_id == nodes["Program"])
        self.assertTrue(regulation_a_id == nodes["Regulation"])
Пример #4
0
    def test_get_identifier_mappings(self):
        """Test _get_identifier_mappings function."""
        count = 3
        regulation = factories.RegulationFactory()
        markets = [factories.MarketFactory() for _ in range(count)]
        controls = [factories.ControlFactory() for _ in range(count)]
        expected_id_map = {
            "Market": {o.id: o.slug
                       for o in markets},
            "Control": {o.id: o.slug
                        for o in controls},
        }

        relationships = []
        for i in range(count):
            relationships.append(
                factories.RelationshipFactory(
                    source=regulation if i % 2 == 0 else markets[i],
                    destination=regulation if i % 2 == 1 else markets[i],
                ))
            relationships.append(
                factories.RelationshipFactory(
                    source=regulation if i % 2 == 0 else controls[i],
                    destination=regulation if i % 2 == 1 else controls[i],
                ))

        block = base_block.BlockConverter(mock.MagicMock())
        block.object_class = models.Regulation
        block.object_ids = [regulation.id]
        id_map = block._get_identifier_mappings(relationships)
        self.assertEqual(expected_id_map, id_map)
Пример #5
0
  def test_simple_multi_export(self):
    """Test basic export of multiple objects"""
    match = 1
    with factories.single_commit():
      programs = [factories.ProgramFactory().title for _ in range(3)]
      regulations = [factories.RegulationFactory().title for _ in range(3)]

    data = [{
        "object_name": "Program",
        "filters": {
            "expression": define_op_expr("title", "=", programs[match]),
        },
        "fields": "all",
    }, {
        "object_name": "Regulation",
        "filters": {
            "expression": define_op_expr("title", "=", regulations[match]),
        },
        "fields": "all",
    }]
    response = self.export_csv(data)
    for i in range(3):
      if i == match:
        self.assertIn(programs[i], response.data)
        self.assertIn(regulations[i], response.data)
      else:
        self.assertNotIn(programs[i], response.data)
        self.assertNotIn(regulations[i], response.data)
Пример #6
0
    def test_create_mapping_cache(self, count):
        """Test creation of mapping cache for export."""
        regulations = [factories.RegulationFactory() for _ in range(count)]
        markets = [factories.MarketFactory() for _ in range(count)]
        controls = [factories.ControlFactory() for _ in range(count)]

        expected_cache = defaultdict(lambda: defaultdict(list))
        for i in range(count):
            for j in range(i):
                factories.RelationshipFactory(
                    source=regulations[j] if i % 2 == 0 else markets[i],
                    destination=regulations[j] if i % 2 == 1 else markets[i],
                )
                factories.RelationshipFactory(
                    source=regulations[j] if i % 2 == 0 else controls[i],
                    destination=regulations[j] if i % 2 == 1 else controls[i],
                )
                expected_cache[regulations[j].id]["Control"].append(
                    controls[i].slug)
                expected_cache[regulations[j].id]["Market"].append(
                    markets[i].slug)
        block = base_block.BlockConverter(mock.MagicMock())
        block.object_class = models.Regulation
        block.object_ids = [r.id for r in regulations]

        with QueryCounter() as counter:
            cache = block._create_mapping_cache()
            self.assertEqual(
                self.dd_to_dict(cache),
                self.dd_to_dict(expected_cache),
            )
            self.assertLess(counter.get, self.QUERY_LIMIT)
Пример #7
0
    def test_exportable_items(self):
        """Test multi export with exportable items."""
        with factories.single_commit():
            program = factories.ProgramFactory()
            regulation = factories.RegulationFactory()

        data = [{
            "object_name": "Program",
            "filters": {
                "expression": {
                    "left": "title",
                    "op": {
                        "name": "="
                    },
                    "right": program.title
                },
            },
            "fields": "all",
        }, {
            "object_name": "Regulation",
            "filters": {
                "expression": {
                    "left": "title",
                    "op": {
                        "name": "="
                    },
                    "right": regulation.title
                },
            },
            "fields": "all",
        }]
        response = self.export_csv(data, exportable_objects=[1])
        response_data = response.data
        self.assertIn(regulation.title, response_data)
        self.assertNotIn(program.title, response_data)
Пример #8
0
    def test_mapped_regulations_acl(self):
        """Test creation of acl roles for Regulations and Objective snapshots."""
        with factories.single_commit():
            person = factories.PersonFactory()
            person_email = person.email
            audit = factories.AuditFactory()
            assessment = factories.AssessmentFactory(audit=audit)
            for ac_role_id in self.assignee_roles.values():
                assessment.add_person_with_role_id(person, ac_role_id)
            factories.RelationshipFactory(source=audit, destination=assessment)
            control = factories.ControlFactory()
            objective = factories.ObjectiveFactory()
            regulation = factories.RegulationFactory()
            snapshots = self._create_snapshots(
                audit, [control, objective, regulation])
            factories.RelationshipFactory(source=snapshots[0],
                                          destination=snapshots[1])
            factories.RelationshipFactory(source=snapshots[2],
                                          destination=snapshots[0])
            factories.RelationshipFactory(source=assessment,
                                          destination=snapshots[0])

        for role in ["Assignees", "Creators", "Verifiers"]:
            for snapshot in snapshots:
                # Mapped Assignee roles should be created for all snapshots, not only
                # for control that related to assessment
                self.assert_propagated_role(role, person_email, snapshot)
Пример #9
0
    def test_exportable_items_incorrect(self):
        """Test export with exportable items and incorrect index"""
        with factories.single_commit():
            program = factories.ProgramFactory()
            regulation = factories.RegulationFactory()

        data = [{
            "object_name": "Program",
            "filters": {
                "expression": {
                    "left": "title",
                    "op": {
                        "name": "="
                    },
                    "right": program.title
                },
            },
            "fields": "all",
        }, {
            "object_name": "Regulation",
            "filters": {
                "expression": {
                    "left": "title",
                    "op": {
                        "name": "="
                    },
                    "right": regulation.title
                },
            },
            "fields": "all",
        }]
        response = self.export_csv(data, exportable_objects=[3])
        response_data = response.data
        self.assertEquals(response_data, "")
Пример #10
0
    def test_metrics_import(self):
        """Test metrics import"""
        regulation = factories.RegulationFactory()
        standard = factories.StandardFactory()
        metric_data = [
            collections.OrderedDict([
                ("object_type", "Metric"),
                ("Code*", ""),
                ("Title*", "Metric-1"),
                ("Admin*", "*****@*****.**"),
                ("Assignee", "*****@*****.**"),
                ("Verifier", "*****@*****.**"),
                ("map:regulation", ""),
                ("map:standard", ""),
            ]),
            collections.OrderedDict([
                ("object_type", "Metric"),
                ("Code*", ""),
                ("Title*", "Metric-2"),
                ("Admin*", "*****@*****.**"),
                ("Assignee", "*****@*****.**"),
                ("Verifier", "*****@*****.**"),
                ("map:regulation", regulation.slug),
                ("map:standard", ""),
            ]),
            collections.OrderedDict([
                ("object_type", "Metric"),
                ("Code*", ""),
                ("Title*", "Metric-3"),
                ("Admin*", "*****@*****.**"),
                ("Assignee", "*****@*****.**"),
                ("Verifier", "*****@*****.**"),
                ("map:regulation", ""),
                ("map:standard", standard.slug),
            ]),
        ]

        response = self.import_data(*metric_data)

        metric_response = response[0]
        self.assertEqual(metric_response["created"], 3)
        self._check_csv_response(
            response, {
                "Metric": {
                    "row_warnings": {
                        errors.MAP_UNMAP_SCOPE_ERROR.format(
                            line=4,
                            object_type="Regulation",
                            action="map",
                        ),
                        errors.MAP_UNMAP_SCOPE_ERROR.format(
                            line=5,
                            object_type="Standard",
                            action="map",
                        ),
                    },
                }
            })
Пример #11
0
  def test_snapshot_counts_query(self):
    """Test snapshot_counts endpoint"""

    with factories.single_commit():
      audit = factories.AuditFactory()
      issue_1 = factories.IssueFactory(audit=audit)
      control = factories.ControlFactory()
      regulation = factories.RegulationFactory()
      factories.RelationshipFactory(
          source=issue_1,
          destination=control
      )
      issue_2 = factories.IssueFactory(audit=audit)

    with factories.single_commit():
      revision = all_models.Revision.query.filter(
          all_models.Revision.resource_type == "Issue",
          all_models.Revision.resource_id == issue_1.id
      ).first()
      revision_2 = all_models.Revision.query.filter(
          all_models.Revision.resource_type == "Issue",
          all_models.Revision.resource_id == issue_2.id
      ).first()
      snapshot_1 = factories.SnapshotFactory(
          parent=issue_1.audit,
          child_type=control.type,
          child_id=control.id,
          revision=revision
      )
      factories.RelationshipFactory(
          source=issue_1,
          destination=snapshot_1,
      )
      snapshot_2 = factories.SnapshotFactory(
          parent=issue_2.audit,
          child_type=regulation.type,
          child_id=regulation.id,
          revision=revision_2
      )
      factories.RelationshipFactory(
          source=issue_2,
          destination=snapshot_2,
      )

    issues = [issue_1, issue_2]
    expected_snapshot_counts = {
        issue_1.id: {"Control": 1},
        issue_2.id: {"Regulation": 1},
    }

    for issue in issues:
      response = self.api.client.get(
          "/api/issues/{}/snapshot_counts".format(issue.id),
      )
      snapshot_counts = json.loads(response.data)
      self.assertEqual(snapshot_counts,
                       expected_snapshot_counts[issue.id])
Пример #12
0
    def test_snapshot_relations_remove(self):
        """Test if snapshots will be unmapped if original objects are unmapped"""
        total_reg_count = 5
        unmapped_reg_count = 2
        with factories.single_commit():
            with mock.patch('ggrc.models.relationship.is_external_app_user',
                            return_value=True):
                program = factories.ProgramFactory()
                control = factories.ControlFactory()
                factories.RelationshipFactory(source=control,
                                              destination=program,
                                              is_external=True)
                for _ in xrange(total_reg_count):
                    regulation = factories.RegulationFactory()
                    factories.RelationshipFactory(source=control,
                                                  destination=regulation,
                                                  is_external=True)
                    factories.RelationshipFactory(source=regulation,
                                                  destination=program,
                                                  is_external=True)

        audit = self.create_audit(program)

        rels = models.Relationship.query.filter(
            models.Relationship.source_type == "Control",
            models.Relationship.destination_type == "Regulation",
        ).all()
        removed_reg_rels = []
        keep_reg_rels = []
        with mock.patch('ggrc.models.relationship.is_external_app_user',
                        return_value=True):
            for num, rel in enumerate(rels):
                if num < unmapped_reg_count:
                    # We know that source is the same Control
                    removed_reg_rels.append(
                        (rel.destination_type, rel.destination_id))
                    # Unmap Regulation object from Control
                    db.session.delete(rel)
                else:
                    keep_reg_rels.append(
                        (rel.destination_type, rel.destination_id))
            db.session.commit()

        snap_mappings = self.collect_snapshot_mappings(removed_reg_rels)
        self.assertEqual(snap_mappings.count(), unmapped_reg_count)

        self.api.modify_object(audit, {"snapshots": {"operation": "upsert"}})
        # After updating to the latest version part of Snapshots of Regulations
        # should be unmapped from Snapshot of Control
        snap_mappings = self.collect_snapshot_mappings(removed_reg_rels)
        self.assertEqual(snap_mappings.count(), 0)
        # Check that other Snapshots still have their mappings
        snap_mappings = self.collect_snapshot_mappings(keep_reg_rels)
        self.assertEqual(snap_mappings.count(),
                         total_reg_count - unmapped_reg_count)
Пример #13
0
    def test_snapshot_counts_query(self):
        """Test snapshot_counts endpoint"""

        with factories.single_commit():
            assessment_1 = factories.AssessmentFactory()
            control = factories.ControlFactory()
            regulation = factories.RegulationFactory()
            factories.RelationshipFactory(source=assessment_1,
                                          destination=control)
            assessment_2 = factories.AssessmentFactory()

        with factories.single_commit():
            revision = all_models.Revision.query.filter(
                all_models.Revision.resource_type == "Assessment",
                all_models.Revision.resource_id == assessment_1.id).first()
            revision_2 = all_models.Revision.query.filter(
                all_models.Revision.resource_type == "Assessment",
                all_models.Revision.resource_id == assessment_2.id).first()
            snapshot_1 = factories.SnapshotFactory(parent=assessment_1.audit,
                                                   child_type=control.type,
                                                   child_id=control.id,
                                                   revision=revision)
            factories.RelationshipFactory(
                source=assessment_1,
                destination=snapshot_1,
            )
            snapshot_2 = factories.SnapshotFactory(parent=assessment_2.audit,
                                                   child_type=regulation.type,
                                                   child_id=regulation.id,
                                                   revision=revision_2)
            factories.RelationshipFactory(
                source=assessment_2,
                destination=snapshot_2,
            )

        assessments = [assessment_1, assessment_2]
        expected_snapshot_counts = {
            assessment_1.id: {
                "Control": 1
            },
            assessment_2.id: {
                "Regulation": 1
            },
        }

        for assessment in assessments:
            response = self.api.client.get(
                "/api/assessments/{}/snapshot_counts".format(assessment.id), )
            snapshot_counts = json.loads(response.data)
            self.assertEqual(snapshot_counts,
                             expected_snapshot_counts[assessment.id])
Пример #14
0
    def test_create_mapping_cache(self, count):
        """Test creation of mapping cache for export."""

        with factories.single_commit():
            regulations = [factories.RegulationFactory() for _ in range(count)]
            requirements = [
                factories.RequirementFactory() for _ in range(count)
            ]
            controls = [factories.ControlFactory() for _ in range(count)]

            expected_cache = defaultdict(lambda: defaultdict(list))
            for i in range(count):
                for j in range(i):
                    factories.RelationshipFactory(
                        source=regulations[j] if i %
                        2 == 0 else requirements[i],
                        destination=regulations[j] if i %
                        2 == 1 else requirements[i],
                    )
                    with mock.patch(
                            'ggrc.models.relationship.is_external_app_user',
                            return_value=True):
                        factories.RelationshipFactory(
                            source=regulations[j] if i %
                            2 == 0 else controls[i],
                            destination=regulations[j] if i %
                            2 == 1 else controls[i],
                            is_external=True,
                        )
                    expected_cache[regulations[j].id]["Control"].append(
                        controls[i].slug)
                    expected_cache[regulations[j].id]["Requirement"].append(
                        requirements[i].slug)

        block = base_block.ExportBlockConverter(
            mock.MagicMock(),
            object_class=models.Regulation,
            fields="all",
            object_ids=[r.id for r in regulations],
            class_name=models.Regulation.__name__,
        )

        with QueryCounter() as counter:
            cache = block._create_mapping_cache()
            self.assertEqual(
                self.dd_to_dict(cache),
                self.dd_to_dict(expected_cache),
            )
            self.assertLess(counter.get, self.QUERY_LIMIT)
Пример #15
0
  def setUp(self):
    super(TestWithSimilarityScore, self).setUp()
    self.client.get("/login")

    self.assessment = factories.AssessmentFactory()
    self.audit = factories.AuditFactory()
    self.control = factories.ControlFactory()
    self.regulation = factories.RegulationFactory()

    self.make_relationships(
        self.assessment, (
            self.audit,
            self.control,
            self.regulation,
        ),
    )

    self.other_assessments, self.id_weight_map = self.make_assessments()
Пример #16
0
    def test_mapped_regulations_read(self):
        """Test if creators can CRUD mapped Regulations and Objective snapshots."""
        self.api.set_user(self.people.get("Editor"))
        with factories.single_commit():
            audit = factories.AuditFactory()
            assessment = factories.AssessmentFactory(audit=audit)
            factories.AccessControlPersonFactory(
                ac_list=assessment.acr_name_acl_map["Creators"],
                person=self.people.get("Editor"),
            )
            factories.AccessControlPersonFactory(
                ac_list=assessment.acr_name_acl_map["Assignees"],
                person=self.people.get("Creator"),
            )
            factories.RelationshipFactory(source=audit, destination=assessment)
            control = factories.ControlFactory()
            objective = factories.ObjectiveFactory()
            regulation = factories.RegulationFactory()
            snapshots = self._create_snapshots(
                audit, [control, objective, regulation])
            for snapshot in snapshots:
                factories.RelationshipFactory(source=audit,
                                              destination=snapshot)
            factories.RelationshipFactory(source=snapshots[0],
                                          destination=snapshots[1])
            factories.RelationshipFactory(source=snapshots[2],
                                          destination=snapshots[0])
            factories.RelationshipFactory(source=assessment,
                                          destination=snapshots[0])

        self.api.set_user(self.people.get("Creator"))
        for snapshot in snapshots:
            db.session.add(snapshot)
            response = self.api.get(type(snapshot), snapshot.id)
            self.assertEqual(
                response.status_code, 200,
                "Cannot GET mapped object. Received {}".format(
                    response.status))
            db.session.add(snapshot)
            response = self.api.delete(snapshot)
            self.assertEqual(
                response.status_code, 403,
                "Can DELETE mapped object. Received {}".format(
                    response.status))
    def setUp(self):
        """Setup for import with mapped regulation testcase."""

        super(TestImportAssessmentWithMap, self).setUp()

        self.client.get("/login")
        self.api = Api()

        with factories.single_commit():
            program = factories.ProgramFactory()
            self.program_id = program.id
            audit = factories.AuditFactory(program=program)
            factories.RelationshipFactory(
                source=audit,
                destination=program,
            )
            self.audit_id = audit.id
            assessment = factories.AssessmentFactory(audit=audit)
            self.assessment_id = assessment.id
            factories.RelationshipFactory(source=audit, destination=assessment)
            regulation = factories.RegulationFactory()
            self.regulation_id = regulation.id
            self.regulation_slug = regulation.slug
            snapshot = self._create_snapshots(audit, [regulation])[0]
            factories.RelationshipFactory(
                source=audit,
                destination=snapshot,
            )
            factories.RelationshipFactory(
                source=snapshot,
                destination=audit,
            )

        data = [{
            "object_name": "Assessment",
            "filters": {
                "expression": {},
            },
            "fields": "all",
        }]
        response = self.export_parsed_csv(data)
        self.asmt = response["Assessment"][0]
        self.asmt["map:regulation versions"] = self.regulation_slug
Пример #18
0
    def test_similar_partially_matching(self):
        """Basic check of similar objects manually and via Query API.

    We create three programs, map one them to the two regulations, create two
    audits and verify that we get the same result manually and via Query API.

    We also ensure that for only single matching regulation we do not
    fetch that assessment is as related.
    """

        # pylint: disable=too-many-locals

        program_1 = factories.ProgramFactory(title="Program 1")
        program_2 = factories.ProgramFactory(title="Program 2")
        program_3 = factories.ProgramFactory(title="Program 3")

        regulation_1_program_1 = factories.RegulationFactory(
            title="Regulation 1")
        regulation_2_program_1 = factories.RegulationFactory(
            title="Regulation 2")

        self.make_relationships(
            program_1,
            [
                regulation_1_program_1,
                regulation_2_program_1,
            ],
        )

        self.make_relationships(
            program_2,
            [
                regulation_1_program_1,
                regulation_2_program_1,
            ],
        )

        self.make_relationships(
            program_3,
            [
                regulation_1_program_1,
            ],
        )

        program_1 = models.Program.query.filter_by(title="Program 1").one()
        program_2 = models.Program.query.filter_by(title="Program 2").one()
        program_3 = models.Program.query.filter_by(title="Program 3").one()

        regulation_1_program_1 = models.Regulation.query.filter_by(
            title="Regulation 1").one()
        regulation_2_program_1 = models.Regulation.query.filter_by(
            title="Regulation 2").one()

        _, audit_1 = self.obj_gen.generate_object(
            models.Audit, {
                "title": "Audit 1",
                "program": {
                    "id": program_1.id
                },
                "status": "Planned",
            })

        _, audit_2 = self.obj_gen.generate_object(
            models.Audit, {
                "title": "Audit 2",
                "program": {
                    "id": program_2.id
                },
                "status": "Planned",
            })

        _, audit_3 = self.obj_gen.generate_object(
            models.Audit, {
                "title": "Audit 3",
                "program": {
                    "id": program_3.id
                },
                "status": "Planned",
            })

        assessment_mappings = [
            [audit_1, regulation_1_program_1, regulation_2_program_1],
            [audit_2, regulation_1_program_1, regulation_2_program_1],
            [audit_3, regulation_1_program_1],
        ]

        assessment_1, assessment_2, assessment_3 = self.make_assessments(
            assessment_mappings)

        similar_objects = models.Assessment.get_similar_objects_query(
            id_=assessment_1.id,
            types=["Assessment"],
        ).all()

        expected_ids = {assessment_2.id}

        self.assertSetEqual(
            {obj.id
             for obj in similar_objects},
            expected_ids,
        )
        self.assertNotIn(assessment_3.id, similar_objects)

        query = [{
            "object_name": "Assessment",
            "type": "ids",
            "filters": {
                "expression": {
                    "op": {
                        "name": "similar"
                    },
                    "object_name": "Assessment",
                    "ids": [str(assessment_1.id)],
                },
            },
        }]
        response = self.client.post(
            "/query",
            data=json.dumps(query),
            headers={"Content-Type": "application/json"},
        )
        self.assertSetEqual(
            set(json.loads(response.data)[0]["Assessment"]["ids"]),
            expected_ids,
        )
Пример #19
0
    def test_sort_by_similarity(self):
        """Check sorting by __similarity__ value with query API."""

        # pylint: disable=too-many-locals

        program_1 = factories.ProgramFactory(title="Program 1")

        regulation_1_program_1 = factories.RegulationFactory(
            title="Regulation 1")
        regulation_2_program_1 = factories.RegulationFactory(
            title="Regulation 2")
        control_1_program_1 = factories.ControlFactory(title="Control 1")
        control_2_program_1 = factories.ControlFactory(title="Control 2")

        self.make_relationships(
            program_1,
            [
                regulation_1_program_1, regulation_2_program_1,
                control_1_program_1, control_2_program_1
            ],
        )

        program_1 = models.Program.query.filter_by(title="Program 1").one()

        _, audit_1 = self.obj_gen.generate_object(
            models.Audit, {
                "title": "Audit 1",
                "program": {
                    "id": program_1.id
                },
                "status": "Planned",
            })

        _, audit_2 = self.obj_gen.generate_object(
            models.Audit, {
                "title": "Audit 2",
                "program": {
                    "id": program_1.id
                },
                "status": "Planned",
            })

        regulation_1_program_1 = models.Regulation.query.filter_by(
            title="Regulation 1").one()
        regulation_2_program_1 = models.Regulation.query.filter_by(
            title="Regulation 2").one()
        control_1_program_1 = models.Control.query.filter_by(
            title="Control 1").one()
        control_2_program_1 = models.Control.query.filter_by(
            title="Control 2").one()

        assessment_mappings = [
            [
                audit_1, regulation_1_program_1, regulation_2_program_1,
                control_1_program_1, control_2_program_1
            ],
            [audit_1, control_1_program_1, control_2_program_1],
            [audit_1, regulation_1_program_1, control_1_program_1],
            [
                audit_2, regulation_1_program_1, regulation_2_program_1,
                control_1_program_1, control_2_program_1
            ],
            [audit_2, regulation_1_program_1, control_1_program_1],
            [audit_2, control_1_program_1, control_2_program_1],
        ]

        weights = [[13, 18, 20, 25, 26], [10, 15, 20, 20, 25],
                   [10, 13, 13, 15, 18], [13, 18, 20, 25, 26],
                   [10, 13, 13, 15, 18], [10, 15, 20, 20, 25]]

        assessments = self.make_assessments(assessment_mappings)
        assessment_ids = [ass.id for ass in assessments]

        for aid, weight_defs in zip(assessment_ids, weights):
            similar_objects = models.Assessment.get_similar_objects_query(
                id_=aid,
                types=["Assessment"],
            ).all()

            sorted_similar = sorted(similar_objects, key=lambda x: x.weight)

            self.assertEqual(weight_defs, [x.weight for x in sorted_similar])

            query = [{
                "object_name": "Assessment",
                "type": "ids",
                "order_by": [{
                    "name": "__similarity__"
                }],
                "filters": {
                    "expression": {
                        "op": {
                            "name": "similar"
                        },
                        "object_name": "Assessment",
                        "ids": [str(aid)],
                    },
                },
            }]
            response = self.client.post(
                "/query",
                data=json.dumps(query),
                headers={"Content-Type": "application/json"},
            )

            # our sorted results are only unstably sorted. As such we verify that
            # weights match and not actual object ids
            obj_weight = {so.id: so.weight for so in similar_objects}
            response_ids = json.loads(response.data)[0]["Assessment"]["ids"]
            response_weights = [obj_weight[rid] for rid in response_ids]

            self.assertListEqual(
                response_weights,
                [obj.weight for obj in sorted_similar],
            )
Пример #20
0
    def setUp(self):
        """Create indexable objects which provide required set of fulltext attrs

    Fulltext attributes are: title, name, email, notes, description, slug
    """

        super(TestMultipleTypes, self).setUp()

        self.api = Api()

        self.objects = dict()

        with factories.single_commit():
            # Create Requirements
            requirements = [
                factories.RequirementFactory(title=title,
                                             description=desc,
                                             notes=notes)
                for title, desc, notes in (
                    ('t01', 'd01', 'n01'),
                    ('t02', 'd02', 'n02'),
                    ('t11', 'd11', 'n11'),
                )
            ]
            self.objects['Requirement'] = dict(
                (i.title, i.id) for i in requirements)

            # Create people
            people = [
                factories.PersonFactory(name=name, email=email)
                for name, email in (
                    ('n01', '*****@*****.**'),
                    ('n02', '*****@*****.**'),
                    ('n11', '*****@*****.**'),
                )
            ]
            self.objects['Person'] = dict((i.name, i.id) for i in people)

            # Create regulations (regulations represented as directives)
            regulations = [
                factories.RegulationFactory(title=title,
                                            notes=notes,
                                            description=desc)
                for title, notes, desc in (
                    ('t01r', 'n01', 'd01'),
                    ('t02r', 'n02-qq1', 'd02'),
                    ('t11r', 'n11', 'd11'),
                )
            ]
            self.objects['Regulation'] = dict(
                (i.title, i.id) for i in regulations)

            # Create standards (standards represented as directives)
            standards = [
                factories.StandardFactory(title=title,
                                          notes=notes,
                                          description=desc)
                for title, notes, desc in (
                    ('t01s', 'n01-qq1', 'd01'),
                    ('t02s', 'n02', 'd02'),
                    ('t11s', 'n11', 'd11'),
                    ('t21s', 'n21', 'd11'),
                )
            ]
            self.objects['Standard'] = dict((i.title, i.id) for i in standards)
Пример #21
0
  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))
Пример #22
0
    def test_complex_propagation_count(self):
        """Test multiple object ACL propagation.

    This test is meant to catch invalid ACL entries for propagation that can
    not happen.
    Example for this is propagation control -> relationships -> document. In
    that propagation rule we should only propagate control acl entries to
    relationships to documents. But what can happen is; when a control has
    multiple relationships, some to objects and only one to document, all of
    those relationships would get an ACL entry even though in some cases that
    is a propagation dead end.

    Setup for this test is:

    Objects:
      control
      regulation
      objective
      program
      audit
      assessment, assessment_2

    Relationships:
      control - regulation
      control - objective
      objective - regulations
      program - control, regulation, objective, audit
      audit - assessment, assessment_2,
      audit - control-snapshot, regulation-snapshot, objective-snapshot
      control_snapshot - regulation_snapshot
      control_snapshot - objective_snapshot
      objective_snapshot - regulations_snapshot
      document - regulation, objective, control
      evidence - assessment
    """
        # pylint: disable=too-many-locals
        with factories.single_commit():
            control = factories.ControlFactory()
            regulation = factories.RegulationFactory()
            objective = factories.ObjectiveFactory()
            normal_objects = [control, regulation, objective]

            for obj1, obj2 in itertools.combinations(normal_objects, 2):
                if control in (obj1, obj2):
                    with mock.patch(
                            'ggrc.models.relationship.is_external_app_user',
                            return_value=True):
                        factories.RelationshipFactory(source=obj1,
                                                      destination=obj2,
                                                      is_external=True)
                else:
                    factories.RelationshipFactory(source=obj1,
                                                  destination=obj2)

            assessment = factories.AssessmentFactory()
            assessment_2 = factories.AssessmentFactory(audit=assessment.audit)
            factories.RelationshipFactory(
                source=assessment,
                destination=assessment.audit,
            )
            factories.RelationshipFactory(
                source=assessment_2,
                destination=assessment.audit,
            )
            factories.RelationshipFactory(
                source=assessment.audit,
                destination=assessment.audit.program,
            )
            for obj in normal_objects:
                factories.RelationshipFactory(
                    source=assessment.audit.program,
                    destination=obj,
                )

            snapshots = self._create_snapshots(assessment.audit,
                                               normal_objects)
            for snapshot in snapshots:
                factories.RelationshipFactory(
                    source=assessment.audit,
                    destination=snapshot,
                )

            for obj1, obj2 in itertools.combinations(snapshots, 2):
                factories.RelationshipFactory(source=obj1, destination=obj2)

            for obj in normal_objects:
                document = factories.DocumentFactory()
                factories.RelationshipFactory(source=obj, destination=document)

            evidence = factories.EvidenceUrlFactory()
            factories.RelationshipFactory(source=evidence,
                                          destination=assessment)

        acl_entry = control._access_control_list[0]

        propagation._propagate([acl_entry.id], self.user_id)

        self.assertEqual(
            all_models.AccessControlList.query.filter(
                all_models.AccessControlList.parent_id.isnot(None)).count(),
            2,  # 1 for relationship to document and 1 for document.
        )

        acl_entry = next(
            acl for acl in assessment.audit.program._access_control_list
            if acl.ac_role.name == "Program Editors")
        propagation._propagate([acl_entry.id], self.user_id)

        self.assertEqual(
            all_models.AccessControlList.query.filter(
                all_models.AccessControlList.parent_id.isnot(None)).count(),
            2 + 2 + 4 + 6 + 2 + 6 + 6
            # 2 previous entries for control
            # 2 for audit (relationship + audit propagation)
            # 4 for assessments (2 assessments and 2 relationships for them)
            # 6 for snapshots (3 snapshots with relationships)
            # 2 assessment document with relationships
            # 6 for normal objects
            # 6 for normal object documents
        )
Пример #23
0
    def test_complex_propagation_count(self):
        """Test multiple object ACL propagation.

    This test is meant to catch invalid ACL entries for propagation that can
    not happen.
    Example for this is propagation control -> relationships -> document. In
    that propagation rule we should only propagate control acl entries to
    relationships to documents. But what can happen is; when a control has
    multiple relationships, some to objects and only one to document, all of
    those relationships would get an ACL entry even though in some cases that
    is a propagation dead end.

    Setup for this test is:

    Objects:
      control
      regulation
      objective
      program
      audit
      assessment, assessment_2

    Relationships:
      control - regulation
      control - objective
      objective - regulations
      program - control, regulation, objective, audit
      audit - assessment, assessment_2,
      audit - control-snapshot, regulation-snapshot, objective-snapshot
      control_snapshot - regulation_snapshot
      control_snapshot - objective_snapshot
      objective_snapshot - regulations_snapshot
      document - regulation, objective, control
      evidence - assessment
    """
        with factories.single_commit():
            person = factories.PersonFactory()
            control = factories.ControlFactory()
            regulation = factories.RegulationFactory()
            objective = factories.ObjectiveFactory()
            normal_objects = [control, regulation, objective]

            for obj1, obj2 in itertools.combinations(normal_objects, 2):
                factories.RelationshipFactory(source=obj1, destination=obj2)

            assessment = factories.AssessmentFactory()
            assessment_2 = factories.AssessmentFactory(audit=assessment.audit)
            factories.RelationshipFactory(
                source=assessment,
                destination=assessment.audit,
            )
            factories.RelationshipFactory(
                source=assessment_2,
                destination=assessment.audit,
            )
            factories.RelationshipFactory(
                source=assessment.audit,
                destination=assessment.audit.program,
            )
            for obj in normal_objects:
                factories.RelationshipFactory(
                    source=assessment.audit.program,
                    destination=obj,
                )

            snapshots = self._create_snapshots(assessment.audit,
                                               normal_objects)
            for snapshot in snapshots:
                factories.RelationshipFactory(
                    source=assessment.audit,
                    destination=snapshot,
                )

            for obj1, obj2 in itertools.combinations(snapshots, 2):
                factories.RelationshipFactory(source=obj1, destination=obj2)

            for obj in normal_objects:
                document = factories.DocumentFactory()
                factories.RelationshipFactory(source=obj, destination=document)

            evidence = factories.EvidenceUrlFactory()
            factories.RelationshipFactory(source=evidence,
                                          destination=assessment)

        acl_entry = factories.AccessControlListFactory(
            person=person,
            ac_role=self.roles["Control"]["Admin"],
            object=control,
        )

        propagation._propagate([acl_entry.id])

        self.assertEqual(
            all_models.AccessControlList.query.count(),
            3,  # 1 for control, 1 for relationship to document and 1 for document.
        )

        acl_entry = factories.AccessControlListFactory(
            person=person,
            ac_role=self.roles["Program"]["Program Editors"],
            object=assessment.audit.program,
        )
        propagation._propagate([acl_entry.id])

        self.assertEqual(
            all_models.AccessControlList.query.count(),
            3 + 1 + 2 + 4 + 6 + 2 + 6 + 6
            # 3 previous entries for control
            # 1 original program ACL entry
            # 2 for audit (relationship + audit propagation)
            # 4 for assessments (2 assessments and 2 relationships for them)
            # 6 for snapshots (3 snapshots with relationships)
            # 2 assessment document with relationships
            # 6 for normal objects
            # 6 for normal object documents
        )