Skip to content

Commit

Permalink
feature: payload validation
Browse files Browse the repository at this point in the history
  • Loading branch information
siepra committed Feb 29, 2024
1 parent 4c0a3d9 commit 609feb4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
Expand Down
3 changes: 2 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Put, Query, Body, NotFoundException, InternalServerErrorException } from '@nestjs/common';
import { Controller, Get, Put, Query, Body, NotFoundException, InternalServerErrorException, UsePipes, ValidationPipe } from '@nestjs/common';
import { AppService } from './app.service';
import { InvitationDTO } from './dto/invitation.dto';

Expand All @@ -16,6 +16,7 @@ export class AppController {
}

@Put('invite')
@UsePipes(new ValidationPipe())
storeInvitation(@Query('CID') cid: string, @Body() invitationDTO: InvitationDTO) {
const filename = `${cid}.json`;
const result = this.appService.storeInvitationFile(filename, invitationDTO);
Expand Down
9 changes: 8 additions & 1 deletion test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('AppController (e2e)', () => {
await app.init();
});

it('/invite (PUT) validates payload schema', () => {
return request(app.getHttpServer())
.put(`/invite/?CID=${cid}`)
.send({})
.expect(400)
});

it('/invite (PUT) 200', () => {
return request(app.getHttpServer())
.put(`/invite/?CID=${cid}`)
Expand All @@ -54,7 +61,7 @@ describe('AppController (e2e)', () => {
it('/invite (PUT) doesn\'t override file', () => {
return request(app.getHttpServer())
.put(`/invite/?CID=${cid}`)
.send({})
.send(invitationData)
.expect(500)
.expect({"message":"File already exists","error":"Internal Server Error","statusCode":500});
});
Expand Down

0 comments on commit 609feb4

Please sign in to comment.