Example #1
0
  def _create_external_object():
    """Populate external model object that could not be imported."""
    with factories.single_commit():
      objects = [
          factories.ControlFactory(directive=None),
          factories.RiskFactory()
      ]

      ca_definitions = {
          cad.title: cad
          for object in objects
          for cad in object.get_custom_attribute_definitions([
              "CA text",
              "CA rich text",
              "CA date",
              "CA multiselect",
              "CA dropdown"
          ])
      }
      ca_values = {
          "CA text": "Control ca text",
          "CA rich text": "control<br><br>\nrich text",
          "CA date": "22/02/2022",
          "CA multiselect": "yes",
          "CA dropdown": "one"
      }

      for title, value in ca_values.items():
        for obj in objects:
          factories.ExternalCustomAttributeValueFactory(
              custom_attribute=ca_definitions[title],
              attributable=obj,
              attribute_value=value
          )
  def test_control_export_with_ecad(self, attribute_type, multi_choice_options,
                                    attribute_value):
    """Test exporting of control with filtering by external cad."""
    with factories.single_commit():
      cad = factories.ExternalCustomAttributeDefinitionFactory(
          definition_type="control",
          attribute_type=attribute_type,
          multi_choice_options=multi_choice_options,
      )
      control = factories.ControlFactory(slug="Control 1")
      factories.ExternalCustomAttributeValueFactory(
          attributable=control,
          custom_attribute=cad,
          attribute_value=attribute_value,
      )

    search_request = [{
        "object_name": "Control",
        "filters": {
            "expression": {
                "left": cad.title,
                "op": {"name": "="},
                "right": attribute_value,
            },
        },
    }]
    parsed_data = self.export_parsed_csv(search_request)["Control"][0]
    self.assertIn(cad.title, parsed_data)
    if attribute_type == "Date":
      attribute_value = iso_to_us_date(attribute_value)
    self.assertEqual(parsed_data[cad.title], attribute_value)
Example #3
0
    def test_empty_control_export(self):
        """Test exporting of a single full control snapshot."""
        cads = self._create_ecads("control")
        with factories.single_commit():
            controls = [factories.ControlFactory(slug="Control 1")]
            for cad in cads:
                factories.ExternalCustomAttributeValueFactory(
                    attributable=controls[0],
                    custom_attribute=cad,
                )
            audit = factories.AuditFactory()
            snapshots = self._create_snapshots(audit, controls)

        control_dicts = {
            control.slug: self._control_dict({
                # normal fields
                "Code":
                "*" + control.slug,
                "Revision Date":
                snapshot.revision.created_at.strftime(DATE_FORMAT_US),
                "Title":
                control.title,
                # Special snapshot export fields
                "Audit":
                audit.slug,
                "Archived":
                u"yes" if audit.archived else u"no",
                # Custom attributes
                "RT":
                u"",
                "date":
                u"",
                "dropdown":
                u"",
                "multiselect":
                u"",
                # Fields that are not included in snapshots - Known bugs.
                "Assertions":
                u",".join(json.loads(control.assertions)),
                'Created Date':
                control.created_at.strftime(DATE_FORMAT_US),
                'Last Updated Date':
                control.updated_at.strftime(DATE_FORMAT_US),
            })
            for snapshot, control in zip(snapshots, controls)
        }
        parsed_data = self.export_parsed_csv(
            self.search_request)["Control Snapshot"]
        parsed_dict = {line["Code"]: line for line in parsed_data}

        self.assertEqual(
            parsed_dict["*Control 1"],
            control_dicts["Control 1"],
        )
