Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keys in the create tenant DTO #195

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
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) {}
Loading