From e43a8fd11146220077eda55a3d46829a51853330 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 9 Apr 2023 15:13:14 +0900 Subject: [PATCH] Fix duplicated guests --- back/src/auth/auth.controller.ts | 8 ++------ back/src/users/users.service.ts | 3 ++- back/test/robot/auth/guest.robot | 32 +++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/back/src/auth/auth.controller.ts b/back/src/auth/auth.controller.ts index 5bc7335..35af1f1 100644 --- a/back/src/auth/auth.controller.ts +++ b/back/src/auth/auth.controller.ts @@ -57,12 +57,8 @@ export class AuthController { @HttpCode(200) @Post('guest') async guest(): Promise { - try { - const user = await this.usersService.createGuest(); - return this.authService.login(user); - } catch { - throw new BadRequestException(); - } + const user = await this.usersService.createGuest(); + return this.authService.login(user); } @UseGuards(JwtAuthGuard) diff --git a/back/src/users/users.service.ts b/back/src/users/users.service.ts index 0284a8f..0bcfab8 100644 --- a/back/src/users/users.service.ts +++ b/back/src/users/users.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { User, Prisma } from '@prisma/client'; import { PrismaService } from 'src/prisma/prisma.service'; import * as bcrypt from 'bcryptjs'; +import { randomUUID } from 'crypto'; @Injectable() export class UsersService { @@ -42,7 +43,7 @@ export class UsersService { async createGuest(): Promise { return this.prisma.user.create({ data: { - username: 'Guest', + username: `Guest ${randomUUID()}`, isGuest: true, // Not realyl clean but better than a separate table or breaking the api by adding nulls. email: '', diff --git a/back/test/robot/auth/guest.robot b/back/test/robot/auth/guest.robot index 0d87f88..f6fa40d 100644 --- a/back/test/robot/auth/guest.robot +++ b/back/test/robot/auth/guest.robot @@ -23,6 +23,36 @@ LoginAsGuest [Teardown] DELETE /auth/me +TwoGuests + [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}"} + + GET /auth/me + Output + Integer response status 200 + Boolean response body isGuest true + Integer response body partyPlayed 0 + + &{res2}= POST /auth/guest + Output + Integer response status 200 + String response body access_token + Set Headers {"Authorization": "Bearer ${res2.body.access_token}"} + + GET /auth/me + Output + Integer response status 200 + Boolean response body isGuest true + Integer response body partyPlayed 0 + + [Teardown] Run Keywords DELETE /auth/me + ... AND Set Headers {"Authorization": "Bearer ${res.body.access_token}"} + ... AND DELETE /auth/me + GuestToNormal [Documentation] Login as a guest and convert to a normal account &{res}= POST /auth/guest @@ -36,7 +66,7 @@ GuestToNormal Integer response status 200 Boolean response body isGuest true - ${res}= PUT /auth/me { "username": "toto", "password": "toto", "email": "a@b.c"} + ${res}= PUT /auth/me { "username": "toto", "password": "toto", "email": "a@b.c"} Output Integer response status 200 String response body username "toto"