From 2f78d4daf2dbf49299c82d493924ed6201217307 Mon Sep 17 00:00:00 2001 From: Artem Derevnjuk Date: Thu, 30 Mar 2023 23:22:45 +0400 Subject: [PATCH] fix(users): respond with 404 when user does not exist (#34) --- src/users/users.controller.ts | 14 +++++++++++--- test/e2e/users.e2e-spec.ts | 20 +++++++++++++++----- test/global-teardown.js | 4 +++- test/sec/users.e2e-spec.ts | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 0c81e82..b858ad9 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -8,7 +8,8 @@ import { Get, Param, Post, - Query + Query, + NotFoundException } from '@nestjs/common'; import { ApiResponse, ApiTags } from '@nestjs/swagger'; @@ -35,8 +36,15 @@ export class UsersController { @Get(':id') @ApiResponse({ status: 200, type: User }) - public findOne(@Param('id') id: number): Promise { - return this.usersService.findOne(id); + @ApiResponse({ status: 404, description: 'No such user.' }) + public async findOne(@Param('id') id: number): Promise { + const user = await this.usersService.findOne(id); + + if (!user) { + throw new NotFoundException('No such user.'); + } + + return user; } @Delete(':id') diff --git a/test/e2e/users.e2e-spec.ts b/test/e2e/users.e2e-spec.ts index 0fa82f2..71246cb 100644 --- a/test/e2e/users.e2e-spec.ts +++ b/test/e2e/users.e2e-spec.ts @@ -46,7 +46,9 @@ describe('/users', () => { it('should return a list of users', async () => { const res = await request(app.getHttpServer()).get('/users').expect(200); - expect(res).toMatchObject({ body: expect.arrayContaining([user]) }); + expect(res).toMatchObject({ + body: expect.arrayContaining([expect.objectContaining(user)]) + }); }); }); @@ -56,20 +58,28 @@ describe('/users', () => { .get('/users/2') .expect(200); - expect(res).toMatchObject({ body: user }); + expect(res).toMatchObject({ + body: user + }); }); it('should return an user if boolean-based blind is used', async () => { const res = await request(app.getHttpServer()) - .get('/users/2 AND 1858=1858') + .get('/users/1000000 OR 2028=2028') .expect(200); - expect(res).toMatchObject({ body: user }); + expect(res).toMatchObject({ + body: { + firstName: expect.any(String), + lastName: expect.any(String), + isActive: expect.any(Boolean) + } + }); }); }); describe('DEL /:id', () => { it('should remove an user by ID', () => - request(app.getHttpServer()).delete('/users/1').expect(200)); + request(app.getHttpServer()).delete('/users/2').expect(200)); }); }); diff --git a/test/global-teardown.js b/test/global-teardown.js index cc53867..efefa37 100644 --- a/test/global-teardown.js +++ b/test/global-teardown.js @@ -14,7 +14,9 @@ module.exports = async () => { try { orm = await MikroORM.init(config); - await orm.em.getConnection().execute('delete from "user";'); + await orm.em + .getConnection() + .execute('delete from "user" where "id" != 1;'); } finally { await orm?.close(); } diff --git a/test/sec/users.e2e-spec.ts b/test/sec/users.e2e-spec.ts index 9da490b..afd8e8f 100644 --- a/test/sec/users.e2e-spec.ts +++ b/test/sec/users.e2e-spec.ts @@ -9,7 +9,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; import { Server } from 'https'; describe('/users', () => { - const timeout = 300000; + const timeout = 600000; jest.setTimeout(timeout); let runner!: SecRunner;