Exemplo n.º 1
0
    def test_editing_view_retrieve_data(self):
        """Test constraint filter for editing API - SELECT"""

        client = APIClient()
        editing_layer = Layer.objects.get(name='editing_layer')
        self.assertTrue(
            client.login(username=self.test_user2.username,
                         password=self.test_user2.username))
        assign_perm('change_layer', self.test_user2, editing_layer)
        self.assertTrue(
            self.test_user2.has_perm('qdjango.change_layer', editing_layer))
        response = client.post(
            '/vector/api/editing/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id), {},
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        fids = [int(f['id']) for f in jcontent['vector']['data']['features']]
        # All fids should be here
        self.assertEqual(fids, [1, 2, 3, 4])

        # Now add a constraint for user2
        constraint_layer = Layer.objects.get(name=self.constraint_layer_name)
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer,
                                   for_editing=True)
        constraint.save()
        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user2,
                                 rule='name=\'bagnolo\'')
        rule.save()
        response = client.post(
            '/vector/api/editing/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id), {},
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        fids = [int(f['id']) for f in jcontent['vector']['data']['features']]
        # Only allowed fids
        self.assertEqual(fids, [1, 2])

        # Test with inactive constraint
        constraint.active = False
        constraint.save()
        response = client.post(
            '/vector/api/editing/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id), {},
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        fids = [int(f['id']) for f in jcontent['vector']['data']['features']]
        # All fids should be here
        self.assertEqual(fids, [1, 2, 3, 4])

        # reset test db
        self.reset_db_data()
Exemplo n.º 2
0
    def test_unique(self):
        """Check unique together conditions"""

        editing_layer = Layer.objects.get(name='editing_layer')
        constraint_layer = Layer.objects.get(name=self.constraint_layer_name)
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer)
        constraint.save()

        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user1,
                                 rule='int_f=1')
        rule.save()

        # Check unique_together
        with transaction.atomic():
            with self.assertRaises(IntegrityError) as ex:
                rule_duplicate = GeoConstraintRule(constraint=constraint,
                                                   user=self.test_user1,
                                                   rule='int_f=1')
                rule_duplicate.save()

        rule3 = GeoConstraintRule(constraint=constraint,
                                  group=self.group,
                                  rule='int_f=1')
        rule3.save()
        with transaction.atomic():
            with self.assertRaises(IntegrityError) as ex:
                rule3_duplicate = GeoConstraintRule(constraint=constraint,
                                                    group=self.group,
                                                    rule='int_f=1')
                rule3_duplicate.save()
