示例#1
0
 def setUp(self):
     create_models(b'dataset')
     create_models(b'map')
     create_models(b'document')
     User = get_user_model()
     self.user = User.objects.create(username='******', email='*****@*****.**')
     self.rm = ResourceManager()
示例#2
0
 def test_get_concrete_manager(self):
     original_r_m_c_c = rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS
     # mock class
     rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS = 'geonode.resource.tests.ResourceManagerClassTest'
     rm = ResourceManager()
     self.assertEqual(rm._concrete_resource_manager.__class__.__name__, 'ResourceManagerClassTest')
     # re assign class to original
     rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS = original_r_m_c_c
示例#3
0
    def setUpClass(cls):
        super().setUpClass()
        cls.rm = ResourceManager()
        cls.user_admin = get_user_model().objects.get(username="******")

        if check_ogc_backend(geoserver.BACKEND_PACKAGE):
            cls.dataset_coast_line = create_single_dataset(
                'san_andres_y_providencia_coastline')
            cls.dataset_highway = create_single_dataset(
                'san_andres_y_providencia_highway')

            # create a map from loaded layers
            admin_user = get_user_model().objects.get(username="******")
            cls.map_composition = Map.objects.create(
                title="composition",
                abstract="abstract",
                owner=admin_user,
            )
            cls.map_composition.id
            MapLayer.objects.create(
                map=cls.map_composition,
                extra_params={},
                name="geonode:san_andres_y_providencia_coastline",
                store=None,
                current_style=None,
                ows_url=None,
                local=True)
            MapLayer.objects.create(
                map=cls.map_composition,
                extra_params={},
                name="geonode:san_andres_y_providencia_highway",
                store=None,
                current_style=None,
                ows_url=None,
                local=True)
            # update MapLayers to correctly show layers' location
            with DisableDjangoSignals():
                for maplayer in cls.map_composition.maplayers.iterator():
                    if maplayer.name in [
                            cls.dataset_coast_line.alternate,
                            cls.dataset_highway.alternate
                    ]:
                        maplayer.local = True
                        maplayer.save(force_update=True)
                        maplayer.refresh_from_db()

            cls.map_composition.refresh_from_db()
