Ejemplo n.º 1
0
    def test_ext_app_recreate_normal_relationship(self):
        """If ext app create already created relationship
    it has to be with is_external=False"""
        with factories.single_commit():
            market1 = factories.MarketFactory()
            market2 = factories.MarketFactory()
            relationship = factories.RelationshipFactory(
                source=market1,
                destination=market2,
                is_external=False,
            )
            relationship_id = relationship.id
        ext_api = ExternalApiClient(use_ggrcq_service_account=True)

        response = ext_api.post(all_models.Relationship,
                                data={
                                    "relationship": {
                                        "source": {
                                            "id": market1.id,
                                            "type": market1.type
                                        },
                                        "destination": {
                                            "id": market2.id,
                                            "type": market2.type
                                        },
                                        "is_external": True,
                                        "context": None,
                                    },
                                })

        self.assert200(response)
        relationship = all_models.Relationship.query.get(relationship_id)
        self.assertFalse(relationship.is_external)
Ejemplo n.º 2
0
    def _create_snapshotable_objects():
        """Create original objects that will be snapshotted."""
        text_cad = factories.CustomAttributeDefinitionFactory(
            title="text cad",
            definition_type="market",
        )
        date_cad = factories.CustomAttributeDefinitionFactory(
            title="date cad",
            definition_type="market",
            attribute_type="Date",
        )

        for i in range(5):
            factories.ControlFactory(title="Control {}".format(i + 1))
            factories.OrgGroupFactory()
            market = factories.MarketFactory(title="Market {}".format(i + 1))
            factories.CustomAttributeValueFactory(
                custom_attribute=date_cad,
                attributable=market,
                attribute_value="2016-11-0{}".format(i + 3),
            )
            factories.CustomAttributeValueFactory(
                custom_attribute=text_cad,
                attributable=market,
                attribute_value="2016-11-0{}".format(i + 1),
            )
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
  def test_checkbox_fulltext(self, value, search_value):
    """Test filter by checkbox value."""

    title = "checkbox"
    checkbox_type = all_models.CustomAttributeDefinition.ValidTypes.CHECKBOX
    with factories.single_commit():
      market = factories.MarketFactory()
      cad = factories.CustomAttributeDefinitionFactory(
          title=title,
          definition_type="market",
          attribute_type=checkbox_type)
      factories.CustomAttributeValueFactory(
          custom_attribute=cad,
          attributable=market,
          attribute_value=value)

    views.do_reindex()

    contents = [
        i.content
        for i in mysql.MysqlRecordProperty.query.filter(
            mysql.MysqlRecordProperty.property == title,
            mysql.MysqlRecordProperty.type == market.type,
            mysql.MysqlRecordProperty.key == market.id,
        )
    ]
    self.assertEqual([search_value], contents)
Ejemplo n.º 6
0
    def test_type_cad(self):
        """Test CAD with the name 'type' """

        title1 = "type"
        cad1 = CAD(title=title1, definition_type="market")
        factory = factories.MarketFactory()
        CAV(custom_attribute=cad1, attributable=factory, attribute_value="x")

        title1_count = mysql.MysqlRecordProperty.query.filter(
            mysql.MysqlRecordProperty.property == title1).count()
        self.assertEqual(title1_count, 1)
Ejemplo n.º 7
0
    def test_cad_length(self):
        """Custom attribute titles must support all lengths that can be stored."""

        title1 = "a" * 200 + "1"
        title2 = "a" * 200 + "2"
        cad1 = CAD(title=title1, definition_type="market")
        cad2 = CAD(
            title=title2,
            definition_type="market",
        )
        factory = factories.MarketFactory()
        CAV(custom_attribute=cad1, attributable=factory, attribute_value="x")
        CAV(custom_attribute=cad2, attributable=factory, attribute_value="x")

        title1_count = mysql.MysqlRecordProperty.query.filter(
            mysql.MysqlRecordProperty.property == title1).count()
        self.assertEqual(title1_count, 1)