Exemplo n.º 3
0
    def test_editing_view_insert_data(self):
        """Test constraint filter for editing API - INSERT"""

        client = APIClient()
        editing_layer = Layer.objects.get(name='editing_layer')
        self.assertTrue(
            client.login(username=self.test_user2.username,
                         password=self.test_user2.username))
        assign_perm('change_layer', self.test_user2, editing_layer)
        self.assertTrue(
            self.test_user2.has_perm('qdjango.change_layer', editing_layer))

        # Now add a constraint for user2: strict for editing
        constraint_layer = Layer.objects.get(name=self.constraint_layer_name)
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer,
                                   for_editing=True,
                                   for_view=False)
        constraint.save()
        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user2,
                                 rule='name=\'bagnolo\'')
        rule.save()

        # Retrieve the data
        response = client.post(
            '/vector/api/editing/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id), {},
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        fids = [int(f['id']) for f in jcontent['vector']['data']['features']]
        # All fids should be here
        self.assertEqual(fids, [1, 2])

        # Get lock id for fid 1
        lock_id = [
            l['lockid'] for l in jcontent['featurelocks']
            if l['featureid'] == '1'
        ][0]

        # Add the geometry outside the allowed rule
        new_geom = [10, 55]
        payload = {
            "add": [{
                "geometry": {
                    "coordinates": new_geom,
                    "type": "Point"
                },
                "id": "_new_1564320704661",
                "properties": {
                    "name": "constraint violation"
                },
                "type": "Feature"
            }],
            "delete": [],
            "lockids": [{
                "featureid": "1",
                "lockid": "%s" % lock_id
            }],
            "relations": {},
            "update": []
        }

        # Verify that the update has failed
        response = client.post(
            '/vector/api/commit/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id),
            payload,
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        self.assertEqual(
            jcontent["errors"],
            "Constraint validation failed for geometry: POINT (10 55)")

        # Test with inactive constraint
        constraint.active = False
        constraint.save()
        # Add the geometry outside the allowed rule
        new_geom = [10, 55]
        payload = {
            "add": [{
                "geometry": {
                    "coordinates": new_geom,
                    "type": "Point"
                },
                "id": "_new_1564320704661",
                "properties": {
                    "name": "constraint violation"
                },
                "type": "Feature"
            }],
            "delete": [],
            "lockids": [{
                "featureid": "1",
                "lockid": "%s" % lock_id
            }],
            "relations": {},
            "update": []
        }

        # Verify that the update was successful
        response = client.post(
            '/vector/api/commit/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id),
            payload,
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        new_fid = int(jcontent['response']['new'][0]['id'])
        self.assertTrue(new_fid > 0)
        # Retrieve the data
        response = client.post(
            '/vector/api/editing/qdjango/%s/%s/' %
            (editing_layer.project_id, editing_layer.qgs_layer_id), {},
            format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        geom = jcontent['vector']['data']['features'][-1]['geometry'][
            'coordinates']
        self.assertEqual(geom, [10, 55])
        self.assertEqual(jcontent['vector']['data']['features'][-1]['id'],
                         str(new_fid))

        # reset test db
        self.reset_db_data()
Exemplo n.º 4
0
    def test_create_constraint(self):
        """Test constraints creation"""

        editing_layer = Layer.objects.get(name='editing_layer')
        constraint_layer = Layer.objects.get(name=self.constraint_layer_name)
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer)
        # Test validation
        constraint.clean()
        constraint.save()

        # Check layer types (PG or SL)
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=editing_layer,
                          constraint_layer=Layer(layer_type='GDAL')).clean()

        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=Layer(layer_type='GDAL'),
                          constraint_layer=constraint_layer).clean()

        # Check if constraints layer is polygon
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=constraint_layer,
                          constraint_layer=editing_layer).clean()

        # Check self constraint
        with self.assertRaises(ValidationError) as ex:
            GeoConstraint(layer=constraint_layer,
                          constraint_layer=constraint_layer).clean()

        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user1,
                                 rule='int_f=1')
        rule.save()

        # Test validation
        with self.assertRaises(ValidationError) as ex:
            rule2 = GeoConstraintRule(constraint=constraint,
                                      user=self.test_user1,
                                      group=self.group,
                                      rule='int_f=1')
            rule2.clean()

        # Test constraints for user
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user1, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule)

        # Test the other path with group
        rule3 = GeoConstraintRule(constraint=constraint,
                                  group=self.group,
                                  rule='int_f=1')
        rule3.save()
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule3)

        # Test we need a user OR a group
        with self.assertRaises(ValidationError) as ex:
            rule4 = GeoConstraintRule(constraint=constraint, rule='int_f=1')
            rule4.clean()

        # Test we get nothing for the other layer and user
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user2, constraint_layer)
        self.assertEqual(len(rules), 0)

        # Test inactive constraints for user
        constraint.active = False
        constraint.save()
        rules = GeoConstraintRule.get_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], rule3)
        rules = GeoConstraintRule.get_active_constraints_for_user(
            self.test_user3, editing_layer)
        self.assertEqual(len(rules), 0)
