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

30.81% Statements 49/159
100% Branches 2/2
100% Functions 0/0
30.81% Lines 49/159

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x 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 1x 1x 2x 2x 1x 1x 1x                                             1x 1x 1x 1x 1x 1x 1x                                                                               1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { http, HttpResponse } from 'msw';
 
import {
  GoogleOAuthExchangeRequest,
  GoogleOAuthExchangeResponse,
  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));
});
 
// 구글 OAuth 코드 교환
const exchangeGoogleCodeMock = http.post('*/api/v1/auth/google', async ({ request }) => {
  const body = (await request.json()) as GoogleOAuthExchangeRequest;

  if (!body.authorizationCode || !body.redirectUri) {
    return HttpResponse.json(
      createMockErrorResponse({
        status: 400,
        detail: 'authorizationCode 또는 redirectUri가 누락되었습니다.',
        errorCode: 'A005',
      }),
      { status: 400 },
    );
  }

  const response: GoogleOAuthExchangeResponse = {
    accessToken: 'mock-google-access-token',
    tokenType: 'Bearer',
    expiresIn: 3600,
    expiresAt: new Date(Date.now() + 3600 * 1000).toISOString(),
    user: {
      userId: 999,
      email: 'googleuser@test.com',
      nickName: '구글유저',
      mbti: null,
      profileImage: null,
      profileMessage: null,
      followeesCnt: 0,
      followersCnt: 0,
      groupJoinedCnt: 0,
      groupCreatedCnt: 0,
      isNotificationEnabled: true,
      isFollow: false,
      createdAt: new Date().toISOString(),
    },
  };

  return HttpResponse.json(createMockSuccessResponse<GoogleOAuthExchangeResponse>(response), {
    status: 200,
  });
});
 
export const authHandlers = [
  signupMock,
  loginMock,
  logoutMock,
  refreshMock,
  withdrawMock,
  exchangeGoogleCodeMock,
];