-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add policy engine resource path environment variable
- Loading branch information
1 parent
33ae82c
commit cd18f44
Showing
75 changed files
with
4,152 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
apps/policy-engine/src/engine/__test__/e2e/tenant.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { EncryptionModuleOptionProvider } from '@narval/encryption-module' | ||
import { HttpStatus, INestApplication } from '@nestjs/common' | ||
import { ConfigModule, ConfigService } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import request from 'supertest' | ||
import { v4 as uuid } from 'uuid' | ||
import { EngineService } from '../../../engine/core/service/engine.service' | ||
import { Config, load } from '../../../policy-engine.config' | ||
import { REQUEST_HEADER_API_KEY } from '../../../policy-engine.constant' | ||
import { KeyValueRepository } from '../../../shared/module/key-value/core/repository/key-value.repository' | ||
import { InMemoryKeyValueRepository } from '../../../shared/module/key-value/persistence/repository/in-memory-key-value.repository' | ||
import { TestPrismaService } from '../../../shared/module/persistence/service/test-prisma.service' | ||
import { getTestRawAesKeyring } from '../../../shared/testing/encryption.testing' | ||
import { CreateTenantDto } from '../../../tenant/http/rest/dto/create-tenant.dto' | ||
import { EngineModule } from '../../engine.module' | ||
import { TenantRepository } from '../../persistence/repository/tenant.repository' | ||
|
||
describe('Tenant', () => { | ||
let app: INestApplication | ||
let module: TestingModule | ||
let testPrismaService: TestPrismaService | ||
let tenantRepository: TenantRepository | ||
let engineService: EngineService | ||
let configService: ConfigService<Config, true> | ||
|
||
const adminApiKey = 'test-admin-api-key' | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [load], | ||
isGlobal: true | ||
}), | ||
EngineModule | ||
] | ||
}) | ||
.overrideProvider(KeyValueRepository) | ||
.useValue(new InMemoryKeyValueRepository()) | ||
.overrideProvider(EncryptionModuleOptionProvider) | ||
.useValue({ | ||
keyring: getTestRawAesKeyring() | ||
}) | ||
.compile() | ||
|
||
app = module.createNestApplication() | ||
|
||
engineService = module.get<EngineService>(EngineService) | ||
tenantRepository = module.get<TenantRepository>(TenantRepository) | ||
testPrismaService = module.get<TestPrismaService>(TestPrismaService) | ||
configService = module.get<ConfigService<Config, true>>(ConfigService) | ||
|
||
await testPrismaService.truncateAll() | ||
|
||
await engineService.save({ | ||
id: configService.get('engine.id', { infer: true }), | ||
masterKey: 'unsafe-test-master-key', | ||
adminApiKey | ||
}) | ||
|
||
await app.init() | ||
}) | ||
|
||
afterAll(async () => { | ||
await testPrismaService.truncateAll() | ||
await module.close() | ||
await app.close() | ||
}) | ||
|
||
describe('POST /tenants', () => { | ||
const clientId = uuid() | ||
|
||
const dataStoreConfiguration = { | ||
dataUrl: 'http://some.host', | ||
signatureUrl: 'http://some.host' | ||
} | ||
|
||
const payload: CreateTenantDto = { | ||
clientId, | ||
entityDataStore: dataStoreConfiguration, | ||
policyDataStore: dataStoreConfiguration | ||
} | ||
|
||
it('creates a new tenant', async () => { | ||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/tenants') | ||
.set(REQUEST_HEADER_API_KEY, adminApiKey) | ||
.send(payload) | ||
const actualTenant = await tenantRepository.findByClientId(clientId) | ||
|
||
expect(body).toMatchObject({ | ||
clientId, | ||
clientSecret: expect.any(String), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
dataStore: { | ||
policy: { | ||
...dataStoreConfiguration, | ||
keys: [] | ||
}, | ||
entity: { | ||
...dataStoreConfiguration, | ||
keys: [] | ||
} | ||
} | ||
}) | ||
expect(body).toEqual({ | ||
...actualTenant, | ||
createdAt: actualTenant?.createdAt.toISOString(), | ||
updatedAt: actualTenant?.updatedAt.toISOString() | ||
}) | ||
expect(status).toEqual(HttpStatus.CREATED) | ||
}) | ||
|
||
it('responds with an error when clientId already exist', async () => { | ||
await request(app.getHttpServer()).post('/tenants').set(REQUEST_HEADER_API_KEY, adminApiKey).send(payload) | ||
|
||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/tenants') | ||
.set(REQUEST_HEADER_API_KEY, adminApiKey) | ||
.send(payload) | ||
|
||
expect(body).toEqual({ | ||
message: 'Tenant already exist', | ||
statusCode: HttpStatus.BAD_REQUEST | ||
}) | ||
expect(status).toEqual(HttpStatus.BAD_REQUEST) | ||
}) | ||
|
||
it('responds with forbidden when admin api key is invalid', async () => { | ||
const { status, body } = await request(app.getHttpServer()) | ||
.post('/tenants') | ||
.set(REQUEST_HEADER_API_KEY, 'invalid-api-key') | ||
.send(payload) | ||
|
||
expect(body).toMatchObject({ | ||
message: 'Forbidden resource', | ||
statusCode: HttpStatus.FORBIDDEN | ||
}) | ||
expect(status).toEqual(HttpStatus.FORBIDDEN) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
apps/policy-engine/src/engine/core/exception/data-store.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
|
||
export class DataStoreException extends ApplicationException {} |
33 changes: 33 additions & 0 deletions
33
apps/policy-engine/src/engine/core/factory/data-store-repository.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { FileSystemDataStoreRepository } from '../../persistence/repository/file-system-data-store.repository' | ||
import { HttpDataStoreRepository } from '../../persistence/repository/http-data-store.repository' | ||
import { DataStoreException } from '../exception/data-store.exception' | ||
import { DataStoreRepository } from '../repository/data-store.repository' | ||
|
||
@Injectable() | ||
export class DataStoreRepositoryFactory { | ||
constructor( | ||
private fileSystemRepository: FileSystemDataStoreRepository, | ||
private httpRepository: HttpDataStoreRepository | ||
) {} | ||
|
||
getRepository(url: string): DataStoreRepository { | ||
switch (this.getProtocol(url)) { | ||
case 'file': | ||
return this.fileSystemRepository | ||
case 'http': | ||
case 'https': | ||
return this.httpRepository | ||
default: | ||
throw new DataStoreException({ | ||
message: 'Data store URL protocol not supported', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { url } | ||
}) | ||
} | ||
} | ||
|
||
private getProtocol(url: string): string { | ||
return url.split(':')[0] | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
apps/policy-engine/src/engine/core/repository/data-store.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface DataStoreRepository { | ||
fetch<Data>(url: string): Promise<Data> | ||
} |
Oops, something went wrong.