@@ -279,18 +279,25 @@ mod tests {
279279
280280 use super :: * ;
281281 use crate :: {
282+ api:: model:: datatypes:: RasterDataType as ApiRasterDataType ,
282283 contexts:: PostgresContext ,
284+ datasets:: upload:: { Upload , UploadDb , UploadId } ,
283285 ge_context,
286+ layers:: { layer:: AddLayer , listing:: LayerCollectionProvider , storage:: LayerDb } ,
287+ machine_learning:: { MlModel , MlModelIdAndName , MlModelMetadata } ,
284288 users:: { UserAuth , UserCredentials , UserRegistration } ,
285289 util:: tests:: {
286290 add_ndvi_to_datasets2, add_ports_to_datasets, admin_login, read_body_string,
287291 send_test_request,
288292 } ,
293+ workflows:: workflow:: Workflow ,
289294 } ;
290295 use actix_http:: header;
291296 use actix_web_httpauth:: headers:: authorization:: Bearer ;
297+ use geoengine_datatypes:: { primitives:: Coordinate2D , util:: Identifier } ;
292298 use geoengine_operators:: {
293- engine:: { RasterOperator , VectorOperator , WorkflowOperatorPath } ,
299+ engine:: { RasterOperator , TypedOperator , VectorOperator , WorkflowOperatorPath } ,
300+ mock:: { MockPointSource , MockPointSourceParams } ,
294301 source:: { GdalSource , GdalSourceParameters , OgrSource , OgrSourceParameters } ,
295302 } ;
296303 use serde_json:: { json, Value } ;
@@ -394,7 +401,7 @@ mod tests {
394401
395402 #[ ge_context:: test]
396403 #[ allow( clippy:: too_many_lines) ]
397- async fn it_lists_permissions ( app_ctx : PostgresContext < NoTls > ) {
404+ async fn it_lists_dataset_permissions ( app_ctx : PostgresContext < NoTls > ) {
398405 let admin_session = admin_login ( & app_ctx) . await ;
399406
400407 let ( _dataset_id, dataset_name) = add_ndvi_to_datasets2 ( & app_ctx, true , true ) . await ;
@@ -451,4 +458,222 @@ mod tests {
451458 )
452459 ) ;
453460 }
461+
462+ #[ ge_context:: test]
463+ #[ allow( clippy:: too_many_lines) ]
464+ async fn it_lists_ml_model_permissions ( app_ctx : PostgresContext < NoTls > ) {
465+ let admin_session = admin_login ( & app_ctx) . await ;
466+
467+ let db = app_ctx. session_context ( admin_session. clone ( ) ) . db ( ) ;
468+
469+ let upload_id = UploadId :: new ( ) ;
470+ let upload = Upload {
471+ id : upload_id,
472+ files : vec ! [ ] ,
473+ } ;
474+ db. create_upload ( upload) . await . unwrap ( ) ;
475+
476+ let model = MlModel {
477+ description : "No real model here" . to_owned ( ) ,
478+ display_name : "my unreal model" . to_owned ( ) ,
479+ metadata : MlModelMetadata {
480+ file_name : "myUnrealmodel.onnx" . to_owned ( ) ,
481+ input_type : ApiRasterDataType :: F32 ,
482+ num_input_bands : 17 ,
483+ output_type : ApiRasterDataType :: F64 ,
484+ } ,
485+ name : MlModelName :: new ( None , "myUnrealModel" ) . into ( ) ,
486+ upload : upload_id,
487+ } ;
488+
489+ let MlModelIdAndName {
490+ id : _model_id,
491+ name : model_name,
492+ } = db. add_model ( model) . await . unwrap ( ) ;
493+
494+ let req = actix_web:: test:: TestRequest :: get ( )
495+ . uri ( & format ! (
496+ "/permissions/resources/mlModel/{model_name}?offset=0&limit=10" ,
497+ ) )
498+ . append_header ( ( header:: CONTENT_LENGTH , 0 ) )
499+ . append_header ( (
500+ header:: AUTHORIZATION ,
501+ Bearer :: new ( admin_session. id . to_string ( ) ) ,
502+ ) ) ;
503+ let res = send_test_request ( req, app_ctx) . await ;
504+
505+ let res_status = res. status ( ) ;
506+ let res_body = serde_json:: from_str :: < Value > ( & read_body_string ( res) . await ) . unwrap ( ) ;
507+ assert_eq ! ( res_status, 200 , "{res_body}" ) ;
508+
509+ assert_eq ! (
510+ res_body,
511+ json!( [ {
512+ "permission" : "Owner" ,
513+ "resource" : {
514+ "id" : model_name. to_string( ) ,
515+ "type" : "mlModel"
516+ } ,
517+ "role" : {
518+ "id" : "d5328854-6190-4af9-ad69-4e74b0961ac9" ,
519+ "name" : "admin"
520+ }
521+ } ]
522+ )
523+ ) ;
524+ }
525+
526+ #[ ge_context:: test]
527+ #[ allow( clippy:: too_many_lines) ]
528+ async fn it_lists_layer_collection_permissions ( app_ctx : PostgresContext < NoTls > ) {
529+ let admin_session = admin_login ( & app_ctx) . await ;
530+
531+ let db = app_ctx. session_context ( admin_session. clone ( ) ) . db ( ) ;
532+
533+ let root_collection = & db. get_root_layer_collection_id ( ) . await . unwrap ( ) ;
534+
535+ let req = actix_web:: test:: TestRequest :: get ( )
536+ . uri ( & format ! (
537+ "/permissions/resources/layerCollection/{root_collection}?offset=0&limit=10" ,
538+ ) )
539+ . append_header ( ( header:: CONTENT_LENGTH , 0 ) )
540+ . append_header ( (
541+ header:: AUTHORIZATION ,
542+ Bearer :: new ( admin_session. id . to_string ( ) ) ,
543+ ) ) ;
544+ let res = send_test_request ( req, app_ctx) . await ;
545+
546+ let res_status = res. status ( ) ;
547+ let res_body = serde_json:: from_str :: < Value > ( & read_body_string ( res) . await ) . unwrap ( ) ;
548+ assert_eq ! ( res_status, 200 , "{res_body}" ) ;
549+
550+ assert_eq ! (
551+ res_body,
552+ json!( [ {
553+ "permission" : "Owner" ,
554+ "resource" : {
555+ "id" : root_collection. to_string( ) ,
556+ "type" : "layerCollection"
557+ } ,
558+ "role" : {
559+ "id" : "d5328854-6190-4af9-ad69-4e74b0961ac9" ,
560+ "name" :
561+ "admin"
562+ }
563+ } , {
564+ "permission" : "Read" ,
565+ "resource" : {
566+ "id" : root_collection. to_string( ) ,
567+ "type" : "layerCollection"
568+ } ,
569+ "role" : {
570+ "id" : "fd8e87bf-515c-4f36-8da6-1a53702ff102" ,
571+ "name" : "anonymous"
572+ }
573+ } , {
574+ "permission" : "Read" ,
575+ "resource" : {
576+ "id" : root_collection. to_string( ) ,
577+ "type" : "layerCollection" ,
578+ } ,
579+ "role" : {
580+ "id" : "4e8081b6-8aa6-4275-af0c-2fa2da557d28" ,
581+ "name" : "user"
582+ }
583+ } ]
584+ )
585+ ) ;
586+ }
587+
588+ #[ ge_context:: test]
589+ #[ allow( clippy:: too_many_lines) ]
590+ async fn it_lists_layer_permissions ( app_ctx : PostgresContext < NoTls > ) {
591+ let admin_session = admin_login ( & app_ctx) . await ;
592+
593+ let db = app_ctx. session_context ( admin_session. clone ( ) ) . db ( ) ;
594+
595+ let root_collection = & db. get_root_layer_collection_id ( ) . await . unwrap ( ) ;
596+
597+ let layer = AddLayer {
598+ name : "layer" . to_string ( ) ,
599+ description : "description" . to_string ( ) ,
600+ workflow : Workflow {
601+ operator : TypedOperator :: Vector (
602+ MockPointSource {
603+ params : MockPointSourceParams {
604+ points : vec ! [ Coordinate2D :: new( 1. , 2. ) ; 3 ] ,
605+ } ,
606+ }
607+ . boxed ( ) ,
608+ ) ,
609+ } ,
610+ symbology : None ,
611+ metadata : Default :: default ( ) ,
612+ properties : Default :: default ( ) ,
613+ } ;
614+
615+ let l_id = db. add_layer ( layer, root_collection) . await . unwrap ( ) ;
616+
617+ let req = actix_web:: test:: TestRequest :: get ( )
618+ . uri ( & format ! (
619+ "/permissions/resources/layer/{l_id}?offset=0&limit=10" ,
620+ ) )
621+ . append_header ( ( header:: CONTENT_LENGTH , 0 ) )
622+ . append_header ( (
623+ header:: AUTHORIZATION ,
624+ Bearer :: new ( admin_session. id . to_string ( ) ) ,
625+ ) ) ;
626+ let res = send_test_request ( req, app_ctx) . await ;
627+
628+ let res_status = res. status ( ) ;
629+ let res_body = serde_json:: from_str :: < Value > ( & read_body_string ( res) . await ) . unwrap ( ) ;
630+ assert_eq ! ( res_status, 200 , "{res_body}" ) ;
631+
632+ assert_eq ! (
633+ res_body,
634+ json!( [ {
635+ "permission" : "Owner" ,
636+ "resource" : {
637+ "id" : l_id. to_string( ) ,
638+ "type" : "layer"
639+ } ,
640+ "role" : {
641+ "id" : "d5328854-6190-4af9-ad69-4e74b0961ac9" ,
642+ "name" :
643+ "admin"
644+ }
645+ } ]
646+ )
647+ ) ;
648+ }
649+
650+ #[ test]
651+ fn resource_from_str_tuple ( ) {
652+ let test_uuid = Uuid :: new_v4 ( ) ;
653+
654+ let layer_res = Resource :: try_from ( ( "layer" . to_owned ( ) , "cats" . to_owned ( ) ) ) . unwrap ( ) ;
655+ assert_eq ! ( layer_res, Resource :: Layer ( LayerId ( "cats" . to_owned( ) ) ) ) ;
656+
657+ let layer_col_res =
658+ Resource :: try_from ( ( "layerCollection" . to_owned ( ) , "cats" . to_owned ( ) ) ) . unwrap ( ) ;
659+ assert_eq ! (
660+ layer_col_res,
661+ Resource :: LayerCollection ( LayerCollectionId ( "cats" . to_owned( ) ) )
662+ ) ;
663+
664+ let project_res = Resource :: try_from ( ( "project" . to_owned ( ) , test_uuid. into ( ) ) ) . unwrap ( ) ;
665+ assert_eq ! ( project_res, Resource :: Project ( ProjectId ( test_uuid) ) ) ;
666+
667+ let dataset_res = Resource :: try_from ( ( "dataset" . to_owned ( ) , "cats" . to_owned ( ) ) ) . unwrap ( ) ;
668+ assert_eq ! (
669+ dataset_res,
670+ Resource :: Dataset ( DatasetName :: new( None , "cats" . to_owned( ) ) )
671+ ) ;
672+
673+ let ml_model_res = Resource :: try_from ( ( "mlModel" . to_owned ( ) , "cats" . to_owned ( ) ) ) . unwrap ( ) ;
674+ assert_eq ! (
675+ ml_model_res,
676+ Resource :: MlModel ( MlModelName :: new( None , "cats" . to_owned( ) ) )
677+ ) ;
678+ }
454679}
0 commit comments