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

28.33% Statements 17/60
100% Branches 0/0
0% Functions 0/8
28.33% Lines 17/60

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 611x 1x 1x 1x 1x 1x   1x 1x   1x 1x         1x 1x                                 1x 1x                                           1x 1x 1x  
import type { LoginResponse, SignupResponse } from '@/types/service/auth';
 
import { AuthMockUser, authMockUsers } from './auth-mock';
 
const findUserByEmail = (email: string) => authMockUsers.find((user) => user.email === email);
const findUserByNickname = (nickName: string) =>
  authMockUsers.find((user) => user.nickName === nickName);
 
const findUserByCredentials = (email: string, password: string) =>
  authMockUsers.find((user) => user.email === email && user.password === password);
 
const createMockTokens = () => ({
  accessToken: 'mock-access-token',
  tokenType: 'Bearer' as const,
  expiresIn: 3600,
});
 
export const createLoginResponse = (email: string, password: string): LoginResponse => {
  const user = findUserByCredentials(email, password);
  if (!user) {
    throw new Error('INVALID_CREDENTIALS');
  }

  const tokens = createMockTokens();

  return {
    ...tokens,
    user: {
      userId: user.id,
      email: user.email,
      nickName: user.nickName,
    },
  };
};
 
export const createSignupResponse = (
  email: string,
  nickName: string,
  password: string,
): SignupResponse => {
  const newUser: AuthMockUser = {
    id: authMockUsers.length + 1,
    email,
    nickName,
    password,
    createdAt: new Date().toISOString(),
  };

  authMockUsers.push(newUser);

  return {
    id: newUser.id,
    email: newUser.email,
    nickName: newUser.nickName,
    createdAt: newUser.createdAt,
  };
};
 
export const isEmailTaken = (email: string) => !!findUserByEmail(email);
export const isNicknameTaken = (nickName: string) => !!findUserByNickname(nickName);