• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-engine / geoengine / 13054453594

30 Jan 2025 02:25PM UTC coverage: 90.025% (-0.002%) from 90.027%
13054453594

Pull #1014

github

web-flow
Merge 0524ef02f into 2efb42db2
Pull Request #1014: verify expression deps

125594 of 139510 relevant lines covered (90.03%)

57696.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.83
/services/src/contexts/postgres.rs
1
use self::migrations::all_migrations;
2
use crate::api::model::services::Volume;
3
use crate::config::{get_config_element, Cache, Oidc, Quota};
4
use crate::contexts::{
5
    initialize_database, migrations, ApplicationContext, CurrentSchemaMigration, MigrationResult,
6
    QueryContextImpl, SessionId,
7
};
8
use crate::contexts::{ExecutionContextImpl, QuotaCheckerImpl};
9
use crate::contexts::{GeoEngineDb, SessionContext};
10
use crate::datasets::upload::Volumes;
11
use crate::datasets::DatasetName;
12
use crate::error::{self, Error, Result};
13
use crate::layers::add_from_directory::{
14
    add_datasets_from_directory, add_layer_collections_from_directory, add_layers_from_directory,
15
    add_providers_from_directory,
16
};
17
use crate::machine_learning::error::MachineLearningError;
18
use crate::machine_learning::name::MlModelName;
19
use crate::quota::{initialize_quota_tracking, QuotaTrackingFactory};
20
use crate::tasks::SimpleTaskManagerContext;
21
use crate::tasks::{TypedTaskManagerBackend, UserTaskManager};
22
use crate::users::OidcManager;
23
use crate::users::{UserAuth, UserSession};
24
use async_trait::async_trait;
25
use bb8_postgres::{
26
    bb8::Pool,
27
    bb8::PooledConnection,
28
    tokio_postgres::{tls::MakeTlsConnect, tls::TlsConnect, Config, Socket},
29
    PostgresConnectionManager,
30
};
31
use geoengine_datatypes::raster::TilingSpecification;
32
use geoengine_datatypes::util::test::TestDefault;
33
use geoengine_operators::cache::shared_cache::SharedCache;
34
use geoengine_operators::engine::ChunkByteSize;
35
use geoengine_operators::meta::quota::QuotaChecker;
36
use geoengine_operators::util::create_rayon_thread_pool;
37
use log::info;
38
use rayon::ThreadPool;
39
use snafu::ResultExt;
40
use std::path::PathBuf;
41
use std::sync::Arc;
42
use tokio_postgres::error::SqlState;
43
use uuid::Uuid;
44

45
// TODO: do not report postgres error details to user
46

47
/// A contex with references to Postgres backends of the dbs. Automatically migrates schema on instantiation
48
#[derive(Clone)]
49
pub struct PostgresContext<Tls>
50
where
51
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
52
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
53
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
54
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
55
{
56
    thread_pool: Arc<ThreadPool>,
57
    exe_ctx_tiling_spec: TilingSpecification,
58
    query_ctx_chunk_size: ChunkByteSize,
59
    task_manager: Arc<TypedTaskManagerBackend>,
60
    oidc_manager: OidcManager,
61
    quota: QuotaTrackingFactory,
62
    pub(crate) pool: Pool<PostgresConnectionManager<Tls>>,
63
    volumes: Volumes,
64
    tile_cache: Arc<SharedCache>,
65
}
66

67
impl<Tls> PostgresContext<Tls>
68
where
69
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
70
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
71
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
72
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
73
{
74
    pub async fn new_with_context_spec(
298✔
75
        config: Config,
298✔
76
        tls: Tls,
298✔
77
        exe_ctx_tiling_spec: TilingSpecification,
298✔
78
        query_ctx_chunk_size: ChunkByteSize,
298✔
79
        quota_config: Quota,
298✔
80
        oidc_db: OidcManager,
298✔
81
    ) -> Result<Self> {
298✔
82
        let pg_mgr = PostgresConnectionManager::new(config, tls);
298✔
83

84
        let pool = Pool::builder().build(pg_mgr).await?;
298✔
85

86
        Self::create_pro_database(pool.get().await?).await?;
298✔
87

88
        let db = PostgresDb::new(pool.clone(), UserSession::admin_session());
298✔
89
        let quota = initialize_quota_tracking(
298✔
90
            quota_config.mode,
298✔
91
            db,
298✔
92
            quota_config.increment_quota_buffer_size,
298✔
93
            quota_config.increment_quota_buffer_timeout_seconds,
298✔
94
        );
298✔
95

298✔
96
        Ok(PostgresContext {
298✔
97
            task_manager: Default::default(),
298✔
98
            thread_pool: create_rayon_thread_pool(0),
298✔
99
            exe_ctx_tiling_spec,
298✔
100
            query_ctx_chunk_size,
298✔
101
            oidc_manager: oidc_db,
298✔
102
            quota,
298✔
103
            pool,
298✔
104
            volumes: Default::default(),
298✔
105
            tile_cache: Arc::new(SharedCache::test_default()),
298✔
106
        })
298✔
107
    }
298✔
108

109
    #[allow(clippy::missing_panics_doc)]
110
    pub async fn new_with_oidc(
×
111
        config: Config,
×
112
        tls: Tls,
×
113
        oidc_db: OidcManager,
×
114
        cache_config: Cache,
×
115
        quota_config: Quota,
×
116
    ) -> Result<Self> {
×
117
        let pg_mgr = PostgresConnectionManager::new(config, tls);
×
118

119
        let pool = Pool::builder().build(pg_mgr).await?;
×
120

121
        Self::create_pro_database(pool.get().await?).await?;
×
122

123
        let db = PostgresDb::new(pool.clone(), UserSession::admin_session());
×
124
        let quota = initialize_quota_tracking(
×
125
            quota_config.mode,
×
126
            db,
×
127
            quota_config.increment_quota_buffer_size,
×
128
            quota_config.increment_quota_buffer_timeout_seconds,
×
129
        );
×
130

×
131
        Ok(PostgresContext {
×
132
            task_manager: Default::default(),
×
133
            thread_pool: create_rayon_thread_pool(0),
×
134
            exe_ctx_tiling_spec: TestDefault::test_default(),
×
135
            query_ctx_chunk_size: TestDefault::test_default(),
×
136
            oidc_manager: oidc_db,
×
137
            quota,
×
138
            pool,
×
139
            volumes: Default::default(),
×
140
            tile_cache: Arc::new(
×
141
                SharedCache::new(cache_config.size_in_mb, cache_config.landing_zone_ratio)
×
142
                    .expect("tile cache creation should work because the config is valid"),
×
143
            ),
×
144
        })
×
145
    }
×
146

147
    // TODO: check if the datasets exist already and don't output warnings when skipping them
148
    #[allow(clippy::too_many_arguments, clippy::missing_panics_doc)]
149
    pub async fn new_with_data(
×
150
        config: Config,
×
151
        tls: Tls,
×
152
        dataset_defs_path: PathBuf,
×
153
        provider_defs_path: PathBuf,
×
154
        layer_defs_path: PathBuf,
×
155
        layer_collection_defs_path: PathBuf,
×
156
        exe_ctx_tiling_spec: TilingSpecification,
×
157
        query_ctx_chunk_size: ChunkByteSize,
×
158
        oidc_config: Oidc,
×
159
        cache_config: Cache,
×
160
        quota_config: Quota,
×
161
    ) -> Result<Self> {
×
162
        let pg_mgr = PostgresConnectionManager::new(config, tls);
×
163

164
        let pool = Pool::builder().build(pg_mgr).await?;
×
165

166
        let created_schema = Self::create_pro_database(pool.get().await?).await?;
×
167

168
        let db = PostgresDb::new(pool.clone(), UserSession::admin_session());
×
169
        let quota = initialize_quota_tracking(
×
170
            quota_config.mode,
×
171
            db,
×
172
            quota_config.increment_quota_buffer_size,
×
173
            quota_config.increment_quota_buffer_timeout_seconds,
×
174
        );
×
175

×
176
        let app_ctx = PostgresContext {
×
177
            task_manager: Default::default(),
×
178
            thread_pool: create_rayon_thread_pool(0),
×
179
            exe_ctx_tiling_spec,
×
180
            query_ctx_chunk_size,
×
181
            oidc_manager: OidcManager::from(oidc_config),
×
182
            quota,
×
183
            pool,
×
184
            volumes: Default::default(),
×
185
            tile_cache: Arc::new(
×
186
                SharedCache::new(cache_config.size_in_mb, cache_config.landing_zone_ratio)
×
187
                    .expect("tile cache creation should work because the config is valid"),
×
188
            ),
×
189
        };
×
190

×
191
        if created_schema {
×
192
            info!("Populating database with initial data...");
×
193

194
            let mut db = app_ctx.session_context(UserSession::admin_session()).db();
×
195

×
196
            add_layers_from_directory(&mut db, layer_defs_path).await;
×
197
            add_layer_collections_from_directory(&mut db, layer_collection_defs_path).await;
×
198

199
            add_datasets_from_directory(&mut db, dataset_defs_path).await;
×
200

201
            add_providers_from_directory(&mut db, provider_defs_path.clone()).await;
×
202
        }
×
203

204
        Ok(app_ctx)
×
205
    }
×
206

207
    #[allow(clippy::too_many_lines)]
208
    /// Creates the database schema. Returns true if the schema was created, false if it already existed.
209
    pub(crate) async fn create_pro_database(
298✔
210
        mut conn: PooledConnection<'_, PostgresConnectionManager<Tls>>,
298✔
211
    ) -> Result<bool> {
298✔
212
        Self::maybe_clear_database(&conn).await?;
298✔
213

214
        let migration = initialize_database(
298✔
215
            &mut conn,
298✔
216
            Box::new(CurrentSchemaMigration),
298✔
217
            &all_migrations(),
298✔
218
        )
298✔
219
        .await?;
298✔
220

221
        Ok(migration == MigrationResult::CreatedDatabase)
298✔
222
    }
298✔
223

224
    /// Clears the database if the Settings demand and the database properties allows it.
225
    pub(crate) async fn maybe_clear_database(
298✔
226
        conn: &PooledConnection<'_, PostgresConnectionManager<Tls>>,
298✔
227
    ) -> Result<()> {
298✔
228
        let postgres_config = get_config_element::<crate::config::Postgres>()?;
298✔
229
        let database_status = Self::check_schema_status(conn).await?;
298✔
230
        let schema_name = postgres_config.schema;
298✔
231

232
        match database_status {
×
233
            DatabaseStatus::InitializedClearDatabase
×
234
                if postgres_config.clear_database_on_start && schema_name != "pg_temp" =>
×
235
            {
×
236
                info!("Clearing schema {}.", schema_name);
×
237
                conn.batch_execute(&format!("DROP SCHEMA {schema_name} CASCADE;"))
×
238
                    .await?;
×
239
            }
240
            DatabaseStatus::InitializedKeepDatabase if postgres_config.clear_database_on_start => {
×
241
                return Err(Error::ClearDatabaseOnStartupNotAllowed)
×
242
            }
243
            DatabaseStatus::InitializedClearDatabase
244
            | DatabaseStatus::InitializedKeepDatabase
245
            | DatabaseStatus::Unitialized => (),
298✔
246
        };
247

248
        Ok(())
298✔
249
    }
298✔
250

251
    async fn check_schema_status(
298✔
252
        conn: &PooledConnection<'_, PostgresConnectionManager<Tls>>,
298✔
253
    ) -> Result<DatabaseStatus> {
298✔
254
        let stmt = match conn
298✔
255
            .prepare("SELECT clear_database_on_start from geoengine;")
298✔
256
            .await
298✔
257
        {
258
            Ok(stmt) => stmt,
×
259
            Err(e) => {
298✔
260
                if let Some(code) = e.code() {
298✔
261
                    if *code == SqlState::UNDEFINED_TABLE {
298✔
262
                        info!("Initializing schema.");
298✔
263
                        return Ok(DatabaseStatus::Unitialized);
298✔
264
                    }
×
265
                }
×
266
                return Err(error::Error::TokioPostgres { source: e });
×
267
            }
268
        };
269

270
        let row = conn.query_one(&stmt, &[]).await?;
×
271

272
        if row.get(0) {
×
273
            Ok(DatabaseStatus::InitializedClearDatabase)
×
274
        } else {
275
            Ok(DatabaseStatus::InitializedKeepDatabase)
×
276
        }
277
    }
298✔
278
}
279

280
enum DatabaseStatus {
281
    Unitialized,
282
    InitializedClearDatabase,
283
    InitializedKeepDatabase,
284
}
285

286
#[async_trait]
287
impl<Tls> ApplicationContext for PostgresContext<Tls>
288
where
289
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
290
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
291
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
292
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
293
{
294
    type SessionContext = PostgresSessionContext<Tls>;
295
    type Session = UserSession;
296

297
    fn session_context(&self, session: Self::Session) -> Self::SessionContext {
577✔
298
        PostgresSessionContext {
577✔
299
            session,
577✔
300
            context: self.clone(),
577✔
301
        }
577✔
302
    }
577✔
303

304
    async fn session_by_id(&self, session_id: SessionId) -> Result<Self::Session> {
171✔
305
        self.user_session_by_id(session_id)
171✔
306
            .await
171✔
307
            .map_err(Box::new)
165✔
308
            .context(error::Unauthorized)
165✔
309
    }
336✔
310

311
    fn oidc_manager(&self) -> &OidcManager {
30✔
312
        &self.oidc_manager
30✔
313
    }
30✔
314
}
315

316
#[derive(Clone)]
317
pub struct PostgresSessionContext<Tls>
318
where
319
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
320
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
321
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
322
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
323
{
324
    session: UserSession,
325
    context: PostgresContext<Tls>,
326
}
327

328
#[async_trait]
329
impl<Tls> SessionContext for PostgresSessionContext<Tls>
330
where
331
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
332
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
333
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
334
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
335
{
336
    type Session = UserSession;
337
    type GeoEngineDB = PostgresDb<Tls>;
338

339
    type TaskContext = SimpleTaskManagerContext;
340
    type TaskManager = UserTaskManager; // this does not persist across restarts
341
    type QueryContext = QueryContextImpl;
342
    type ExecutionContext = ExecutionContextImpl<Self::GeoEngineDB>;
343

344
    fn db(&self) -> Self::GeoEngineDB {
820✔
345
        PostgresDb::new(self.context.pool.clone(), self.session.clone())
820✔
346
    }
820✔
347

348
    fn tasks(&self) -> Self::TaskManager {
41✔
349
        UserTaskManager::new(self.context.task_manager.clone(), self.session.clone())
41✔
350
    }
41✔
351

352
    fn query_context(&self, workflow: Uuid, computation: Uuid) -> Result<Self::QueryContext> {
32✔
353
        // TODO: load config only once
32✔
354

32✔
355
        Ok(QueryContextImpl::new_with_extensions(
32✔
356
            self.context.query_ctx_chunk_size,
32✔
357
            self.context.thread_pool.clone(),
32✔
358
            Some(self.context.tile_cache.clone()),
32✔
359
            Some(
32✔
360
                self.context
32✔
361
                    .quota
32✔
362
                    .create_quota_tracking(&self.session, workflow, computation),
32✔
363
            ),
32✔
364
            Some(Box::new(QuotaCheckerImpl { user_db: self.db() }) as QuotaChecker),
32✔
365
        ))
32✔
366
    }
32✔
367

368
    fn execution_context(&self) -> Result<Self::ExecutionContext> {
57✔
369
        Ok(ExecutionContextImpl::<PostgresDb<Tls>>::new(
57✔
370
            self.db(),
57✔
371
            self.context.thread_pool.clone(),
57✔
372
            self.context.exe_ctx_tiling_spec,
57✔
373
        ))
57✔
374
    }
57✔
375

376
    fn volumes(&self) -> Result<Vec<Volume>> {
1✔
377
        Ok(self
1✔
378
            .context
1✔
379
            .volumes
1✔
380
            .volumes
1✔
381
            .iter()
1✔
382
            .map(|v| Volume {
1✔
383
                name: v.name.0.clone(),
1✔
384
                path: if self.session.is_admin() {
1✔
385
                    Some(v.path.to_string_lossy().to_string())
1✔
386
                } else {
387
                    None
×
388
                },
389
            })
1✔
390
            .collect())
1✔
391
    }
1✔
392

393
    fn session(&self) -> &Self::Session {
30✔
394
        &self.session
30✔
395
    }
30✔
396
}
397

398
#[derive(Debug)]
399
pub struct PostgresDb<Tls>
400
where
401
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
402
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
403
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
404
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
405
{
406
    pub(crate) conn_pool: Pool<PostgresConnectionManager<Tls>>,
407
    pub(crate) session: UserSession,
408
}
409

410
impl<Tls> PostgresDb<Tls>
411
where
412
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
413
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
414
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
415
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
416
{
417
    pub fn new(conn_pool: Pool<PostgresConnectionManager<Tls>>, session: UserSession) -> Self {
1,119✔
418
        Self { conn_pool, session }
1,119✔
419
    }
1,119✔
420

421
    /// Check whether the namepsace of the given dataset is allowed for insertion
422
    pub(crate) fn check_dataset_namespace(&self, id: &DatasetName) -> Result<()> {
97✔
423
        let is_ok = match &id.namespace {
97✔
424
            Some(namespace) => namespace.as_str() == self.session.user.id.to_string(),
32✔
425
            None => self.session.is_admin(),
65✔
426
        };
427

428
        if is_ok {
97✔
429
            Ok(())
97✔
430
        } else {
431
            Err(Error::InvalidDatasetIdNamespace)
×
432
        }
433
    }
97✔
434

435
    /// Check whether the namepsace of the given model is allowed for insertion
436
    pub(crate) fn check_ml_model_namespace(
1✔
437
        &self,
1✔
438
        name: &MlModelName,
1✔
439
    ) -> Result<(), MachineLearningError> {
1✔
440
        let is_ok = match &name.namespace {
1✔
441
            Some(namespace) => namespace.as_str() == self.session.user.id.to_string(),
1✔
442
            None => self.session.is_admin(),
×
443
        };
444

445
        if is_ok {
1✔
446
            Ok(())
1✔
447
        } else {
448
            Err(MachineLearningError::InvalidModelNamespace { name: name.clone() })
×
449
        }
450
    }
1✔
451
}
452

453
impl<Tls> GeoEngineDb for PostgresDb<Tls>
454
where
455
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static + std::fmt::Debug,
456
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
457
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
458
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
459
{
460
}
461

