Skip to content

Commit

Permalink
pr comments fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnlaugurG committed Oct 4, 2024
1 parent 3ed8368 commit eeb80b0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
UseGuards,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import flatMap from 'lodash/flatMap'

import {
BypassAuth,
Expand All @@ -24,11 +25,11 @@ import {
DelegationAdminCustomDto,
DelegationAdminCustomService,
DelegationDTO,
ZendeskWebhookInputDto,
} from '@island.is/auth-api-lib'
import { Documentation } from '@island.is/nest/swagger'
import { Audit, AuditService } from '@island.is/nest/audit'
import { DelegationAdminScopes } from '@island.is/auth/scopes'
import flatMap from 'lodash/flatMap'
import { isDefined } from '@island.is/shared/utils'

const namespace = '@island.is/auth/delegation-admin'
Expand All @@ -45,8 +46,8 @@ export class DelegationAdminController {
private readonly auditService: AuditService,
) {}

@Scopes(DelegationAdminScopes.read)
@Get()
@Scopes(DelegationAdminScopes.read)
@Documentation({
response: { status: 200, type: DelegationAdminCustomDto },
request: {
Expand Down Expand Up @@ -97,22 +98,14 @@ export class DelegationAdminController {

@BypassAuth()
@UseGuards(ZendeskAuthGuard(ZENDESK_WEBHOOK_SECRET_GENERAL_MANDATE))
@Post(':zendeskId')
@Post('/zendesk')
@Documentation({
response: { status: 201, type: DelegationDTO },
request: {
params: {
zendeskId: {
required: true,
description: 'The id of the zendesk ticket containing the delegation',
},
},
},
response: { status: 200, type: DelegationDTO },
})
createByZendeskId(
@Param('zendeskId') zendeskId: string,
@Body() { id }: ZendeskWebhookInputDto,
): Promise<DelegationDTO> {
return this.delegationAdminService.createDelegationByZendeskId(zendeskId)
return this.delegationAdminService.createDelegationByZendeskId(id)
}

@Delete(':delegationId')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DelegationDTO, SequelizeConfigService } from '@island.is/auth-api-lib'
import { DelegationAdminCustomService } from '@island.is/auth-api-lib'

import { AppModule } from '../../../app.module'
import { includeRawBodyMiddleware } from '@island.is/infra-nest-server'

describe('withoutAuth and permissions', () => {
async function formatUrl(app: TestApp, endpoint: string, user?: User) {
Expand Down Expand Up @@ -147,17 +148,7 @@ describe('withoutAuth and permissions', () => {
dbType: 'postgres',
beforeServerStart: async (app) => {
await new Promise((resolve) =>
resolve(
app.use(
bodyParser.json({
verify: (req: any, res, buf) => {
if (buf && buf.length) {
req.rawBody = buf
}
},
}),
),
),
resolve(app.use(includeRawBodyMiddleware())),
)
},
})
Expand All @@ -175,14 +166,14 @@ describe('withoutAuth and permissions', () => {
app.cleanUp()
})

it('POST /delegation-admin/:zendeskId should return 403 Forbidden when user does not have correct headers for the body', async () => {
it('POST /delegation-admin/zendesk should return 403 Forbidden when request signature is invalid.', async () => {
// Act
const res = await getRequestMethod(
server,
'POST',
)('/delegation-admin/123')
)('/delegation-admin/zendesk')
.send({
custom: 'Incorrect body',
id: 'Incorrect body',
})
.set(
'x-zendesk-webhook-signature',
Expand All @@ -200,23 +191,23 @@ describe('withoutAuth and permissions', () => {
})
})

it('POST /delegation-admin/:zendeskId should return 201 since the correct headers are set for that body', async () => {
it('POST /delegation-admin/zendesk should return 201 when signature is valid', async () => {
// Act
const res = await getRequestMethod(
server,
'POST',
)('/delegation-admin/123')
)('/delegation-admin/zendesk')
.send({
custom: 'test',
id: 'test',
})
.set(
'x-zendesk-webhook-signature',
'6sUtGV8C8OdoGgCdsV2xRm3XeskZ33Bc5124RiAK4Q4=',
'ntgS06VGgd4z73lHjIpC2sk9azhRNi4u1xkXF/KPKTs=',
)
.set('x-zendesk-webhook-signature-timestamp', '2024-10-02T14:21:04Z')

// Assert
expect(res.status).toEqual(201)
expect(res.status).toEqual(200)
})
})
})
16 changes: 5 additions & 11 deletions apps/services/auth/admin-api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { bootstrap } from '@island.is/infra-nest-server'
import {
bootstrap,
includeRawBodyMiddleware,
} from '@island.is/infra-nest-server'

import { AppModule } from './app/app.module'
import { environment as env } from './environments'
import { openApi } from './openApi'
import bodyParser from 'body-parser'

bootstrap({
appModule: AppModule,
Expand All @@ -16,14 +18,6 @@ bootstrap({
database: true,
},
beforeServerStart: async (app) => {
app.use(
bodyParser.json({
verify: (req: any, res, buf) => {
if (buf && buf.length) {
req.rawBody = buf
}
},
}),
)
app.use(includeRawBodyMiddleware())
},
})
1 change: 1 addition & 0 deletions libs/auth-api-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from './lib/delegations/dto/delegation-index.dto'
export * from './lib/delegations/dto/paginated-delegation-provider.dto'
export * from './lib/delegations/dto/delegation-provider.dto'
export * from './lib/delegations/dto/merged-delegation.dto'
export * from './lib/delegations/dto/zendesk-webhook-input.dto'
export * from './lib/delegations/models/delegation.model'
export * from './lib/delegations/models/delegation.model'
export * from './lib/delegations/models/delegation-scope.model'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IsString } from 'class-validator'
import { ApiProperty } from '@nestjs/swagger'

export class ZendeskWebhookInputDto {
@IsString()
@ApiProperty()
id!: string
}
1 change: 1 addition & 0 deletions libs/infra-nest-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './lib/buildOpenApi'
export * from './lib/infra/infra.controller'
export { InfraModule } from './lib/infra/infra.module'
export { HealthCheckOptions } from './lib/infra/health/types'
export * from './lib/includeRawBodyMiddleware'
export * from './lib/processJob'
export * from './lib/types'
14 changes: 14 additions & 0 deletions libs/infra-nest-server/src/lib/includeRawBodyMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import bodyParser from 'body-parser'

/**
* Middleware that includes the raw body in the request object.
*/
export const includeRawBodyMiddleware = () => {
return bodyParser.json({
verify: (req: any, res, buf) => {
if (buf && buf.length) {
req.rawBody = buf
}
},
})
}

0 comments on commit eeb80b0

Please sign in to comment.