Skip to content

Commit

Permalink
Merge pull request #33 from trade-tariff/add-description-for-create
Browse files Browse the repository at this point in the history
allow create to add a description
  • Loading branch information
shjohnson authored May 7, 2024
2 parents f4aad76 + 5c56095 commit a495a7a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions spec/controllers/apiKeysControllerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('ApiKeyController', () => {
repository = jasmine.createSpyObj('CustomerApiKeyRepository', { createKey: createKeyResult })
repository.createKey.bind(repository)
controller = new ApiKeyController(repository)
req = { params: { fpoId: 'fpoId' } } as any
req = { params: { fpoId: 'fpoId' }, body: { apiKeyDescription: 'description' } } as any
const res = {
status: function (code: number) {
this.statusCode = code
Expand All @@ -202,7 +202,7 @@ describe('ApiKeyController', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await controller.create(req, res)

expect(repository.createKey).toHaveBeenCalledWith('fpoId')
expect(repository.createKey).toHaveBeenCalledWith('fpoId', 'description')
expect(res.statusCode).toBe(201)
expect(res.data).toEqual({
CustomerApiKeyId: '',
Expand Down
4 changes: 2 additions & 2 deletions spec/operations/createCustomerApiKeySpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ describe('CreateCustomerApiKey', () => {
})

it('generates a fresh api key', async () => {
const created = await createCustomerApiKey.call('fpoId')
const created = await createCustomerApiKey.call('fpoId', 'description')
const decrypted = await created.toDecryptedJson()

expect(created.FpoId).toEqual('fpoId')
expect(created.CustomerApiKeyId).toMatch(/^HUB[A-Z0-9]{17}$/)
expect(created.Secret).toMatch(/^[\w\d+/=]+:[\w\d+/=]+$/)
expect(created.Enabled).toEqual(true)
expect(decrypted.Secret).toMatch(/^[\w\d+/=]+$/)
expect(created.Description).toMatch(/^Autogenerated on \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/)
expect(created.Description).toMatch(/^description/)
expect(created.ApiGatewayId).toEqual('apiGatewayId')
expect(created.UsagePlanId).toEqual('usagePlanId')
expect(created.Saved).toEqual(true)
Expand Down
4 changes: 2 additions & 2 deletions spec/repositories/customerApiKeyRepositorySpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ describe('CustomerApiKeyRepository', () => {
})

it('returns a CustomerApiKey', async () => {
const actual = await repository.createKey('customer-id')
const actual = await repository.createKey('customer-id', 'description')

expect(actual).toEqual(jasmine.any(CustomerApiKey))
expect(actual.CustomerApiKeyId).toEqual('customer-api-key-id')
expect(mockCreateOperation.call).toHaveBeenCalledWith('customer-id')
expect(mockCreateOperation.call).toHaveBeenCalledWith('customer-id', 'description')
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import fs from 'fs'

export default function initEnvironment (): void {
const environment = process.env.NODE_ENV
const environment = process.env.NODE_ENV ?? 'development'
const envFilePath = path.join(process.cwd(), `.env.${environment}`)

if (environment === 'production') {
Expand Down
9 changes: 8 additions & 1 deletion src/controllers/apiKeysController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ export class ApiKeyController {

async create (req: Request, res: Response): Promise<void> {
const fpoId = req.params.fpoId
const apiKey = await this.repository.createKey(fpoId)
const description = req.body.apiKeyDescription

if (typeof description !== 'string') {
res.status(400).json({ error: 'Invalid description type' })
return
}

const apiKey = await this.repository.createKey(fpoId, description)

res.status(201).json(apiKey.toJson())
}
Expand Down
4 changes: 2 additions & 2 deletions src/operations/createCustomerApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class CreateCustomerApiKey {
private readonly apiGatewayClient: APIGatewayClient
) {}

async call (fpoId: string): Promise<CustomerApiKey> {
async call (fpoId: string, description: string): Promise<CustomerApiKey> {
const customerApiKey = new CustomerApiKey()

customerApiKey.FpoId = fpoId
customerApiKey.CustomerApiKeyId = this.generateClientId()
customerApiKey.Secret = await this.generateRandomSecret()
customerApiKey.Enabled = true
customerApiKey.Description = `Autogenerated on ${new Date().toISOString()}`
customerApiKey.Description = description ?? `Autogenerated on ${new Date().toISOString()}`
customerApiKey.UsagePlanId = process.env.USAGE_PLAN_ID ?? 'unknown'

await this.createInApiGateway(customerApiKey)
Expand Down
4 changes: 2 additions & 2 deletions src/repositories/customerApiKeyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class CustomerApiKeyRepository {
return await this.listOperation.call(fpoId)
}

async createKey (fpoId: string): Promise<CustomerApiKey> {
return await this.createOperation.call(fpoId)
async createKey (fpoId: string, description: string): Promise<CustomerApiKey> {
return await this.createOperation.call(fpoId, description)
}

async getKey (fpoId: string, id: string): Promise<CustomerApiKey | null> {
Expand Down

0 comments on commit a495a7a

Please sign in to comment.