462
#[cfg(test)]
463
mod tests {
464
    use super::*;
465
    use crate::config::QuotaTrackingMode;
466
    use crate::datasets::external::netcdfcf::NetCdfCfDataProviderDefinition;
467
    use crate::datasets::listing::{DatasetListOptions, DatasetListing, ProvenanceOutput};
468
    use crate::datasets::listing::{DatasetProvider, Provenance};
469
    use crate::datasets::storage::{DatasetStore, MetaDataDefinition};
470
    use crate::datasets::upload::{FileId, UploadId};
471
    use crate::datasets::upload::{FileUpload, Upload, UploadDb};
472
    use crate::datasets::{AddDataset, DatasetIdAndName};
473
    use crate::ge_context;
474
    use crate::layers::add_from_directory::UNSORTED_COLLECTION_ID;
475
    use crate::layers::layer::{
476
        AddLayer, AddLayerCollection, CollectionItem, LayerCollection, LayerCollectionListOptions,
477
        LayerCollectionListing, LayerListing, ProviderLayerCollectionId, ProviderLayerId,
478
    };
479
    use crate::layers::listing::{
480
        LayerCollectionId, LayerCollectionProvider, SearchParameters, SearchType,
481
    };
482
    use crate::layers::storage::{
483
        LayerDb, LayerProviderDb, LayerProviderListing, LayerProviderListingOptions,
484
        INTERNAL_PROVIDER_ID,
485
    };
486
    use crate::permissions::{Permission, PermissionDb, Role, RoleDescription, RoleId};
487
    use crate::projects::{
488
        CreateProject, LayerUpdate, LoadVersion, OrderBy, Plot, PlotUpdate, PointSymbology,
489
        ProjectDb, ProjectId, ProjectLayer, ProjectListOptions, ProjectListing, STRectangle,
490
        UpdateProject,
491
    };
492
    use crate::users::{OidcTokens, SessionTokenStore};
493
    use crate::users::{RoleDb, UserClaims, UserCredentials, UserDb, UserId, UserRegistration};
494
    use crate::util::tests::mock_oidc::{mock_refresh_server, MockRefreshServerConfig};
495
    use crate::util::tests::{admin_login, register_ndvi_workflow_helper, MockQuotaTracking};
496
    use crate::workflows::registry::WorkflowRegistry;
497
    use crate::workflows::workflow::Workflow;
498
    use bb8_postgres::tokio_postgres::NoTls;
499
    use futures::join;
500
    use geoengine_datatypes::collections::VectorDataType;
501
    use geoengine_datatypes::dataset::{DataProviderId, LayerId};
502
    use geoengine_datatypes::primitives::{
503
        BoundingBox2D, Coordinate2D, DateTime, Duration, FeatureDataType, Measurement,
504
        RasterQueryRectangle, SpatialResolution, TimeGranularity, TimeInstance, TimeInterval,
505
        TimeStep, VectorQueryRectangle,
506
    };
507
    use geoengine_datatypes::primitives::{CacheTtlSeconds, ColumnSelection};
508
    use geoengine_datatypes::raster::RasterDataType;
509
    use geoengine_datatypes::spatial_reference::{SpatialReference, SpatialReferenceOption};
510
    use geoengine_datatypes::test_data;
511
    use geoengine_datatypes::util::Identifier;
512
    use geoengine_operators::engine::{
513
        MetaData, MetaDataProvider, MultipleRasterOrSingleVectorSource, PlotOperator,
514
        RasterBandDescriptors, RasterResultDescriptor, StaticMetaData, TypedOperator,
515
        TypedResultDescriptor, VectorColumnInfo, VectorOperator, VectorResultDescriptor,
516
    };
517
    use geoengine_operators::mock::{MockPointSource, MockPointSourceParams};
518
    use geoengine_operators::plot::{Statistics, StatisticsParams};
519
    use geoengine_operators::source::{
520
        CsvHeader, FileNotFoundHandling, FormatSpecifics, GdalDatasetGeoTransform,
521
        GdalDatasetParameters, GdalLoadingInfo, GdalMetaDataList, GdalMetaDataRegular,
522
        GdalMetaDataStatic, GdalMetadataNetCdfCf, OgrSourceColumnSpec, OgrSourceDataset,
523
        OgrSourceDatasetTimeType, OgrSourceDurationSpec, OgrSourceErrorSpec, OgrSourceTimeFormat,
524
    };
525
    use geoengine_operators::util::input::MultiRasterOrVectorOperator::Raster;
526
    use httptest::Server;
527
    use oauth2::{AccessToken, RefreshToken};
528
    use openidconnect::SubjectIdentifier;
529
    use serde_json::json;
530
    use std::str::FromStr;
531

532
    #[ge_context::test]
1✔
533
    async fn test(app_ctx: PostgresContext<NoTls>) {
1✔
534
        anonymous(&app_ctx).await;
1✔
535

536
        let _user_id = user_reg_login(&app_ctx).await;
1✔
537

538
        let session = app_ctx
1✔
539
            .login(UserCredentials {
1✔
540
                email: "foo@example.com".into(),
1✔
541
                password: "secret123".into(),
1✔
542
            })
1✔
543
            .await
1✔
544
            .unwrap();
1✔
545

1✔
546
        create_projects(&app_ctx, &session).await;
1✔
547

548
        let projects = list_projects(&app_ctx, &session).await;
1✔
549

550
        set_session(&app_ctx, &projects).await;
1✔
551

552
        let project_id = projects[0].id;
1✔
553

1✔
554
        update_projects(&app_ctx, &session, project_id).await;
1✔
555

556
        add_permission(&app_ctx, &session, project_id).await;
1✔
557

558
        delete_project(&app_ctx, &session, project_id).await;
1✔
559
    }
1✔
560

561
    #[ge_context::test]
1✔
562
    async fn test_external(app_ctx: PostgresContext<NoTls>) {
1✔
563
        anonymous(&app_ctx).await;
1✔
564

565
        let session = external_user_login_twice(&app_ctx).await;
1✔
566

567
        create_projects(&app_ctx, &session).await;
1✔
568

569
        let projects = list_projects(&app_ctx, &session).await;
1✔
570

571
        set_session_external(&app_ctx, &projects).await;
1✔
572

573
        let project_id = projects[0].id;
1✔
574

1✔
575
        update_projects(&app_ctx, &session, project_id).await;
1✔
576

577
        add_permission(&app_ctx, &session, project_id).await;
1✔
578

579
        delete_project(&app_ctx, &session, project_id).await;
1✔
580
    }
1✔
581

582
    fn tokens_from_duration(duration: Duration) -> OidcTokens {
3✔
583
        OidcTokens {
3✔
584
            access: AccessToken::new("AccessToken".to_string()),
3✔
585
            refresh: None,
3✔
586
            expires_in: duration,
3✔
587
        }
3✔
588
    }
3✔
589

590
    async fn set_session(app_ctx: &PostgresContext<NoTls>, projects: &[ProjectListing]) {
1✔
591
        let credentials = UserCredentials {
1✔
592
            email: "foo@example.com".into(),
1✔
593
            password: "secret123".into(),
1✔
594
        };
1✔
595

596
        let session = app_ctx.login(credentials).await.unwrap();
1✔
597

1✔
598
        set_session_in_database(app_ctx, projects, session).await;
1✔
599
    }
1✔
600

601
    async fn set_session_external(app_ctx: &PostgresContext<NoTls>, projects: &[ProjectListing]) {
1✔
602
        let external_user_claims = UserClaims {
1✔
603
            external_id: SubjectIdentifier::new("Foo bar Id".into()),
1✔
604
            email: "foo@bar.de".into(),
1✔
605
            real_name: "Foo Bar".into(),
1✔
606
        };
1✔
607

608
        let session = app_ctx
1✔
609
            .login_external(
1✔
610
                external_user_claims,
1✔
611
                tokens_from_duration(Duration::minutes(10)),
1✔
612
            )
1✔
613
            .await
1✔
614
            .unwrap();
1✔
615

1✔
616
        set_session_in_database(app_ctx, projects, session).await;
1✔
617
    }
1✔
618

619
    async fn set_session_in_database(
2✔
620
        app_ctx: &PostgresContext<NoTls>,
2✔
621
        projects: &[ProjectListing],
2✔
622
        session: UserSession,
2✔
623
    ) {
2✔
624
        let db = app_ctx.session_context(session.clone()).db();
2✔
625

2✔
626
        db.set_session_project(projects[0].id).await.unwrap();
2✔
627

628
        assert_eq!(
2✔
629
            app_ctx.session_by_id(session.id).await.unwrap().project,
2✔
630
            Some(projects[0].id)
2✔
631
        );
632

633
        let rect = STRectangle::new_unchecked(SpatialReference::epsg_4326(), 0., 1., 2., 3., 1, 2);
2✔
634
        db.set_session_view(rect.clone()).await.unwrap();
2✔
635
        assert_eq!(
2✔
636
            app_ctx.session_by_id(session.id).await.unwrap().view,
2✔
637
            Some(rect)
2✔
638
        );
639
    }
2✔
640

641
    async fn delete_project(
2✔
642
        app_ctx: &PostgresContext<NoTls>,
2✔
643
        session: &UserSession,
2✔
644
        project_id: ProjectId,
2✔
645
    ) {
2✔
646
        let db = app_ctx.session_context(session.clone()).db();
2✔
647

2✔
648
        db.delete_project(project_id).await.unwrap();
2✔
649

2✔
650
        assert!(db.load_project(project_id).await.is_err());
2✔
651
    }
2✔
652

653
    async fn add_permission(
2✔
654
        app_ctx: &PostgresContext<NoTls>,
2✔
655
        session: &UserSession,
2✔
656
        project_id: ProjectId,
2✔
657
    ) {
2✔
658
        let db = app_ctx.session_context(session.clone()).db();
2✔
659

2✔
660
        assert!(db
2✔
661
            .has_permission(project_id, Permission::Owner)
2✔
662
            .await
2✔
663
            .unwrap());
2✔
664

665
        let user2 = app_ctx
2✔
666
            .register_user(UserRegistration {
2✔
667
                email: "user2@example.com".into(),
2✔
668
                password: "12345678".into(),
2✔
669
                real_name: "User2".into(),
2✔
670
            })
2✔
671
            .await
2✔
672
            .unwrap();
2✔
673

674
        let session2 = app_ctx
2✔
675
            .login(UserCredentials {
2✔
676
                email: "user2@example.com".into(),
2✔
677
                password: "12345678".into(),
2✔
678
            })
2✔
679
            .await
2✔
680
            .unwrap();
2✔
681

2✔
682
        let db2 = app_ctx.session_context(session2.clone()).db();
2✔
683
        assert!(!db2
2✔
684
            .has_permission(project_id, Permission::Owner)
2✔
685
            .await
2✔
686
            .unwrap());
2✔
687

688
        db.add_permission(user2.into(), project_id, Permission::Read)
2✔
689
            .await
2✔
690
            .unwrap();
2✔
691

2✔
692
        assert!(db2
2✔
693
            .has_permission(project_id, Permission::Read)
2✔
694
            .await
2✔
695
            .unwrap());
2✔
696
    }
2✔
697

698
    #[allow(clippy::too_many_lines)]
699
    async fn update_projects(
2✔
700
        app_ctx: &PostgresContext<NoTls>,
2✔
701
        session: &UserSession,
2✔
702
        project_id: ProjectId,
2✔
703
    ) {
2✔
704
        let db = app_ctx.session_context(session.clone()).db();
2✔
705

706
        let project = db
2✔
707
            .load_project_version(project_id, LoadVersion::Latest)
2✔
708
            .await
2✔
709
            .unwrap();
2✔
710

711
        let layer_workflow_id = db
2✔
712
            .register_workflow(Workflow {
2✔
713
                operator: TypedOperator::Vector(
2✔
714
                    MockPointSource {
2✔
715
                        params: MockPointSourceParams {
2✔
716
                            points: vec![Coordinate2D::new(1., 2.); 3],
2✔
717
                        },
2✔
718
                    }
2✔
719
                    .boxed(),
2✔
720
                ),
2✔
721
            })
2✔
722
            .await
2✔
723
            .unwrap();
2✔
724

2✔
725
        assert!(db.load_workflow(&layer_workflow_id).await.is_ok());
2✔
726

727
        let plot_workflow_id = db
2✔
728
            .register_workflow(Workflow {
2✔
729
                operator: Statistics {
2✔
730
                    params: StatisticsParams {
2✔
731
                        column_names: vec![],
2✔
732
                        percentiles: vec![],
2✔
733
                    },
2✔
734
                    sources: MultipleRasterOrSingleVectorSource {
2✔
735
                        source: Raster(vec![]),
2✔
736
                    },
2✔
737
                }
2✔
738
                .boxed()
2✔
739
                .into(),
2✔
740
            })
2✔
741
            .await
2✔
742
            .unwrap();
2✔
743

2✔
744
        assert!(db.load_workflow(&plot_workflow_id).await.is_ok());
2✔
745

746
        // add a plot
747
        let update = UpdateProject {
2✔
748
            id: project.id,
2✔
749
            name: Some("Test9 Updated".into()),
2✔
750
            description: None,
2✔
751
            layers: Some(vec![LayerUpdate::UpdateOrInsert(ProjectLayer {
2✔
752
                workflow: layer_workflow_id,
2✔
753
                name: "TestLayer".into(),
2✔
754
                symbology: PointSymbology::default().into(),
2✔
755
                visibility: Default::default(),
2✔
756
            })]),
2✔
757
            plots: Some(vec![PlotUpdate::UpdateOrInsert(Plot {
2✔
758
                workflow: plot_workflow_id,
2✔
759
                name: "Test Plot".into(),
2✔
760
            })]),
2✔
761
            bounds: None,
2✔
762
            time_step: None,
2✔
763
        };
2✔
764
        db.update_project(update).await.unwrap();
2✔
765

766
        let versions = db.list_project_versions(project_id).await.unwrap();
2✔
767
        assert_eq!(versions.len(), 2);
2✔
768

769
        // add second plot
770
        let update = UpdateProject {
2✔
771
            id: project.id,
2✔
772
            name: Some("Test9 Updated".into()),
2✔
773
            description: None,
2✔
774
            layers: Some(vec![LayerUpdate::UpdateOrInsert(ProjectLayer {
2✔
775
                workflow: layer_workflow_id,
2✔
776
                name: "TestLayer".into(),
2✔
777
                symbology: PointSymbology::default().into(),
2✔
778
                visibility: Default::default(),
2✔
779
            })]),
2✔
780
            plots: Some(vec![
2✔
781
                PlotUpdate::UpdateOrInsert(Plot {
2✔
782
                    workflow: plot_workflow_id,
2✔
783
                    name: "Test Plot".into(),
2✔
784
                }),
2✔
785
                PlotUpdate::UpdateOrInsert(Plot {
2✔
786
                    workflow: plot_workflow_id,
2✔
787
                    name: "Test Plot".into(),
2✔
788
                }),
2✔
789
            ]),
2✔
790
            bounds: None,
2✔
791
            time_step: None,
2✔
792
        };
2✔
793
        db.update_project(update).await.unwrap();
2✔
794

795
        let versions = db.list_project_versions(project_id).await.unwrap();
2✔
796
        assert_eq!(versions.len(), 3);
2✔
797

798
        // delete plots
799
        let update = UpdateProject {
2✔
800
            id: project.id,
2✔
801
            name: None,
2✔
802
            description: None,
2✔
803
            layers: None,
2✔
804
            plots: Some(vec![]),
2✔
805
            bounds: None,
2✔
806
            time_step: None,
2✔
807
        };
2✔
808
        db.update_project(update).await.unwrap();
2✔
809

810
        let versions = db.list_project_versions(project_id).await.unwrap();
2✔
811
        assert_eq!(versions.len(), 4);
2✔
812
    }
2✔
813

814
    async fn list_projects(
2✔
815
        app_ctx: &PostgresContext<NoTls>,
2✔
816
        session: &UserSession,
2✔
817
    ) -> Vec<ProjectListing> {
2✔
818
        let options = ProjectListOptions {
2✔
819
            order: OrderBy::NameDesc,
2✔
820
            offset: 0,
2✔
821
            limit: 2,
2✔
822
        };
2✔
823

2✔
824
        let db = app_ctx.session_context(session.clone()).db();
2✔
825

826
        let projects = db.list_projects(options).await.unwrap();
2✔
827

2✔
828
        assert_eq!(projects.len(), 2);
2✔
829
        assert_eq!(projects[0].name, "Test9");
2✔
830
        assert_eq!(projects[1].name, "Test8");
2✔
831
        projects
2✔
832
    }
2✔
833

834
    async fn create_projects(app_ctx: &PostgresContext<NoTls>, session: &UserSession) {
2✔
835
        let db = app_ctx.session_context(session.clone()).db();
2✔
836

837
        for i in 0..10 {
22✔
838
            let create = CreateProject {
20✔
839
                name: format!("Test{i}"),
20✔
840
                description: format!("Test{}", 10 - i),
20✔
841
                bounds: STRectangle::new(
20✔
842
                    SpatialReferenceOption::Unreferenced,
20✔
843
                    0.,
20✔
844
                    0.,
20✔
845
                    1.,
20✔
846
                    1.,
20✔
847
                    0,
20✔
848
                    1,
20✔
849
                )
20✔
850
                .unwrap(),
20✔
851
                time_step: None,
20✔
852
            };
20✔
853
            db.create_project(create).await.unwrap();
20✔
854
        }
855
    }
2✔
856

857
    async fn user_reg_login(app_ctx: &PostgresContext<NoTls>) -> UserId {
1✔
858
        let user_registration = UserRegistration {
1✔
859
            email: "foo@example.com".into(),
1✔
860
            password: "secret123".into(),
1✔
861
            real_name: "Foo Bar".into(),
1✔
862
        };
1✔
863

864
        let user_id = app_ctx.register_user(user_registration).await.unwrap();
1✔
865

1✔
866
        let credentials = UserCredentials {
1✔
867
            email: "foo@example.com".into(),
1✔
868
            password: "secret123".into(),
1✔
869
        };
1✔
870

871
        let session = app_ctx.login(credentials).await.unwrap();
1✔
872

1✔
873
        let db = app_ctx.session_context(session.clone()).db();
1✔
874

1✔
875
        app_ctx.session_by_id(session.id).await.unwrap();
1✔
876

1✔
877
        db.logout().await.unwrap();
1✔
878

1✔
879
        assert!(app_ctx.session_by_id(session.id).await.is_err());
1✔
880

881
        user_id
1✔
882
    }
1✔
883

884
    async fn external_user_login_twice(app_ctx: &PostgresContext<NoTls>) -> UserSession {
1✔
885
        let external_user_claims = UserClaims {
1✔
886
            external_id: SubjectIdentifier::new("Foo bar Id".into()),
1✔
887
            email: "foo@bar.de".into(),
1✔
888
            real_name: "Foo Bar".into(),
1✔
889
        };
1✔
890
        let duration = Duration::minutes(30);
1✔
891

892
        let login_result = app_ctx
1✔
893
            .login_external(external_user_claims.clone(), tokens_from_duration(duration))
1✔
894
            .await;
1✔
895
        assert!(login_result.is_ok());
1✔
896

897
        let session_1 = login_result.unwrap();
1✔
898
        let user_id = session_1.user.id; //TODO: Not a deterministic test.
1✔
899

1✔
900
        let db1 = app_ctx.session_context(session_1.clone()).db();
1✔
901

1✔
902
        assert!(session_1.user.email.is_some());
1✔
903
        assert_eq!(session_1.user.email.unwrap(), "foo@bar.de");
1✔
904
        assert!(session_1.user.real_name.is_some());
1✔
905
        assert_eq!(session_1.user.real_name.unwrap(), "Foo Bar");
1✔
906

907
        let expected_duration = session_1.created + duration;
1✔
908
        assert_eq!(session_1.valid_until, expected_duration);
1✔
909

910
        assert!(app_ctx.session_by_id(session_1.id).await.is_ok());
1✔
911

912
        assert!(db1.logout().await.is_ok());
1✔
913

914
        assert!(app_ctx.session_by_id(session_1.id).await.is_err());
1✔
915

916
        let duration = Duration::minutes(10);
1✔
917
        let login_result = app_ctx
1✔
918
            .login_external(external_user_claims.clone(), tokens_from_duration(duration))
1✔
919
            .await;
1✔
920
        assert!(login_result.is_ok());
1✔
921

922
        let session_2 = login_result.unwrap();
1✔
923
        let result = session_2.clone();
1✔
924

1✔
925
        assert!(session_2.user.email.is_some()); //TODO: Technically, user details could change for each login. For simplicity, this is not covered yet.
1✔
926
        assert_eq!(session_2.user.email.unwrap(), "foo@bar.de");
1✔
927
        assert!(session_2.user.real_name.is_some());
1✔
928
        assert_eq!(session_2.user.real_name.unwrap(), "Foo Bar");
1✔
929
        assert_eq!(session_2.user.id, user_id);
1✔
930

931
        let expected_duration = session_2.created + duration;
1✔
932
        assert_eq!(session_2.valid_until, expected_duration);
1✔
933

934
        assert!(app_ctx.session_by_id(session_2.id).await.is_ok());
1✔
935

936
        result
1✔
937
    }
1✔
938

939
    async fn anonymous(app_ctx: &PostgresContext<NoTls>) {
2✔
940
        let now: DateTime = chrono::offset::Utc::now().into();
2✔
941
        let session = app_ctx.create_anonymous_session().await.unwrap();
2✔
942
        let then: DateTime = chrono::offset::Utc::now().into();
2✔
943

2✔
944
        assert!(session.created >= now && session.created <= then);
2✔
945
        assert!(session.valid_until > session.created);
2✔
946

947
        let session = app_ctx.session_by_id(session.id).await.unwrap();
2✔
948

2✔
949
        let db = app_ctx.session_context(session.clone()).db();
2✔
950

2✔
951
        db.logout().await.unwrap();
2✔
952

2✔
953
        assert!(app_ctx.session_by_id(session.id).await.is_err());
2✔
954
    }
2✔
955

956
    #[ge_context::test]
1✔
957
    async fn it_persists_workflows(app_ctx: PostgresContext<NoTls>) {
1✔
958
        let workflow = Workflow {
1✔
959
            operator: TypedOperator::Vector(
1✔
960
                MockPointSource {
1✔
961
                    params: MockPointSourceParams {
1✔
962
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
963
                    },
1✔
964
                }
1✔
965
                .boxed(),
1✔
966
            ),
1✔
967
        };
1✔
968

969
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
970
        let ctx = app_ctx.session_context(session);
1✔
971

1✔
972
        let db = ctx.db();
1✔
973
        let id = db.register_workflow(workflow).await.unwrap();
1✔
974

1✔
975
        drop(ctx);
1✔
976

977
        let workflow = db.load_workflow(&id).await.unwrap();
1✔
978

1✔
979
        let json = serde_json::to_string(&workflow).unwrap();
1✔
980
        assert_eq!(
1✔
981
            json,
1✔
982
            r#"{"type":"Vector","operator":{"type":"MockPointSource","params":{"points":[{"x":1.0,"y":2.0},{"x":1.0,"y":2.0},{"x":1.0,"y":2.0}]}}}"#
1✔
983
        );
1✔
984
    }
1✔
985

986
    #[allow(clippy::too_many_lines)]
987
    #[ge_context::test]
1✔
988
    async fn it_persists_datasets(app_ctx: PostgresContext<NoTls>) {
1✔
989
        let loading_info = OgrSourceDataset {
1✔
990
            file_name: PathBuf::from("test.csv"),
1✔
991
            layer_name: "test.csv".to_owned(),
1✔
992
            data_type: Some(VectorDataType::MultiPoint),
1✔
993
            time: OgrSourceDatasetTimeType::Start {
1✔
994
                start_field: "start".to_owned(),
1✔
995
                start_format: OgrSourceTimeFormat::Auto,
1✔
996
                duration: OgrSourceDurationSpec::Zero,
1✔
997
            },
1✔
998
            default_geometry: None,
1✔
999
            columns: Some(OgrSourceColumnSpec {
1✔
1000
                format_specifics: Some(FormatSpecifics::Csv {
1✔
1001
                    header: CsvHeader::Auto,
1✔
1002
                }),
1✔
1003
                x: "x".to_owned(),
1✔
1004
                y: None,
1✔
1005
                int: vec![],
1✔
1006
                float: vec![],
1✔
1007
                text: vec![],
1✔
1008
                bool: vec![],
1✔
1009
                datetime: vec![],
1✔
1010
                rename: None,
1✔
1011
            }),
1✔
1012
            force_ogr_time_filter: false,
1✔
1013
            force_ogr_spatial_filter: false,
1✔
1014
            on_error: OgrSourceErrorSpec::Ignore,
1✔
1015
            sql_query: None,
1✔
1016
            attribute_query: None,
1✔
1017
            cache_ttl: CacheTtlSeconds::default(),
1✔
1018
        };
1✔
1019

1✔
1020
        let meta_data = MetaDataDefinition::OgrMetaData(StaticMetaData::<
1✔
1021
            OgrSourceDataset,
1✔
1022
            VectorResultDescriptor,
1✔
1023
            VectorQueryRectangle,
1✔
1024
        > {
1✔
1025
            loading_info: loading_info.clone(),
1✔
1026
            result_descriptor: VectorResultDescriptor {
1✔
1027
                data_type: VectorDataType::MultiPoint,
1✔
1028
                spatial_reference: SpatialReference::epsg_4326().into(),
1✔
1029
                columns: [(
1✔
1030
                    "foo".to_owned(),
1✔
1031
                    VectorColumnInfo {
1✔
1032
                        data_type: FeatureDataType::Float,
1✔
1033
                        measurement: Measurement::Unitless,
1✔
1034
                    },
1✔
1035
                )]
1✔
1036
                .into_iter()
1✔
1037
                .collect(),
1✔
1038
                time: None,
1✔
1039
                bbox: None,
1✔
1040
            },
1✔
1041
            phantom: Default::default(),
1✔
1042
        });
1✔
1043

1044
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
1045

1✔
1046
        let dataset_name = DatasetName::new(Some(session.user.id.to_string()), "my_dataset");
1✔
1047

1✔
1048
        let db = app_ctx.session_context(session.clone()).db();
1✔
1049
        let DatasetIdAndName {
1050
            id: dataset_id,
1✔
1051
            name: dataset_name,
1✔
1052
        } = db
1✔
1053
            .add_dataset(
1✔
1054
                AddDataset {
1✔
1055
                    name: Some(dataset_name.clone()),
1✔
1056
                    display_name: "Ogr Test".to_owned(),
1✔
1057
                    description: "desc".to_owned(),
1✔
1058
                    source_operator: "OgrSource".to_owned(),
1✔
1059
                    symbology: None,
1✔
1060
                    provenance: Some(vec![Provenance {
1✔
1061
                        citation: "citation".to_owned(),
1✔
1062
                        license: "license".to_owned(),
1✔
1063
                        uri: "uri".to_owned(),
1✔
1064
                    }]),
1✔
1065
                    tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1066
                },
1✔
1067
                meta_data,
1✔
1068
            )
1✔
1069
            .await
1✔
1070
            .unwrap();
1✔
1071

1072
        let datasets = db
1✔
1073
            .list_datasets(DatasetListOptions {
1✔
1074
                filter: None,
1✔
1075
                order: crate::datasets::listing::OrderBy::NameAsc,
1✔
1076
                offset: 0,
1✔
1077
                limit: 10,
1✔
1078
                tags: None,
1✔
1079
            })
1✔
1080
            .await
1✔
1081
            .unwrap();
1✔
1082

1✔
1083
        assert_eq!(datasets.len(), 1);
1✔
1084

1085
        assert_eq!(
1✔
1086
            datasets[0],
1✔
1087
            DatasetListing {
1✔
1088
                id: dataset_id,
1✔
1089
                name: dataset_name,
1✔
1090
                display_name: "Ogr Test".to_owned(),
1✔
1091
                description: "desc".to_owned(),
1✔
1092
                source_operator: "OgrSource".to_owned(),
1✔
1093
                symbology: None,
1✔
1094
                tags: vec!["upload".to_owned(), "test".to_owned()],
1✔
1095
                result_descriptor: TypedResultDescriptor::Vector(VectorResultDescriptor {
1✔
1096
                    data_type: VectorDataType::MultiPoint,
1✔
1097
                    spatial_reference: SpatialReference::epsg_4326().into(),
1✔
1098
                    columns: [(
1✔
1099
                        "foo".to_owned(),
1✔
1100
                        VectorColumnInfo {
1✔
1101
                            data_type: FeatureDataType::Float,
1✔
1102
                            measurement: Measurement::Unitless
1✔
1103
                        }
1✔
1104
                    )]
1✔
1105
                    .into_iter()
1✔
1106
                    .collect(),
1✔
1107
                    time: None,
1✔
1108
                    bbox: None,
1✔
1109
                })
1✔
1110
            },
1✔
1111
        );
1✔
1112

1113
        let provenance = db.load_provenance(&dataset_id).await.unwrap();
1✔
1114

1✔
1115
        assert_eq!(
1✔
1116
            provenance,
1✔
1117
            ProvenanceOutput {
1✔
1118
                data: dataset_id.into(),
1✔
1119
                provenance: Some(vec![Provenance {
1✔
1120
                    citation: "citation".to_owned(),
1✔
1121
                    license: "license".to_owned(),
1✔
1122
                    uri: "uri".to_owned(),
1✔
1123
                }])
1✔
1124
            }
1✔
1125
        );
1✔
1126

1127
        let meta_data: Box<dyn MetaData<OgrSourceDataset, _, _>> =
1✔
1128
            db.meta_data(&dataset_id.into()).await.unwrap();
1✔
1129

1130
        assert_eq!(
1✔
1131
            meta_data
1✔
1132
                .loading_info(VectorQueryRectangle {
1✔
1133
                    spatial_bounds: BoundingBox2D::new_unchecked(
1✔
1134
                        (-180., -90.).into(),
1✔
1135
                        (180., 90.).into()
1✔
1136
                    ),
1✔
1137
                    time_interval: TimeInterval::default(),
1✔
1138
                    spatial_resolution: SpatialResolution::zero_point_one(),
1✔
1139
                    attributes: ColumnSelection::all()
1✔
1140
                })
1✔
1141
                .await
1✔
1142
                .unwrap(),
1✔
1143
            loading_info
1144
        );
1145
    }
1✔
1146

1147
    #[ge_context::test]
1✔
1148
    async fn it_persists_uploads(app_ctx: PostgresContext<NoTls>) {
1✔
1149
        let id = UploadId::from_str("2de18cd8-4a38-4111-a445-e3734bc18a80").unwrap();
1✔
1150
        let input = Upload {
1✔
1151
            id,
1✔
1152
            files: vec![FileUpload {
1✔
1153
                id: FileId::from_str("e80afab0-831d-4d40-95d6-1e4dfd277e72").unwrap(),
1✔
1154
                name: "test.csv".to_owned(),
1✔
1155
                byte_size: 1337,
1✔
1156
            }],
1✔
1157
        };
1✔
1158

1159
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
1160

1✔
1161
        let db = app_ctx.session_context(session.clone()).db();
1✔
1162

1✔
1163
        db.create_upload(input.clone()).await.unwrap();
1✔
1164

1165
        let upload = db.load_upload(id).await.unwrap();
1✔
1166

1✔
1167
        assert_eq!(upload, input);
1✔
1168
    }
1✔
1169

1170
    #[allow(clippy::too_many_lines)]
1171
    #[ge_context::test]
1✔
1172
    async fn it_persists_layer_providers(app_ctx: PostgresContext<NoTls>) {
1✔
1173
        let db = app_ctx.session_context(UserSession::admin_session()).db();
1✔
1174

1✔
1175
        let provider = NetCdfCfDataProviderDefinition {
1✔
1176
            name: "netcdfcf".to_string(),
1✔
1177
            description: "NetCdfCfProviderDefinition".to_string(),
1✔
1178
            priority: Some(33),
1✔
1179
            data: test_data!("netcdf4d/").into(),
1✔
1180
            overviews: test_data!("netcdf4d/overviews/").into(),
1✔
1181
            cache_ttl: CacheTtlSeconds::new(0),
1✔
1182
        };
1✔
1183

1184
        let provider_id = db.add_layer_provider(provider.into()).await.unwrap();
1✔
1185

1186
        let providers = db
1✔
1187
            .list_layer_providers(LayerProviderListingOptions {
1✔
1188
                offset: 0,
1✔
1189
                limit: 10,
1✔
1190
            })
1✔
1191
            .await
1✔
1192
            .unwrap();
1✔
1193

1✔
1194
        assert_eq!(providers.len(), 1);
1✔
1195

1196
        assert_eq!(
1✔
1197
            providers[0],
1✔
1198
            LayerProviderListing {
1✔
1199
                id: provider_id,
1✔
1200
                name: "netcdfcf".to_owned(),
1✔
1201
                priority: 33,
1✔
1202
            }
1✔
1203
        );
1✔
1204

1205
        let provider = db.load_layer_provider(provider_id).await.unwrap();
1✔
1206

1207
        let datasets = provider
1✔
1208
            .load_layer_collection(
1✔
1209
                &provider.get_root_layer_collection_id().await.unwrap(),
1✔
1210
                LayerCollectionListOptions {
1✔
1211
                    offset: 0,
1✔
1212
                    limit: 10,
1✔
1213
                },
1✔
1214
            )
1✔
1215
            .await
1✔
1216
            .unwrap();
1✔
1217

1✔
1218
        assert_eq!(datasets.items.len(), 5);
1✔
1219
    }
1✔
1220

1221
    #[ge_context::test]
1✔
1222
    async fn it_lists_only_permitted_datasets(app_ctx: PostgresContext<NoTls>) {
1✔
1223
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1224
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1225

1✔
1226
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1227
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1228

1✔
1229
        let descriptor = VectorResultDescriptor {
1✔
1230
            data_type: VectorDataType::Data,
1✔
1231
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1232
            columns: Default::default(),
1✔
1233
            time: None,
1✔
1234
            bbox: None,
1✔
1235
        };
1✔
1236

1✔
1237
        let ds = AddDataset {
1✔
1238
            name: None,
1✔
1239
            display_name: "OgrDataset".to_string(),
1✔
1240
            description: "My Ogr dataset".to_string(),
1✔
1241
            source_operator: "OgrSource".to_string(),
1✔
1242
            symbology: None,
1✔
1243
            provenance: None,
1✔
1244
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1245
        };
1✔
1246

1✔
1247
        let meta = StaticMetaData {
1✔
1248
            loading_info: OgrSourceDataset {
1✔
1249
                file_name: Default::default(),
1✔
1250
                layer_name: String::new(),
1✔
1251
                data_type: None,
1✔
1252
                time: Default::default(),
1✔
1253
                default_geometry: None,
1✔
1254
                columns: None,
1✔
1255
                force_ogr_time_filter: false,
1✔
1256
                force_ogr_spatial_filter: false,
1✔
1257
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1258
                sql_query: None,
1✔
1259
                attribute_query: None,
1✔
1260
                cache_ttl: CacheTtlSeconds::default(),
1✔
1261
            },
1✔
1262
            result_descriptor: descriptor.clone(),
1✔
1263
            phantom: Default::default(),
1✔
1264
        };
1✔
1265

1266
        let _id = db1.add_dataset(ds, meta.into()).await.unwrap();
1✔
1267

1268
        let list1 = db1
1✔
1269
            .list_datasets(DatasetListOptions {
1✔
1270
                filter: None,
1✔
1271
                order: crate::datasets::listing::OrderBy::NameAsc,
1✔
1272
                offset: 0,
1✔
1273
                limit: 1,
1✔
1274
                tags: None,
1✔
1275
            })
1✔
1276
            .await
1✔
1277
            .unwrap();
1✔
1278

1✔
1279
        assert_eq!(list1.len(), 1);
1✔
1280

1281
        let list2 = db2
1✔
1282
            .list_datasets(DatasetListOptions {
1✔
1283
                filter: None,
1✔
1284
                order: crate::datasets::listing::OrderBy::NameAsc,
1✔
1285
                offset: 0,
1✔
1286
                limit: 1,
1✔
1287
                tags: None,
1✔
1288
            })
1✔
1289
            .await
1✔
1290
            .unwrap();
1✔
1291

1✔
1292
        assert_eq!(list2.len(), 0);
1✔
1293
    }
1✔
1294

1295
    #[ge_context::test]
1✔
1296
    async fn it_shows_only_permitted_provenance(app_ctx: PostgresContext<NoTls>) {
1✔
1297
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1298
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1299

1✔
1300
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1301
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1302

1✔
1303
        let descriptor = VectorResultDescriptor {
1✔
1304
            data_type: VectorDataType::Data,
1✔
1305
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1306
            columns: Default::default(),
1✔
1307
            time: None,
1✔
1308
            bbox: None,
1✔
1309
        };
1✔
1310

1✔
1311
        let ds = AddDataset {
1✔
1312
            name: None,
1✔
1313
            display_name: "OgrDataset".to_string(),
1✔
1314
            description: "My Ogr dataset".to_string(),
1✔
1315
            source_operator: "OgrSource".to_string(),
1✔
1316
            symbology: None,
1✔
1317
            provenance: None,
1✔
1318
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1319
        };
1✔
1320

1✔
1321
        let meta = StaticMetaData {
1✔
1322
            loading_info: OgrSourceDataset {
1✔
1323
                file_name: Default::default(),
1✔
1324
                layer_name: String::new(),
1✔
1325
                data_type: None,
1✔
1326
                time: Default::default(),
1✔
1327
                default_geometry: None,
1✔
1328
                columns: None,
1✔
1329
                force_ogr_time_filter: false,
1✔
1330
                force_ogr_spatial_filter: false,
1✔
1331
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1332
                sql_query: None,
1✔
1333
                attribute_query: None,
1✔
1334
                cache_ttl: CacheTtlSeconds::default(),
1✔
1335
            },
1✔
1336
            result_descriptor: descriptor.clone(),
1✔
1337
            phantom: Default::default(),
1✔
1338
        };
1✔
1339

1340
        let id = db1.add_dataset(ds, meta.into()).await.unwrap().id;
1✔
1341

1✔
1342
        assert!(db1.load_provenance(&id).await.is_ok());
1✔
1343

1344
        assert!(db2.load_provenance(&id).await.is_err());
1✔
1345
    }
1✔
1346

1347
    #[ge_context::test]
1✔
1348
    async fn it_updates_permissions(app_ctx: PostgresContext<NoTls>) {
1✔
1349
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1350
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1351

1✔
1352
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1353
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1354

1✔
1355
        let descriptor = VectorResultDescriptor {
1✔
1356
            data_type: VectorDataType::Data,
1✔
1357
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1358
            columns: Default::default(),
1✔
1359
            time: None,
1✔
1360
            bbox: None,
1✔
1361
        };
1✔
1362

1✔
1363
        let ds = AddDataset {
1✔
1364
            name: None,
1✔
1365
            display_name: "OgrDataset".to_string(),
1✔
1366
            description: "My Ogr dataset".to_string(),
1✔
1367
            source_operator: "OgrSource".to_string(),
1✔
1368
            symbology: None,
1✔
1369
            provenance: None,
1✔
1370
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1371
        };
1✔
1372

1✔
1373
        let meta = StaticMetaData {
1✔
1374
            loading_info: OgrSourceDataset {
1✔
1375
                file_name: Default::default(),
1✔
1376
                layer_name: String::new(),
1✔
1377
                data_type: None,
1✔
1378
                time: Default::default(),
1✔
1379
                default_geometry: None,
1✔
1380
                columns: None,
1✔
1381
                force_ogr_time_filter: false,
1✔
1382
                force_ogr_spatial_filter: false,
1✔
1383
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1384
                sql_query: None,
1✔
1385
                attribute_query: None,
1✔
1386
                cache_ttl: CacheTtlSeconds::default(),
1✔
1387
            },
1✔
1388
            result_descriptor: descriptor.clone(),
1✔
1389
            phantom: Default::default(),
1✔
1390
        };
1✔
1391

1392
        let id = db1.add_dataset(ds, meta.into()).await.unwrap().id;
1✔
1393

1✔
1394
        assert!(db1.load_dataset(&id).await.is_ok());
1✔
1395

1396
        assert!(db2.load_dataset(&id).await.is_err());
1✔
1397

1398
        db1.add_permission(session2.user.id.into(), id, Permission::Read)
1✔
1399
            .await
1✔
1400
            .unwrap();
1✔
1401

1✔
1402
        assert!(db2.load_dataset(&id).await.is_ok());
1✔
1403
    }
1✔
1404

1405
    #[ge_context::test]
1✔
1406
    async fn it_uses_roles_for_permissions(app_ctx: PostgresContext<NoTls>) {
1✔
1407
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1408
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1409

1✔
1410
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1411
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1412

1✔
1413
        let descriptor = VectorResultDescriptor {
1✔
1414
            data_type: VectorDataType::Data,
1✔
1415
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1416
            columns: Default::default(),
1✔
1417
            time: None,
1✔
1418
            bbox: None,
1✔
1419
        };
1✔
1420

1✔
1421
        let ds = AddDataset {
1✔
1422
            name: None,
1✔
1423
            display_name: "OgrDataset".to_string(),
1✔
1424
            description: "My Ogr dataset".to_string(),
1✔
1425
            source_operator: "OgrSource".to_string(),
1✔
1426
            symbology: None,
1✔
1427
            provenance: None,
1✔
1428
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1429
        };
1✔
1430

1✔
1431
        let meta = StaticMetaData {
1✔
1432
            loading_info: OgrSourceDataset {
1✔
1433
                file_name: Default::default(),
1✔
1434
                layer_name: String::new(),
1✔
1435
                data_type: None,
1✔
1436
                time: Default::default(),
1✔
1437
                default_geometry: None,
1✔
1438
                columns: None,
1✔
1439
                force_ogr_time_filter: false,
1✔
1440
                force_ogr_spatial_filter: false,
1✔
1441
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1442
                sql_query: None,
1✔
1443
                attribute_query: None,
1✔
1444
                cache_ttl: CacheTtlSeconds::default(),
1✔
1445
            },
1✔
1446
            result_descriptor: descriptor.clone(),
1✔
1447
            phantom: Default::default(),
1✔
1448
        };
1✔
1449

1450
        let id = db1.add_dataset(ds, meta.into()).await.unwrap().id;
1✔
1451

1✔
1452
        assert!(db1.load_dataset(&id).await.is_ok());
1✔
1453

1454
        assert!(db2.load_dataset(&id).await.is_err());
1✔
1455

1456
        db1.add_permission(session2.user.id.into(), id, Permission::Read)
1✔
1457
            .await
1✔
1458
            .unwrap();
1✔
1459

1✔
1460
        assert!(db2.load_dataset(&id).await.is_ok());
1✔
1461
    }
1✔
1462

1463
    #[ge_context::test]
1✔
1464
    async fn it_secures_meta_data(app_ctx: PostgresContext<NoTls>) {
1✔
1465
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1466
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1467

1✔
1468
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1469
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1470

1✔
1471
        let descriptor = VectorResultDescriptor {
1✔
1472
            data_type: VectorDataType::Data,
1✔
1473
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1474
            columns: Default::default(),
1✔
1475
            time: None,
1✔
1476
            bbox: None,
1✔
1477
        };
1✔
1478

1✔
1479
        let ds = AddDataset {
1✔
1480
            name: None,
1✔
1481
            display_name: "OgrDataset".to_string(),
1✔
1482
            description: "My Ogr dataset".to_string(),
1✔
1483
            source_operator: "OgrSource".to_string(),
1✔
1484
            symbology: None,
1✔
1485
            provenance: None,
1✔
1486
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1487
        };
1✔
1488

1✔
1489
        let meta = StaticMetaData {
1✔
1490
            loading_info: OgrSourceDataset {
1✔
1491
                file_name: Default::default(),
1✔
1492
                layer_name: String::new(),
1✔
1493
                data_type: None,
1✔
1494
                time: Default::default(),
1✔
1495
                default_geometry: None,
1✔
1496
                columns: None,
1✔
1497
                force_ogr_time_filter: false,
1✔
1498
                force_ogr_spatial_filter: false,
1✔
1499
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1500
                sql_query: None,
1✔
1501
                attribute_query: None,
1✔
1502
                cache_ttl: CacheTtlSeconds::default(),
1✔
1503
            },
1✔
1504
            result_descriptor: descriptor.clone(),
1✔
1505
            phantom: Default::default(),
1✔
1506
        };
1✔
1507

1508
        let id = db1.add_dataset(ds, meta.into()).await.unwrap().id;
1✔
1509

1510
        let meta: geoengine_operators::util::Result<
1✔
1511
            Box<dyn MetaData<OgrSourceDataset, VectorResultDescriptor, VectorQueryRectangle>>,
1✔
1512
        > = db1.meta_data(&id.into()).await;
1✔
1513

1514
        assert!(meta.is_ok());
1✔
1515

1516
        let meta: geoengine_operators::util::Result<
1✔
1517
            Box<dyn MetaData<OgrSourceDataset, VectorResultDescriptor, VectorQueryRectangle>>,
1✔
1518
        > = db2.meta_data(&id.into()).await;
1✔
1519

1520
        assert!(meta.is_err());
1✔
1521

1522
        db1.add_permission(session2.user.id.into(), id, Permission::Read)
1✔
1523
            .await
1✔
1524
            .unwrap();
1✔
1525

1526
        let meta: geoengine_operators::util::Result<
1✔
1527
            Box<dyn MetaData<OgrSourceDataset, VectorResultDescriptor, VectorQueryRectangle>>,
1✔
1528
        > = db2.meta_data(&id.into()).await;
1✔
1529

1530
        assert!(meta.is_ok());
1✔
1531
    }
1✔
1532

1533
    #[allow(clippy::too_many_lines)]
1534
    #[ge_context::test]
1✔
1535
    async fn it_loads_all_meta_data_types(app_ctx: PostgresContext<NoTls>) {
1✔
1536
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
1537

1✔
1538
        let db = app_ctx.session_context(session.clone()).db();
1✔
1539

1✔
1540
        let vector_descriptor = VectorResultDescriptor {
1✔
1541
            data_type: VectorDataType::Data,
1✔
1542
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1543
            columns: Default::default(),
1✔
1544
            time: None,
1✔
1545
            bbox: None,
1✔
1546
        };
1✔
1547

1✔
1548
        let raster_descriptor = RasterResultDescriptor {
1✔
1549
            data_type: RasterDataType::U8,
1✔
1550
            spatial_reference: SpatialReferenceOption::Unreferenced,
1✔
1551
            time: None,
1✔
1552
            bbox: None,
1✔
1553
            resolution: None,
1✔
1554
            bands: RasterBandDescriptors::new_single_band(),
1✔
1555
        };
1✔
1556

1✔
1557
        let vector_ds = AddDataset {
1✔
1558
            name: None,
1✔
1559
            display_name: "OgrDataset".to_string(),
1✔
1560
            description: "My Ogr dataset".to_string(),
1✔
1561
            source_operator: "OgrSource".to_string(),
1✔
1562
            symbology: None,
1✔
1563
            provenance: None,
1✔
1564
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1565
        };
1✔
1566

1✔
1567
        let raster_ds = AddDataset {
1✔
1568
            name: None,
1✔
1569
            display_name: "GdalDataset".to_string(),
1✔
1570
            description: "My Gdal dataset".to_string(),
1✔
1571
            source_operator: "GdalSource".to_string(),
1✔
1572
            symbology: None,
1✔
1573
            provenance: None,
1✔
1574
            tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
1575
        };
1✔
1576

1✔
1577
        let gdal_params = GdalDatasetParameters {
1✔
1578
            file_path: Default::default(),
1✔
1579
            rasterband_channel: 0,
1✔
1580
            geo_transform: GdalDatasetGeoTransform {
1✔
1581
                origin_coordinate: Default::default(),
1✔
1582
                x_pixel_size: 0.0,
1✔
1583
                y_pixel_size: 0.0,
1✔
1584
            },
1✔
1585
            width: 0,
1✔
1586
            height: 0,
1✔
1587
            file_not_found_handling: FileNotFoundHandling::NoData,
1✔
1588
            no_data_value: None,
1✔
1589
            properties_mapping: None,
1✔
1590
            gdal_open_options: None,
1✔
1591
            gdal_config_options: None,
1✔
1592
            allow_alphaband_as_mask: false,
1✔
1593
            retry: None,
1✔
1594
        };
1✔
1595

1✔
1596
        let meta = StaticMetaData {
1✔
1597
            loading_info: OgrSourceDataset {
1✔
1598
                file_name: Default::default(),
1✔
1599
                layer_name: String::new(),
1✔
1600
                data_type: None,
1✔
1601
                time: Default::default(),
1✔
1602
                default_geometry: None,
1✔
1603
                columns: None,
1✔
1604
                force_ogr_time_filter: false,
1✔
1605
                force_ogr_spatial_filter: false,
1✔
1606
                on_error: OgrSourceErrorSpec::Ignore,
1✔
1607
                sql_query: None,
1✔
1608
                attribute_query: None,
1✔
1609
                cache_ttl: CacheTtlSeconds::default(),
1✔
1610
            },
1✔
1611
            result_descriptor: vector_descriptor.clone(),
1✔
1612
            phantom: Default::default(),
1✔
1613
        };
1✔
1614

1615
        let id = db.add_dataset(vector_ds, meta.into()).await.unwrap().id;
1✔
1616

1617
        let meta: geoengine_operators::util::Result<
1✔
1618
            Box<dyn MetaData<OgrSourceDataset, VectorResultDescriptor, VectorQueryRectangle>>,
1✔
1619
        > = db.meta_data(&id.into()).await;
1✔
1620

1621
        assert!(meta.is_ok());
1✔
1622

1623
        let meta = GdalMetaDataRegular {
1✔
1624
            result_descriptor: raster_descriptor.clone(),
1✔
1625
            params: gdal_params.clone(),
1✔
1626
            time_placeholders: Default::default(),
1✔
1627
            data_time: Default::default(),
1✔
1628
            step: TimeStep {
1✔
1629
                granularity: TimeGranularity::Millis,
1✔
1630
                step: 0,
1✔
1631
            },
1✔
1632
            cache_ttl: CacheTtlSeconds::default(),
1✔
1633
        };
1✔
1634

1635
        let id = db
1✔
1636
            .add_dataset(raster_ds.clone(), meta.into())
1✔
1637
            .await
1✔
1638
            .unwrap()
1✔
1639
            .id;
1640

1641
        let meta: geoengine_operators::util::Result<
1✔
1642
            Box<dyn MetaData<GdalLoadingInfo, RasterResultDescriptor, RasterQueryRectangle>>,
1✔
1643
        > = db.meta_data(&id.into()).await;
1✔
1644

1645
        assert!(meta.is_ok());
1✔
1646

1647
        let meta = GdalMetaDataStatic {
1✔
1648
            time: None,
1✔
1649
            params: gdal_params.clone(),
1✔
1650
            result_descriptor: raster_descriptor.clone(),
1✔
1651
            cache_ttl: CacheTtlSeconds::default(),
1✔
1652
        };
1✔
1653

1654
        let id = db
1✔
1655
            .add_dataset(raster_ds.clone(), meta.into())
1✔
1656
            .await
1✔
1657
            .unwrap()
1✔
1658
            .id;
1659

1660
        let meta: geoengine_operators::util::Result<
1✔
1661
            Box<dyn MetaData<GdalLoadingInfo, RasterResultDescriptor, RasterQueryRectangle>>,
1✔
1662
        > = db.meta_data(&id.into()).await;
1✔
1663

1664
        assert!(meta.is_ok());
1✔
1665

1666
        let meta = GdalMetaDataList {
1✔
1667
            result_descriptor: raster_descriptor.clone(),
1✔
1668
            params: vec![],
1✔
1669
        };
1✔
1670

1671
        let id = db
1✔
1672
            .add_dataset(raster_ds.clone(), meta.into())
1✔
1673
            .await
1✔
1674
            .unwrap()
1✔
1675
            .id;
1676

1677
        let meta: geoengine_operators::util::Result<
1✔
1678
            Box<dyn MetaData<GdalLoadingInfo, RasterResultDescriptor, RasterQueryRectangle>>,
1✔
1679
        > = db.meta_data(&id.into()).await;
1✔
1680

1681
        assert!(meta.is_ok());
1✔
1682

1683
        let meta = GdalMetadataNetCdfCf {
1✔
1684
            result_descriptor: raster_descriptor.clone(),
1✔
1685
            params: gdal_params.clone(),
1✔
1686
            start: TimeInstance::MIN,
1✔
1687
            end: TimeInstance::MAX,
1✔
1688
            step: TimeStep {
1✔
1689
                granularity: TimeGranularity::Millis,
1✔
1690
                step: 0,
1✔
1691
            },
1✔
1692
            band_offset: 0,
1✔
1693
            cache_ttl: CacheTtlSeconds::default(),
1✔
1694
        };
1✔
1695

1696
        let id = db
1✔
1697
            .add_dataset(raster_ds.clone(), meta.into())
1✔
1698
            .await
1✔
1699
            .unwrap()
1✔
1700
            .id;
1701

1702
        let meta: geoengine_operators::util::Result<
1✔
1703
            Box<dyn MetaData<GdalLoadingInfo, RasterResultDescriptor, RasterQueryRectangle>>,
1✔
1704
        > = db.meta_data(&id.into()).await;
1✔
1705

1706
        assert!(meta.is_ok());
1✔
1707
    }
1✔
1708

1709
    #[ge_context::test]
1✔
1710
    async fn it_secures_uploads(app_ctx: PostgresContext<NoTls>) {
1✔
1711
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1712
        let session2 = app_ctx.create_anonymous_session().await.unwrap();
1✔
1713

1✔
1714
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
1715
        let db2 = app_ctx.session_context(session2.clone()).db();
1✔
1716

1✔
1717
        let upload_id = UploadId::new();
1✔
1718

1✔
1719
        let upload = Upload {
1✔
1720
            id: upload_id,
1✔
1721
            files: vec![FileUpload {
1✔
1722
                id: FileId::new(),
1✔
1723
                name: "test.bin".to_owned(),
1✔
1724
                byte_size: 1024,
1✔
1725
            }],
1✔
1726
        };
1✔
1727

1✔
1728
        db1.create_upload(upload).await.unwrap();
1✔
1729

1✔
1730
        assert!(db1.load_upload(upload_id).await.is_ok());
1✔
1731

1732
        assert!(db2.load_upload(upload_id).await.is_err());
1✔
1733
    }
1✔
1734

1735
    #[allow(clippy::too_many_lines)]
1736
    #[ge_context::test]
1✔
1737
    async fn it_collects_layers(app_ctx: PostgresContext<NoTls>) {
1✔
1738
        let session = admin_login(&app_ctx).await;
1✔
1739

1740
        let layer_db = app_ctx.session_context(session).db();
1✔
1741

1✔
1742
        let workflow = Workflow {
1✔
1743
            operator: TypedOperator::Vector(
1✔
1744
                MockPointSource {
1✔
1745
                    params: MockPointSourceParams {
1✔
1746
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
1747
                    },
1✔
1748
                }
1✔
1749
                .boxed(),
1✔
1750
            ),
1✔
1751
        };
1✔
1752

1753
        let root_collection_id = layer_db.get_root_layer_collection_id().await.unwrap();
1✔
1754

1755
        let layer1 = layer_db
1✔
1756
            .add_layer(
1✔
1757
                AddLayer {
1✔
1758
                    name: "Layer1".to_string(),
1✔
1759
                    description: "Layer 1".to_string(),
1✔
1760
                    symbology: None,
1✔
1761
                    workflow: workflow.clone(),
1✔
1762
                    metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
1763
                    properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
1764
                },
1✔
1765
                &root_collection_id,
1✔
1766
            )
1✔
1767
            .await
1✔
1768
            .unwrap();
1✔
1769

1770
        assert_eq!(
1✔
1771
            layer_db.load_layer(&layer1).await.unwrap(),
1✔
1772
            crate::layers::layer::Layer {
1✔
1773
                id: ProviderLayerId {
1✔
1774
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
1775
                    layer_id: layer1.clone(),
1✔
1776
                },
1✔
1777
                name: "Layer1".to_string(),
1✔
1778
                description: "Layer 1".to_string(),
1✔
1779
                symbology: None,
1✔
1780
                workflow: workflow.clone(),
1✔
1781
                metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
1782
                properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
1783
            }
1✔
1784
        );
1785

1786
        let collection1_id = layer_db
1✔
1787
            .add_layer_collection(
1✔
1788
                AddLayerCollection {
1✔
1789
                    name: "Collection1".to_string(),
1✔
1790
                    description: "Collection 1".to_string(),
1✔
1791
                    properties: Default::default(),
1✔
1792
                },
1✔
1793
                &root_collection_id,
1✔
1794
            )
1✔
1795
            .await
1✔
1796
            .unwrap();
1✔
1797

1798
        let layer2 = layer_db
1✔
1799
            .add_layer(
1✔
1800
                AddLayer {
1✔
1801
                    name: "Layer2".to_string(),
1✔
1802
                    description: "Layer 2".to_string(),
1✔
1803
                    symbology: None,
1✔
1804
                    workflow: workflow.clone(),
1✔
1805
                    metadata: Default::default(),
1✔
1806
                    properties: Default::default(),
1✔
1807
                },
1✔
1808
                &collection1_id,
1✔
1809
            )
1✔
1810
            .await
1✔
1811
            .unwrap();
1✔
1812

1813
        let collection2_id = layer_db
1✔
1814
            .add_layer_collection(
1✔
1815
                AddLayerCollection {
1✔
1816
                    name: "Collection2".to_string(),
1✔
1817
                    description: "Collection 2".to_string(),
1✔
1818
                    properties: Default::default(),
1✔
1819
                },
1✔
1820
                &collection1_id,
1✔
1821
            )
1✔
1822
            .await
1✔
1823
            .unwrap();
1✔
1824

1✔
1825
        layer_db
1✔
1826
            .add_collection_to_parent(&collection2_id, &collection1_id)
1✔
1827
            .await
1✔
1828
            .unwrap();
1✔
1829

1830
        let root_collection = layer_db
1✔
1831
            .load_layer_collection(
1✔
1832
                &root_collection_id,
1✔
1833
                LayerCollectionListOptions {
1✔
1834
                    offset: 0,
1✔
1835
                    limit: 20,
1✔
1836
                },
1✔
1837
            )
1✔
1838
            .await
1✔
1839
            .unwrap();
1✔
1840

1✔
1841
        assert_eq!(
1✔
1842
            root_collection,
1✔
1843
            LayerCollection {
1✔
1844
                id: ProviderLayerCollectionId {
1✔
1845
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
1846
                    collection_id: root_collection_id,
1✔
1847
                },
1✔
1848
                name: "Layers".to_string(),
1✔
1849
                description: "All available Geo Engine layers".to_string(),
1✔
1850
                items: vec![
1✔
1851
                    CollectionItem::Collection(LayerCollectionListing {
1✔
1852
                        id: ProviderLayerCollectionId {
1✔
1853
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
1854
                            collection_id: collection1_id.clone(),
1✔
1855
                        },
1✔
1856
                        name: "Collection1".to_string(),
1✔
1857
                        description: "Collection 1".to_string(),
1✔
1858
                        properties: Default::default(),
1✔
1859
                    }),
1✔
1860
                    CollectionItem::Collection(LayerCollectionListing {
1✔
1861
                        id: ProviderLayerCollectionId {
1✔
1862
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
1863
                            collection_id: LayerCollectionId(UNSORTED_COLLECTION_ID.to_string()),
1✔
1864
                        },
1✔
1865
                        name: "Unsorted".to_string(),
1✔
1866
                        description: "Unsorted Layers".to_string(),
1✔
1867
                        properties: Default::default(),
1✔
1868
                    }),
1✔
1869
                    CollectionItem::Layer(LayerListing {
1✔
1870
                        id: ProviderLayerId {
1✔
1871
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
1872
                            layer_id: layer1,
1✔
1873
                        },
1✔
1874
                        name: "Layer1".to_string(),
1✔
1875
                        description: "Layer 1".to_string(),
1✔
1876
                        properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
1877
                    })
1✔
1878
                ],
1✔
1879
                entry_label: None,
1✔
1880
                properties: vec![],
1✔
1881
            }
1✔
1882
        );
1✔
1883

1884
        let collection1 = layer_db
1✔
1885
            .load_layer_collection(
1✔
1886
                &collection1_id,
1✔
1887
                LayerCollectionListOptions {
1✔
1888
                    offset: 0,
1✔
1889
                    limit: 20,
1✔
1890
                },
1✔
1891
            )
1✔
1892
            .await
1✔
1893
            .unwrap();
1✔
1894

1✔
1895
        assert_eq!(
1✔
1896
            collection1,
1✔
1897
            LayerCollection {
1✔
1898
                id: ProviderLayerCollectionId {
1✔
1899
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
1900
                    collection_id: collection1_id,
1✔
1901
                },
1✔
1902
                name: "Collection1".to_string(),
1✔
1903
                description: "Collection 1".to_string(),
1✔
1904
                items: vec![
1✔
1905
                    CollectionItem::Collection(LayerCollectionListing {
1✔
1906
                        id: ProviderLayerCollectionId {
1✔
1907
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
1908
                            collection_id: collection2_id,
1✔
1909
                        },
1✔
1910
                        name: "Collection2".to_string(),
1✔
1911
                        description: "Collection 2".to_string(),
1✔
1912
                        properties: Default::default(),
1✔
1913
                    }),
1✔
1914
                    CollectionItem::Layer(LayerListing {
1✔
1915
                        id: ProviderLayerId {
1✔
1916
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
1917
                            layer_id: layer2,
1✔
1918
                        },
1✔
1919
                        name: "Layer2".to_string(),
1✔
1920
                        description: "Layer 2".to_string(),
1✔
1921
                        properties: vec![],
1✔
1922
                    })
1✔
1923
                ],
1✔
1924
                entry_label: None,
1✔
1925
                properties: vec![],
1✔
1926
            }
1✔
1927
        );
1✔
1928
    }
1✔
1929

1930
    #[allow(clippy::too_many_lines)]
1931
    #[ge_context::test]
1✔
1932
    async fn it_searches_layers(app_ctx: PostgresContext<NoTls>) {
1✔
1933
        let session = admin_login(&app_ctx).await;
1✔
1934

1935
        let layer_db = app_ctx.session_context(session).db();
1✔
1936

1✔
1937
        let workflow = Workflow {
1✔
1938
            operator: TypedOperator::Vector(
1✔
1939
                MockPointSource {
1✔
1940
                    params: MockPointSourceParams {
1✔
1941
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
1942
                    },
1✔
1943
                }
1✔
1944
                .boxed(),
1✔
1945
            ),
1✔
1946
        };
1✔
1947

1948
        let root_collection_id = layer_db.get_root_layer_collection_id().await.unwrap();
1✔
1949

1950
        let layer1 = layer_db
1✔
1951
            .add_layer(
1✔
1952
                AddLayer {
1✔
1953
                    name: "Layer1".to_string(),
1✔
1954
                    description: "Layer 1".to_string(),
1✔
1955
                    symbology: None,
1✔
1956
                    workflow: workflow.clone(),
1✔
1957
                    metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
1958
                    properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
1959
                },
1✔
1960
                &root_collection_id,
1✔
1961
            )
1✔
1962
            .await
1✔
1963
            .unwrap();
1✔
1964

1965
        let collection1_id = layer_db
1✔
1966
            .add_layer_collection(
1✔
1967
                AddLayerCollection {
1✔
1968
                    name: "Collection1".to_string(),
1✔
1969
                    description: "Collection 1".to_string(),
1✔
1970
                    properties: Default::default(),
1✔
1971
                },
1✔
1972
                &root_collection_id,
1✔
1973
            )
1✔
1974
            .await
1✔
1975
            .unwrap();
1✔
1976

1977
        let layer2 = layer_db
1✔
1978
            .add_layer(
1✔
1979
                AddLayer {
1✔
1980
                    name: "Layer2".to_string(),
1✔
1981
                    description: "Layer 2".to_string(),
1✔
1982
                    symbology: None,
1✔
1983
                    workflow: workflow.clone(),
1✔
1984
                    metadata: Default::default(),
1✔
1985
                    properties: Default::default(),
1✔
1986
                },
1✔
1987
                &collection1_id,
1✔
1988
            )
1✔
1989
            .await
1✔
1990
            .unwrap();
1✔
1991

1992
        let collection2_id = layer_db
1✔
1993
            .add_layer_collection(
1✔
1994
                AddLayerCollection {
1✔
1995
                    name: "Collection2".to_string(),
1✔
1996
                    description: "Collection 2".to_string(),
1✔
1997
                    properties: Default::default(),
1✔
1998
                },
1✔
1999
                &collection1_id,
1✔
2000
            )
1✔
2001
            .await
1✔
2002
            .unwrap();
1✔
2003

2004
        let root_collection_all = layer_db
1✔
2005
            .search(
1✔
2006
                &root_collection_id,
1✔
2007
                SearchParameters {
1✔
2008
                    search_type: SearchType::Fulltext,
1✔
2009
                    search_string: String::new(),
1✔
2010
                    limit: 10,
1✔
2011
                    offset: 0,
1✔
2012
                },
1✔
2013
            )
1✔
2014
            .await
1✔
2015
            .unwrap();
1✔
2016

1✔
2017
        assert_eq!(
1✔
2018
            root_collection_all,
1✔
2019
            LayerCollection {
1✔
2020
                id: ProviderLayerCollectionId {
1✔
2021
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2022
                    collection_id: root_collection_id.clone(),
1✔
2023
                },
1✔
2024
                name: "Layers".to_string(),
1✔
2025
                description: "All available Geo Engine layers".to_string(),
1✔
2026
                items: vec![
1✔
2027
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2028
                        id: ProviderLayerCollectionId {
1✔
2029
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2030
                            collection_id: collection1_id.clone(),
1✔
2031
                        },
1✔
2032
                        name: "Collection1".to_string(),
1✔
2033
                        description: "Collection 1".to_string(),
1✔
2034
                        properties: Default::default(),
1✔
2035
                    }),
1✔
2036
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2037
                        id: ProviderLayerCollectionId {
1✔
2038
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2039
                            collection_id: collection2_id.clone(),
1✔
2040
                        },
1✔
2041
                        name: "Collection2".to_string(),
1✔
2042
                        description: "Collection 2".to_string(),
1✔
2043
                        properties: Default::default(),
1✔
2044
                    }),
1✔
2045
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2046
                        id: ProviderLayerCollectionId {
1✔
2047
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2048
                            collection_id: LayerCollectionId(
1✔
2049
                                "ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f".to_string()
1✔
2050
                            ),
1✔
2051
                        },
1✔
2052
                        name: "Unsorted".to_string(),
1✔
2053
                        description: "Unsorted Layers".to_string(),
1✔
2054
                        properties: Default::default(),
1✔
2055
                    }),
