All files / src/mock/service/auth auth-handlers.ts

45.37% Statements 49/108
75% Branches 3/4
100% Functions 0/0
45.37% Lines 49/108

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 1091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                         1x 1x 1x                                           1x 1x 1x 2x 2x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                     1x 1x 1x 1x 1x 1x  
import { http, HttpResponse } from 'msw';
 
import {
  LoginRequest,
  LoginResponse,
  RefreshResponse,
  SignupRequest,
  SignupResponse,
} from '@/types/service/auth';
 
import { createMockErrorResponse, createMockSuccessResponse } from '../common/common-mock';
import {
  createLoginResponse,
  createSignupResponse,
  isEmailTaken,
  isNicknameTaken,
} from './auth-utils';
 
//  회원가입
const signupMock = http.post('*/api/v1/auth/signup', async ({ request }) => {
  const body = (await request.json()) as SignupRequest;

  if (isEmailTaken(body.email)) {
    return HttpResponse.json(
      createMockErrorResponse({
        status: 400,
        detail: '이미 존재하는 이메일입니다.',
        errorCode: 'A002',
      }),
      { status: 400 },
    );
  }

  if (isNicknameTaken(body.nickName)) {
    return HttpResponse.json(
      createMockErrorResponse({
        status: 400,
        detail: '이미 존재하는 닉네임입니다.',
        errorCode: 'A003',
      }),
      { status: 400 },
    );
  }

  const response = createSignupResponse(body.email, body.nickName, body.password);

  return HttpResponse.json(createMockSuccessResponse<SignupResponse>(response));
});
 
// 로그인
const loginMock = http.post('*/api/v1/auth/login', async ({ request }) => {
  const body = (await request.json()) as LoginRequest;

  try {
    const response = createLoginResponse(body.email, body.password);

    return HttpResponse.json(createMockSuccessResponse<LoginResponse>(response), {
      headers: {
        'Set-Cookie': 'refreshToken=mock-refresh-token; Path=/; HttpOnly; SameSite=Strict; Secure',
      },
    });
  } catch {
    return HttpResponse.json(
      createMockErrorResponse({
        status: 400,
        detail: '이메일 또는 비밀번호가 올바르지 않습니다.',
        errorCode: 'A001',
      }),
      { status: 400 },
    );
  }
});
 
// 로그아웃
const logoutMock = http.post('*/api/v1/auth/logout', async () => {
  return HttpResponse.json(createMockSuccessResponse<void>(undefined));
});
 
// 액세스 토큰 재발급
const refreshMock = http.post('*/api/v1/auth/refresh', async ({ cookies }) => {
  const refreshToken = cookies['refreshToken'];
 
  if (!refreshToken) {
    return HttpResponse.json(
      createMockErrorResponse({
        status: 401,
        detail: '리프레시 토큰이 없습니다.',
        errorCode: 'A004',
      }),
      { status: 401 },
    );
  }

  const response: RefreshResponse = {
    accessToken: 'refreshed-mock-access-token',
    tokenType: 'Bearer',
    expiresIn: 3600,
    expiresAt: new Date(Date.now() + 3600 * 1000).toISOString(),
  };

  return HttpResponse.json(createMockSuccessResponse<RefreshResponse>(response));
});
 
const withdrawMock = http.delete('*/api/v1/auth/withdraw', async () => {
  return HttpResponse.json(createMockSuccessResponse<void>(undefined));
});
 
export const authHandlers = [signupMock, loginMock, logoutMock, refreshMock, withdrawMock];