From f788872f9b9ffd7358c99f9b1636e36f3cb30a15 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 23 Mar 2023 17:45:45 +0900 Subject: [PATCH] Fix tests and cleanup api responses --- back/src/auth/auth.controller.ts | 8 +++--- back/src/models/user.ts | 6 +++-- back/test/robot/auth/auth.robot | 41 ----------------------------- back/test/robot/auth/guest.robot | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 46 deletions(-) create mode 100644 back/test/robot/auth/guest.robot diff --git a/back/src/auth/auth.controller.ts b/back/src/auth/auth.controller.ts index 6e868f2..5bc7335 100644 --- a/back/src/auth/auth.controller.ts +++ b/back/src/auth/auth.controller.ts @@ -9,6 +9,7 @@ import { BadRequestException, HttpCode, Put, + InternalServerErrorException, } from '@nestjs/common'; import { AuthService } from './auth.service'; import { JwtAuthGuard } from './jwt-auth.guard'; @@ -54,7 +55,6 @@ export class AuthController { } @HttpCode(200) - @UseGuards(LocalAuthGuard) @Post('guest') async guest(): Promise { try { @@ -70,8 +70,10 @@ export class AuthController { @ApiOkResponse({ description: 'Successfully logged in', type: User }) @ApiUnauthorizedResponse({ description: 'Invalid token' }) @Get('me') - getProfile(@Request() req: any): User { - return req.user; + async getProfile(@Request() req: any): Promise { + const user = await this.usersService.user({ id: req.user.id }); + if (!user) throw new InternalServerErrorException(); + return user; } @UseGuards(JwtAuthGuard) diff --git a/back/src/models/user.ts b/back/src/models/user.ts index d650ae0..e684bb8 100644 --- a/back/src/models/user.ts +++ b/back/src/models/user.ts @@ -6,7 +6,9 @@ export class User { @ApiProperty() username: string; @ApiProperty() - password: string; - @ApiProperty() email: string; + @ApiProperty() + isGuest: boolean; + @ApiProperty() + partyPlayed: number; } diff --git a/back/test/robot/auth/auth.robot b/back/test/robot/auth/auth.robot index ba19b5b..08a7273 100644 --- a/back/test/robot/auth/auth.robot +++ b/back/test/robot/auth/auth.robot @@ -59,44 +59,3 @@ Login Should Be Equal As Strings ${res["body"]} ${me["body"]} [Teardown] DELETE /auth/me - -LoginAsGuest - [Documentation] Login as a guest - &{res}= POST /auth/guest - Output - Integer response status 200 - String response body access_token - Set Headers {"Authorization": "Bearer ${res.body.access_token}"} - - ${res}= GET /auth/me - Output - Integer response status 200 - Boolean response body isGuest true - - [Teardown] DELETE /auth/me - -GuestToNormal - [Documentation] Login as a guest and convert to a normal account - &{res}= POST /auth/guest - Output - Integer response status 200 - String response body access_token - Set Headers {"Authorization": "Bearer ${res.body.access_token}"} - - ${res}= GET /auth/me - Output - Integer response status 200 - Boolean response body isGuest true - - ${res}= PUT /auth/me { "username": "toto", "passord": "toto", "email": "a@b.c"} - Output - Integer response status 200 - Boolean response body isGuest true - - ${res}= GET /auth/me - Output - Integer response status 200 - String response body username "toto" - Boolean response body isGuest false - - [Teardown] DELETE /auth/me diff --git a/back/test/robot/auth/guest.robot b/back/test/robot/auth/guest.robot new file mode 100644 index 0000000..0d87f88 --- /dev/null +++ b/back/test/robot/auth/guest.robot @@ -0,0 +1,45 @@ +*** Settings *** +Documentation Tests of the /auth route. +... Ensures that the user can authenticate on kyoo. + +Resource ../rest.resource +Resource ./auth.resource + + +*** Test Cases *** +LoginAsGuest + [Documentation] Login as a guest + &{res}= POST /auth/guest + Output + Integer response status 200 + String response body access_token + Set Headers {"Authorization": "Bearer ${res.body.access_token}"} + + ${res}= GET /auth/me + Output + Integer response status 200 + Boolean response body isGuest true + Integer response body partyPlayed 0 + + [Teardown] DELETE /auth/me + +GuestToNormal + [Documentation] Login as a guest and convert to a normal account + &{res}= POST /auth/guest + Output + Integer response status 200 + String response body access_token + Set Headers {"Authorization": "Bearer ${res.body.access_token}"} + + ${res}= GET /auth/me + Output + Integer response status 200 + Boolean response body isGuest true + + ${res}= PUT /auth/me { "username": "toto", "password": "toto", "email": "a@b.c"} + Output + Integer response status 200 + String response body username "toto" + Boolean response body isGuest false + + [Teardown] DELETE /auth/me