Skip to content

Commit

Permalink
Add keys in the create tenant DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalderipe committed Mar 27, 2024
1 parent af6bc5c commit 39c18fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
40 changes: 21 additions & 19 deletions apps/policy-engine/src/engine/__test__/e2e/tenant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ConfigModule, ConfigService } from '@narval/config-module'
import { EncryptionModuleOptionProvider } from '@narval/encryption-module'
import { DataStoreConfiguration } from '@narval/policy-engine-shared'
import { secp256k1PrivateKeyToJwk } from '@narval/signature'
import { HttpStatus, INestApplication } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import request from 'supertest'
import { v4 as uuid } from 'uuid'
import { generatePrivateKey } from 'viem/accounts'
import { EngineService } from '../../../engine/core/service/engine.service'
import { Config, load } from '../../../policy-engine.config'
import {
Expand All @@ -29,24 +32,15 @@ describe('Tenant', () => {
let tenantService: TenantService
let engineService: EngineService
let configService: ConfigService<Config>
let dataStoreConfiguration: DataStoreConfiguration
let createTenantPayload: CreateTenantDto

const adminApiKey = 'test-admin-api-key'

const clientId = uuid()

const dataStoreUrl = 'http://127.0.0.1:9999/test-data-store'

const dataStoreConfiguration = {
dataUrl: dataStoreUrl,
signatureUrl: dataStoreUrl
}

const createTenantPayload: CreateTenantDto = {
clientId,
entityDataStore: dataStoreConfiguration,
policyDataStore: dataStoreConfiguration
}

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
Expand All @@ -73,6 +67,20 @@ describe('Tenant', () => {
testPrismaService = module.get<TestPrismaService>(TestPrismaService)
configService = module.get<ConfigService<Config>>(ConfigService)

const jwk = secp256k1PrivateKeyToJwk(generatePrivateKey())

dataStoreConfiguration = {
dataUrl: dataStoreUrl,
signatureUrl: dataStoreUrl,
keys: [jwk]
}

createTenantPayload = {
clientId,
entityDataStore: dataStoreConfiguration,
policyDataStore: dataStoreConfiguration
}

await testPrismaService.truncateAll()

await engineService.save({
Expand Down Expand Up @@ -108,14 +116,8 @@ describe('Tenant', () => {
createdAt: expect.any(String),
updatedAt: expect.any(String),
dataStore: {
policy: {
...dataStoreConfiguration,
keys: []
},
entity: {
...dataStoreConfiguration,
keys: []
}
policy: dataStoreConfiguration,
entity: dataStoreConfiguration
}
})
expect(body).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ export class TenantController {
clientId: body.clientId || uuid(),
clientSecret: randomBytes(42).toString('hex'),
dataStore: {
entity: {
...body.entityDataStore,
keys: []
},
policy: {
...body.policyDataStore,
keys: []
}
entity: body.entityDataStore,
policy: body.policyDataStore
},
createdAt: now,
updatedAt: now
Expand Down
31 changes: 9 additions & 22 deletions apps/policy-engine/src/engine/http/rest/dto/create-tenant.dto.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsDefined, IsString } from 'class-validator'
import { dataStoreConfigurationSchema } from '@narval/policy-engine-shared'
import { createZodDto } from 'nestjs-zod'
import { z } from 'zod'

class DataStoreConfigurationDto {
dataUrl: string
signatureUrl: string
}
const createTenantSchema = z.object({
clientId: z.string().optional().describe('foo'),
entityDataStore: dataStoreConfigurationSchema,
policyDataStore: dataStoreConfigurationSchema
})

export class CreateTenantDto {
@IsString()
@ApiPropertyOptional()
clientId?: string

@IsDefined()
@Type(() => DataStoreConfigurationDto)
@ApiProperty()
entityDataStore: DataStoreConfigurationDto

@IsDefined()
@Type(() => DataStoreConfigurationDto)
@ApiProperty()
policyDataStore: DataStoreConfigurationDto
}
export class CreateTenantDto extends createZodDto(createTenantSchema) {}

0 comments on commit 39c18fc

Please sign in to comment.