Skip to content

Commit

Permalink
feat: Add deleteUser function to mockDatabase and update user-handler…
Browse files Browse the repository at this point in the history
….mock.ts
  • Loading branch information
seonghunYang committed Apr 25, 2024
1 parent 8bbc2f5 commit 259ed10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/mocks/db.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
19 changes: 18 additions & 1 deletion app/mocks/handlers/user-handler.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +23,7 @@ function mockDecryptToken(token: string) {
};
}

export const devModeAuthGuard = (request: StrictRequest<never>) => {
export const devModeAuthGuard = (request: StrictRequest<any>) => {
if (process.env.NODE_ENV === 'development') {
const accessToken = request.headers.get('Authorization')?.replace('Bearer ', '');
if (accessToken === 'undefined' || !accessToken) {
Expand All @@ -47,6 +48,22 @@ export const userHandlers = [
accessToken: 'fake-access-token',
});
}),
http.delete<never, UserDeleteRequestBody, never>(`${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<never, never, UserInfoResponse | ErrorResponseData>(`${API_PATH.user}`, async ({ request }) => {
try {
const { authId } = devModeAuthGuard(request);
Expand Down

0 comments on commit 259ed10

Please sign in to comment.