Example #1
0
    def test_updating_ca_values(self):
        """Test updating custom attribute values."""
        cad1 = CAD(
            definition_type="program",
            title="CA 1",
        )

        val1 = models.CustomAttributeValue(
            attribute_value="55",
            custom_attribute=cad1,
        )

        prog = ProgramFactory()
        prog.custom_attribute_values = [val1]
        db.session.commit()

        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(prog.custom_attribute_values[0].attribute_value, "55")

        val2 = models.CustomAttributeValue(
            attribute_value="129",
            custom_attribute=cad1,
        )

        prog.custom_attribute_values = [val2]
        db.session.commit()

        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(prog.custom_attribute_values[0].attribute_value,
                         "129")
    def test_setting_ca_values(self):
        """Test normal setting of custom attribute values."""
        with factories.single_commit():
            prog = factories.ProgramFactory()
            cad1 = factories.CustomAttributeDefinitionFactory(
                definition_type="program",
                title="CA 1",
            )
            cad2 = factories.CustomAttributeDefinitionFactory(
                definition_type="program",
                title="CA 2",
            )

        prog = prog.__class__.query.get(prog.id)

        val1 = models.CustomAttributeValue(
            attribute_value="55",
            custom_attribute=cad1,
        )
        val2 = models.CustomAttributeValue(
            attribute_value="129aaaaaa",
            custom_attribute=cad2,
        )

        prog.custom_attribute_values = [val1, val1, val1]
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(len(prog.custom_attribute_values), 1)

        prog = factories.ProgramFactory()
        prog.custom_attribute_values.append(val1)
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)
        self.assertEqual(len(prog.custom_attribute_values), 1)
        self.assertEqual(
            {"55"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )

        prog = factories.ProgramFactory()
        prog.custom_attribute_values = [val1, val2]
        db.session.commit()
        prog = prog.__class__.query.get(prog.id)

        self.assertEqual(
            {"55", "129aaaaaa"},
            set(v.attribute_value for v in prog.custom_attribute_values),
        )
        self.assertEqual(len(prog.custom_attribute_values), 2)
Example #3
0
    def _get_or_create_ca(self):
        """Get a CA value object for the current definition.

    This function returns a custom attribute value object that already existed
    or creates a new one.

    Returns:
        custom attribute value object.
    """
        ca_definition = self.get_ca_definition()
        if not self.row_converter.obj or not ca_definition:
            return None
        for ca_value in self.row_converter.obj.custom_attribute_values:
            if ca_value.custom_attribute_id == ca_definition.id:
                return ca_value
        ca_value = models.CustomAttributeValue(
            custom_attribute=ca_definition,
            attributable=self.row_converter.obj,
        )
        return ca_value
Example #4
0
    def _create(cls, target_class, *args, **kwargs):
        """Create instance of model"""
        acls = []
        if "access_control_list_" in kwargs:
            acls = kwargs.pop("access_control_list_")
        cavs = []
        if "custom_attribute_values_" in kwargs:
            cavs = kwargs.pop("custom_attribute_values_")

        instance = target_class(**kwargs)
        db.session.add(instance)
        db.session.flush()

        if acls and isinstance(instance, Roleable):
            for acl in acls:
                db.session.add(
                    models.AccessControlList(
                        object=instance,
                        ac_role_id=acl.get("ac_role_id"),
                        person_id=acl.get("person_id"),
                    ))
        if cavs and isinstance(instance, CustomAttributable):
            for cav in cavs:
                db.session.add(
                    models.CustomAttributeValue(
                        attributable=instance,
                        attribute_value=cav.get("attribute_value"),
                        attribute_object_id=cav.get("attribute_object_id"),
                        custom_attribute_id=cav.get("custom_attribute_id"),
                    ))

        if isinstance(instance, models.CustomAttributeValue):
            cls._log_event(instance.attributable)
        if hasattr(instance, "log_json"):
            cls._log_event(instance)
        if getattr(db.session, "single_commit", True):
            db.session.commit()
        return instance
    def test_snapshot_update_after_CA_value_changed(self):
        """Test update of a snapshot after CA value changed

    1. Create program with mapped control.
    2. Create audit, verify there is a snapshot for control
    3. Update control's CA value
    4. Run refresh on control's snapshot object
    5. Verify control's CA is changed
    """
        program = self.create_object(models.Program,
                                     {"title": "Test Program Snapshot 1"})
        control = self.create_object(models.Control,
                                     {"title": "Test Control Snapshot 1"})
        custom_attribute_defs = self.create_custom_attribute_definitions()
        cav = {
            "custom_attribute": custom_attribute_defs["control"],
            "attributable": control,
            "attribute_value": "CA value 1",
        }
        factories.CustomAttributeValueFactory(**cav)
        self.create_mapping(program, control)
        control = self.refresh_object(control)
        self.create_audit(program)
        audit = db.session.query(models.Audit).filter(
            models.Audit.title.like("%Snapshotable audit%")).one()
        self.assertEqual(
            db.session.query(models.Snapshot).filter(
                models.Snapshot.parent_type == "Audit",
                models.Snapshot.parent_id == audit.id).count(), 1)
        control = self.refresh_object(control)
        cad2 = models.CustomAttributeDefinition.query.filter(
            models.CustomAttributeDefinition.title ==
            "control text field 1").one()
        val2 = models.CustomAttributeValue(attribute_value="CA value 1",
                                           custom_attribute=cad2)

        with self.api.as_external():
            self.api.put(
                control, {
                    "custom_attribute_values":
                    [{
                        "attributable_id": control.id,
                        "attributable_type": "Assessment",
                        "id": val2.id,
                        "custom_attribute_id": cad2.id,
                        "attribute_value": "CA value 1 EDIT 1",
                    }]
                })

        control_snapshot = db.session.query(models.Snapshot).filter(
            models.Snapshot.child_type == "Control",
            models.Snapshot.child_id == control.id).first()
        cav = control_snapshot.revision.content["custom_attribute_values"][0]
        self.assertEqual(cav["attribute_value"], "CA value 1")

        self.api.modify_object(control_snapshot, {"update_revision": "latest"})

        expected = [
            (control, "CA value 1 EDIT 1"),
        ]
        for obj, expected_title in expected:
            snapshot = db.session.query(models.Snapshot).filter(
                models.Snapshot.child_type == obj.__class__.__name__,
                models.Snapshot.child_id == obj.id).first()
            cav = snapshot.revision.content["custom_attribute_values"][0]
            self.assertEquals(cav["attribute_value"], expected_title)

        control_snapshot_revisions = db.session.query(models.Revision).filter(
            models.Revision.resource_type == "Snapshot",
            models.Revision.resource_id == control_snapshot.id)
        self.assertEqual(control_snapshot_revisions.count(), 2)