Ejemplo n.º 8
0
    def test_relationship_creation(self):
        """Test external relationship post on behalf of external user."""
        destination = factories.SystemFactory()
        source = factories.MarketFactory()
        relationship_data = json.dumps([{
            "relationship": {
                "source": {
                    "id": source.id,
                    "type": source.type
                },
                "destination": {
                    "id": destination.id,
                    "type": destination.type
                },
                "context": {
                    "id": None
                },
                "is_external": True
            }
        }])
        response = self._post("/api/relationships",
                              data=relationship_data,
                              headers=self.headers)
        self.assert200(response)

        ext_person = all_models.Person.query.filter_by(
            email="*****@*****.**").first()
        ext_person_id = ext_person.id

        relationship = all_models.Relationship.query.get(
            response.json[0][-1]["relationship"]["id"])
        self.assertEqual(relationship.source_type, source.type)
        self.assertEqual(relationship.source_id, source.id)
        self.assertEqual(relationship.destination_type, "System")
        self.assertEqual(relationship.destination_id, destination.id)
        self.assertTrue(relationship.is_external)
        self.assertEqual(relationship.modified_by_id, ext_person_id)
        self.assertIsNone(relationship.parent_id)
        self.assertIsNone(relationship.automapping_id)
        self.assertIsNone(relationship.context_id)

        # check that POST on creation of existing relation return 200 code
        response = self._post("/api/relationships",
                              data=relationship_data,
                              headers=self.headers)
        self.assert200(response)
Ejemplo n.º 9
0
  def _setup_objects():
    """Create and reindex objects needed for tests"""
    text_cad = factories.CustomAttributeDefinitionFactory(
        title="text cad",
        definition_type="market",
    )
    date_cad = factories.CustomAttributeDefinitionFactory(
        title="date cad",
        definition_type="market",
        attribute_type="Date",
    )

    audit = factories.AuditFactory()

    for i in range(5):
      factories.OrgGroupFactory()
      market = factories.MarketFactory()
      factories.CustomAttributeValueFactory(
          custom_attribute=date_cad,
          attributable=market,
          attribute_value="2016-11-0{}".format(i + 3),
      )
      factories.CustomAttributeValueFactory(
          custom_attribute=text_cad,
          attributable=market,
          attribute_value="2016-11-0{}".format(i + 1),
      )

    revisions = models.Revision.query.filter(
        models.Revision.resource_type.in_(["OrgGroup", "Market"]),
        models.Revision.id.in_(
            db.session.query(func.max(models.Revision.id)).group_by(
                models.Revision.resource_type,
                models.Revision.resource_id,
            )
        ),
    )

    for revision in revisions:
      factories.SnapshotFactory(
          child_id=revision.resource_id,
          child_type=revision.resource_type,
          revision=revision,
          parent=audit,
      )
    views.do_reindex()
Ejemplo n.º 10
0
    def test_checkbox_fulltext_no_cav(self):
        """Test filter by checkbox value without cav."""

        title = "checkbox"
        search_value = "No"
        checkbox_type = all_models.CustomAttributeDefinition.ValidTypes.CHECKBOX
        with factories.single_commit():
            market = factories.MarketFactory()
            factories.CustomAttributeDefinitionFactory(
                title=title,
                definition_type="market",
                attribute_type=checkbox_type)

        contents = [
            i.content for i in mysql.MysqlRecordProperty.query.filter(
                mysql.MysqlRecordProperty.property == title,
                mysql.MysqlRecordProperty.type == market.type,
                mysql.MysqlRecordProperty.key == market.id,
            )
        ]
        self.assertEqual([search_value], contents)
Ejemplo n.º 11
0
    def test_permissions_cleared(self):
        """Test that permissions where cleared after import."""
        with factories.single_commit():
            user = factories.PersonFactory()
            user_id = user.id
            market = factories.MarketFactory(slug="test market")
            system_role = all_models.Role.query.filter(
                all_models.Role.name == "Creator").one()
            rbac_factories.UserRoleFactory(role=system_role, person=user)
            market.add_person_with_role_name(user, "Admin")

        user_perm_key = 'permissions:{}'.format(user_id)

        # Recalculate permissions under new user
        self.api.set_user(user)
        self.api.client.get("/permissions")

        perm_ids = self.memcache_client.get('permissions:list')
        self.assertEqual(perm_ids, {user_perm_key})
        user_perm = self.memcache_client.get(user_perm_key)
        self.assertIsNotNone(user_perm)

        data = [
            collections.OrderedDict([
                ("Code*", ""),
                ("Title*", "Test Objective"),
                ("Admin", "*****@*****.**"),
                ("map:market", "test market"),
            ])
        ]
        response = self.api.run_import_job(user, "Objective", data)
        self.assert200(response)
        self.assertEqual(all_models.Objective.query.count(), 1)

        perm_ids = self.memcache_client.get('permissions:list')
        self.assertEqual(perm_ids, set())
        user_perm = self.memcache_client.get(user_perm_key)
        self.assertIsNone(user_perm)