1✔
2056
                    CollectionItem::Layer(LayerListing {
1✔
2057
                        id: ProviderLayerId {
1✔
2058
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2059
                            layer_id: layer1.clone(),
1✔
2060
                        },
1✔
2061
                        name: "Layer1".to_string(),
1✔
2062
                        description: "Layer 1".to_string(),
1✔
2063
                        properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2064
                    }),
1✔
2065
                    CollectionItem::Layer(LayerListing {
1✔
2066
                        id: ProviderLayerId {
1✔
2067
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2068
                            layer_id: layer2.clone(),
1✔
2069
                        },
1✔
2070
                        name: "Layer2".to_string(),
1✔
2071
                        description: "Layer 2".to_string(),
1✔
2072
                        properties: vec![],
1✔
2073
                    }),
1✔
2074
                ],
1✔
2075
                entry_label: None,
1✔
2076
                properties: vec![],
1✔
2077
            }
1✔
2078
        );
1✔
2079

2080
        let root_collection_filtered = layer_db
1✔
2081
            .search(
1✔
2082
                &root_collection_id,
1✔
2083
                SearchParameters {
1✔
2084
                    search_type: SearchType::Fulltext,
1✔
2085
                    search_string: "lection".to_string(),
1✔
2086
                    limit: 10,
1✔
2087
                    offset: 0,
1✔
2088
                },
1✔
2089
            )
