@@ -29,6 +29,13 @@ class CubejsServerCoreOpen extends CubejsServerCore {
2929 }
3030}
3131
32+ // Mock to expose protected methods for testing
33+ class CompilerApiOpen extends CompilerApi {
34+ public getRolesFromContext = super . getRolesFromContext ;
35+
36+ public getGroupsFromContext = super . getGroupsFromContext ;
37+ }
38+
3239const repositoryWithoutPreAggregations : SchemaFileRepository = {
3340 localPath : ( ) => __dirname ,
3441 dataSchemaFiles : ( ) => Promise . resolve ( [
@@ -449,6 +456,42 @@ describe('index.test', () => {
449456 }
450457 ) ) . not . toThrow ( ) ;
451458 } ) ;
459+
460+ test ( 'contextToRoles should be called and return expected roles' , async ( ) => {
461+ const logger = jest . fn ( ( ) => { } ) ;
462+ const contextToRoles = jest . fn ( async ( ) => [ 'admin' , 'manager' ] ) ;
463+
464+ const compilerApi = new CompilerApiOpen (
465+ repositoryWithoutPreAggregations ,
466+ async ( ) => 'mysql' ,
467+ {
468+ logger,
469+ contextToRoles
470+ }
471+ ) ;
472+
473+ const roles = await compilerApi . getRolesFromContext ( { securityContext : { userId : 123 } } ) ;
474+ expect ( contextToRoles ) . toHaveBeenCalledWith ( { securityContext : { userId : 123 } } ) ;
475+ expect ( roles ) . toEqual ( new Set ( [ 'admin' , 'manager' ] ) ) ;
476+ } ) ;
477+
478+ test ( 'contextToGroups should be called and return expected groups' , async ( ) => {
479+ const logger = jest . fn ( ( ) => { } ) ;
480+ const contextToGroups = jest . fn ( async ( ) => [ 'analytics' , 'engineering' ] ) ;
481+
482+ const compilerApi = new CompilerApiOpen (
483+ repositoryWithoutPreAggregations ,
484+ async ( ) => 'mysql' ,
485+ {
486+ logger,
487+ contextToGroups
488+ }
489+ ) ;
490+
491+ const groups = await compilerApi . getGroupsFromContext ( { securityContext : { userId : 456 } } ) ;
492+ expect ( contextToGroups ) . toHaveBeenCalledWith ( { securityContext : { userId : 456 } } ) ;
493+ expect ( groups ) . toEqual ( new Set ( [ 'analytics' , 'engineering' ] ) ) ;
494+ } ) ;
452495 } ) ;
453496
454497 describe ( 'CompilerApi dataSources method' , ( ) => {
0 commit comments