Skip to content

Commit

Permalink
feat: Support multiple polar files (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Youngjoon Lee authored Dec 31, 2020
1 parent e1457d3 commit e86fc2e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import { OsoModule } from 'nestjs-oso';
imports: [
OsoModule.forRoot({
loadFile: './permissions.polar',
// or
// loadFile: './**/*.polar',
// loadFiles: ['./rules/*.polar', './permissions.polar'],
}),
],
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"peerDependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"fast-glob": "^3.2.4",
"oso": "^0.9.0",
"reflect-metadata": "^0.1.12"
}
Expand Down
58 changes: 50 additions & 8 deletions src/__tests__/oso-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,64 @@ describe('OsoService', () => {
it('should register TestClass', () => {
const classes = OsoMetadataStorage.getClasses();
expect(classes).toContain(TestClass);
})
});

it('should load files', async () => {
it('should load a file', async () => {
const mod = await Test.createTestingModule({
imports: [OsoModule.forRoot({
loadFile: './dummy-file.polar',
})],
imports: [
OsoModule.forRoot({
loadFile: `${__dirname}/rule1.polar`,
}),
],
}).compile();

const loadFileApp = mod.createNestApplication();
const service = loadFileApp.get<OsoService>(OsoService);
const func = jest.spyOn(service, 'loadFile').mockImplementation(async () => {});
const func = jest
.spyOn(service, 'loadFile')
.mockImplementation(async () => {});
await loadFileApp.init();

expect(func).toBeCalled();
})
expect(func).toBeCalledTimes(1);
});

it('should load a files', async () => {
const mod = await Test.createTestingModule({
imports: [
OsoModule.forRoot({
loadFiles: [`${__dirname}/rule1.polar`, `${__dirname}/rule2.polar`],
}),
],
}).compile();

const loadFileApp = mod.createNestApplication();
const service = loadFileApp.get<OsoService>(OsoService);
const func = jest
.spyOn(service, 'loadFile')
.mockImplementation(async () => {});
await loadFileApp.init();

expect(func).toBeCalledTimes(2);
});

it('should load files using regex', async () => {
const mod = await Test.createTestingModule({
imports: [
OsoModule.forRoot({
loadFile: './**/*.polar',
}),
],
}).compile();

const loadFileApp = mod.createNestApplication();
const service = loadFileApp.get<OsoService>(OsoService);
const func = jest
.spyOn(service, 'loadFile')
.mockImplementation(async () => {});
await loadFileApp.init();

expect(func).toBeCalledTimes(2);
});

it('should load strings', async () => {
const mod = await Test.createTestingModule({
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/rule1.polar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow(_, _, _);
1 change: 1 addition & 0 deletions src/__tests__/rule2.polar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow(_, _, _);
1 change: 1 addition & 0 deletions src/oso.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { OsoService } from './oso.service';
export interface OsoModuleConfig {
loadStr?: string;
loadFile?: string;
loadFiles?: string[];
osoOptions?: Options;
isGlobal?: boolean; // If true, registers `OsoModule` as a global module.
}
Expand Down
16 changes: 15 additions & 1 deletion src/oso.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Oso } from 'oso';
import { OsoMetadataStorage } from './oso-metadata.storage';
import { OSO_MODULE_OPTIONS } from './oso.constants';
import { OsoModuleConfig } from './oso.module';
import * as FastGlob from 'fast-glob';

@Injectable()
export class OsoService extends Oso implements OnModuleInit {
Expand All @@ -19,11 +20,24 @@ export class OsoService extends Oso implements OnModuleInit {
});

if (this.options.loadFile) {
await this.loadFile(this.options.loadFile);
await this.loadFiles([this.options.loadFile]);
}

if (this.options.loadFiles) {
await this.loadFiles(this.options.loadFiles);
}

if (this.options.loadStr) {
await this.loadStr(this.options.loadStr);
}
}

private async loadFiles(patterns: string[]): Promise<void> {
const files = await FastGlob(patterns);
await Promise.all(
files.map(async (file: string) => {
await this.loadFile(file);
}),
);
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ fast-deep-equal@^3.1.1:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-glob@^3.1.1:
fast-glob@^3.1.1, fast-glob@^3.2.4:
version "3.2.4"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.4.tgz#d20aefbf99579383e7f3cc66529158c9b98554d3"
integrity sha512-kr/Oo6PX51265qeuCYsyGypiO5uJFgBS0jksyG7FUeCyQzNwYnzrNIMR1NXfkZXsMYXYLRAHgISHBz8gQcxKHQ==
Expand Down

0 comments on commit e86fc2e

Please sign in to comment.