1✔
2090
            .await
1✔
2091
            .unwrap();
1✔
2092

1✔
2093
        assert_eq!(
1✔
2094
            root_collection_filtered,
1✔
2095
            LayerCollection {
1✔
2096
                id: ProviderLayerCollectionId {
1✔
2097
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2098
                    collection_id: root_collection_id.clone(),
1✔
2099
                },
1✔
2100
                name: "Layers".to_string(),
1✔
2101
                description: "All available Geo Engine layers".to_string(),
1✔
2102
                items: vec![
1✔
2103
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2104
                        id: ProviderLayerCollectionId {
1✔
2105
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2106
                            collection_id: collection1_id.clone(),
1✔
2107
                        },
1✔
2108
                        name: "Collection1".to_string(),
1✔
2109
                        description: "Collection 1".to_string(),
1✔
2110
                        properties: Default::default(),
1✔
2111
                    }),
1✔
2112
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2113
                        id: ProviderLayerCollectionId {
1✔
2114
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2115
                            collection_id: collection2_id.clone(),
1✔
2116
                        },
1✔
2117
                        name: "Collection2".to_string(),
1✔
2118
                        description: "Collection 2".to_string(),
1✔
2119
                        properties: Default::default(),
1✔
2120
                    }),
1✔
2121
                ],
1✔
2122
                entry_label: None,
1✔
2123
                properties: vec![],
1✔
2124
            }
1✔
2125
        );
1✔
2126

2127
        let collection1_all = layer_db
1✔
2128
            .search(
1✔
2129
                &collection1_id,
1✔
2130
                SearchParameters {
1✔
2131
                    search_type: SearchType::Fulltext,
1✔
2132
                    search_string: String::new(),
1✔
2133
                    limit: 10,
1✔
2134
                    offset: 0,
1✔
2135
                },
1✔
2136
            )
1✔
2137
            .await
1✔
2138
            .unwrap();
1✔
2139

1✔
2140
        assert_eq!(
1✔
2141
            collection1_all,
1✔
2142
            LayerCollection {
1✔
2143
                id: ProviderLayerCollectionId {
1✔
2144
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2145
                    collection_id: collection1_id.clone(),
1✔
2146
                },
1✔
2147
                name: "Collection1".to_string(),
1✔
2148
                description: "Collection 1".to_string(),
1✔
2149
                items: vec![
1✔
2150
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2151
                        id: ProviderLayerCollectionId {
1✔
2152
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2153
                            collection_id: collection2_id.clone(),
1✔
2154
                        },
1✔
2155
                        name: "Collection2".to_string(),
1✔
2156
                        description: "Collection 2".to_string(),
1✔
2157
                        properties: Default::default(),
1✔
2158
                    }),
1✔
2159
                    CollectionItem::Layer(LayerListing {
1✔
2160
                        id: ProviderLayerId {
1✔
2161
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2162
                            layer_id: layer2.clone(),
1✔
2163
                        },
1✔
2164
                        name: "Layer2".to_string(),
1✔
2165
                        description: "Layer 2".to_string(),
1✔
2166
                        properties: vec![],
1✔
2167
                    }),
1✔
2168
                ],
1✔
2169
                entry_label: None,
1✔
2170
                properties: vec![],
1✔
2171
            }
1✔
2172
        );
1✔
2173

2174
        let collection1_filtered_fulltext = layer_db
1✔
2175
            .search(
1✔
2176
                &collection1_id,
1✔
2177
                SearchParameters {
1✔
2178
                    search_type: SearchType::Fulltext,
1✔
2179
                    search_string: "ay".to_string(),
1✔
2180
                    limit: 10,
1✔
2181
                    offset: 0,
1✔
2182
                },
1✔
2183
            )
1✔
2184
            .await
1✔
2185
            .unwrap();
1✔
2186

1✔
2187
        assert_eq!(
1✔
2188
            collection1_filtered_fulltext,
1✔
2189
            LayerCollection {
1✔
2190
                id: ProviderLayerCollectionId {
1✔
2191
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2192
                    collection_id: collection1_id.clone(),
1✔
2193
                },
1✔
2194
                name: "Collection1".to_string(),
1✔
2195
                description: "Collection 1".to_string(),
1✔
2196
                items: vec![CollectionItem::Layer(LayerListing {
1✔
2197
                    id: ProviderLayerId {
1✔
2198
                        provider_id: INTERNAL_PROVIDER_ID,
1✔
2199
                        layer_id: layer2.clone(),
1✔
2200
                    },
1✔
2201
                    name: "Layer2".to_string(),
1✔
2202
                    description: "Layer 2".to_string(),
1✔
2203
                    properties: vec![],
1✔
2204
                }),],
1✔
2205
                entry_label: None,
1✔
2206
                properties: vec![],
1✔
2207
            }
1✔
2208
        );
1✔
2209

2210
        let collection1_filtered_prefix = layer_db
1✔
2211
            .search(
1✔
2212
                &collection1_id,
1✔
2213
                SearchParameters {
1✔
2214
                    search_type: SearchType::Prefix,
1✔
2215
                    search_string: "ay".to_string(),
1✔
2216
                    limit: 10,
1✔
2217
                    offset: 0,
1✔
2218
                },
1✔
2219
            )
1✔
2220
            .await
1✔
2221
            .unwrap();
1✔
2222

1✔
2223
        assert_eq!(
1✔
2224
            collection1_filtered_prefix,
1✔
2225
            LayerCollection {
1✔
2226
                id: ProviderLayerCollectionId {
1✔
2227
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2228
                    collection_id: collection1_id.clone(),
1✔
2229
                },
1✔
2230
                name: "Collection1".to_string(),
1✔
2231
                description: "Collection 1".to_string(),
1✔
2232
                items: vec![],
1✔
2233
                entry_label: None,
1✔
2234
                properties: vec![],
1✔
2235
            }
1✔
2236
        );
1✔
2237

2238
        let collection1_filtered_prefix2 = layer_db
1✔
2239
            .search(
1✔
2240
                &collection1_id,
1✔
2241
                SearchParameters {
1✔
2242
                    search_type: SearchType::Prefix,
1✔
2243
                    search_string: "Lay".to_string(),
1✔
2244
                    limit: 10,
1✔
2245
                    offset: 0,
1✔
2246
                },
1✔
2247
            )
1✔
2248
            .await
1✔
2249
            .unwrap();
1✔
2250

1✔
2251
        assert_eq!(
1✔
2252
            collection1_filtered_prefix2,
1✔
2253
            LayerCollection {
1✔
2254
                id: ProviderLayerCollectionId {
1✔
2255
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2256
                    collection_id: collection1_id.clone(),
1✔
2257
                },
1✔
2258
                name: "Collection1".to_string(),
1✔
2259
                description: "Collection 1".to_string(),
1✔
2260
                items: vec![CollectionItem::Layer(LayerListing {
1✔
2261
                    id: ProviderLayerId {
1✔
2262
                        provider_id: INTERNAL_PROVIDER_ID,
1✔
2263
                        layer_id: layer2.clone(),
1✔
2264
                    },
1✔
2265
                    name: "Layer2".to_string(),
1✔
2266
                    description: "Layer 2".to_string(),
1✔
2267
                    properties: vec![],
1✔
2268
                }),],
1✔
2269
                entry_label: None,
1✔
2270
                properties: vec![],
1✔
2271
            }
1✔
2272
        );
1✔
2273
    }
1✔
2274

2275
    #[allow(clippy::too_many_lines)]
2276
    #[ge_context::test]
1✔
2277
    async fn it_searches_layers_with_permissions(app_ctx: PostgresContext<NoTls>) {
1✔
2278
        let admin_session = admin_login(&app_ctx).await;
1✔
2279
        let admin_layer_db = app_ctx.session_context(admin_session).db();
1✔
2280

2281
        let user_session = app_ctx.create_anonymous_session().await.unwrap();
1✔
2282
        let user_layer_db = app_ctx.session_context(user_session.clone()).db();
1✔
2283

1✔
2284
        let workflow = Workflow {
1✔
2285
            operator: TypedOperator::Vector(
1✔
2286
                MockPointSource {
1✔
2287
                    params: MockPointSourceParams {
1✔
2288
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
2289
                    },
1✔
2290
                }
1✔
2291
                .boxed(),
1✔
2292
            ),
1✔
2293
        };
1✔
2294

2295
        let root_collection_id = admin_layer_db.get_root_layer_collection_id().await.unwrap();
1✔
2296

2297
        let layer1 = admin_layer_db
1✔
2298
            .add_layer(
1✔
2299
                AddLayer {
1✔
2300
                    name: "Layer1".to_string(),
1✔
2301
                    description: "Layer 1".to_string(),
1✔
2302
                    symbology: None,
1✔
2303
                    workflow: workflow.clone(),
1✔
2304
                    metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
2305
                    properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2306
                },
1✔
2307
                &root_collection_id,
1✔
2308
            )
1✔
2309
            .await
1✔
2310
            .unwrap();
1✔
2311

2312
        let collection1_id = admin_layer_db
1✔
2313
            .add_layer_collection(
1✔
2314
                AddLayerCollection {
1✔
2315
                    name: "Collection1".to_string(),
1✔
2316
                    description: "Collection 1".to_string(),
1✔
2317
                    properties: Default::default(),
1✔
2318
                },
1✔
2319
                &root_collection_id,
1✔
2320
            )
1✔
2321
            .await
1✔
2322
            .unwrap();
1✔
2323

2324
        let layer2 = admin_layer_db
1✔
2325
            .add_layer(
1✔
2326
                AddLayer {
1✔
2327
                    name: "Layer2".to_string(),
1✔
2328
                    description: "Layer 2".to_string(),
1✔
2329
                    symbology: None,
1✔
2330
                    workflow: workflow.clone(),
1✔
2331
                    metadata: Default::default(),
1✔
2332
                    properties: Default::default(),
1✔
2333
                },
1✔
2334
                &collection1_id,
1✔
2335
            )
1✔
2336
            .await
1✔
2337
            .unwrap();
1✔
2338

2339
        let collection2_id = admin_layer_db
1✔
2340
            .add_layer_collection(
1✔
2341
                AddLayerCollection {
1✔
2342
                    name: "Collection2".to_string(),
1✔
2343
                    description: "Collection 2".to_string(),
1✔
2344
                    properties: Default::default(),
1✔
2345
                },
1✔
2346
                &collection1_id,
1✔
2347
            )
1✔
2348
            .await
1✔
2349
            .unwrap();
1✔
2350

2351
        let collection3_id = admin_layer_db
1✔
2352
            .add_layer_collection(
1✔
2353
                AddLayerCollection {
1✔
2354
                    name: "Collection3".to_string(),
1✔
2355
                    description: "Collection 3".to_string(),
1✔
2356
                    properties: Default::default(),
1✔
2357
                },
1✔
2358
                &collection1_id,
1✔
2359
            )
1✔
2360
            .await
1✔
2361
            .unwrap();
1✔
2362

2363
        let layer3 = admin_layer_db
1✔
2364
            .add_layer(
1✔
2365
                AddLayer {
1✔
2366
                    name: "Layer3".to_string(),
1✔
2367
                    description: "Layer 3".to_string(),
1✔
2368
                    symbology: None,
1✔
2369
                    workflow: workflow.clone(),
1✔
2370
                    metadata: Default::default(),
1✔
2371
                    properties: Default::default(),
1✔
2372
                },
1✔
2373
                &collection2_id,
1✔
2374
            )
1✔
2375
            .await
1✔
2376
            .unwrap();
1✔
2377

1✔
2378
        // Grant user permissions for collection1, collection2, layer1 and layer2
1✔
2379
        admin_layer_db
1✔
2380
            .add_permission(
1✔
2381
                user_session.user.id.into(),
1✔
2382
                collection1_id.clone(),
1✔
2383
                Permission::Read,
1✔
2384
            )
1✔
2385
            .await
1✔
2386
            .unwrap();
1✔
2387

1✔
2388
        admin_layer_db
1✔
2389
            .add_permission(
1✔
2390
                user_session.user.id.into(),
1✔
2391
                collection2_id.clone(),
1✔
2392
                Permission::Read,
1✔
2393
            )
1✔
2394
            .await
1✔
2395
            .unwrap();
1✔
2396

1✔
2397
        admin_layer_db
1✔
2398
            .add_permission(
1✔
2399
                user_session.user.id.into(),
1✔
2400
                layer1.clone(),
1✔
2401
                Permission::Read,
1✔
2402
            )
1✔
2403
            .await
1✔
2404
            .unwrap();
1✔
2405

1✔
2406
        admin_layer_db
1✔
2407
            .add_permission(
1✔
2408
                user_session.user.id.into(),
1✔
2409
                layer2.clone(),
1✔
2410
                Permission::Read,
1✔
2411
            )
1✔
2412
            .await
1✔
2413
            .unwrap();
1✔
2414

2415
        // Ensure admin sees everything we added
2416
        let admin_root_collection_all = admin_layer_db
1✔
2417
            .search(
1✔
2418
                &root_collection_id,
1✔
2419
                SearchParameters {
1✔
2420
                    search_type: SearchType::Fulltext,
1✔
2421
                    search_string: String::new(),
1✔
2422
                    limit: 10,
1✔
2423
                    offset: 0,
1✔
2424
                },
1✔
2425
            )
1✔
2426
            .await
1✔
2427
            .unwrap();
1✔
2428

1✔
2429
        assert_eq!(
1✔
2430
            admin_root_collection_all,
1✔
2431
            LayerCollection {
1✔
2432
                id: ProviderLayerCollectionId {
1✔
2433
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2434
                    collection_id: root_collection_id.clone(),
1✔
2435
                },
1✔
2436
                name: "Layers".to_string(),
1✔
2437
                description: "All available Geo Engine layers".to_string(),
1✔
2438
                items: vec![
1✔
2439
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2440
                        id: ProviderLayerCollectionId {
1✔
2441
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2442
                            collection_id: collection1_id.clone(),
1✔
2443
                        },
1✔
2444
                        name: "Collection1".to_string(),
1✔
2445
                        description: "Collection 1".to_string(),
1✔
2446
                        properties: Default::default(),
1✔
2447
                    }),
1✔
2448
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2449
                        id: ProviderLayerCollectionId {
1✔
2450
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2451
                            collection_id: collection2_id.clone(),
1✔
2452
                        },
1✔
2453
                        name: "Collection2".to_string(),
1✔
2454
                        description: "Collection 2".to_string(),
1✔
2455
                        properties: Default::default(),
1✔
2456
                    }),
1✔
2457
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2458
                        id: ProviderLayerCollectionId {
1✔
2459
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2460
                            collection_id: collection3_id.clone(),
1✔
2461
                        },
1✔
2462
                        name: "Collection3".to_string(),
1✔
2463
                        description: "Collection 3".to_string(),
