@@ -387,8 +387,72 @@ pub async fn sync_field(
387387
388388#[ cfg( test) ]
389389mod tests {
390+ use super :: * ;
391+ use lazy_static:: lazy_static;
392+
393+ lazy_static ! {
394+ static ref SCOPES : Vec <AuthorizationMapping > = vec![ AuthorizationMapping {
395+ name: "scope1" . to_string( ) ,
396+ grants: vec![ AuthorizationGrants {
397+ endpoint: Endpoint :: Target . to_string( ) ,
398+ permissions: vec![ Permission :: Read . to_string( ) ] ,
399+ } , ] ,
400+ } , ] ;
401+ static ref GROUPS : Vec <AuthorizationMapping > = vec![ AuthorizationMapping {
402+ name: "group1" . to_string( ) ,
403+ grants: vec![ AuthorizationGrants {
404+ endpoint: Endpoint :: Prompt . to_string( ) ,
405+ permissions: vec![ Permission :: Write . to_string( ) ] ,
406+ } , ] ,
407+ } , ] ;
408+ static ref SCOPES_WILDCARD : Vec <AuthorizationMapping > = vec![ AuthorizationMapping {
409+ name: "scope2" . to_string( ) ,
410+ grants: vec![ AuthorizationGrants {
411+ endpoint: "*" . to_string( ) ,
412+ permissions: vec![ "*" . to_string( ) ] ,
413+ } , ] ,
414+ } , ] ;
415+ static ref SCOPES_NO_MATCH : Vec <AuthorizationMapping > = vec![ AuthorizationMapping {
416+ name: "scope3" . to_string( ) ,
417+ grants: vec![ AuthorizationGrants {
418+ endpoint: "NonExistent" . to_string( ) ,
419+ permissions: vec![ "NonExistent" . to_string( ) ] ,
420+ } , ] ,
421+ } , ] ;
422+ }
423+
390424 #[ test]
391425 fn test_read_permission_matrix ( ) {
392- // TODO
426+ let matrix = read_permission_matrix ( & SCOPES , & GROUPS ) ;
427+
428+ assert ! ( matrix. contains_key( & ( Endpoint :: Target , Permission :: Read ) ) ) ;
429+ assert ! ( matrix. contains_key( & ( Endpoint :: Prompt , Permission :: Write ) ) ) ;
430+
431+ let entry = matrix. get ( & ( Endpoint :: Target , Permission :: Read ) ) . unwrap ( ) ;
432+ assert ! ( entry. 0 . contains( & Cow :: Borrowed ( "scope1" ) ) ) ;
433+ assert ! ( entry. 1 . is_empty( ) ) ;
434+
435+ let entry = matrix. get ( & ( Endpoint :: Prompt , Permission :: Write ) ) . unwrap ( ) ;
436+ assert ! ( entry. 0 . is_empty( ) ) ;
437+ assert ! ( entry. 1 . contains( & Cow :: Borrowed ( "group1" ) ) ) ;
438+ }
439+
440+ #[ test]
441+ fn test_read_permission_matrix_with_wildcards ( ) {
442+ let matrix = read_permission_matrix ( & SCOPES_WILDCARD , & [ ] ) ;
443+
444+ for endpoint in Endpoint :: iter ( ) {
445+ for permission in Permission :: iter ( ) {
446+ let entry = matrix. get ( & ( endpoint. clone ( ) , permission. clone ( ) ) ) . unwrap ( ) ;
447+ assert ! ( entry. 0 . contains( & Cow :: Borrowed ( "scope2" ) ) ) ;
448+ }
449+ }
450+ }
451+
452+ #[ test]
453+ fn test_read_permission_matrix_no_matches ( ) {
454+ let matrix = read_permission_matrix ( & SCOPES_NO_MATCH , & [ ] ) ;
455+
456+ assert ! ( matrix. is_empty( ) ) ;
393457 }
394458}
0 commit comments