From 259ed1036c784fb18ee6692848f3edca311a98cb Mon Sep 17 00:00:00 2001 From: SeonghunYang Date: Thu, 25 Apr 2024 12:41:42 +0000 Subject: [PATCH] feat: Add deleteUser function to mockDatabase and update user-handler.mock.ts --- app/mocks/db.mock.ts | 9 +++++++++ app/mocks/handlers/user-handler.mock.ts | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/mocks/db.mock.ts b/app/mocks/db.mock.ts index a611b6de..7aafa81e 100644 --- a/app/mocks/db.mock.ts +++ b/app/mocks/db.mock.ts @@ -28,6 +28,7 @@ type MockDatabaseAction = { createUser: (user: SignUpRequestBody) => boolean; signIn: (userData: SignInRequestBody) => boolean; getUserInfo: (authId: string) => UserInfoResponse; + deleteUser: (authId: string, password: string) => boolean; }; export const mockDatabase: MockDatabaseAction = { @@ -93,6 +94,14 @@ export const mockDatabase: MockDatabaseAction = { isSumbitted: user.isSumbitted, }; }, + deleteUser: (authId: string, password: string) => { + const user = mockDatabaseStore.users.find((u) => u.authId === authId && u.password === password); + if (user) { + mockDatabaseStore.users = mockDatabaseStore.users.filter((u) => u.authId !== authId); + return true; + } + return false; + }, }; const initialState: MockDatabaseState = { diff --git a/app/mocks/handlers/user-handler.mock.ts b/app/mocks/handlers/user-handler.mock.ts index 7aa98163..d9a52164 100644 --- a/app/mocks/handlers/user-handler.mock.ts +++ b/app/mocks/handlers/user-handler.mock.ts @@ -7,6 +7,7 @@ import { SignInResponse, ValidateTokenResponse, UserInfoResponse, + UserDeleteRequestBody, } from '@/app/business/user/user.type'; import { ErrorResponseData } from '@/app/utils/http/http-error-handler'; import { StrictRequest } from 'msw'; @@ -22,7 +23,7 @@ function mockDecryptToken(token: string) { }; } -export const devModeAuthGuard = (request: StrictRequest) => { +export const devModeAuthGuard = (request: StrictRequest) => { if (process.env.NODE_ENV === 'development') { const accessToken = request.headers.get('Authorization')?.replace('Bearer ', ''); if (accessToken === 'undefined' || !accessToken) { @@ -47,6 +48,22 @@ export const userHandlers = [ accessToken: 'fake-access-token', }); }), + http.delete(`${API_PATH.user}/delete-me`, async ({ request }) => { + try { + const { authId } = devModeAuthGuard(request); + const { password } = await request.json(); + + const result = mockDatabase.deleteUser(authId, password); + + if (result) { + return HttpResponse.json({ status: 200 }); + } else { + return HttpResponse.json({ status: 400, message: '비밀번호가 일치하지 않습니다' }, { status: 400 }); + } + } catch { + return HttpResponse.json({ status: 401, message: 'Unauthorized' }, { status: 401 }); + } + }), http.get(`${API_PATH.user}`, async ({ request }) => { try { const { authId } = devModeAuthGuard(request);