1✔
2464
                        properties: Default::default(),
1✔
2465
                    }),
1✔
2466
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2467
                        id: ProviderLayerCollectionId {
1✔
2468
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2469
                            collection_id: LayerCollectionId(
1✔
2470
                                "ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f".to_string()
1✔
2471
                            ),
1✔
2472
                        },
1✔
2473
                        name: "Unsorted".to_string(),
1✔
2474
                        description: "Unsorted Layers".to_string(),
1✔
2475
                        properties: Default::default(),
1✔
2476
                    }),
1✔
2477
                    CollectionItem::Layer(LayerListing {
1✔
2478
                        id: ProviderLayerId {
1✔
2479
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2480
                            layer_id: layer1.clone(),
1✔
2481
                        },
1✔
2482
                        name: "Layer1".to_string(),
1✔
2483
                        description: "Layer 1".to_string(),
1✔
2484
                        properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2485
                    }),
1✔
2486
                    CollectionItem::Layer(LayerListing {
1✔
2487
                        id: ProviderLayerId {
1✔
2488
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2489
                            layer_id: layer2.clone(),
1✔
2490
                        },
1✔
2491
                        name: "Layer2".to_string(),
1✔
2492
                        description: "Layer 2".to_string(),
1✔
2493
                        properties: vec![],
1✔
2494
                    }),
1✔
2495
                    CollectionItem::Layer(LayerListing {
1✔
2496
                        id: ProviderLayerId {
1✔
2497
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2498
                            layer_id: layer3.clone(),
1✔
2499
                        },
1✔
2500
                        name: "Layer3".to_string(),
1✔
2501
                        description: "Layer 3".to_string(),
1✔
2502
                        properties: vec![],
1✔
2503
                    }),
1✔
2504
                ],
1✔
2505
                entry_label: None,
1✔
2506
                properties: vec![],
1✔
2507
            }
1✔
2508
        );
1✔
2509

2510
        let root_collection_all = user_layer_db
1✔
2511
            .search(
1✔
2512
                &root_collection_id,
1✔
2513
                SearchParameters {
1✔
2514
                    search_type: SearchType::Fulltext,
1✔
2515
                    search_string: String::new(),
1✔
2516
                    limit: 10,
1✔
2517
                    offset: 0,
1✔
2518
                },
1✔
2519
            )
1✔
2520
            .await
1✔
2521
            .unwrap();
1✔
2522

1✔
2523
        assert_eq!(
1✔
2524
            root_collection_all,
1✔
2525
            LayerCollection {
1✔
2526
                id: ProviderLayerCollectionId {
1✔
2527
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2528
                    collection_id: root_collection_id.clone(),
1✔
2529
                },
1✔
2530
                name: "Layers".to_string(),
1✔
2531
                description: "All available Geo Engine layers".to_string(),
1✔
2532
                items: vec![
1✔
2533
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2534
                        id: ProviderLayerCollectionId {
1✔
2535
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2536
                            collection_id: collection1_id.clone(),
1✔
2537
                        },
1✔
2538
                        name: "Collection1".to_string(),
1✔
2539
                        description: "Collection 1".to_string(),
1✔
2540
                        properties: Default::default(),
1✔
2541
                    }),
1✔
2542
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2543
                        id: ProviderLayerCollectionId {
1✔
2544
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2545
                            collection_id: collection2_id.clone(),
1✔
2546
                        },
1✔
2547
                        name: "Collection2".to_string(),
1✔
2548
                        description: "Collection 2".to_string(),
1✔
2549
                        properties: Default::default(),
1✔
2550
                    }),
1✔
2551
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2552
                        id: ProviderLayerCollectionId {
1✔
2553
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2554
                            collection_id: LayerCollectionId(
1✔
2555
                                "ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f".to_string()
1✔
2556
                            ),
1✔
2557
                        },
1✔
2558
                        name: "Unsorted".to_string(),
1✔
2559
                        description: "Unsorted Layers".to_string(),
1✔
2560
                        properties: Default::default(),
1✔
2561
                    }),
1✔
2562
                    CollectionItem::Layer(LayerListing {
1✔
2563
                        id: ProviderLayerId {
1✔
2564
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2565
                            layer_id: layer1.clone(),
1✔
2566
                        },
1✔
2567
                        name: "Layer1".to_string(),
1✔
2568
                        description: "Layer 1".to_string(),
1✔
2569
                        properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2570
                    }),
1✔
2571
                    CollectionItem::Layer(LayerListing {
1✔
2572
                        id: ProviderLayerId {
1✔
2573
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2574
                            layer_id: layer2.clone(),
1✔
2575
                        },
1✔
2576
                        name: "Layer2".to_string(),
1✔
2577
                        description: "Layer 2".to_string(),
1✔
2578
                        properties: vec![],
1✔
2579
                    }),
1✔
2580
                ],
1✔
2581
                entry_label: None,
1✔
2582
                properties: vec![],
1✔
2583
            }
1✔
2584
        );
1✔
2585

2586
        let root_collection_filtered = user_layer_db
1✔
2587
            .search(
1✔
2588
                &root_collection_id,
1✔
2589
                SearchParameters {
1✔
2590
                    search_type: SearchType::Fulltext,
1✔
2591
                    search_string: "lection".to_string(),
1✔
2592
                    limit: 10,
1✔
2593
                    offset: 0,
1✔
2594
                },
1✔
2595
            )
1✔
2596
            .await
1✔
2597
            .unwrap();
1✔
2598

1✔
2599
        assert_eq!(
1✔
2600
            root_collection_filtered,
1✔
2601
            LayerCollection {
1✔
2602
                id: ProviderLayerCollectionId {
1✔
2603
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2604
                    collection_id: root_collection_id.clone(),
1✔
2605
                },
1✔
2606
                name: "Layers".to_string(),
1✔
2607
                description: "All available Geo Engine layers".to_string(),
1✔
2608
                items: vec![
1✔
2609
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2610
                        id: ProviderLayerCollectionId {
1✔
2611
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2612
                            collection_id: collection1_id.clone(),
1✔
2613
                        },
1✔
2614
                        name: "Collection1".to_string(),
1✔
2615
                        description: "Collection 1".to_string(),
1✔
2616
                        properties: Default::default(),
1✔
2617
                    }),
1✔
2618
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2619
                        id: ProviderLayerCollectionId {
1✔
2620
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2621
                            collection_id: collection2_id.clone(),
1✔
2622
                        },
1✔
2623
                        name: "Collection2".to_string(),
1✔
2624
                        description: "Collection 2".to_string(),
1✔
2625
                        properties: Default::default(),
1✔
2626
                    }),
1✔
2627
                ],
1✔
2628
                entry_label: None,
1✔
2629
                properties: vec![],
1✔
2630
            }
1✔
2631
        );
1✔
2632

2633
        let collection1_all = user_layer_db
1✔
2634
            .search(
1✔
2635
                &collection1_id,
1✔
2636
                SearchParameters {
1✔
2637
                    search_type: SearchType::Fulltext,
1✔
2638
                    search_string: String::new(),
1✔
2639
                    limit: 10,
1✔
2640
                    offset: 0,
1✔
2641
                },
1✔
2642
            )
1✔
2643
            .await
1✔
2644
            .unwrap();
1✔
2645

1✔
2646
        assert_eq!(
1✔
2647
            collection1_all,
1✔
2648
            LayerCollection {
1✔
2649
                id: ProviderLayerCollectionId {
1✔
2650
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2651
                    collection_id: collection1_id.clone(),
1✔
2652
                },
1✔
2653
                name: "Collection1".to_string(),
1✔
2654
                description: "Collection 1".to_string(),
1✔
2655
                items: vec![
1✔
2656
                    CollectionItem::Collection(LayerCollectionListing {
1✔
2657
                        id: ProviderLayerCollectionId {
1✔
2658
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2659
                            collection_id: collection2_id.clone(),
1✔
2660
                        },
1✔
2661
                        name: "Collection2".to_string(),
1✔
2662
                        description: "Collection 2".to_string(),
1✔
2663
                        properties: Default::default(),
1✔
2664
                    }),
1✔
2665
                    CollectionItem::Layer(LayerListing {
1✔
2666
                        id: ProviderLayerId {
1✔
2667
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
2668
                            layer_id: layer2.clone(),
1✔
2669
                        },
1✔
2670
                        name: "Layer2".to_string(),
1✔
2671
                        description: "Layer 2".to_string(),
1✔
2672
                        properties: vec![],
1✔
2673
                    }),
1✔
2674
                ],
1✔
2675
                entry_label: None,
1✔
2676
                properties: vec![],
1✔
2677
            }
1✔
2678
        );
1✔
2679

2680
        let collection1_filtered_fulltext = user_layer_db
1✔
2681
            .search(
1✔
2682
                &collection1_id,
1✔
2683
                SearchParameters {
1✔
2684
                    search_type: SearchType::Fulltext,
1✔
2685
                    search_string: "ay".to_string(),
1✔
2686
                    limit: 10,
1✔
2687
                    offset: 0,
1✔
2688
                },
1✔
2689
            )
1✔
2690
            .await
1✔
2691
            .unwrap();
1✔
2692

1✔
2693
        assert_eq!(
1✔
2694
            collection1_filtered_fulltext,
1✔
2695
            LayerCollection {
1✔
2696
                id: ProviderLayerCollectionId {
1✔
2697
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2698
                    collection_id: collection1_id.clone(),
1✔
2699
                },
1✔
2700
                name: "Collection1".to_string(),
1✔
2701
                description: "Collection 1".to_string(),
1✔
2702
                items: vec![CollectionItem::Layer(LayerListing {
1✔
2703
                    id: ProviderLayerId {
1✔
2704
                        provider_id: INTERNAL_PROVIDER_ID,
1✔
2705
                        layer_id: layer2.clone(),
1✔
2706
                    },
1✔
2707
                    name: "Layer2".to_string(),
1✔
2708
                    description: "Layer 2".to_string(),
1✔
2709
                    properties: vec![],
1✔
2710
                }),],
1✔
2711
                entry_label: None,
1✔
2712
                properties: vec![],
1✔
2713
            }
1✔
2714
        );
1✔
2715

2716
        let collection1_filtered_prefix = user_layer_db
1✔
2717
            .search(
1✔
2718
                &collection1_id,
1✔
2719
                SearchParameters {
1✔
2720
                    search_type: SearchType::Prefix,
1✔
2721
                    search_string: "ay".to_string(),
1✔
2722
                    limit: 10,
1✔
2723
                    offset: 0,
1✔
2724
                },
1✔
2725
            )
1✔
2726
            .await
1✔
2727
            .unwrap();
1✔
2728

1✔
2729
        assert_eq!(
1✔
2730
            collection1_filtered_prefix,
1✔
2731
            LayerCollection {
1✔
2732
                id: ProviderLayerCollectionId {
1✔
2733
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2734
                    collection_id: collection1_id.clone(),
1✔
2735
                },
1✔
2736
                name: "Collection1".to_string(),
1✔
2737
                description: "Collection 1".to_string(),
1✔
2738
                items: vec![],
1✔
2739
                entry_label: None,
1✔
2740
                properties: vec![],
1✔
2741
            }
1✔
2742
        );
1✔
2743

2744
        let collection1_filtered_prefix2 = user_layer_db
1✔
2745
            .search(
1✔
2746
                &collection1_id,
1✔
2747
                SearchParameters {
1✔
2748
                    search_type: SearchType::Prefix,
1✔
2749
                    search_string: "Lay".to_string(),
1✔
2750
                    limit: 10,
1✔
2751
                    offset: 0,
1✔
2752
                },
1✔
2753
            )
1✔
2754
            .await
1✔
2755
            .unwrap();
1✔
2756

1✔
2757
        assert_eq!(
1✔
2758
            collection1_filtered_prefix2,
1✔
2759
            LayerCollection {
1✔
2760
                id: ProviderLayerCollectionId {
1✔
2761
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
2762
                    collection_id: collection1_id.clone(),
1✔
2763
                },
1✔
2764
                name: "Collection1".to_string(),
1✔
2765
                description: "Collection 1".to_string(),
1✔
2766
                items: vec![CollectionItem::Layer(LayerListing {
1✔
2767
                    id: ProviderLayerId {
1✔
2768
                        provider_id: INTERNAL_PROVIDER_ID,
1✔
2769
                        layer_id: layer2.clone(),
1✔
2770
                    },
1✔
2771
                    name: "Layer2".to_string(),
1✔
2772
                    description: "Layer 2".to_string(),
1✔
2773
                    properties: vec![],
1✔
2774
                }),],
1✔
2775
                entry_label: None,
1✔
2776
                properties: vec![],
1✔
2777
            }
1✔
2778
        );
1✔
2779
    }
1✔
2780

2781
    #[allow(clippy::too_many_lines)]
2782
    #[ge_context::test]
1✔
2783
    async fn it_autocompletes_layers(app_ctx: PostgresContext<NoTls>) {
1✔
2784
        let session = admin_login(&app_ctx).await;
1✔
2785

2786
        let layer_db = app_ctx.session_context(session).db();
1✔
2787

1✔
2788
        let workflow = Workflow {
1✔
2789
            operator: TypedOperator::Vector(
1✔
2790
                MockPointSource {
1✔
2791
                    params: MockPointSourceParams {
1✔
2792
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
2793
                    },
1✔
2794
                }
1✔
2795
                .boxed(),
1✔
2796
            ),
1✔
2797
        };
1✔
2798

2799
        let root_collection_id = layer_db.get_root_layer_collection_id().await.unwrap();
1✔
2800

2801
        let _layer1 = layer_db
1✔
2802
            .add_layer(
1✔
2803
                AddLayer {
1✔
2804
                    name: "Layer1".to_string(),
1✔
2805
                    description: "Layer 1".to_string(),
1✔
2806
                    symbology: None,
1✔
2807
                    workflow: workflow.clone(),
1✔
2808
                    metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
2809
                    properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2810
                },
1✔
2811
                &root_collection_id,
1✔
2812
            )
1✔
2813
            .await
1✔
2814
            .unwrap();
1✔
2815

2816
        let collection1_id = layer_db
1✔
2817
            .add_layer_collection(
1✔
2818
                AddLayerCollection {
1✔
2819
                    name: "Collection1".to_string(),
1✔
2820
                    description: "Collection 1".to_string(),
1✔
2821
                    properties: Default::default(),
1✔
2822
                },
1✔
2823
                &root_collection_id,
1✔
2824
            )
1✔
2825
            .await
1✔
2826
            .unwrap();
1✔
2827

2828
        let _layer2 = layer_db
1✔
2829
            .add_layer(
1✔
2830
                AddLayer {
1✔
2831
                    name: "Layer2".to_string(),
1✔
2832
                    description: "Layer 2".to_string(),
1✔
2833
                    symbology: None,
1✔
2834
                    workflow: workflow.clone(),
1✔
2835
                    metadata: Default::default(),
1✔
2836
                    properties: Default::default(),
1✔
2837
                },
1✔
2838
                &collection1_id,
1✔
2839
            )
1✔
2840
            .await
1✔
2841
            .unwrap();
1✔
2842

2843
        let _collection2_id = layer_db
1✔
2844
            .add_layer_collection(
1✔
2845
                AddLayerCollection {
1✔
2846
                    name: "Collection2".to_string(),
1✔
2847
                    description: "Collection 2".to_string(),
1✔
2848
                    properties: Default::default(),
1✔
2849
                },
1✔
2850
                &collection1_id,
1✔
2851
            )
1✔
2852
            .await
1✔
2853
            .unwrap();
1✔
2854

2855
        let root_collection_all = layer_db
1✔
2856
            .autocomplete_search(
1✔
2857
                &root_collection_id,
1✔
2858
                SearchParameters {
1✔
2859
                    search_type: SearchType::Fulltext,
1✔
2860
                    search_string: String::new(),
1✔
2861
                    limit: 10,
1✔
2862
                    offset: 0,
1✔
2863
                },
1✔
2864
            )
1✔
2865
            .await
1✔
2866
            .unwrap();
1✔
2867

1✔
2868
        assert_eq!(
1✔
2869
            root_collection_all,
1✔
2870
            vec![
1✔
2871
                "Collection1".to_string(),
1✔
2872
                "Collection2".to_string(),
1✔
2873
                "Layer1".to_string(),
1✔
2874
                "Layer2".to_string(),
1✔
2875
                "Unsorted".to_string(),
1✔
2876
            ]
1✔
2877
        );
1✔
2878

2879
        let root_collection_filtered = layer_db
1✔
2880
            .autocomplete_search(
1✔
2881
                &root_collection_id,
1✔
2882
                SearchParameters {
1✔
2883
                    search_type: SearchType::Fulltext,
1✔
2884
                    search_string: "lection".to_string(),
1✔
2885
                    limit: 10,
1✔
2886
                    offset: 0,
1✔
2887
                },
1✔
2888
            )
1✔
2889
            .await
1✔
2890
            .unwrap();
1✔
2891

1✔
2892
        assert_eq!(
1✔
2893
            root_collection_filtered,
1✔
2894
            vec!["Collection1".to_string(), "Collection2".to_string(),]
1✔
2895
        );
1✔
2896

2897
        let collection1_all = layer_db
1✔
2898
            .autocomplete_search(
1✔
2899
                &collection1_id,
1✔
2900
                SearchParameters {
1✔
2901
                    search_type: SearchType::Fulltext,
1✔
2902
                    search_string: String::new(),
1✔
2903
                    limit: 10,
1✔
2904
                    offset: 0,
1✔
2905
                },
1✔
2906
            )
1✔
2907
            .await
1✔
2908
            .unwrap();
1✔
2909

1✔
2910
        assert_eq!(
1✔
2911
            collection1_all,
1✔
2912
            vec!["Collection2".to_string(), "Layer2".to_string(),]
1✔
2913
        );
1✔
2914

2915
        let collection1_filtered_fulltext = layer_db
1✔
2916
            .autocomplete_search(
1✔
2917
                &collection1_id,
1✔
2918
                SearchParameters {
1✔
2919
                    search_type: SearchType::Fulltext,
1✔
2920
                    search_string: "ay".to_string(),
1✔
2921
                    limit: 10,
1✔
2922
                    offset: 0,
1✔
2923
                },
1✔
2924
            )
1✔
2925
            .await
1✔
2926
            .unwrap();
1✔
2927

1✔
2928
        assert_eq!(collection1_filtered_fulltext, vec!["Layer2".to_string(),]);
1✔
2929

2930
        let collection1_filtered_prefix = layer_db
1✔
2931
            .autocomplete_search(
1✔
2932
                &collection1_id,
1✔
2933
                SearchParameters {
1✔
2934
                    search_type: SearchType::Prefix,
1✔
2935
                    search_string: "ay".to_string(),
1✔
2936
                    limit: 10,
1✔
2937
                    offset: 0,
1✔
2938
                },
1✔
2939
            )
1✔
2940
            .await
1✔
2941
            .unwrap();
1✔
2942

1✔
2943
        assert_eq!(collection1_filtered_prefix, Vec::<String>::new());
1✔
2944

2945
        let collection1_filtered_prefix2 = layer_db
1✔
2946
            .autocomplete_search(
1✔
2947
                &collection1_id,
1✔
2948
                SearchParameters {
1✔
2949
                    search_type: SearchType::Prefix,
1✔
2950
                    search_string: "Lay".to_string(),
1✔
2951
                    limit: 10,
1✔
2952
                    offset: 0,
1✔
2953
                },
1✔
2954
            )
1✔
2955
            .await
1✔
2956
            .unwrap();
1✔
2957

1✔
2958
        assert_eq!(collection1_filtered_prefix2, vec!["Layer2".to_string(),]);
1✔
2959
    }
1✔
2960

2961
    #[allow(clippy::too_many_lines)]
2962
    #[ge_context::test]
1✔
2963
    async fn it_autocompletes_layers_with_permissions(app_ctx: PostgresContext<NoTls>) {
1✔
2964
        let admin_session = admin_login(&app_ctx).await;
1✔
2965
        let admin_layer_db = app_ctx.session_context(admin_session).db();
1✔
2966

2967
        let user_session = app_ctx.create_anonymous_session().await.unwrap();
1✔
2968
        let user_layer_db = app_ctx.session_context(user_session.clone()).db();
1✔
2969

1✔
2970
        let workflow = Workflow {
1✔
2971
            operator: TypedOperator::Vector(
1✔
2972
                MockPointSource {
1✔
2973
                    params: MockPointSourceParams {
1✔
2974
                        points: vec![Coordinate2D::new(1., 2.); 3],
1✔
2975
                    },
1✔
2976
                }
1✔
2977
                .boxed(),
1✔
2978
            ),
1✔
2979
        };
1✔
2980

2981
        let root_collection_id = admin_layer_db.get_root_layer_collection_id().await.unwrap();
1✔
2982

2983
        let layer1 = admin_layer_db
1✔
2984
            .add_layer(
1✔
2985
                AddLayer {
1✔
2986
                    name: "Layer1".to_string(),
1✔
2987
                    description: "Layer 1".to_string(),
1✔
2988
                    symbology: None,
1✔
2989
                    workflow: workflow.clone(),
1✔
2990
                    metadata: [("meta".to_string(), "datum".to_string())].into(),
1✔
2991
                    properties: vec![("proper".to_string(), "tee".to_string()).into()],
1✔
2992
                },
1✔
2993
                &root_collection_id,
1✔
2994
            )
1✔
2995
            .await
1✔
2996
            .unwrap();
1✔
2997

2998
        let collection1_id = admin_layer_db
1✔
2999
            .add_layer_collection(
1✔
3000
                AddLayerCollection {
1✔
3001
                    name: "Collection1".to_string(),
1✔
3002
                    description: "Collection 1".to_string(),
1✔
3003
                    properties: Default::default(),
1✔
3004
                },
1✔
3005
                &root_collection_id,
1✔
3006
            )
1✔
3007
            .await
1✔
3008
            .unwrap();
1✔
3009

3010
        let layer2 = admin_layer_db
1✔
3011
            .add_layer(
1✔
3012
                AddLayer {
1✔
3013
                    name: "Layer2".to_string(),
1✔
3014
                    description: "Layer 2".to_string(),
1✔
3015
                    symbology: None,
1✔
3016
                    workflow: workflow.clone(),
1✔
3017
                    metadata: Default::default(),
1✔
3018
                    properties: Default::default(),
1✔
3019
                },
1✔
3020
                &collection1_id,
1✔
3021
            )
1✔
3022
            .await
1✔
3023
            .unwrap();
1✔
3024

3025
        let collection2_id = admin_layer_db
1✔
3026
            .add_layer_collection(
1✔
3027
                AddLayerCollection {
1✔
3028
                    name: "Collection2".to_string(),
1✔
3029
                    description: "Collection 2".to_string(),
1✔
3030
                    properties: Default::default(),
1✔
3031
                },
1✔
3032
                &collection1_id,
1✔
3033
            )
1✔
3034
            .await
1✔
3035
            .unwrap();
1✔
3036

3037
        let _collection3_id = admin_layer_db
1✔
3038
            .add_layer_collection(
1✔
3039
                AddLayerCollection {
1✔
3040
                    name: "Collection3".to_string(),
1✔
3041
                    description: "Collection 3".to_string(),
1✔
3042
                    properties: Default::default(),
1✔
3043
                },
1✔
3044
                &collection1_id,
1✔
3045
            )
1✔
3046
            .await
1✔
3047
            .unwrap();
1✔
3048

3049
        let _layer3 = admin_layer_db
1✔
3050
            .add_layer(
1✔
3051
                AddLayer {
1✔
3052
                    name: "Layer3".to_string(),
1✔
3053
                    description: "Layer 3".to_string(),
1✔
3054
                    symbology: None,
1✔
3055
                    workflow: workflow.clone(),
1✔
3056
                    metadata: Default::default(),
1✔
3057
                    properties: Default::default(),
1✔
3058
                },
1✔
3059
                &collection2_id,
1✔
3060
            )
1✔
3061
            .await
1✔
3062
            .unwrap();
1✔
3063

1✔
3064
        // Grant user permissions for collection1, collection2, layer1 and layer2
1✔
3065
        admin_layer_db
1✔
3066
            .add_permission(
1✔
3067
                user_session.user.id.into(),
1✔
3068
                collection1_id.clone(),
1✔
3069
                Permission::Read,
1✔
3070
            )
1✔
3071
            .await
1✔
3072
            .unwrap();
1✔
3073

1✔
3074
        admin_layer_db
1✔
3075
            .add_permission(
1✔
3076
                user_session.user.id.into(),
1✔
3077
                collection2_id.clone(),
1✔
3078
                Permission::Read,
1✔
3079
            )
1✔
3080
            .await
1✔
3081
            .unwrap();
1✔
3082

1✔
3083
        admin_layer_db
1✔
3084
            .add_permission(
1✔
3085
                user_session.user.id.into(),
1✔
3086
                layer1.clone(),
1✔
3087
                Permission::Read,
1✔
3088
            )
1✔
3089
            .await
1✔
3090
            .unwrap();
1✔
3091

1✔
3092
        admin_layer_db
1✔
3093
            .add_permission(
1✔
3094
                user_session.user.id.into(),
1✔
3095
                layer2.clone(),
1✔
3096
                Permission::Read,
1✔
3097
            )
1✔
3098
            .await
1✔
3099
            .unwrap();
1✔
3100

3101
        // Ensure admin sees everything we added
3102
        let admin_root_collection_all = admin_layer_db
1✔
3103
            .autocomplete_search(
1✔
3104
                &root_collection_id,
1✔
3105
                SearchParameters {
1✔
3106
                    search_type: SearchType::Fulltext,
1✔
3107
                    search_string: String::new(),
1✔
3108
                    limit: 10,
1✔
3109
                    offset: 0,
1✔
3110
                },
1✔
3111
            )
1✔
3112
            .await
1✔
3113
            .unwrap();
1✔
3114

1✔
3115
        assert_eq!(
1✔
3116
            admin_root_collection_all,
1✔
3117
            vec![
1✔
3118
                "Collection1".to_string(),
1✔
3119
                "Collection2".to_string(),
1✔
3120
                "Collection3".to_string(),
1✔
3121
                "Layer1".to_string(),
1✔
3122
                "Layer2".to_string(),
1✔
3123
                "Layer3".to_string(),
1✔
3124
                "Unsorted".to_string(),
1✔
3125
            ]
1✔
3126
        );
1✔
3127

3128
        let root_collection_all = user_layer_db
1✔
3129
            .autocomplete_search(
1✔
3130
                &root_collection_id,
1✔
3131
                SearchParameters {
1✔
3132
                    search_type: SearchType::Fulltext,
1✔
3133
                    search_string: String::new(),
1✔
3134
                    limit: 10,
1✔
3135
                    offset: 0,
1✔
3136
                },
1✔
3137
            )
1✔
3138
            .await
1✔
3139
            .unwrap();
1✔
3140

1✔
3141
        assert_eq!(
1✔
3142
            root_collection_all,
1✔
3143
            vec![
1✔
3144
                "Collection1".to_string(),
1✔
3145
                "Collection2".to_string(),
1✔
3146
                "Layer1".to_string(),
1✔
3147
                "Layer2".to_string(),
1✔
3148
                "Unsorted".to_string(),
1✔
3149
            ]
1✔
3150
        );
1✔
3151

3152
        let root_collection_filtered = user_layer_db
1✔
3153
            .autocomplete_search(
1✔
3154
                &root_collection_id,
1✔
3155
                SearchParameters {
1✔
3156
                    search_type: SearchType::Fulltext,
1✔
3157
                    search_string: "lection".to_string(),
1✔
3158
                    limit: 10,
1✔
3159
                    offset: 0,
1✔
3160
                },
1✔
3161
            )
1✔
3162
            .await
1✔
3163
            .unwrap();
1✔
3164

1✔
3165
        assert_eq!(
1✔
3166
            root_collection_filtered,
1✔
3167
            vec!["Collection1".to_string(), "Collection2".to_string(),]
1✔
3168
        );
1✔
3169

3170
        let collection1_all = user_layer_db
1✔
3171
            .autocomplete_search(
1✔
3172
                &collection1_id,
1✔
3173
                SearchParameters {
1✔
3174
                    search_type: SearchType::Fulltext,
1✔
3175
                    search_string: String::new(),
1✔
3176
                    limit: 10,
1✔
3177
                    offset: 0,
1✔
3178
                },
1✔
3179
            )
1✔
3180
            .await
1✔
3181
            .unwrap();
1✔
3182

1✔
3183
        assert_eq!(
1✔
3184
            collection1_all,
1✔
3185
            vec!["Collection2".to_string(), "Layer2".to_string(),]
1✔
3186
        );
1✔
3187

3188
        let collection1_filtered_fulltext = user_layer_db
1✔
3189
            .autocomplete_search(
1✔
3190
                &collection1_id,
1✔
3191
                SearchParameters {
1✔
3192
                    search_type: SearchType::Fulltext,
1✔
3193
                    search_string: "ay".to_string(),
1✔
3194
                    limit: 10,
1✔
3195
                    offset: 0,
1✔
3196
                },
1✔
3197
            )
1✔
3198
            .await
1✔
3199
            .unwrap();
1✔
3200

1✔
3201
        assert_eq!(collection1_filtered_fulltext, vec!["Layer2".to_string(),]);
1✔
3202

3203
        let collection1_filtered_prefix = user_layer_db
1✔
3204
            .autocomplete_search(
1✔
3205
                &collection1_id,
1✔
3206
                SearchParameters {
1✔
3207
                    search_type: SearchType::Prefix,
1✔
3208
                    search_string: "ay".to_string(),
1✔
3209
                    limit: 10,
1✔
3210
                    offset: 0,
1✔
3211
                },
1✔
3212
            )
1✔
3213
            .await
1✔
3214
            .unwrap();
1✔
3215

1✔
3216
        assert_eq!(collection1_filtered_prefix, Vec::<String>::new());
1✔
3217

3218
        let collection1_filtered_prefix2 = user_layer_db
1✔
3219
            .autocomplete_search(
1✔
3220
                &collection1_id,
1✔
3221
                SearchParameters {
1✔
3222
                    search_type: SearchType::Prefix,
1✔
3223
                    search_string: "Lay".to_string(),
1✔
3224
                    limit: 10,
1✔
3225
                    offset: 0,
1✔
3226
                },
1✔
3227
            )
1✔
3228
            .await
1✔
3229
            .unwrap();
1✔
3230

1✔
3231
        assert_eq!(collection1_filtered_prefix2, vec!["Layer2".to_string(),]);
1✔
3232
    }
