def test_audit_bad(self): """ Test an operator on an audit with an empty result Make sure that the operator returns an empty result if the audit has relations with several different assessments. """ with factories.single_commit(): audit = factories.AuditFactory() issue = factories.IssueFactory() assessment = factories.AssessmentFactory() factories.RelationshipFactory(source=assessment, destination=audit) rel_assmt_issue = factories.RelationshipFactory(source=assessment, destination=issue) factories.AutomappingFactory(parent=rel_assmt_issue) other_assessment = factories.AssessmentFactory() factories.RelationshipFactory(source=audit, destination=other_assessment) issue_id = issue.id assmt_id = assessment.id resp = self._perform_query(issue_id, assmt_id) self.assert200(resp) self.assertEqual(len(resp.json), 1) self.assertEqual(resp.json[0]["Audit"]["count"], 0)
def test_audit_ok(self): """ Test an operator on an audit with a non-empty result Make sure that the operator returns an audit object if the audit has relations with an issue and an assessment, and doesn't have relations with other assessments. """ with factories.single_commit(): audit = factories.AuditFactory() issue = factories.IssueFactory() assessment = factories.AssessmentFactory() factories.RelationshipFactory(source=assessment, destination=audit) rel_assmt_issue = factories.RelationshipFactory(source=assessment, destination=issue) factories.AutomappingFactory(parent=rel_assmt_issue) audit_id = audit.id issue_id = issue.id assmt_id = assessment.id resp = self._perform_query(issue_id, assmt_id) self.assert200(resp) self.assertEqual(len(resp.json), 1) self.assertEqual(resp.json[0]["Audit"]["count"], 1) self.assertEqual(audit_id, resp.json[0]["Audit"]["values"][0]["id"])
def _get_import_data(model1, model2, unmap): """Returns data for csv import of the respective objects""" name1 = model1.__name__ name2 = model2.__name__ title1 = utils.title_from_camelcase(name1) with factories.single_commit(): with mock.patch('ggrc.models.relationship.is_external_app_user', return_value=True): obj1 = factories.get_model_factory(name1)() obj2 = factories.get_model_factory(name2)() if unmap: factories.RelationshipFactory(source=obj1, destination=obj2, is_external=True) slug1 = obj1.slug slug2 = obj2.slug data_block = [ collections.OrderedDict([ ("object_type", name1), ("Code*", slug1), ]), collections.OrderedDict([ ("object_type", name2), ("Code*", slug2), ("{}:{}".format("unmap" if unmap else "map", title1), slug1), ]), ] return data_block
def test_system_unmap_on_update(self, readonly, rel_obj_type): """Test unmapping on update System with readonly={0}""" rel_factory = factories.get_model_factory(rel_obj_type) with factories.single_commit(): obj = factories.SystemFactory(title='a', readonly=readonly) obj_id = obj.id rel_obj = rel_factory() rel_obj_id = rel_obj.id factories.RelationshipFactory(source=obj, destination=rel_obj) data = OrderedDict([ ("object_type", "System"), ("Code*", obj.slug), ("unmap:{}".format(rel_obj_type), rel_obj.slug), ]) response = self.import_data(data) self._check_csv_response(response, {}) obj = get_model("System").query.get(obj_id) rel_obj = get_model(rel_obj_type).query.get(rel_obj_id) rel = all_models.Relationship.find_related(obj, rel_obj) self.assertIsNone(rel)
def test_relationship_delete(self, obj_type, readonly, rel_obj_type, swap, expected_code): """Test DELETE relationship {0}.readonly={1}, related object type {2}""" factory = factories.get_model_factory(obj_type) rel_factory = factories.get_model_factory(rel_obj_type) with factories.single_commit(): obj = factory(title='a', readonly=readonly) rel_obj = rel_factory() if swap: source, destination = rel_obj, obj else: source, destination = obj, rel_obj robj = factories.RelationshipFactory(source=source, destination=destination) resp = self.object_generator.api.delete(robj) self.assertStatus(resp, expected_code)
def test_unmap_objects(self, model1, model2): """Test deprecated unmapping between {0.__name__} and {1.__name__} """ name1 = model1.__name__ name2 = model2.__name__ title1 = title_from_camelcase(name1) with factories.single_commit(): with mock.patch('ggrc.models.relationship.is_external_app_user', return_value=True): obj1 = factories.get_model_factory(name1)() obj2 = factories.get_model_factory(name2)() factories.RelationshipFactory(source=obj1, destination=obj2, is_external=True) slug1 = obj1.slug slug2 = obj2.slug data_block = [ OrderedDict([ ("object_type", name1), ("Code*", slug1), ]), OrderedDict([ ("object_type", name2), ("Code*", slug2), ("unmap:{}".format(title1), slug1), ]), ] response = self.import_data(*data_block) # Check that mapping is not added self.assertEqual(len(response[1]['row_warnings']), 1) self.assertIn( u'Line 7: You do not have the necessary permissions to unmap', response[1]['row_warnings'][0])