Skip to content

Commit

Permalink
feat: Support snake case security_context in COMPILE_CONTEXT (#6519)
Browse files Browse the repository at this point in the history
* chore: Support snake case security_context in COMPILE_CONTEXT

* fix:lint
  • Loading branch information
keydunov committed May 1, 2023
1 parent f023484 commit feb3fe3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Expand Up @@ -235,13 +235,21 @@ export class DataSchemaCompiler {
return exports[foundFile.fileName];
}
},
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : R.clone(this.compileContext || {}),
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : this.cloneCompileContextWithGetterAlias(this.compileContext || {}),
}, { filename: file.fileName, timeout: 15000 });
} catch (e) {
errorsReport.error(e);
}
}

// Alias "securityContext" with "security_context" (snake case version)
// to support snake case based data models
cloneCompileContextWithGetterAlias(compileContext) {
const clone = R.clone(compileContext || {});
clone.security_context = compileContext.securityContext;
return clone;
}

standaloneCompileContextProxy() {
return new Proxy({}, {
get: () => {
Expand Down
Expand Up @@ -444,4 +444,41 @@ cubes:
}]
);
});

it('COMPILE_CONTEXT', async () => {
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
cubes:
- name: orders
sql: "SELECT 1 as id, 'completed' as status"
public: COMPILE_CONTEXT.security_context.can_see_orders
measures:
- name: count
type: count
`,
{},
{
compileContext: {
authInfo: null,
securityContext: { can_see_orders: true },
requestId: 'XXX'
}
});

await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'orders.count'
],
timeDimensions: [],
timezone: 'America/Los_Angeles'
});

return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
expect(res).toEqual(
[{ orders__count: '1' }]
);
});
});
});
Expand Up @@ -7,7 +7,7 @@ export const prepareCompiler = (content, options) => originalPrepareCompiler({
])
}, { adapter: 'postgres', ...options });

export const prepareYamlCompiler = (content, { yamlExtension, ...options } = {}) => originalPrepareCompiler({
export const prepareYamlCompiler = (content, yamlExtension, options = {}) => originalPrepareCompiler({
localPath: () => __dirname,
dataSchemaFiles: () => Promise.resolve([
{ fileName: yamlExtension ? 'main.yaml' : 'main.yml', content }
Expand Down

0 comments on commit feb3fe3

Please sign in to comment.