1✔
3233

3234
    #[allow(clippy::too_many_lines)]
3235
    #[ge_context::test]
1✔
3236
    async fn it_reports_search_capabilities(app_ctx: PostgresContext<NoTls>) {
1✔
3237
        let session = admin_login(&app_ctx).await;
1✔
3238

3239
        let layer_db = app_ctx.session_context(session).db();
1✔
3240

1✔
3241
        let capabilities = layer_db.capabilities().search;
1✔
3242

3243
        let root_collection_id = layer_db.get_root_layer_collection_id().await.unwrap();
1✔
3244

1✔
3245
        if capabilities.search_types.fulltext {
1✔
3246
            assert!(layer_db
1✔
3247
                .search(
1✔
3248
                    &root_collection_id,
1✔
3249
                    SearchParameters {
1✔
3250
                        search_type: SearchType::Fulltext,
1✔
3251
                        search_string: String::new(),
1✔
3252
                        limit: 10,
1✔
3253
                        offset: 0,
1✔
3254
                    },
1✔
3255
                )
1✔
3256
                .await
1✔
3257
                .is_ok());
1✔
3258

3259
            if capabilities.autocomplete {
1✔
3260
                assert!(layer_db
1✔
3261
                    .autocomplete_search(
1✔
3262
                        &root_collection_id,
1✔
3263
                        SearchParameters {
1✔
3264
                            search_type: SearchType::Fulltext,
1✔
3265
                            search_string: String::new(),
1✔
3266
                            limit: 10,
1✔
3267
                            offset: 0,
1✔
3268
                        },
1✔
3269
                    )
1✔
3270
                    .await
1✔
3271
                    .is_ok());
1✔
3272
            } else {
3273
                assert!(layer_db
×
3274
                    .autocomplete_search(
×
3275
                        &root_collection_id,
×
3276
                        SearchParameters {
×
3277
                            search_type: SearchType::Fulltext,
×
3278
                            search_string: String::new(),
×
3279
                            limit: 10,
×
3280
                            offset: 0,
×
3281
                        },
×
3282
                    )
×
3283
                    .await
×
3284
                    .is_err());
×
3285
            }
3286
        }
×
3287
        if capabilities.search_types.prefix {
1✔
3288
            assert!(layer_db
1✔
3289
                .search(
1✔
3290
                    &root_collection_id,
1✔
3291
                    SearchParameters {
1✔
3292
                        search_type: SearchType::Prefix,
1✔
3293
                        search_string: String::new(),
1✔
3294
                        limit: 10,
1✔
3295
                        offset: 0,
1✔
3296
                    },
1✔
3297
                )
1✔
3298
                .await
1✔
3299
                .is_ok());
1✔
3300

3301
            if capabilities.autocomplete {
1✔
3302
                assert!(layer_db
1✔
3303
                    .autocomplete_search(
1✔
3304
                        &root_collection_id,
1✔
3305
                        SearchParameters {
1✔
3306
                            search_type: SearchType::Prefix,
1✔
3307
                            search_string: String::new(),
1✔
3308
                            limit: 10,
1✔
3309
                            offset: 0,
1✔
3310
                        },
1✔
3311
                    )
1✔
3312
                    .await
1✔
3313
                    .is_ok());
1✔
3314
            } else {
3315
                assert!(layer_db
×
3316
                    .autocomplete_search(
×
3317
                        &root_collection_id,
×
3318
                        SearchParameters {
×
3319
                            search_type: SearchType::Prefix,
×
3320
                            search_string: String::new(),
×
3321
                            limit: 10,
×
3322
                            offset: 0,
×
3323
                        },
×
3324
                    )
×
3325
                    .await
×
3326
                    .is_err());
×
3327
            }
3328
        }
×
3329
    }
1✔
3330

3331
    #[ge_context::test]
1✔
3332
    async fn it_tracks_used_quota_in_postgres(app_ctx: PostgresContext<NoTls>) {
1✔
3333
        let _user = app_ctx
1✔
3334
            .register_user(UserRegistration {
1✔
3335
                email: "foo@example.com".to_string(),
1✔
3336
                password: "secret1234".to_string(),
1✔
3337
                real_name: "Foo Bar".to_string(),
1✔
3338
            })
1✔
3339
            .await
1✔
3340
            .unwrap();
1✔
3341

3342
        let session = app_ctx
1✔
3343
            .login(UserCredentials {
1✔
3344
                email: "foo@example.com".to_string(),
1✔
3345
                password: "secret1234".to_string(),
1✔
3346
            })
1✔
3347
            .await
1✔
3348
            .unwrap();
1✔
3349

3350
        let admin_session = admin_login(&app_ctx).await;
1✔
3351

3352
        let quota = initialize_quota_tracking(
1✔
3353
            QuotaTrackingMode::Check,
1✔
3354
            app_ctx.session_context(admin_session).db(),
1✔
3355
            0,
1✔
3356
            60,
1✔
3357
        );
1✔
3358

1✔
3359
        let tracking = quota.create_quota_tracking(&session, Uuid::new_v4(), Uuid::new_v4());
1✔
3360

1✔
3361
        tracking.mock_work_unit_done();
1✔
3362
        tracking.mock_work_unit_done();
1✔
3363

1✔
3364
        let db = app_ctx.session_context(session).db();
1✔
3365

1✔
3366
        // wait for quota to be recorded
1✔
3367
        let mut success = false;
1✔
3368
        for _ in 0..10 {
1✔
3369
            let used = db.quota_used().await.unwrap();
1✔
3370
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
1✔
3371

3372
            if used == 2 {
1✔
3373
                success = true;
1✔
3374
                break;
1✔
3375
            }
×
3376
        }
3377

3378
        assert!(success);
1✔
3379
    }
1✔
3380

3381
    #[ge_context::test]
1✔
3382
    async fn it_tracks_available_quota(app_ctx: PostgresContext<NoTls>) {
1✔
3383
        let user = app_ctx
1✔
3384
            .register_user(UserRegistration {
1✔
3385
                email: "foo@example.com".to_string(),
1✔
3386
                password: "secret1234".to_string(),
1✔
3387
                real_name: "Foo Bar".to_string(),
1✔
3388
            })
1✔
3389
            .await
1✔
3390
            .unwrap();
1✔
3391

3392
        let session = app_ctx
1✔
3393
            .login(UserCredentials {
1✔
3394
                email: "foo@example.com".to_string(),
1✔
3395
                password: "secret1234".to_string(),
1✔
3396
            })
1✔
3397
            .await
1✔
3398
            .unwrap();
1✔
3399

3400
        let admin_session = admin_login(&app_ctx).await;
1✔
3401

3402
        app_ctx
1✔
3403
            .session_context(admin_session.clone())
1✔
3404
            .db()
1✔
3405
            .update_quota_available_by_user(&user, 1)
1✔
3406
            .await
1✔
3407
            .unwrap();
1✔
3408

1✔
3409
        let quota = initialize_quota_tracking(
1✔
3410
            QuotaTrackingMode::Check,
1✔
3411
            app_ctx.session_context(admin_session).db(),
1✔
3412
            0,
1✔
3413
            60,
1✔
3414
        );
1✔
3415

1✔
3416
        let tracking = quota.create_quota_tracking(&session, Uuid::new_v4(), Uuid::new_v4());
1✔
3417

1✔
3418
        tracking.mock_work_unit_done();
1✔
3419
        tracking.mock_work_unit_done();
1✔
3420

1✔
3421
        let db = app_ctx.session_context(session).db();
1✔
3422

1✔
3423
        // wait for quota to be recorded
1✔
3424
        let mut success = false;
1✔
3425
        for _ in 0..10 {
1✔
3426
            let available = db.quota_available().await.unwrap();
1✔
3427
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
1✔
3428

3429
            if available == -1 {
1✔
3430
                success = true;
1✔
3431
                break;
1✔
3432
            }
×
3433
        }
3434

3435
        assert!(success);
1✔
3436
    }
1✔
3437

3438
    #[ge_context::test]
1✔
3439
    async fn it_updates_quota_in_postgres(app_ctx: PostgresContext<NoTls>) {
1✔
3440
        let user = app_ctx
1✔
3441
            .register_user(UserRegistration {
1✔
3442
                email: "foo@example.com".to_string(),
1✔
3443
                password: "secret1234".to_string(),
1✔
3444
                real_name: "Foo Bar".to_string(),
1✔
3445
            })
1✔
3446
            .await
1✔
3447
            .unwrap();
1✔
3448

3449
        let session = app_ctx
1✔
3450
            .login(UserCredentials {
1✔
3451
                email: "foo@example.com".to_string(),
1✔
3452
                password: "secret1234".to_string(),
1✔
3453
            })
1✔
3454
            .await
1✔
3455
            .unwrap();
1✔
3456

1✔
3457
        let db = app_ctx.session_context(session.clone()).db();
1✔
3458
        let admin_db = app_ctx.session_context(UserSession::admin_session()).db();
1✔
3459

3460
        assert_eq!(
1✔
3461
            db.quota_available().await.unwrap(),
1✔
3462
            crate::config::get_config_element::<crate::config::Quota>()
1✔
3463
                .unwrap()
1✔
3464
                .initial_credits
3465
        );
3466

3467
        assert_eq!(
1✔
3468
            admin_db.quota_available_by_user(&user).await.unwrap(),
1✔
3469
            crate::config::get_config_element::<crate::config::Quota>()
1✔
3470
                .unwrap()
1✔
3471
                .initial_credits
3472
        );
3473

3474
        admin_db
1✔
3475
            .update_quota_available_by_user(&user, 123)
1✔
3476
            .await
1✔
3477
            .unwrap();
1✔
3478

3479
        assert_eq!(db.quota_available().await.unwrap(), 123);
1✔
3480

3481
        assert_eq!(admin_db.quota_available_by_user(&user).await.unwrap(), 123);
1✔
3482
    }
1✔
3483

3484
    #[allow(clippy::too_many_lines)]
3485
    #[ge_context::test]
1✔
3486
    async fn it_removes_layer_collections(app_ctx: PostgresContext<NoTls>) {
1✔
3487
        let session = admin_login(&app_ctx).await;
1✔
3488

3489
        let layer_db = app_ctx.session_context(session).db();
1✔
3490

1✔
3491
        let layer = AddLayer {
1✔
3492
            name: "layer".to_string(),
1✔
3493
            description: "description".to_string(),
1✔
3494
            workflow: Workflow {
1✔
3495
                operator: TypedOperator::Vector(
1✔
3496
                    MockPointSource {
1✔
3497
                        params: MockPointSourceParams {
1✔
3498
                            points: vec![Coordinate2D::new(1., 2.); 3],
1✔
3499
                        },
1✔
3500
                    }
1✔
3501
                    .boxed(),
1✔
3502
                ),
1✔
3503
            },
1✔
3504
            symbology: None,
1✔
3505
            metadata: Default::default(),
1✔
3506
            properties: Default::default(),
1✔
3507
        };
1✔
3508

3509
        let root_collection = &layer_db.get_root_layer_collection_id().await.unwrap();
1✔
3510

1✔
3511
        let collection = AddLayerCollection {
1✔
3512
            name: "top collection".to_string(),
1✔
3513
            description: "description".to_string(),
1✔
3514
            properties: Default::default(),
1✔
3515
        };
1✔
3516

3517
        let top_c_id = layer_db
1✔
3518
            .add_layer_collection(collection, root_collection)
1✔
3519
            .await
1✔
3520
            .unwrap();
1✔
3521

3522
        let l_id = layer_db.add_layer(layer, &top_c_id).await.unwrap();
1✔
3523

1✔
3524
        let collection = AddLayerCollection {
1✔
3525
            name: "empty collection".to_string(),
1✔
3526
            description: "description".to_string(),
1✔
3527
            properties: Default::default(),
1✔
3528
        };
1✔
3529

3530
        let empty_c_id = layer_db
1✔
3531
            .add_layer_collection(collection, &top_c_id)
1✔
3532
            .await
1✔
3533
            .unwrap();
1✔
3534

3535
        let items = layer_db
1✔
3536
            .load_layer_collection(
1✔
3537
                &top_c_id,
1✔
3538
                LayerCollectionListOptions {
1✔
3539
                    offset: 0,
1✔
3540
                    limit: 20,
1✔
3541
                },
1✔
3542
            )
1✔
3543
            .await
1✔
3544
            .unwrap();
1✔
3545

1✔
3546
        assert_eq!(
1✔
3547
            items,
1✔
3548
            LayerCollection {
1✔
3549
                id: ProviderLayerCollectionId {
1✔
3550
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
3551
                    collection_id: top_c_id.clone(),
1✔
3552
                },
1✔
3553
                name: "top collection".to_string(),
1✔
3554
                description: "description".to_string(),
1✔
3555
                items: vec![
1✔
3556
                    CollectionItem::Collection(LayerCollectionListing {
1✔
3557
                        id: ProviderLayerCollectionId {
1✔
3558
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
3559
                            collection_id: empty_c_id.clone(),
1✔
3560
                        },
1✔
3561
                        name: "empty collection".to_string(),
1✔
3562
                        description: "description".to_string(),
1✔
3563
                        properties: Default::default(),
1✔
3564
                    }),
1✔
3565
                    CollectionItem::Layer(LayerListing {
1✔
3566
                        id: ProviderLayerId {
1✔
3567
                            provider_id: INTERNAL_PROVIDER_ID,
1✔
3568
                            layer_id: l_id.clone(),
1✔
3569
                        },
1✔
3570
                        name: "layer".to_string(),
1✔
3571
                        description: "description".to_string(),
1✔
3572
                        properties: vec![],
1✔
3573
                    })
1✔
3574
                ],
1✔
3575
                entry_label: None,
1✔
3576
                properties: vec![],
1✔
3577
            }
1✔
3578
        );
1✔
3579

3580
        // remove empty collection
3581
        layer_db.remove_layer_collection(&empty_c_id).await.unwrap();
1✔
3582

3583
        let items = layer_db
1✔
3584
            .load_layer_collection(
1✔
3585
                &top_c_id,
1✔
3586
                LayerCollectionListOptions {
1✔
3587
                    offset: 0,
1✔
3588
                    limit: 20,
1✔
3589
                },
1✔
3590
            )
1✔
3591
            .await
1✔
3592
            .unwrap();
1✔
3593

1✔
3594
        assert_eq!(
1✔
3595
            items,
1✔
3596
            LayerCollection {
1✔
3597
                id: ProviderLayerCollectionId {
1✔
3598
                    provider_id: INTERNAL_PROVIDER_ID,
1✔
3599
                    collection_id: top_c_id.clone(),
1✔
3600
                },
1✔
3601
                name: "top collection".to_string(),
1✔
3602
                description: "description".to_string(),
1✔
3603
                items: vec![CollectionItem::Layer(LayerListing {
1✔
3604
                    id: ProviderLayerId {
1✔
3605
                        provider_id: INTERNAL_PROVIDER_ID,
1✔
3606
                        layer_id: l_id.clone(),
1✔
3607
                    },
1✔
3608
                    name: "layer".to_string(),
1✔
3609
                    description: "description".to_string(),
1✔
3610
                    properties: vec![],
1✔
3611
                })],
1✔
3612
                entry_label: None,
1✔
3613
                properties: vec![],
1✔
3614
            }
1✔
3615
        );
1✔
3616

3617
        // remove top (not root) collection
3618
        layer_db.remove_layer_collection(&top_c_id).await.unwrap();
1✔
3619

1✔
3620
        layer_db
1✔
3621
            .load_layer_collection(
1✔
3622
                &top_c_id,
1✔
3623
                LayerCollectionListOptions {
1✔
3624
                    offset: 0,
1✔
3625
                    limit: 20,
1✔
3626
                },
1✔
3627
            )
1✔
3628
            .await
1✔
3629
            .unwrap_err();
1✔
3630

1✔
3631
        // should be deleted automatically
1✔
3632
        layer_db.load_layer(&l_id).await.unwrap_err();
1✔
3633

1✔
3634
        // it is not allowed to remove the root collection
1✔
3635
        layer_db
1✔
3636
            .remove_layer_collection(root_collection)
1✔
3637
            .await
1✔
3638
            .unwrap_err();
1✔
3639
        layer_db
1✔
3640
            .load_layer_collection(
1✔
3641
                root_collection,
1✔
3642
                LayerCollectionListOptions {
1✔
3643
                    offset: 0,
1✔
3644
                    limit: 20,
1✔
3645
                },
1✔
3646
            )
1✔
3647
            .await
1✔
3648
            .unwrap();
1✔
3649
    }
1✔
3650

3651
    #[ge_context::test]
1✔
3652
    #[allow(clippy::too_many_lines)]
3653
    async fn it_removes_collections_from_collections(app_ctx: PostgresContext<NoTls>) {
1✔
3654
        let session = admin_login(&app_ctx).await;
1✔
3655

3656
        let db = app_ctx.session_context(session).db();
1✔
3657

3658
        let root_collection_id = &db.get_root_layer_collection_id().await.unwrap();
1✔
3659

3660
        let mid_collection_id = db
1✔
3661
            .add_layer_collection(
1✔
3662
                AddLayerCollection {
1✔
3663
                    name: "mid collection".to_string(),
1✔
3664
                    description: "description".to_string(),
1✔
3665
                    properties: Default::default(),
1✔
3666
                },
1✔
3667
                root_collection_id,
1✔
3668
            )
1✔
3669
            .await
1✔
3670
            .unwrap();
1✔
3671

3672
        let bottom_collection_id = db
1✔
3673
            .add_layer_collection(
1✔
3674
                AddLayerCollection {
1✔
3675
                    name: "bottom collection".to_string(),
1✔
3676
                    description: "description".to_string(),
1✔
3677
                    properties: Default::default(),
1✔
3678
                },
1✔
3679
                &mid_collection_id,
1✔
3680
            )
1✔
3681
            .await
1✔
3682
            .unwrap();
1✔
3683

3684
        let layer_id = db
1✔
3685
            .add_layer(
1✔
3686
                AddLayer {
1✔
3687
                    name: "layer".to_string(),
1✔
3688
                    description: "description".to_string(),
1✔
3689
                    workflow: Workflow {
1✔
3690
                        operator: TypedOperator::Vector(
1✔
3691
                            MockPointSource {
1✔
3692
                                params: MockPointSourceParams {
1✔
3693
                                    points: vec![Coordinate2D::new(1., 2.); 3],
1✔
3694
                                },
1✔
3695
                            }
1✔
3696
                            .boxed(),
1✔
3697
                        ),
1✔
3698
                    },
1✔
3699
                    symbology: None,
1✔
3700
                    metadata: Default::default(),
1✔
3701
                    properties: Default::default(),
1✔
3702
                },
1✔
3703
                &mid_collection_id,
1✔
3704
            )
1✔
3705
            .await
1✔
3706
            .unwrap();
1✔
3707

1✔
3708
        // removing the mid collection…
1✔
3709
        db.remove_layer_collection_from_parent(&mid_collection_id, root_collection_id)
1✔
3710
            .await
1✔
3711
            .unwrap();
1✔
3712

1✔
3713
        // …should remove itself
1✔
3714
        db.load_layer_collection(&mid_collection_id, LayerCollectionListOptions::default())
1✔
3715
            .await
1✔
3716
            .unwrap_err();
1✔
3717

1✔
3718
        // …should remove the bottom collection
1✔
3719
        db.load_layer_collection(&bottom_collection_id, LayerCollectionListOptions::default())
1✔
3720
            .await
1✔
3721
            .unwrap_err();
1✔
3722

1✔
3723
        // … and should remove the layer of the bottom collection
1✔
3724
        db.load_layer(&layer_id).await.unwrap_err();
1✔
3725

1✔
3726
        // the root collection is still there
1✔
3727
        db.load_layer_collection(root_collection_id, LayerCollectionListOptions::default())
1✔
3728
            .await
1✔
3729
            .unwrap();
1✔
3730
    }
1✔
3731

3732
    #[ge_context::test]
1✔
3733
    #[allow(clippy::too_many_lines)]
