Skip to content

Commit

Permalink
Merge pull request #176 from jihwooon/issue-102
Browse files Browse the repository at this point in the history
장바구니에 auth 검증을 추가하라
  • Loading branch information
jihwooon authored Mar 16, 2024
2 parents dafae9f + 7d84846 commit cf616e6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
16 changes: 11 additions & 5 deletions server/src/cartItems/application/cartItem-list.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { existingCartItem, existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture';
import { existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture';

import { when } from 'jest-when';

import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { existingLike } from 'src/fixture/likes.fixture';

import { validateToken } from 'src/users/jwt/jwt.provider';

import { ACCESS_TOKEN } from 'src/fixture/jwt.fixture';

import { findCartItemWithBook } from '../domain/cartItem.repository';
import { getCartItems } from './cartItem-list.service';

jest.mock('../domain/cartItem.repository.ts');
jest.mock('../../users/jwt/jwt.provider.ts');

describe('cartItem Service', () => {
const SelectedCartItem = [1, 4];
const notSelectedCartItem = [9999, 9999];

beforeEach(() => {
when(findCartItemWithBook as jest.Mock)
.calledWith(existingCartItem.userId, SelectedCartItem)
.mockResolvedValue(existingCartItems);
when(validateToken as jest.Mock).mockResolvedValue({ userId: existingLike.userId });
when(findCartItemWithBook as jest.Mock).mockResolvedValue(existingCartItems);
});

context('사용자 정보 id와 장바구니 상품을 선택하면', () => {
it('장바구니 도서 목록을 반환한다.', async () => {
const cartItems = await getCartItems(existingCartItem.userId, SelectedCartItem);
const cartItems = await getCartItems(ACCESS_TOKEN, SelectedCartItem);

expect(cartItems).toStrictEqual([
{
Expand Down
5 changes: 4 additions & 1 deletion server/src/cartItems/application/cartItem-list.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { validateToken } from 'src/users/jwt/jwt.provider';

import type CartItem from '../domain/cartItem';
import { findCartItemWithBook } from '../domain/cartItem.repository';

export const getCartItems = async (userId: number, selectedId: number[]) => {
export const getCartItems = async (accessToken: any, selectedId: number[]) => {
const { userId } = validateToken(accessToken);
const cartItems = await findCartItemWithBook(userId, selectedId);
if (cartItems.length === 0) {
throw new HttpException('장바구니가 내 도서 정보가 존재하지 않습니다.', StatusCodes.NOT_FOUND);
Expand Down
4 changes: 2 additions & 2 deletions server/src/cartItems/application/cartItem-remove.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import HttpException from 'src/utils/httpException';

import { deleteById } from '../domain/cartItem.repository';

export const removeToCart = async (id: number) => {
const removedCart = await deleteById(id);
export const removeToCart = async (cartItemId: number) => {
const removedCart = await deleteById(cartItemId);
if (!removedCart) {
throw new HttpException('장바구니 제거에 실패했습니다.', StatusCodes.BAD_REQUEST);
}
Expand Down
14 changes: 10 additions & 4 deletions server/src/cartItems/application/cartItem-save.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { validateToken } from 'src/users/jwt/jwt.provider';

import { ACCESS_TOKEN } from 'src/fixture/jwt.fixture';

import { save } from '../domain/cartItem.repository';
import { addToCart } from './cartItem-save.service';

jest.mock('../domain/cartItem.repository.ts');
jest.mock('../../users/jwt/jwt.provider.ts');

describe('cartItem Service', () => {
beforeEach(() => {
(validateToken as jest.Mock).mockResolvedValue({ userId: existingCartItem.userId });
(save as jest.Mock).mockResolvedValue(true);
});

context('사용자 id와 도서 정보 id가 주어지고 수량을 추가하면', () => {
it('true를 반환한다.', async () => {
const savedCartItems = await addToCart(existingCartItem.userId, existingCartItem.bookId, existingCartItem.count);
const savedCartItems = await addToCart(existingCartItem.bookId, existingCartItem.count, ACCESS_TOKEN);

expect(savedCartItems).toBe(true);
});
Expand All @@ -28,9 +34,9 @@ describe('cartItem Service', () => {
});

it('error를 반환한다.', async () => {
await expect(
addToCart(nonExistingCartItem.userId, nonExistingCartItem.bookId, nonExistingCartItem.count),
).rejects.toThrow(new HttpException('장바구니 추가에 실패했습니다.', StatusCodes.BAD_REQUEST));
await expect(addToCart(nonExistingCartItem.bookId, nonExistingCartItem.count, ACCESS_TOKEN)).rejects.toThrow(
new HttpException('장바구니 추가에 실패했습니다.', StatusCodes.BAD_REQUEST),
);
});
});
});
5 changes: 4 additions & 1 deletion server/src/cartItems/application/cartItem-save.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { StatusCodes } from 'http-status-codes';
import HttpException from 'src/utils/httpException';

import { validateToken } from 'src/users/jwt/jwt.provider';

import CartItem from '../domain/cartItem';
import { save } from '../domain/cartItem.repository';

export const addToCart = async (userId: number, bookId: number, count: number): Promise<boolean> => {
export const addToCart = async (bookId: number, count: number, accessToken: any): Promise<boolean> => {
const { userId } = validateToken(accessToken);
const cartItems = CartItem.createCartItems(userId, bookId, count);

const savedCartItems = await save(cartItems);
Expand Down
5 changes: 3 additions & 2 deletions server/src/cartItems/web/cartItem-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ResponseHandler } from 'src/utils/responseHandler';

import { getCartItems } from '../application/cartItem-list.service';

const getCartHandler = async ({ body: { userId, selectedId } }: Request, res: Response) => {
ResponseHandler(() => getCartItems(Number(userId), selectedId), StatusCodes.OK, res);
const getCartHandler = async ({ body: { selectedId }, headers }: Request, res: Response) => {
const accessToken = headers.authorization;
ResponseHandler(() => getCartItems(accessToken, selectedId), StatusCodes.OK, res);
};

export default getCartHandler;
5 changes: 3 additions & 2 deletions server/src/cartItems/web/cartItem-save.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ResponseHandler } from 'src/utils/responseHandler';

import { addToCart } from '../application/cartItem-save.service';

const addCartHandler = async ({ body: { userId, bookId, count } }: Request, res: Response) => {
ResponseHandler(() => addToCart(Number(userId), Number(bookId), Number(count)), StatusCodes.CREATED, res);
const addCartHandler = async ({ body: { bookId, count }, headers }: Request, res: Response) => {
const accessToken = headers.authorization;
ResponseHandler(() => addToCart(Number(bookId), Number(count), accessToken), StatusCodes.CREATED, res);
};

export default addCartHandler;

0 comments on commit cf616e6

Please sign in to comment.