示例#4
0
class TestResourceManager(GeoNodeBaseTestSupport):
    def setUp(self):
        create_models(b'dataset')
        create_models(b'map')
        create_models(b'document')
        User = get_user_model()
        self.user = User.objects.create(username='******', email='*****@*****.**')
        self.rm = ResourceManager()

    def test_get_concrete_manager(self):
        original_r_m_c_c = rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS
        # mock class
        rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS = 'geonode.resource.tests.ResourceManagerClassTest'
        rm = ResourceManager()
        self.assertEqual(rm._concrete_resource_manager.__class__.__name__, 'ResourceManagerClassTest')
        # re assign class to original
        rm_settings.RESOURCE_MANAGER_CONCRETE_CLASS = original_r_m_c_c

    def test__get_instance(self):
        # test with invalid object
        self.assertIsNone(self.rm._get_instance('invalid_uuid'))
        # Test with valid object
        self.assertIsNotNone(self.rm._get_instance(ResourceBase.objects.first().uuid))

    def test_search(self):
        # test with no specific resource_type provided
        result = self.rm.search({"title__icontains": "ipsum", "abstract__icontains": "ipsum"}, resource_type=None)
        self.assertEqual(result.count(), 3)
        # test with specific resource_type
        result = self.rm.search({"title__icontains": "ipsum", "abstract__icontains": "ipsum"}, resource_type=Document)
        self.assertEqual(result.count(), 2)

    def test_exists(self):
        dt = Dataset.objects.filter(uuid__isnull=False).exclude(uuid='').first()
        self.assertFalse(self.rm.exists('invalid_uuid'))
        self.assertTrue(self.rm.exists(dt.uuid))
        self.assertTrue(self.rm.exists("invalid_id", instance=dt))

    def test_delete(self):
        doc = create_single_doc("test_delete_doc")
        dt = create_single_dataset("test_delete_dataset")
        map = create_single_map("test_delete_dataset")
        Service.objects.create(
            base_url="http://fake_test",
            owner=self.user)

        # Add dataset to a map
        MapLayer.objects.create(map=map, name=dt.alternate, stack_order=1).save()
        # Create the rating for dataset
        OverallRating.objects.create(
            category=2,
            object_id=dt.id,
            content_type=ContentType.objects.get(model='dataset'),
            rating=3)
        create_dataset_data(dt.resourcebase_ptr_id)
        res = self.rm.delete(doc.uuid, instance=doc)
        self.assertTrue(res)
        # Before dataset delete
        self.assertEqual(MapLayer.objects.filter(name='geonode:test_delete_dataset').count(), 1)
        self.assertEqual(OverallRating.objects.filter(object_id=dt.id).count(), 1)
        # try deleting with no default style, havest_job withalternate as resource_id, with uploads
        # TODO
        res = self.rm.delete(dt.uuid, instance=dt)
        self.assertTrue(res)

        # After dataset delete
        self.assertEqual(MapLayer.objects.filter(name='geonode:test_delete_dataset').count(), 0)
        self.assertEqual(OverallRating.objects.filter(object_id=dt.id).count(), 0)

    def test_create(self):
        dt = Dataset.objects.filter(uuid__isnull=False).exclude(uuid='').first()
        dataset_defaults = {"owner": self.user, "title": "test_create_dataset"}
        res = self.rm.create(dt.uuid, resource_type=Dataset)
        new_uuid = str(uuid1())
        res = self.rm.create(new_uuid, resource_type=Dataset, defaults=dataset_defaults)
        self.assertEqual(res, Dataset.objects.get(uuid=new_uuid))

    def test_update(self):
        dt = create_single_dataset("test_update_dataset")
        vals = {
            "name": "new_name_test_update_dataset"
        }
        res = self.rm.update(dt.uuid, vals=vals, keywords=["testing"], regions=["not_known", "Africa"])
        self.assertIn("Africa", res.regions.values_list("name", flat=True))
        self.assertTrue(all(x in res.keywords.values_list("name", flat=True) for x in ["testing", "not_known"]))
        self.assertEqual(res.name, vals["name"])
        # TODO test metadatauploaded and xml file

    def test_ingest(self):
        dt_files = [os.path.join(GOOD_DATA, 'raster', 'relief_san_andres.tif')]
        defaults = {"owner": self.user}
        # raises an exception if resource_type is not provided
        self.rm.ingest(dt_files)
        # ingest with documents
        res = self.rm.ingest(dt_files, resource_type=Document, defaults=defaults)
        self.assertTrue(isinstance(res, Document))
        # ingest with datasets
        res = self.rm.ingest(dt_files, resource_type=Dataset, defaults=defaults)
        self.assertTrue(isinstance(res, Dataset))

    def test_copy(self):
        dt = create_single_dataset("test_copy_dataset")
        # test with no reference object provided
        self.assertIsNone(self.rm.copy(None))
        res = self.rm.copy(dt)
        self.assertEqual(res.perms, dt.perms)

    @patch.object(ResourceManager, '_validate_resource')
    def test_append(self, mock_validator):
        mock_validator.return_value = True
        dt = create_single_dataset("test_append_dataset")
        # Before append
        self.assertEqual(dt.name, "test_append_dataset")
        # After append
        self.rm.append(dt, vals={"name": "new_name_test_append_dataset"})
        self.assertEqual(dt.name, "new_name_test_append_dataset")
        # test with failing validator
        mock_validator.return_value = False
        self.rm.append(dt, vals={"name": "new_name2"})
        self.assertEqual(dt.name, "new_name_test_append_dataset")

    @patch.object(ResourceManager, '_validate_resource')
    def test_replace(self, mock_validator):
        dt = create_single_dataset("test_replace_dataset")
        mock_validator.return_value = True
        self.rm.replace(dt, vals={"name": "new_name_test_replace_dataset"})
        self.assertEqual(dt.name, "new_name_test_replace_dataset")
        # test with failing validator
        mock_validator.return_value = False
        self.rm.replace(dt, vals={"name": "new_name2"})
        self.assertEqual(dt.name, "new_name_test_replace_dataset")

    def test_validate_resource(self):
        doc = create_single_doc("test_delete_doc")
        dt = create_single_dataset("test_delete_dataset")
        map = create_single_map("test_delete_dataset")
        with self.assertRaises(Exception):
            # append is for only datasets
            self.rm._validate_resource(doc, action_type="append")
        self.assertTrue(self.rm._validate_resource(doc, action_type="replace"))
        self.assertTrue(self.rm._validate_resource(dt, action_type="replace"))
        self.assertTrue(self.rm._validate_resource(map, action_type="replace"))
        with self.assertRaises(ObjectDoesNotExist):
            # TODO In function rais this only when object is not found
            self.rm._validate_resource(dt, action_type="invalid")

    def test_exec(self):
        map = create_single_map("test_exec_map")
        self.assertIsNone(self.rm.exec("set_style", None, instance=None))
        self.assertEqual(self.rm.exec("set_style", map.uuid, instance=map), map)

    def test_remove_permissions(self):
        with self.settings(DEFAULT_ANONYMOUS_VIEW_PERMISSION=True):
            dt = create_single_dataset("test_dataset")
            map = create_single_map("test_exec_map")
            self.assertFalse(self.rm.remove_permissions("invalid", instance=None))
            self.assertTrue(self.rm.remove_permissions(map.uuid, instance=map))
            self.assertTrue(self.rm.remove_permissions(dt.uuid, instance=dt))

    def test_set_permissions(self):
        norman = get_user_model().objects.get(username="******")
        anonymous = get_user_model().objects.get(username="******")
        dt = create_single_dataset("test_perms_dataset")
        public_group, _public_created = GroupProfile.objects.get_or_create(
            slug='public_group',
            title='public_group',
            access='public')
        private_group, _private_created = GroupProfile.objects.get_or_create(
            slug='private_group',
            title='private_group',
            access='private')

        perm_spec = {
            "users": {
                "AnonymousUser": ['change_dataset_style', 'view_resourcebase'],
                "norman": ['view_resourcebase', 'change_dataset_style'],
            },
            "groups": {
                "public_group": ['view_resourcebase'],
                "private_group": ['view_resourcebase', 'change_resourcebase']
            }
        }
        self.assertTrue(self.rm.set_permissions(dt.uuid, instance=dt, permissions=perm_spec))
        self.assertFalse(self.rm.set_permissions("invalid_uuid", instance=None, permissions=perm_spec))
        # Test permissions assigned
        self.assertTrue(norman.has_perm('change_dataset_style', dt))
        self.assertFalse(norman.has_perm('change_resourcebase', dt))
        # Test with no specified permissions
        with patch('geonode.security.utils.skip_registered_members_common_group') as mock_v:
            mock_v.return_value = True
            with self.settings(DEFAULT_ANONYMOUS_DOWNLOAD_PERMISSION=False, DEFAULT_ANONYMOUS_VIEW_PERMISSION=False):
                self.assertTrue(self.rm.remove_permissions(dt.uuid, instance=dt))
                self.assertFalse(anonymous.has_perm('view_resourcebase', dt))
                self.assertFalse(anonymous.has_perm('download_resourcebase', dt))

    def test_set_workflow_permissions(self):
        dt = create_single_dataset("test_workflow_dataset")

        self.assertFalse(self.rm.set_workflow_permissions('invalid_uuid', instance=None))
        self.assertTrue(self.rm.set_workflow_permissions(dt.uuid, instance=dt, approved=True, published=True))

    def test_set_thumbnail(self):
        doc = create_single_doc("test_thumb_doc")
        dt = create_single_dataset("test_thumb_dataset")
        self.assertFalse(self.rm.set_thumbnail("invalid_uuid"))
        self.assertTrue(self.rm.set_thumbnail(dt.uuid, instance=dt))
        self.assertTrue(self.rm.set_thumbnail(doc.uuid, instance=doc))