3734
    async fn it_removes_layers_from_collections(app_ctx: PostgresContext<NoTls>) {
1✔
3735
        let session = admin_login(&app_ctx).await;
1✔
3736

3737
        let db = app_ctx.session_context(session).db();
1✔
3738

3739
        let root_collection = &db.get_root_layer_collection_id().await.unwrap();
1✔
3740

3741
        let another_collection = db
1✔
3742
            .add_layer_collection(
1✔
3743
                AddLayerCollection {
1✔
3744
                    name: "top collection".to_string(),
1✔
3745
                    description: "description".to_string(),
1✔
3746
                    properties: Default::default(),
1✔
3747
                },
1✔
3748
                root_collection,
1✔
3749
            )
1✔
3750
            .await
1✔
3751
            .unwrap();
1✔
3752

3753
        let layer_in_one_collection = db
1✔
3754
            .add_layer(
1✔
3755
                AddLayer {
1✔
3756
                    name: "layer 1".to_string(),
1✔
3757
                    description: "description".to_string(),
1✔
3758
                    workflow: Workflow {
1✔
3759
                        operator: TypedOperator::Vector(
1✔
3760
                            MockPointSource {
1✔
3761
                                params: MockPointSourceParams {
1✔
3762
                                    points: vec![Coordinate2D::new(1., 2.); 3],
1✔
3763
                                },
1✔
3764
                            }
1✔
3765
                            .boxed(),
1✔
3766
                        ),
1✔
3767
                    },
1✔
3768
                    symbology: None,
1✔
3769
                    metadata: Default::default(),
1✔
3770
                    properties: Default::default(),
1✔
3771
                },
1✔
3772
                &another_collection,
1✔
3773
            )
1✔
3774
            .await
1✔
3775
            .unwrap();
1✔
3776

3777
        let layer_in_two_collections = db
1✔
3778
            .add_layer(
1✔
3779
                AddLayer {
1✔
3780
                    name: "layer 2".to_string(),
1✔
3781
                    description: "description".to_string(),
1✔
3782
                    workflow: Workflow {
1✔
3783
                        operator: TypedOperator::Vector(
1✔
3784
                            MockPointSource {
1✔
3785
                                params: MockPointSourceParams {
1✔
3786
                                    points: vec![Coordinate2D::new(1., 2.); 3],
1✔
3787
                                },
1✔
3788
                            }
1✔
3789
                            .boxed(),
1✔
3790
                        ),
1✔
3791
                    },
1✔
3792
                    symbology: None,
1✔
3793
                    metadata: Default::default(),
1✔
3794
                    properties: Default::default(),
1✔
3795
                },
1✔
3796
                &another_collection,
1✔
3797
            )
1✔
3798
            .await
1✔
3799
            .unwrap();
1✔
3800

1✔
3801
        db.load_layer(&layer_in_two_collections).await.unwrap();
1✔
3802

1✔
3803
        db.add_layer_to_collection(&layer_in_two_collections, root_collection)
1✔
3804
            .await
1✔
3805
            .unwrap();
1✔
3806

1✔
3807
        // remove first layer --> should be deleted entirely
1✔
3808

1✔
3809
        db.remove_layer_from_collection(&layer_in_one_collection, &another_collection)
1✔
3810
            .await
1✔
3811
            .unwrap();
1✔
3812

3813
        let number_of_layer_in_collection = db
1✔
3814
            .load_layer_collection(
1✔
3815
                &another_collection,
1✔
3816
                LayerCollectionListOptions {
1✔
3817
                    offset: 0,
1✔
3818
                    limit: 20,
1✔
3819
                },
1✔
3820
            )
1✔
3821
            .await
1✔
3822
            .unwrap()
1✔
3823
            .items
1✔
3824
            .len();
1✔
3825
        assert_eq!(
1✔
3826
            number_of_layer_in_collection,
1✔
3827
            1 /* only the other collection should be here */
1✔
3828
        );
1✔
3829

3830
        db.load_layer(&layer_in_one_collection).await.unwrap_err();
1✔
3831

1✔
3832
        // remove second layer --> should only be gone in collection
1✔
3833

1✔
3834
        db.remove_layer_from_collection(&layer_in_two_collections, &another_collection)
1✔
3835
            .await
1✔
3836
            .unwrap();
1✔
3837

3838
        let number_of_layer_in_collection = db
1✔
3839
            .load_layer_collection(
1✔
3840
                &another_collection,
1✔
3841
                LayerCollectionListOptions {
1✔
3842
                    offset: 0,
1✔
3843
                    limit: 20,
1✔
3844
                },
1✔
3845
            )
1✔
3846
            .await
1✔
3847
            .unwrap()
1✔
3848
            .items
1✔
3849
            .len();
1✔
3850
        assert_eq!(
1✔
3851
            number_of_layer_in_collection,
1✔
3852
            0 /* both layers were deleted */
1✔
3853
        );
1✔
3854

3855
        db.load_layer(&layer_in_two_collections).await.unwrap();
1✔
3856
    }
1✔
3857

3858
    #[ge_context::test]
1✔
3859
    #[allow(clippy::too_many_lines)]
3860
    async fn it_deletes_dataset(app_ctx: PostgresContext<NoTls>) {
1✔
3861
        let loading_info = OgrSourceDataset {
1✔
3862
            file_name: PathBuf::from("test.csv"),
1✔
3863
            layer_name: "test.csv".to_owned(),
1✔
3864
            data_type: Some(VectorDataType::MultiPoint),
1✔
3865
            time: OgrSourceDatasetTimeType::Start {
1✔
3866
                start_field: "start".to_owned(),
1✔
3867
                start_format: OgrSourceTimeFormat::Auto,
1✔
3868
                duration: OgrSourceDurationSpec::Zero,
1✔
3869
            },
1✔
3870
            default_geometry: None,
1✔
3871
            columns: Some(OgrSourceColumnSpec {
1✔
3872
                format_specifics: Some(FormatSpecifics::Csv {
1✔
3873
                    header: CsvHeader::Auto,
1✔
3874
                }),
1✔
3875
                x: "x".to_owned(),
1✔
3876
                y: None,
1✔
3877
                int: vec![],
1✔
3878
                float: vec![],
1✔
3879
                text: vec![],
1✔
3880
                bool: vec![],
1✔
3881
                datetime: vec![],
1✔
3882
                rename: None,
1✔
3883
            }),
1✔
3884
            force_ogr_time_filter: false,
1✔
3885
            force_ogr_spatial_filter: false,
1✔
3886
            on_error: OgrSourceErrorSpec::Ignore,
1✔
3887
            sql_query: None,
1✔
3888
            attribute_query: None,
1✔
3889
            cache_ttl: CacheTtlSeconds::default(),
1✔
3890
        };
1✔
3891

1✔
3892
        let meta_data = MetaDataDefinition::OgrMetaData(StaticMetaData::<
1✔
3893
            OgrSourceDataset,
1✔
3894
            VectorResultDescriptor,
1✔
3895
            VectorQueryRectangle,
1✔
3896
        > {
1✔
3897
            loading_info: loading_info.clone(),
1✔
3898
            result_descriptor: VectorResultDescriptor {
1✔
3899
                data_type: VectorDataType::MultiPoint,
1✔
3900
                spatial_reference: SpatialReference::epsg_4326().into(),
1✔
3901
                columns: [(
1✔
3902
                    "foo".to_owned(),
1✔
3903
                    VectorColumnInfo {
1✔
3904
                        data_type: FeatureDataType::Float,
1✔
3905
                        measurement: Measurement::Unitless,
1✔
3906
                    },
1✔
3907
                )]
1✔
3908
                .into_iter()
1✔
3909
                .collect(),
1✔
3910
                time: None,
1✔
3911
                bbox: None,
1✔
3912
            },
1✔
3913
            phantom: Default::default(),
1✔
3914
        });
1✔
3915

3916
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
3917

1✔
3918
        let dataset_name = DatasetName::new(Some(session.user.id.to_string()), "my_dataset");
1✔
3919

1✔
3920
        let db = app_ctx.session_context(session.clone()).db();
1✔
3921
        let dataset_id = db
1✔
3922
            .add_dataset(
1✔
3923
                AddDataset {
1✔
3924
                    name: Some(dataset_name),
1✔
3925
                    display_name: "Ogr Test".to_owned(),
1✔
3926
                    description: "desc".to_owned(),
1✔
3927
                    source_operator: "OgrSource".to_owned(),
1✔
3928
                    symbology: None,
1✔
3929
                    provenance: Some(vec![Provenance {
1✔
3930
                        citation: "citation".to_owned(),
1✔
3931
                        license: "license".to_owned(),
1✔
3932
                        uri: "uri".to_owned(),
1✔
3933
                    }]),
1✔
3934
                    tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
3935
                },
1✔
3936
                meta_data,
1✔
3937
            )
1✔
3938
            .await
1✔
3939
            .unwrap()
1✔
3940
            .id;
1✔
3941

1✔
3942
        assert!(db.load_dataset(&dataset_id).await.is_ok());
1✔
3943

3944
        db.delete_dataset(dataset_id).await.unwrap();
1✔
3945

1✔
3946
        assert!(db.load_dataset(&dataset_id).await.is_err());
1✔
3947
    }
1✔
3948

3949
    #[ge_context::test]
1✔
3950
    #[allow(clippy::too_many_lines)]
3951
    async fn it_deletes_admin_dataset(app_ctx: PostgresContext<NoTls>) {
1✔
3952
        let dataset_name = DatasetName::new(None, "my_dataset");
1✔
3953

1✔
3954
        let loading_info = OgrSourceDataset {
1✔
3955
            file_name: PathBuf::from("test.csv"),
1✔
3956
            layer_name: "test.csv".to_owned(),
1✔
3957
            data_type: Some(VectorDataType::MultiPoint),
1✔
3958
            time: OgrSourceDatasetTimeType::Start {
1✔
3959
                start_field: "start".to_owned(),
1✔
3960
                start_format: OgrSourceTimeFormat::Auto,
1✔
3961
                duration: OgrSourceDurationSpec::Zero,
1✔
3962
            },
1✔
3963
            default_geometry: None,
1✔
3964
            columns: Some(OgrSourceColumnSpec {
1✔
3965
                format_specifics: Some(FormatSpecifics::Csv {
1✔
3966
                    header: CsvHeader::Auto,
1✔
3967
                }),
1✔
3968
                x: "x".to_owned(),
1✔
3969
                y: None,
1✔
3970
                int: vec![],
1✔
3971
                float: vec![],
1✔
3972
                text: vec![],
1✔
3973
                bool: vec![],
1✔
3974
                datetime: vec![],
1✔
3975
                rename: None,
1✔
3976
            }),
1✔
3977
            force_ogr_time_filter: false,
1✔
3978
            force_ogr_spatial_filter: false,
1✔
3979
            on_error: OgrSourceErrorSpec::Ignore,
1✔
3980
            sql_query: None,
1✔
3981
            attribute_query: None,
1✔
3982
            cache_ttl: CacheTtlSeconds::default(),
1✔
3983
        };
1✔
3984

1✔
3985
        let meta_data = MetaDataDefinition::OgrMetaData(StaticMetaData::<
1✔
3986
            OgrSourceDataset,
1✔
3987
            VectorResultDescriptor,
1✔
3988
            VectorQueryRectangle,
1✔
3989
        > {
1✔
3990
            loading_info: loading_info.clone(),
1✔
3991
            result_descriptor: VectorResultDescriptor {
1✔
3992
                data_type: VectorDataType::MultiPoint,
1✔
3993
                spatial_reference: SpatialReference::epsg_4326().into(),
1✔
3994
                columns: [(
1✔
3995
                    "foo".to_owned(),
1✔
3996
                    VectorColumnInfo {
1✔
3997
                        data_type: FeatureDataType::Float,
1✔
3998
                        measurement: Measurement::Unitless,
1✔
3999
                    },
1✔
4000
                )]
1✔
4001
                .into_iter()
1✔
4002
                .collect(),
1✔
4003
                time: None,
1✔
4004
                bbox: None,
1✔
4005
            },
1✔
4006
            phantom: Default::default(),
1✔
4007
        });
1✔
4008

4009
        let session = admin_login(&app_ctx).await;
1✔
4010

4011
        let db = app_ctx.session_context(session).db();
1✔
4012
        let dataset_id = db
1✔
4013
            .add_dataset(
1✔
4014
                AddDataset {
1✔
4015
                    name: Some(dataset_name),
1✔
4016
                    display_name: "Ogr Test".to_owned(),
1✔
4017
                    description: "desc".to_owned(),
1✔
4018
                    source_operator: "OgrSource".to_owned(),
1✔
4019
                    symbology: None,
1✔
4020
                    provenance: Some(vec![Provenance {
1✔
4021
                        citation: "citation".to_owned(),
1✔
4022
                        license: "license".to_owned(),
1✔
4023
                        uri: "uri".to_owned(),
1✔
4024
                    }]),
1✔
4025
                    tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
4026
                },
1✔
4027
                meta_data,
1✔
4028
            )
1✔
4029
            .await
1✔
4030
            .unwrap()
1✔
4031
            .id;
1✔
4032

1✔
4033
        assert!(db.load_dataset(&dataset_id).await.is_ok());
1✔
4034

4035
        db.delete_dataset(dataset_id).await.unwrap();
1✔
4036

1✔
4037
        assert!(db.load_dataset(&dataset_id).await.is_err());
1✔
4038
    }
1✔
4039

4040
    #[ge_context::test]
1✔
4041
    async fn test_missing_layer_dataset_in_collection_listing(app_ctx: PostgresContext<NoTls>) {
1✔
4042
        let session = admin_login(&app_ctx).await;
1✔
4043
        let db = app_ctx.session_context(session).db();
1✔
4044

4045
        let root_collection_id = &db.get_root_layer_collection_id().await.unwrap();
1✔
4046

4047
        let top_collection_id = db
1✔
4048
            .add_layer_collection(
1✔
4049
                AddLayerCollection {
1✔
4050
                    name: "top collection".to_string(),
1✔
4051
                    description: "description".to_string(),
1✔
4052
                    properties: Default::default(),
1✔
4053
                },
1✔
4054
                root_collection_id,
1✔
4055
            )
1✔
4056
            .await
1✔
4057
            .unwrap();
1✔
4058

1✔
4059
        let faux_layer = LayerId("faux".to_string());
1✔
4060

1✔
4061
        // this should fail
1✔
4062
        db.add_layer_to_collection(&faux_layer, &top_collection_id)
1✔
4063
            .await
1✔
4064
            .unwrap_err();
1✔
4065

4066
        let root_collection_layers = db
1✔
4067
            .load_layer_collection(
1✔
4068
                &top_collection_id,
1✔
4069
                LayerCollectionListOptions {
1✔
4070
                    offset: 0,
1✔
4071
                    limit: 20,
1✔
4072
                },
1✔
4073
            )
1✔
4074
            .await
1✔
4075
            .unwrap();
1✔
4076

1✔
4077
        assert_eq!(
1✔
4078
            root_collection_layers,
1✔
4079
            LayerCollection {
1✔
4080
                id: ProviderLayerCollectionId {
1✔
4081
                    provider_id: DataProviderId(
1✔
4082
                        "ce5e84db-cbf9-48a2-9a32-d4b7cc56ea74".try_into().unwrap()
1✔
4083
                    ),
1✔
4084
                    collection_id: top_collection_id.clone(),
1✔
4085
                },
1✔
4086
                name: "top collection".to_string(),
1✔
4087
                description: "description".to_string(),
1✔
4088
                items: vec![],
1✔
4089
                entry_label: None,
1✔
4090
                properties: vec![],
1✔
4091
            }
1✔
4092
        );
1✔
4093
    }
1✔
4094

4095
    #[allow(clippy::too_many_lines)]
4096
    #[ge_context::test]
1✔
4097
    async fn it_restricts_layer_permissions(app_ctx: PostgresContext<NoTls>) {
1✔
4098
        let admin_session = admin_login(&app_ctx).await;
1✔
4099
        let session1 = app_ctx.create_anonymous_session().await.unwrap();
1✔
4100

1✔
4101
        let admin_db = app_ctx.session_context(admin_session.clone()).db();
1✔
4102
        let db1 = app_ctx.session_context(session1.clone()).db();
1✔
4103

4104
        let root = admin_db.get_root_layer_collection_id().await.unwrap();
1✔
4105

4106
        // add new collection as admin
4107
        let new_collection_id = admin_db
1✔
4108
            .add_layer_collection(
1✔
4109
                AddLayerCollection {
1✔
4110
                    name: "admin collection".to_string(),
1✔
4111
                    description: String::new(),
1✔
4112
                    properties: Default::default(),
1✔
4113
                },
1✔
4114
                &root,
1✔
4115
            )
1✔
4116
            .await
1✔
4117
            .unwrap();
1✔
4118

4119
        // load as regular user, not visible
4120
        let collection = db1
1✔
4121
            .load_layer_collection(
1✔
4122
                &root,
1✔
4123
                LayerCollectionListOptions {
1✔
4124
                    offset: 0,
1✔
4125
                    limit: 10,
1✔
4126
                },
1✔
4127
            )
1✔
4128
            .await
1✔
4129
            .unwrap();
1✔
4130
        assert!(!collection.items.iter().any(|c| match c {
1✔
4131
            CollectionItem::Collection(c) => c.id.collection_id == new_collection_id,
1✔
4132
            CollectionItem::Layer(_) => false,
×
4133
        }));
1✔
4134

4135
        // give user read permission
4136
        admin_db
1✔
4137
            .add_permission(
1✔
4138
                session1.user.id.into(),
1✔
4139
                new_collection_id.clone(),
1✔
4140
                Permission::Read,
1✔
4141
            )
1✔
4142
            .await
1✔
4143
            .unwrap();
1✔
4144

4145
        // now visible
4146
        let collection = db1
1✔
4147
            .load_layer_collection(
1✔
4148
                &root,
1✔
4149
                LayerCollectionListOptions {
1✔
4150
                    offset: 0,
1✔
4151
                    limit: 10,
1✔
4152
                },
1✔
4153
            )
1✔
4154
            .await
1✔
4155
            .unwrap();
1✔
4156

1✔
4157
        assert!(collection.items.iter().any(|c| match c {
1✔
4158
            CollectionItem::Collection(c) => c.id.collection_id == new_collection_id,
1✔
4159
            CollectionItem::Layer(_) => false,
×
4160
        }));
1✔
4161
    }
1✔
4162

4163
    #[allow(clippy::too_many_lines)]
4164
    #[ge_context::test]
1✔
4165
    async fn it_handles_user_roles(app_ctx: PostgresContext<NoTls>) {
1✔
4166
        let admin_session = admin_login(&app_ctx).await;
1✔
4167
        let user_id = app_ctx
1✔
4168
            .register_user(UserRegistration {
1✔
4169
                email: "foo@example.com".to_string(),
1✔
4170
                password: "secret123".to_string(),
1✔
4171
                real_name: "Foo Bar".to_string(),
1✔
4172
            })
1✔
4173
            .await
1✔
4174
            .unwrap();
1✔
4175

1✔
4176
        let admin_db = app_ctx.session_context(admin_session.clone()).db();
1✔
4177

4178
        // create a new role
4179
        let role_id = admin_db.add_role("foo").await.unwrap();
1✔
4180

4181
        let user_session = app_ctx
1✔
4182
            .login(UserCredentials {
1✔
4183
                email: "foo@example.com".to_string(),
1✔
4184
                password: "secret123".to_string(),
1✔
4185
            })
1✔
4186
            .await
1✔
4187
            .unwrap();
1✔
4188

1✔
4189
        // user does not have the role yet
1✔
4190

1✔
4191
        assert!(!user_session.roles.contains(&role_id));
1✔
4192

4193
        //user can query their role descriptions (user role and registered user)
4194
        assert_eq!(user_session.roles.len(), 2);
1✔
4195

4196
        let expected_user_role_description = RoleDescription {
1✔
4197
            role: Role {
1✔
4198
                id: RoleId::from(user_id),
1✔
4199
                name: "foo@example.com".to_string(),
1✔
4200
            },
1✔
4201
            individual: true,
1✔
4202
        };
1✔
4203
        let expected_registered_role_description = RoleDescription {
1✔
4204
            role: Role {
1✔
4205
                id: Role::registered_user_role_id(),
1✔
4206
                name: "user".to_string(),
1✔
4207
            },
1✔
4208
            individual: false,
1✔
4209
        };
1✔
4210

4211
        let user_role_descriptions = app_ctx
1✔
4212
            .session_context(user_session.clone())
1✔
4213
            .db()
1✔
4214
            .get_role_descriptions(&user_id)
1✔
4215
            .await
1✔
4216
            .unwrap();
1✔
4217
        assert_eq!(
1✔
4218
            vec![
1✔
4219
                expected_user_role_description.clone(),
1✔
4220
                expected_registered_role_description.clone(),
1✔
4221
            ],
1✔
4222
            user_role_descriptions
1✔
4223
        );
1✔
4224

4225
        // we assign the role to the user
4226
        admin_db.assign_role(&role_id, &user_id).await.unwrap();
1✔
4227

4228
        let user_session = app_ctx
1✔
4229
            .login(UserCredentials {
1✔
4230
                email: "foo@example.com".to_string(),
1✔
4231
                password: "secret123".to_string(),
1✔
4232
            })
1✔
4233
            .await
1✔
4234
            .unwrap();
1✔
4235

1✔
4236
        // should be present now
1✔
4237
        assert!(user_session.roles.contains(&role_id));
1✔
4238

4239
        //user can query their role descriptions (now an additional foo role)
4240
        let expected_foo_role_description = RoleDescription {
1✔
4241
            role: Role {
1✔
4242
                id: role_id,
1✔
4243
                name: "foo".to_string(),
1✔
4244
            },
1✔
4245
            individual: false,
1✔
4246
        };
1✔
4247

4248
        let user_role_descriptions = app_ctx
1✔
4249
            .session_context(user_session.clone())
1✔
4250
            .db()
1✔
4251
            .get_role_descriptions(&user_id)
1✔
4252
            .await
1✔
4253
            .unwrap();
1✔
4254
        assert_eq!(
1✔
4255
            vec![
1✔
4256
                expected_foo_role_description,
1✔
4257
                expected_user_role_description.clone(),
1✔
4258
                expected_registered_role_description.clone(),
1✔
4259
            ],
1✔
4260
            user_role_descriptions
1✔
4261
        );
1✔
4262

4263
        // we revoke it
4264
        admin_db.revoke_role(&role_id, &user_id).await.unwrap();
1✔
4265

4266
        let user_session = app_ctx
1✔
4267
            .login(UserCredentials {
1✔
4268
                email: "foo@example.com".to_string(),
1✔
4269
                password: "secret123".to_string(),
1✔
4270
            })
1✔
4271
            .await
1✔
4272
            .unwrap();
1✔
4273

1✔
4274
        // the role is gone now
1✔
4275
        assert!(!user_session.roles.contains(&role_id));
1✔
4276

4277
        //user can query their role descriptions (user role and registered user)
4278
        let user_role_descriptions = app_ctx
1✔
4279
            .session_context(user_session.clone())
1✔
4280
            .db()
1✔
4281
            .get_role_descriptions(&user_id)
1✔
4282
            .await
1✔
4283
            .unwrap();
1✔
4284
        assert_eq!(
1✔
4285
            vec![
1✔
4286
                expected_user_role_description.clone(),
1✔
4287
                expected_registered_role_description.clone(),
1✔
4288
            ],
1✔
4289
            user_role_descriptions
1✔
4290
        );
1✔
4291

4292
        // assign it again and then delete the whole role, should not be present at user
4293

4294
        admin_db.assign_role(&role_id, &user_id).await.unwrap();
1✔
4295

1✔
4296
        admin_db.remove_role(&role_id).await.unwrap();
1✔
4297

4298
        let user_session = app_ctx
1✔
4299
            .login(UserCredentials {
1✔
4300
                email: "foo@example.com".to_string(),
1✔
4301
                password: "secret123".to_string(),
1✔
4302
            })
1✔
4303
            .await
1✔
4304
            .unwrap();
1✔
4305

1✔
4306
        assert!(!user_session.roles.contains(&role_id));
1✔
4307

4308
        //user can query their role descriptions (user role and registered user)
4309
        let user_role_descriptions = app_ctx
1✔
4310
            .session_context(user_session.clone())
1✔
4311
            .db()
1✔
4312
            .get_role_descriptions(&user_id)
1✔
4313
            .await
1✔
4314
            .unwrap();
1✔
4315
        assert_eq!(
1✔
4316
            vec![
1✔
4317
                expected_user_role_description,
1✔
4318
                expected_registered_role_description.clone(),
1✔
4319
            ],
1✔
4320
            user_role_descriptions
1✔
4321
        );
1✔
4322
    }
1✔
4323

4324
    #[allow(clippy::too_many_lines)]
4325
    #[ge_context::test]
