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)
示例#2
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)
    def test_basic_automappings(self, object_name):
        """"Test Basic Automapping for {}"""

        with factories.single_commit():
            program = factories.ProgramFactory(title="program 1")
            program_manager = factories.PersonFactory()
            factories.AccessControlPersonFactory(
                ac_list=program.acr_name_acl_map["Program "
                                                 "Managers"],
                person=program_manager,
            )
            req_one = factories.RequirementFactory(title="section-1")

        self.import_data(
            collections.OrderedDict([
                ("object_type", object_name),
                ("Code*", ""),
                ("Title", object_name),
                ("Admin", "*****@*****.**"),
                ("map:program", program.slug),
                ("map:requirement", req_one.slug),
            ]))

        directive = models.get_model(object_name)
        directives = directive.query.filter_by(title=object_name).first()
        directives_slug = directives.slug

        data = [{
            "object_name": "Program",
            "filters": {
                "expression": {
                    "left": "title",
                    "op": {
                        "name": "="
                    },
                    "right": "program 1",
                },
            },
            "fields": "all",
        }]
        response = self.export_csv(data)

        self.assertIn(req_one.slug, response.data)
        self.assertIn(directives_slug, response.data)
示例#4
0
 def test_snapshot_automapping(self):
     """Test automapping after Snapshot to Audit mapping"""
     with factories.single_commit():
         program = factories.ProgramFactory()
         program_id = program.id
         parent_program = factories.ProgramFactory()
         parent_program_id = parent_program.id
         factories.RelationshipFactory(source=parent_program,
                                       destination=program)
         audit = factories.AuditFactory(program=program)
         standard = factories.StandardFactory()
         requirement = factories.RequirementFactory()
         factories.RelationshipFactory(source=standard,
                                       destination=requirement)
     self.gen.generate_relationship(audit, standard)
     program = all_models.Program.query.get(program_id)
     program_related = program.related_objects()
     parent_program = all_models.Program.query.get(parent_program_id)
     parent_program_related = parent_program.related_objects()
     self.assertEqual(len(program_related), 3)
     self.assertEqual(len(parent_program_related), 3)
     self.assertEqual({o.type
                       for o in parent_program_related},
                      {"Program", "Standard", "Requirement"})
示例#5
0
 def test_comment_import(self):
   """Don't revert state when comment added.
   Review -> REVIEWED
   """
   requirement = factories.RequirementFactory()
   resp, review = generate_review_object(
       requirement, state=all_models.Review.STATES.REVIEWED)
   del review
   requirement_id = requirement.id
   self.assertEqual(201, resp.status_code)
   import_data = OrderedDict(
       [
           ("object_type", "Requirement"),
           ("Code*", requirement.slug),
           ("comments", "some comments")
       ]
   )
   response = self.import_data(import_data)
   self._check_csv_response(response, {})
   requirement = all_models.Requirement.query.get(requirement_id)
   self.assertEqual(
       all_models.Review.STATES.REVIEWED,
       requirement.review_status
   )
示例#6
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))
示例#7
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)