Example #4
0
 def test_control_snapshotting(self, attribute_type, multi_choice_options,
                               attribute_value):
   """Test revisions and snapshots content contains
   external custom attributes."""
   with factories.single_commit():
     control = factories.ControlFactory(slug="Control 1")
     ecad = factories.ExternalCustomAttributeDefinitionFactory(
         definition_type="control",
         attribute_type=attribute_type,
         multi_choice_options=multi_choice_options,
     )
     factories.ExternalCustomAttributeValueFactory(
         custom_attribute=ecad,
         attributable=control,
         attribute_value=attribute_value,
     )
     audit = factories.AuditFactory()
   snapshots = self._create_snapshots(audit, [control])
   content = snapshots[0].revision.content
   self.assertEqual(content["custom_attribute_definitions"][0]["title"],
                    ecad.title)
   self.assertEqual(content["custom_attribute_values"][0]["attribute_value"],
                    attribute_value)
Example #5
0
    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_def = factories.ExternalCustomAttributeDefinitionFactory(
            definition_type="control",
            title="control text field 1",
            attribute_type="Text",
        )
        cav = {
            "custom_attribute": custom_attribute_def,
            "attributable": control,
            "attribute_value": "CA value 1",
        }
        factories.ExternalCustomAttributeValueFactory(**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.ExternalCustomAttributeDefinition.query.filter(
            models.ExternalCustomAttributeDefinition.title ==
            "control text field 1").one()
        val2 = models.ExternalCustomAttributeValue(
            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)
    def test_empty_control_export(self):
        """Test exporting of a single full control snapshot."""
        cads = self._create_ecads("control")
        with factories.single_commit():
            controls = [factories.ControlFactory(slug="Control 1")]
            for cad in cads:
                factories.ExternalCustomAttributeValueFactory(
                    attributable=controls[0],
                    custom_attribute=cad,
                )
            audit = factories.AuditFactory()
            snapshots = self._create_snapshots(audit, controls)

        control_dicts = {
            control.slug: {
                # normal fields
                "Code":
                "*" + control.slug,
                "Revision Date":
                snapshot.revision.created_at.strftime(DATE_FORMAT_US),
                "Description":
                u"",
                "Effective Date":
                u"",
                "Fraud Related":
                u"",
                "Frequency":
                u"",
                "Kind/Nature":
                u"",
                "Notes":
                u"",
                "Review Status":
                u"some status",
                "Significance":
                u"",
                "State":
                u"Draft",
                "Last Deprecated Date":
                u"",
                "Assessment Procedure":
                u"",
                "Title":
                control.title,
                "Type/Means":
                u"",
                # Special snapshot export fields
                "Audit":
                audit.slug,
                "Archived":
                u"yes" if audit.archived else u"no",
                # Computed attributes
                "Last Assessment Date":
                u"",

                # Custom attributes
                "RT":
                u"",
                "Reference URL":
                u"",
                "date":
                u"",
                "dropdown":
                u"",
                "multiselect":
                u"",

                # Fields that are not included in snapshots - Known bugs.
                "Assertions":
                u",".join(json.loads(control.assertions)),
                "Categories":
                u"",
                "Document File":
                u"",
                "Admin":
                u"",
                "Control Operators":
                u"",
                "Control Owners":
                u"",
                "Other Contacts":
                u"",
                "Principal Assignees":
                u"",
                "Secondary Assignees":
                u"",
                'Created Date':
                control.created_at.strftime(DATE_FORMAT_US),
                'Last Updated Date':
                control.updated_at.strftime(DATE_FORMAT_US),
                'Last Updated By':
                "",
                "GDrive Folder ID":
                u"",
            }
            for snapshot, control in zip(snapshots, controls)
        }

        search_request = [{
            "object_name": "Snapshot",
            "filters": {
                "expression": {
                    "left": "child_type",
                    "op": {
                        "name": "="
                    },
                    "right": "Control",
                },
            },
        }]
        parsed_data = self.export_parsed_csv(
            search_request)["Control Snapshot"]
        parsed_dict = {line["Code"]: line for line in parsed_data}

        self.assertEqual(
            parsed_dict["*Control 1"],
            control_dicts["Control 1"],
        )