-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimal create tenant endpoint (#153)
- Loading branch information
1 parent
81a1063
commit 3686ce0
Showing
22 changed files
with
339 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { HttpStatus, INestApplication } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import request from 'supertest' | ||
import { v4 as uuid } from 'uuid' | ||
import { AppModule } from '../../../app/app.module' | ||
import { EncryptionService } from '../../../encryption/core/encryption.service' | ||
import { load } from '../../../policy-engine.config' | ||
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 { CreateTenantDto } from '../../http/rest/dto/create-tenant.dto' | ||
import { TenantRepository } from '../../persistence/repository/tenant.repository' | ||
|
||
describe('Tenant', () => { | ||
let app: INestApplication | ||
let module: TestingModule | ||
let testPrismaService: TestPrismaService | ||
let tenantRepository: TenantRepository | ||
let encryptionService: EncryptionService | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [load], | ||
isGlobal: true | ||
}), | ||
AppModule | ||
] | ||
}) | ||
.overrideProvider(KeyValueRepository) | ||
.useValue(new InMemoryKeyValueRepository()) | ||
.compile() | ||
|
||
app = module.createNestApplication() | ||
|
||
tenantRepository = module.get<TenantRepository>(TenantRepository) | ||
testPrismaService = module.get<TestPrismaService>(TestPrismaService) | ||
encryptionService = module.get<EncryptionService>(EncryptionService) | ||
|
||
await module.get<EncryptionService>(EncryptionService).onApplicationBootstrap() | ||
|
||
await app.init() | ||
}) | ||
|
||
afterAll(async () => { | ||
await testPrismaService.truncateAll() | ||
await module.close() | ||
await app.close() | ||
}) | ||
|
||
beforeEach(async () => { | ||
await testPrismaService.truncateAll() | ||
await encryptionService.onApplicationBootstrap() | ||
}) | ||
|
||
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').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').send(payload) | ||
const { status, body } = await request(app.getHttpServer()).post('/tenants').send(payload) | ||
|
||
expect(body).toEqual({ | ||
message: 'Tenant already exist', | ||
statusCode: HttpStatus.BAD_REQUEST | ||
}) | ||
expect(status).toEqual(HttpStatus.BAD_REQUEST) | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
import { Tenant } from '../../../shared/types/domain.type' | ||
import { TenantRepository } from '../../persistence/repository/tenant.repository' | ||
|
||
@Injectable() | ||
export class TenantService { | ||
constructor(private tenantRepository: TenantRepository) {} | ||
|
||
async findByClientId(clientId: string): Promise<Tenant | null> { | ||
return this.tenantRepository.findByClientId(clientId) | ||
} | ||
|
||
async create(tenant: Tenant): Promise<Tenant> { | ||
if (await this.tenantRepository.findByClientId(tenant.clientId)) { | ||
throw new ApplicationException({ | ||
message: 'Tenant already exist', | ||
suggestedHttpStatusCode: HttpStatus.BAD_REQUEST, | ||
context: { clientId: tenant.clientId } | ||
}) | ||
} | ||
|
||
return this.tenantRepository.create(tenant) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
apps/policy-engine/src/app/http/rest/controller/tenant.controller.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,34 @@ | ||
import { Body, Controller, Post } from '@nestjs/common' | ||
import { randomBytes } from 'crypto' | ||
import { v4 as uuid } from 'uuid' | ||
import { TenantService } from '../../../core/service/tenant.service' | ||
import { CreateTenantDto } from '../dto/create-tenant.dto' | ||
|
||
@Controller('/tenants') | ||
export class TenantController { | ||
constructor(private tenantService: TenantService) {} | ||
|
||
@Post() | ||
async create(@Body() body: CreateTenantDto) { | ||
const now = new Date() | ||
|
||
const tenant = await this.tenantService.create({ | ||
clientId: body.clientId || uuid(), | ||
clientSecret: randomBytes(42).toString('hex'), | ||
dataStore: { | ||
entity: { | ||
...body.entityDataStore, | ||
keys: [] | ||
}, | ||
policy: { | ||
...body.policyDataStore, | ||
keys: [] | ||
} | ||
}, | ||
createdAt: now, | ||
updatedAt: now | ||
}) | ||
|
||
return tenant | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
apps/policy-engine/src/app/http/rest/dto/create-tenant.dto.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,24 @@ | ||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger' | ||
import { Type } from 'class-transformer' | ||
import { IsDefined, IsString } from 'class-validator' | ||
|
||
class DataStoreConfigurationDto { | ||
dataUrl: string | ||
signatureUrl: string | ||
} | ||
|
||
export class CreateTenantDto { | ||
@IsString() | ||
@ApiPropertyOptional() | ||
clientId?: string | ||
|
||
@IsDefined() | ||
@Type(() => DataStoreConfigurationDto) | ||
@ApiProperty() | ||
entityDataStore: DataStoreConfigurationDto | ||
|
||
@IsDefined() | ||
@Type(() => DataStoreConfigurationDto) | ||
@ApiProperty() | ||
policyDataStore: DataStoreConfigurationDto | ||
} |
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
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
25 changes: 25 additions & 0 deletions
25
apps/policy-engine/src/shared/exception/application.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,25 @@ | ||
import { HttpException, HttpStatus } from '@nestjs/common' | ||
|
||
export type ApplicationExceptionParams = { | ||
message: string | ||
suggestedHttpStatusCode: HttpStatus | ||
context?: unknown | ||
origin?: Error | ||
} | ||
|
||
export class ApplicationException extends HttpException { | ||
readonly context: unknown | ||
readonly origin?: Error | ||
|
||
constructor(params: ApplicationExceptionParams) { | ||
super(params.message, params.suggestedHttpStatusCode) | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, ApplicationException) | ||
} | ||
|
||
this.name = this.constructor.name | ||
this.context = params.context | ||
this.origin = params.origin | ||
} | ||
} |
Oops, something went wrong.