Ejemplo n.º 12
0
    def test_import_permissions(self):
        """Test that permissions aren't recalculated during import new objects."""
        with factories.single_commit():
            audit = factories.AuditFactory(slug="audit-1")
            market = factories.MarketFactory()
            user = factories.PersonFactory()
            system_role = all_models.Role.query.filter(
                all_models.Role.name == "Creator").one()
            rbac_factories.UserRoleFactory(role=system_role, person=user)
            audit.add_person_with_role_name(user, "Audit Captains")
            market.add_person_with_role_name(user, "Admin")
        self._create_snapshots(audit, [market])

        data = [
            collections.OrderedDict([
                ("Code*", ""),
                ("Audit*", "audit-1"),
                ("Title*", "assessment{}".format(i)),
                ("State", "Not Started"),
                ("Assignees*", "*****@*****.**"),
                ("Creators*", "*****@*****.**"),
                ("map:market versions", market.slug),
            ]) for i in range(10)
        ]

        self.api.set_user(user)

        with mock.patch("ggrc_basic_permissions.load_access_control_list",
                        side_effect=ggrc_basic_permissions.
                        load_access_control_list) as acl_loader:
            response = self.api.run_import_job(user, "Assessment", data)
            self.assert200(response)
            # 10 Assessments should be created in import
            self.assertEqual(all_models.Assessment.query.count(), 10)
            # Permissions were loaded once on dry run and once on real run
            self.assertEqual(acl_loader.call_count, 2)
Ejemplo n.º 13
0
    def _setup_objects():
        """Create and reindex objects needed for tests"""
        text_cad = factories.CustomAttributeDefinitionFactory(
            title="text cad",
            definition_type="market",
        )
        date_cad = factories.CustomAttributeDefinitionFactory(
            title="date cad",
            definition_type="market",
            attribute_type="Date",
        )
        person_cad = factories.CustomAttributeDefinitionFactory(
            title="CA person",
            definition_type="market",
            attribute_type="Map:Person",
        )
        users = [
            "*****@*****.**", "*****@*****.**", "*****@*****.**",
            "*****@*****.**", "*****@*****.**"
        ]
        for user in users:
            factories.PersonFactory(email=user)

        audit = factories.AuditFactory()

        for i in range(5):
            factories.ControlFactory(title="Control {}".format(i + 1))
            factories.OrgGroupFactory()
            market = factories.MarketFactory(title="Market {}".format(i + 1))
            factories.CustomAttributeValueFactory(
                custom_attribute=date_cad,
                attributable=market,
                attribute_value="2016-11-0{}".format(i + 3),
            )
            factories.CustomAttributeValueFactory(
                custom_attribute=text_cad,
                attributable=market,
                attribute_value="2016-11-0{}".format(i + 1),
            )
            factories.CustomAttributeValueFactory(
                custom_attribute=person_cad,
                attributable=market,
                attribute_value="user{}@example.com".format(i + 1),
            )

        revisions = models.Revision.query.filter(
            models.Revision.resource_type.in_(
                ["OrgGroup", "Market", "Control"]),
            models.Revision.id.in_(
                db.session.query(func.max(models.Revision.id)).group_by(
                    models.Revision.resource_type,
                    models.Revision.resource_id,
                )),
        )

        snapshots = [
            factories.SnapshotFactory(
                child_id=revision.resource_id,
                child_type=revision.resource_type,
                revision=revision,
                parent=audit,
            ) for revision in revisions
        ]

        snapshot_map = {
            snapshot.revision.content["title"]: snapshot
            for snapshot in snapshots
        }

        # create Assessments and issues and map some snapshots to them
        # Markets and Controls represent snapshots of those objects.

        assessment_issues = (
            factories.AssessmentFactory,
            factories.IssueFactory,
        )
        for i in range(4):
            for factory in assessment_issues:
                # pylint: disable=protected-access
                obj = factory(title="{} {}".format(
                    factory._meta.model.__name__, i + 1), )
                factories.RelationshipFactory(
                    source=audit if i % 2 == 0 else obj,
                    destination=audit if i % 2 == 1 else obj,
                )

                market = snapshot_map["Market {}".format(i + 1)]
                factories.RelationshipFactory(
                    source=market if i % 2 == 0 else obj,
                    destination=market if i % 2 == 1 else obj,
                )
                for j in range(i):
                    control = snapshot_map["Control {}".format(j + 1)]
                    factories.RelationshipFactory(
                        source=control if i % 2 == 0 else obj,
                        destination=control if i % 2 == 1 else obj,
                    )

        # Create an unmapped control for base tests
        factories.ControlFactory(title="Control 0")

        views.do_reindex()