1✔
4326
    async fn it_updates_project_layer_symbology(app_ctx: PostgresContext<NoTls>) {
1✔
4327
        let session = app_ctx.create_anonymous_session().await.unwrap();
1✔
4328

4329
        let (_, workflow_id) = register_ndvi_workflow_helper(&app_ctx).await;
1✔
4330

4331
        let db = app_ctx.session_context(session.clone()).db();
1✔
4332

1✔
4333
        let create_project: CreateProject = serde_json::from_value(json!({
1✔
4334
            "name": "Default",
1✔
4335
            "description": "Default project",
1✔
4336
            "bounds": {
1✔
4337
                "boundingBox": {
1✔
4338
                    "lowerLeftCoordinate": {
1✔
4339
                        "x": -180,
1✔
4340
                        "y": -90
1✔
4341
                    },
1✔
4342
                    "upperRightCoordinate": {
1✔
4343
                        "x": 180,
1✔
4344
                        "y": 90
1✔
4345
                    }
1✔
4346
                },
1✔
4347
                "spatialReference": "EPSG:4326",
1✔
4348
                "timeInterval": {
1✔
4349
                    "start": 1_396_353_600_000i64,
1✔
4350
                    "end": 1_396_353_600_000i64
1✔
4351
                }
1✔
4352
            },
1✔
4353
            "timeStep": {
1✔
4354
                "step": 1,
1✔
4355
                "granularity": "months"
1✔
4356
            }
1✔
4357
        }))
1✔
4358
        .unwrap();
1✔
4359

4360
        let project_id = db.create_project(create_project).await.unwrap();
1✔
4361

1✔
4362
        let update: UpdateProject = serde_json::from_value(json!({
1✔
4363
            "id": project_id.to_string(),
1✔
4364
            "layers": [{
1✔
4365
                "name": "NDVI",
1✔
4366
                "workflow": workflow_id.to_string(),
1✔
4367
                "visibility": {
1✔
4368
                    "data": true,
1✔
4369
                    "legend": false
1✔
4370
                },
1✔
4371
                "symbology": {
1✔
4372
                    "type": "raster",
1✔
4373
                    "opacity": 1,
1✔
4374
                    "rasterColorizer": {
1✔
4375
                        "type": "singleBand",
1✔
4376
                        "band": 0,
1✔
4377
                        "bandColorizer": {
1✔
4378
                            "type": "linearGradient",
1✔
4379
                            "breakpoints": [{
1✔
4380
                                "value": 1,
1✔
4381
                                "color": [0, 0, 0, 255]
1✔
4382
                            }, {
1✔
4383
                                "value": 255,
1✔
4384
                                "color": [255, 255, 255, 255]
1✔
4385
                            }],
1✔
4386
                            "noDataColor": [0, 0, 0, 0],
1✔
4387
                            "overColor": [255, 255, 255, 127],
1✔
4388
                            "underColor": [255, 255, 255, 127]
1✔
4389
                        }
1✔
4390
                    }
1✔
4391
                }
1✔
4392
            }]
1✔
4393
        }))
1✔
4394
        .unwrap();
1✔
4395

1✔
4396
        db.update_project(update).await.unwrap();
1✔
4397

1✔
4398
        let update: UpdateProject = serde_json::from_value(json!({
1✔
4399
            "id": project_id.to_string(),
1✔
4400
            "layers": [{
1✔
4401
                "name": "NDVI",
1✔
4402
                "workflow": workflow_id.to_string(),
1✔
4403
                "visibility": {
1✔
4404
                    "data": true,
1✔
4405
                    "legend": false
1✔
4406
                },
1✔
4407
                "symbology": {
1✔
4408
                    "type": "raster",
1✔
4409
                    "opacity": 1,
1✔
4410
                    "rasterColorizer": {
1✔
4411
                        "type": "singleBand",
1✔
4412
                        "band": 0,
1✔
4413
                        "bandColorizer": {
1✔
4414
                        "type": "linearGradient",
1✔
4415
                            "breakpoints": [{
1✔
4416
                                "value": 1,
1✔
4417
                                "color": [0, 0, 4, 255]
1✔
4418
                            }, {
1✔
4419
                                "value": 17.866_666_666_666_667,
1✔
4420
                                "color": [11, 9, 36, 255]
1✔
4421
                            }, {
1✔
4422
                                "value": 34.733_333_333_333_334,
1✔
4423
                                "color": [32, 17, 75, 255]
1✔
4424
                            }, {
1✔
4425
                                "value": 51.6,
1✔
4426
                                "color": [59, 15, 112, 255]
1✔
4427
                            }, {
1✔
4428
                                "value": 68.466_666_666_666_67,
1✔
4429
                                "color": [87, 21, 126, 255]
1✔
4430
                            }, {
1✔
4431
                                "value": 85.333_333_333_333_33,
1✔
4432
                                "color": [114, 31, 129, 255]
1✔
4433
                            }, {
1✔
4434
                                "value": 102.199_999_999_999_99,
1✔
4435
                                "color": [140, 41, 129, 255]
1✔
4436
                            }, {
1✔
4437
                                "value": 119.066_666_666_666_65,
1✔
4438
                                "color": [168, 50, 125, 255]
1✔
4439
                            }, {
1✔
4440
                                "value": 135.933_333_333_333_34,
1✔
4441
                                "color": [196, 60, 117, 255]
1✔
4442
                            }, {
1✔
4443
                                "value": 152.799_999_999_999_98,
1✔
4444
                                "color": [222, 73, 104, 255]
1✔
4445
                            }, {
1✔
4446
                                "value": 169.666_666_666_666_66,
1✔
4447
                                "color": [241, 96, 93, 255]
1✔
4448
                            }, {
1✔
4449
                                "value": 186.533_333_333_333_33,
1✔
4450
                                "color": [250, 127, 94, 255]
1✔
4451
                            }, {
1✔
4452
                                "value": 203.399_999_999_999_98,
1✔
4453
                                "color": [254, 159, 109, 255]
1✔
4454
                            }, {
1✔
4455
                                "value": 220.266_666_666_666_65,
1✔
4456
                                "color": [254, 191, 132, 255]
1✔
4457
                            }, {
1✔
4458
                                "value": 237.133_333_333_333_3,
1✔
4459
                                "color": [253, 222, 160, 255]
1✔
4460
                            }, {
1✔
4461
                                "value": 254,
1✔
4462
                                "color": [252, 253, 191, 255]
1✔
4463
                            }],
1✔
4464
                            "noDataColor": [0, 0, 0, 0],
1✔
4465
                            "overColor": [255, 255, 255, 127],
1✔
4466
                            "underColor": [255, 255, 255, 127]
1✔
4467
                        }
1✔
4468
                    }
1✔
4469
                }
1✔
4470
            }]
1✔
4471
        }))
1✔
4472
        .unwrap();
1✔
4473

1✔
4474
        db.update_project(update).await.unwrap();
1✔
4475

1✔
4476
        let update: UpdateProject = serde_json::from_value(json!({
1✔
4477
            "id": project_id.to_string(),
1✔
4478
            "layers": [{
1✔
4479
                "name": "NDVI",
1✔
4480
                "workflow": workflow_id.to_string(),
1✔
4481
                "visibility": {
1✔
4482
                    "data": true,
1✔
4483
                    "legend": false
1✔
4484
                },
1✔
4485
                "symbology": {
1✔
4486
                    "type": "raster",
1✔
4487
                    "opacity": 1,
1✔
4488
                    "rasterColorizer": {
1✔
4489
                        "type": "singleBand",
1✔
4490
                        "band": 0,
1✔
4491
                        "bandColorizer": {
1✔
4492
                            "type": "linearGradient",
1✔
4493
                            "breakpoints": [{
1✔
4494
                                "value": 1,
1✔
4495
                                "color": [0, 0, 4, 255]
1✔
4496
                            }, {
1✔
4497
                                "value": 17.866_666_666_666_667,
1✔
4498
                                "color": [11, 9, 36, 255]
1✔
4499
                            }, {
1✔
4500
                                "value": 34.733_333_333_333_334,
1✔
4501
                                "color": [32, 17, 75, 255]
1✔
4502
                            }, {
1✔
4503
                                "value": 51.6,
1✔
4504
                                "color": [59, 15, 112, 255]
1✔
4505
                            }, {
1✔
4506
                                "value": 68.466_666_666_666_67,
1✔
4507
                                "color": [87, 21, 126, 255]
1✔
4508
                            }, {
1✔
4509
                                "value": 85.333_333_333_333_33,
1✔
4510
                                "color": [114, 31, 129, 255]
1✔
4511
                            }, {
1✔
4512
                                "value": 102.199_999_999_999_99,
1✔
4513
                                "color": [140, 41, 129, 255]
1✔
4514
                            }, {
1✔
4515
                                "value": 119.066_666_666_666_65,
1✔
4516
                                "color": [168, 50, 125, 255]
1✔
4517
                            }, {
1✔
4518
                                "value": 135.933_333_333_333_34,
1✔
4519
                                "color": [196, 60, 117, 255]
1✔
4520
                            }, {
1✔
4521
                                "value": 152.799_999_999_999_98,
1✔
4522
                                "color": [222, 73, 104, 255]
1✔
4523
                            }, {
1✔
4524
                                "value": 169.666_666_666_666_66,
1✔
4525
                                "color": [241, 96, 93, 255]
1✔
4526
                            }, {
1✔
4527
                                "value": 186.533_333_333_333_33,
1✔
4528
                                "color": [250, 127, 94, 255]
1✔
4529
                            }, {
1✔
4530
                                "value": 203.399_999_999_999_98,
1✔
4531
                                "color": [254, 159, 109, 255]
1✔
4532
                            }, {
1✔
4533
                                "value": 220.266_666_666_666_65,
1✔
4534
                                "color": [254, 191, 132, 255]
1✔
4535
                            }, {
1✔
4536
                                "value": 237.133_333_333_333_3,
1✔
4537
                                "color": [253, 222, 160, 255]
1✔
4538
                            }, {
1✔
4539
                                "value": 254,
1✔
4540
                                "color": [252, 253, 191, 255]
1✔
4541
                            }],
1✔
4542
                            "noDataColor": [0, 0, 0, 0],
1✔
4543
                            "overColor": [255, 255, 255, 127],
1✔
4544
                            "underColor": [255, 255, 255, 127]
1✔
4545
                        }
1✔
4546
                    }
1✔
4547
                }
1✔
4548
            }]
1✔
4549
        }))
1✔
4550
        .unwrap();
1✔
4551

1✔
4552
        db.update_project(update).await.unwrap();
1✔
4553

1✔
4554
        let update: UpdateProject = serde_json::from_value(json!({
1✔
4555
            "id": project_id.to_string(),
1✔
4556
            "layers": [{
1✔
4557
                "name": "NDVI",
1✔
4558
                "workflow": workflow_id.to_string(),
1✔
4559
                "visibility": {
1✔
4560
                    "data": true,
1✔
4561
                    "legend": false
1✔
4562
                },
1✔
4563
                "symbology": {
1✔
4564
                    "type": "raster",
1✔
4565
                    "opacity": 1,
1✔
4566
                    "rasterColorizer": {
1✔
4567
                        "type": "singleBand",
1✔
4568
                        "band": 0,
1✔
4569
                        "bandColorizer": {
1✔
4570
                            "type": "linearGradient",
1✔
4571
                            "breakpoints": [{
1✔
4572
                                "value": 1,
1✔
4573
                                "color": [0, 0, 4, 255]
1✔
4574
                            }, {
1✔
4575
                                "value": 17.933_333_333_333_334,
1✔
4576
                                "color": [11, 9, 36, 255]
1✔
4577
                            }, {
1✔
4578
                                "value": 34.866_666_666_666_67,
1✔
4579
                                "color": [32, 17, 75, 255]
1✔
4580
                            }, {
1✔
4581
                                "value": 51.800_000_000_000_004,
1✔
4582
                                "color": [59, 15, 112, 255]
1✔
4583
                            }, {
1✔
4584
                                "value": 68.733_333_333_333_33,
1✔
4585
                                "color": [87, 21, 126, 255]
1✔
4586
                            }, {
1✔
4587
                                "value": 85.666_666_666_666_66,
1✔
4588
                                "color": [114, 31, 129, 255]
1✔
4589
                            }, {
1✔
4590
                                "value": 102.6,
1✔
4591
                                "color": [140, 41, 129, 255]
1✔
4592
                            }, {
1✔
4593
                                "value": 119.533_333_333_333_32,
1✔
4594
                                "color": [168, 50, 125, 255]
1✔
4595
                            }, {
1✔
4596
                                "value": 136.466_666_666_666_67,
1✔
4597
                                "color": [196, 60, 117, 255]
1✔
4598
                            }, {
1✔
4599
                                "value": 153.4,
1✔
4600
                                "color": [222, 73, 104, 255]
1✔
4601
                            }, {
1✔
4602
                                "value": 170.333_333_333_333_31,
1✔
4603
                                "color": [241, 96, 93, 255]
1✔
4604
                            }, {
1✔
4605
                                "value": 187.266_666_666_666_65,
1✔
4606
                                "color": [250, 127, 94, 255]
1✔
4607
                            }, {
1✔
4608
                                "value": 204.2,
1✔
4609
                                "color": [254, 159, 109, 255]
1✔
4610
                            }, {
1✔
4611
                                "value": 221.133_333_333_333_33,
1✔
4612
                                "color": [254, 191, 132, 255]
1✔
4613
                            }, {
1✔
4614
                                "value": 238.066_666_666_666_63,
1✔
4615
                                "color": [253, 222, 160, 255]
1✔
4616
                            }, {
1✔
4617
                                "value": 255,
1✔
4618
                                "color": [252, 253, 191, 255]
1✔
4619
                            }],
1✔
4620
                            "noDataColor": [0, 0, 0, 0],
1✔
4621
                            "overColor": [255, 255, 255, 127],
1✔
4622
                            "underColor": [255, 255, 255, 127]
1✔
4623
                        }
1✔
4624
                    }
1✔
4625
                }
1✔
4626
            }]
1✔
4627
        }))
1✔
4628
        .unwrap();
1✔
4629

4630
        // run two updates concurrently
4631
        let (r0, r1) = join!(db.update_project(update.clone()), db.update_project(update));
1✔
4632

4633
        assert!(r0.is_ok());
1✔
4634
        assert!(r1.is_ok());
1✔
4635
    }
1✔
4636

4637
    #[ge_context::test]
1✔
4638
    #[allow(clippy::too_many_lines)]
4639
    async fn it_resolves_dataset_names_to_ids(app_ctx: PostgresContext<NoTls>) {
1✔
4640
        let admin_session = UserSession::admin_session();
1✔
4641
        let db = app_ctx.session_context(admin_session.clone()).db();
1✔
4642

1✔
4643
        let loading_info = OgrSourceDataset {
1✔
4644
            file_name: PathBuf::from("test.csv"),
1✔
4645
            layer_name: "test.csv".to_owned(),
1✔
4646
            data_type: Some(VectorDataType::MultiPoint),
1✔
4647
            time: OgrSourceDatasetTimeType::Start {
1✔
4648
                start_field: "start".to_owned(),
1✔
4649
                start_format: OgrSourceTimeFormat::Auto,
1✔
4650
                duration: OgrSourceDurationSpec::Zero,
1✔
4651
            },
1✔
4652
            default_geometry: None,
1✔
4653
            columns: Some(OgrSourceColumnSpec {
1✔
4654
                format_specifics: Some(FormatSpecifics::Csv {
1✔
4655
                    header: CsvHeader::Auto,
1✔
4656
                }),
1✔
4657
                x: "x".to_owned(),
1✔
4658
                y: None,
1✔
4659
                int: vec![],
1✔
4660
                float: vec![],
1✔
4661
                text: vec![],
1✔
4662
                bool: vec![],
1✔
4663
                datetime: vec![],
1✔
4664
                rename: None,
1✔
4665
            }),
1✔
4666
            force_ogr_time_filter: false,
1✔
4667
            force_ogr_spatial_filter: false,
1✔
4668
            on_error: OgrSourceErrorSpec::Ignore,
1✔
4669
            sql_query: None,
1✔
4670
            attribute_query: None,
1✔
4671
            cache_ttl: CacheTtlSeconds::default(),
1✔
4672
        };
1✔
4673

1✔
4674
        let meta_data = MetaDataDefinition::OgrMetaData(StaticMetaData::<
1✔
4675
            OgrSourceDataset,
1✔
4676
            VectorResultDescriptor,
1✔
4677
            VectorQueryRectangle,
1✔
4678
        > {
1✔
4679
            loading_info: loading_info.clone(),
1✔
4680
            result_descriptor: VectorResultDescriptor {
1✔
4681
                data_type: VectorDataType::MultiPoint,
1✔
4682
                spatial_reference: SpatialReference::epsg_4326().into(),
1✔
4683
                columns: [(
1✔
4684
                    "foo".to_owned(),
1✔
4685
                    VectorColumnInfo {
1✔
4686
                        data_type: FeatureDataType::Float,
1✔
4687
                        measurement: Measurement::Unitless,
1✔
4688
                    },
1✔
4689
                )]
1✔
4690
                .into_iter()
1✔
4691
                .collect(),
1✔
4692
                time: None,
1✔
4693
                bbox: None,
1✔
4694
            },
1✔
4695
            phantom: Default::default(),
1✔
4696
        });
1✔
4697

4698
        let DatasetIdAndName {
4699
            id: dataset_id1,
1✔
4700
            name: dataset_name1,
1✔
4701
        } = db
1✔
4702
            .add_dataset(
1✔
4703
                AddDataset {
1✔
4704
                    name: Some(DatasetName::new(None, "my_dataset".to_owned())),
1✔
4705
                    display_name: "Ogr Test".to_owned(),
1✔
4706
                    description: "desc".to_owned(),
1✔
4707
                    source_operator: "OgrSource".to_owned(),
1✔
4708
                    symbology: None,
1✔
4709
                    provenance: Some(vec![Provenance {
1✔
4710
                        citation: "citation".to_owned(),
1✔
4711
                        license: "license".to_owned(),
1✔
4712
                        uri: "uri".to_owned(),
1✔
4713
                    }]),
1✔
4714
                    tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
4715
                },
1✔
4716
                meta_data.clone(),
1✔
4717
            )
1✔
4718
            .await
1✔
4719
            .unwrap();
1✔
4720

4721
        let DatasetIdAndName {
4722
            id: dataset_id2,
1✔
4723
            name: dataset_name2,
1✔
4724
        } = db
1✔
4725
            .add_dataset(
1✔
4726
                AddDataset {
1✔
4727
                    name: Some(DatasetName::new(
1✔
4728
                        Some(admin_session.user.id.to_string()),
1✔
4729
                        "my_dataset".to_owned(),
1✔
4730
                    )),
1✔
4731
                    display_name: "Ogr Test".to_owned(),
1✔
4732
                    description: "desc".to_owned(),
1✔
4733
                    source_operator: "OgrSource".to_owned(),
1✔
4734
                    symbology: None,
1✔
4735
                    provenance: Some(vec![Provenance {
1✔
4736
                        citation: "citation".to_owned(),
1✔
4737
                        license: "license".to_owned(),
1✔
4738
                        uri: "uri".to_owned(),
1✔
4739
                    }]),
1✔
4740
                    tags: Some(vec!["upload".to_owned(), "test".to_owned()]),
1✔
4741
                },
1✔
4742
                meta_data,
1✔
4743
            )
1✔
4744
            .await
1✔
4745
            .unwrap();
1✔
4746

4747
        assert_eq!(
1✔
4748
            db.resolve_dataset_name_to_id(&dataset_name1)
1✔
4749
                .await
1✔
4750
                .unwrap()
1✔
4751
                .unwrap(),
1✔
4752
            dataset_id1
4753
        );
4754
        assert_eq!(
1✔
4755
            db.resolve_dataset_name_to_id(&dataset_name2)
1✔
4756
                .await
1✔
4757
                .unwrap()
1✔
4758
                .unwrap(),
1✔
4759
            dataset_id2
4760
        );
4761
    }
1✔
4762

4763
    #[ge_context::test]
1✔
4764
    #[allow(clippy::too_many_lines)]
4765
    async fn it_bulk_updates_quota(app_ctx: PostgresContext<NoTls>) {
1✔
4766
        let admin_session = UserSession::admin_session();
1✔
4767
        let db = app_ctx.session_context(admin_session.clone()).db();
1✔
4768

4769
        let user1 = app_ctx
1✔
4770
            .register_user(UserRegistration {
1✔
4771
                email: "user1@example.com".into(),
1✔
4772
                password: "12345678".into(),
1✔
4773
                real_name: "User1".into(),
1✔
4774
            })
1✔
4775
            .await
1✔
4776
            .unwrap();
1✔
4777

4778
        let user2 = app_ctx
1✔
4779
            .register_user(UserRegistration {
1✔
4780
                email: "user2@example.com".into(),
1✔
4781
                password: "12345678".into(),
1✔
4782
                real_name: "User2".into(),
1✔
4783
            })
1✔
4784
            .await
1✔
4785
            .unwrap();
1✔
4786

1✔
4787
        // single item in bulk
1✔
4788
        db.bulk_increment_quota_used([(user1, 1)]).await.unwrap();
1✔
4789

4790
        assert_eq!(db.quota_used_by_user(&user1).await.unwrap(), 1);
1✔
4791

4792
        // multiple items in bulk
4793
        db.bulk_increment_quota_used([(user1, 1), (user2, 3)])
1✔
4794
            .await
1✔
4795
            .unwrap();
1✔
4796

4797
        assert_eq!(db.quota_used_by_user(&user1).await.unwrap(), 2);
1✔
4798
        assert_eq!(db.quota_used_by_user(&user2).await.unwrap(), 3);
1✔
4799
    }
1✔
4800

4801
    async fn it_handles_oidc_tokens(app_ctx: PostgresContext<NoTls>) {
2✔
4802
        let external_user_claims = UserClaims {
2✔
4803
            external_id: SubjectIdentifier::new("Foo bar Id".into()),
2✔
4804
            email: "foo@bar.de".into(),
2✔
4805
            real_name: "Foo Bar".into(),
2✔
4806
        };
2✔
4807
        let tokens = OidcTokens {
2✔
4808
            access: AccessToken::new("FIRST_ACCESS_TOKEN".into()),
2✔
4809
            refresh: Some(RefreshToken::new("FIRST_REFRESH_TOKEN".into())),
2✔
4810
            expires_in: Duration::seconds(2),
2✔
4811
        };
2✔
4812

4813
        let login_result = app_ctx
2✔
4814
            .login_external(external_user_claims.clone(), tokens)
2✔
4815
            .await;
2✔
4816
        assert!(login_result.is_ok());
2✔
4817

4818
        let session_id = login_result.unwrap().id;
2✔
4819

4820
        let access_token = app_ctx.get_access_token(session_id).await.unwrap();
2✔
4821

2✔
4822
        assert_eq!(
2✔
4823
            "FIRST_ACCESS_TOKEN".to_string(),
2✔
4824
            access_token.secret().to_owned()
2✔
4825
        );
2✔
4826

4827
        //Access token duration oidc_login_refresh is 2 sec, i.e., session times out after 2 sec.
4828
        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
2✔
4829

4830
        let access_token = app_ctx.get_access_token(session_id).await.unwrap();
2✔
4831

2✔
4832
        assert_eq!(
2✔
4833
            "SECOND_ACCESS_TOKEN".to_string(),
2✔
4834
            access_token.secret().to_owned()
2✔
4835
        );
2✔
4836
    }
2✔
4837

4838
    pub fn oidc_only_refresh() -> (Server, impl Fn() -> OidcManager) {
1✔
4839
        let mock_refresh_server_config = MockRefreshServerConfig {
1✔
4840
            expected_discoveries: 1,
1✔
4841
            token_duration: std::time::Duration::from_secs(2),
1✔
4842
            creates_first_token: false,
1✔
4843
            first_access_token: "FIRST_ACCESS_TOKEN".to_string(),
1✔
4844
            first_refresh_token: "FIRST_REFRESH_TOKEN".to_string(),
1✔
4845
            second_access_token: "SECOND_ACCESS_TOKEN".to_string(),
1✔
4846
            second_refresh_token: "SECOND_REFRESH_TOKEN".to_string(),
1✔
4847
            client_side_password: None,
1✔
4848
        };
1✔
4849

1✔
4850
        let (server, oidc_manager) = mock_refresh_server(mock_refresh_server_config);
1✔
4851

1✔
4852
        (server, move || {
1✔
4853
            OidcManager::from_oidc_with_static_tokens(oidc_manager.clone())
1✔
4854
        })
1✔
4855
    }
1✔
4856

4857
    #[ge_context::test(oidc_db = "oidc_only_refresh")]
1✔
4858
    async fn it_handles_oidc_tokens_without_encryption(app_ctx: PostgresContext<NoTls>) {
1✔
4859
        it_handles_oidc_tokens(app_ctx).await;
1✔
4860
    }
1✔
4861

4862
    pub fn oidc_only_refresh_with_encryption() -> (Server, impl Fn() -> OidcManager) {
1✔
4863
        let mock_refresh_server_config = MockRefreshServerConfig {
1✔
4864
            expected_discoveries: 1,
1✔
4865
            token_duration: std::time::Duration::from_secs(2),
1✔
4866
            creates_first_token: false,
1✔
4867
            first_access_token: "FIRST_ACCESS_TOKEN".to_string(),
1✔
4868
            first_refresh_token: "FIRST_REFRESH_TOKEN".to_string(),
1✔
4869
            second_access_token: "SECOND_ACCESS_TOKEN".to_string(),
1✔
4870
            second_refresh_token: "SECOND_REFRESH_TOKEN".to_string(),
1✔
4871
            client_side_password: Some("password123".to_string()),
1✔
4872
        };
1✔
4873

1✔
4874
        let (server, oidc_manager) = mock_refresh_server(mock_refresh_server_config);
1✔
4875

1✔
4876
        (server, move || {
1✔
4877
            OidcManager::from_oidc_with_static_tokens(oidc_manager.clone())
1✔
4878
        })
1✔
4879
    }
1✔
4880

4881
    #[ge_context::test(oidc_db = "oidc_only_refresh_with_encryption")]
1✔
4882
    async fn it_handles_oidc_tokens_with_encryption(app_ctx: PostgresContext<NoTls>) {
1✔
4883
        it_handles_oidc_tokens(app_ctx).await;
1✔
4884
    }
1✔
4885
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc