Skip to content

Commit 32e368f

Browse files
authored
chore: added ctx to groups test (#10183)
1 parent 46dbb1d commit 32e368f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

packages/cubejs-server-core/test/unit/index.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3239
const 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

Comments
 (0)