示例#5
0
文件: manager.py 项目: pasing/geonode
        _resource = resource_type.objects.get(uuid=uuid)
        if resource_type == Dataset:
            _synced_resource = sync_instance_with_geoserver(_resource.id)
            _resource = _synced_resource or _resource
        return _resource

    def update(self, uuid: str, /, instance: ResourceBase = None, xml_file: str = None, metadata_uploaded: bool = False,
               vals: dict = {}, regions: dict = {}, keywords: dict = {}, custom: dict = {}, notify: bool = True) -> ResourceBase:
        if instance:
            if isinstance(instance.get_real_instance(), Dataset):
                _synced_resource = sync_instance_with_geoserver(instance.id)
                instance = _synced_resource or instance
        return instance

    def ingest(self, files: typing.List[str], /, uuid: str = None, resource_type: typing.Optional[object] = None, defaults: dict = {}, **kwargs) -> ResourceBase:
        instance = ResourceManager._get_instance(uuid)
        if instance and isinstance(instance.get_real_instance(), Dataset):
            instance = self.import_dataset(
                'import_dataset',
                instance.uuid,
                instance=instance,
                files=files,
                user=defaults.get('user', instance.owner),
                defaults=defaults,
                action_type='create',
                **kwargs)
        return instance

    def copy(self, instance: ResourceBase, /, uuid: str = None, owner: settings.AUTH_USER_MODEL = None, defaults: dict = {}) -> ResourceBase:
        if uuid and instance:
            _resource = ResourceManager._get_instance(uuid)
示例#6
0
               keywords: dict = {},
               custom: dict = {},
               notify: bool = True) -> ResourceBase:
        if instance:
            if isinstance(instance.get_real_instance(), Dataset):
                return sync_instance_with_geoserver(instance.id)
        return instance

    def ingest(self,
               files: typing.List[str],
               /,
               uuid: str = None,
               resource_type: typing.Optional[object] = None,
               defaults: dict = {},
               **kwargs) -> ResourceBase:
        instance = ResourceManager._get_instance(uuid)
        if instance and isinstance(instance.get_real_instance(), Dataset):
            instance = self.import_dataset('import_dataset',
                                           instance.uuid,
                                           instance=instance,
                                           files=files,
                                           user=defaults.get(
                                               'user', instance.owner),
                                           defaults=defaults,
                                           action_type='create',
                                           importer_session_opts=kwargs)
        return instance

    def copy(self,
             instance: ResourceBase,
             /,