Exemplo n.º 5
0
    def test_initconfig_plugin_start(self):
        """ Test initconfig api"""

        # activate editing plugins: set editing_layer as editing layer
        G3WEditingLayer.objects.create(app_name='qdjango',
                                       layer_id=self.editing_layer.pk)

        # api client instance
        client = APIClient()
        self.assertTrue(
            client.login(username=self.test_user_admin1.username,
                         password=self.test_user_admin1.username))

        url = reverse('group-map-config',
                      args=[
                          self.project_group.slug, 'qdjango',
                          self.project.instance.pk
                      ])

        response = client.get(url)
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)

        # check editing into plugins section
        self.assertTrue('editing' in jcontent['group']['plugins'])

        plugin = jcontent['group']['plugins']['editing']

        # check gid and TYPES
        self.assertEqual(plugin['gid'],
                         'qdjango:{}'.format(self.project.instance.pk))

        client.logout()

        # check for constraint
        # ===================================

        # add constraints
        editing_layer = Layer.objects.get(name='editing_layer')
        constraint_layer = Layer.objects.get(name='constraint_layer')

        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer)
        constraint.save()

        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user3,
                                 rule='name=\'bagnolo\'')
        rule.save()

        self.assertTrue(
            client.login(username=self.test_user3.username,
                         password=self.test_user3.username))

        response = client.get(url)
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)

        # check archiweb into plugins section
        self.assertTrue('editing' in jcontent['group']['plugins'])

        plugin = jcontent['group']['plugins']['editing']

        # check gid and TYPES
        self.assertEqual(plugin['gid'],
                         'qdjango:{}'.format(self.project.instance.pk))

        self.assertTrue('constraints' in plugin)
        self.assertEqual(
            plugin['constraints'][editing_layer.qgs_layer_id]
            ['geometry_api_url'],
            reverse('geoconstraint-api-geometry',
                    kwargs={'layer_id': editing_layer.pk}))

        constraint.delete()
Exemplo n.º 6
0
    def test_constraintrule_api_permissions(self):
        """Test API constraint rule permissions"""

        client = APIClient()
        self.assertTrue(
            client.login(username=self.test_user1.username,
                         password=self.test_user1.username))

        editing_layer = Layer.objects.get(name='editing_layer')
        constraint_layer = Layer.objects.get(name='constraint_layer')
        constraint = GeoConstraint(layer=editing_layer,
                                   constraint_layer=constraint_layer)
        constraint.save()

        rule = GeoConstraintRule(constraint=constraint,
                                 user=self.test_user3,
                                 group=None,
                                 rule="name='pinerolo'")
        rule.save()

        # check 403 for rule list by constraint
        url = reverse('geoconstraintrule-api-filter-by-constraint',
                      kwargs={'constraint_id': constraint.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 403)

        # check 403 for rule list by editing layer
        url = reverse('geoconstraintrule-api-filter-by-layer',
                      kwargs={'layer_id': editing_layer.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 403)

        # check 403 for rule list by rule user
        url = reverse('geoconstraintrule-api-filter-by-user',
                      kwargs={'user_id': self.test_user3.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 403)

        # check 403 for rule detail
        url = reverse('geoconstraintrule-api-detail', kwargs={'pk': rule.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 403)

        # check pass for rule list by constraint
        client.logout()
        self.assertTrue(
            client.login(username=self.test_user2.username,
                         password=self.test_user2.username))

        url = reverse('geoconstraintrule-api-filter-by-constraint',
                      kwargs={'constraint_id': constraint.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 200)

        # check 200 for rule list by editing layer
        url = reverse('geoconstraintrule-api-filter-by-layer',
                      kwargs={'layer_id': editing_layer.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 200)

        # check 403 for rule list by rule user (only admin can query rules by user)
        url = reverse('geoconstraintrule-api-filter-by-user',
                      kwargs={'user_id': self.test_user3.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 403)

        # check 200 for rule detail
        url = reverse('geoconstraintrule-api-detail', kwargs={'pk': rule.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 200)

        client.logout()

        self.assertTrue(
            client.login(username=self.test_user3.username,
                         password=self.test_user3.username))
        # Test get Geometries constraint for request user
        url = reverse('geoconstraint-api-geometry',
                      kwargs={'layer_id': editing_layer.pk})
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        self.assertTrue(len(jcontent['geometries']) == 1)
        self.assertEqual(jcontent['geometries'][0]['type'], 'MultiPolygon')

        client.logout()
        self.assertTrue(
            client.login(username=self.test_user_admin1.username,
                         password=self.test_user_admin1.username))
        response = client.get(url, {}, format='json')
        self.assertEqual(response.status_code, 200)
        jcontent = json.loads(response.content)
        self.assertTrue(len(jcontent['geometries']) == 0)