Skip to content

Commit 1052e20

Browse files
committed
modify: 테스트 코드 피드백 반영, 리펙토링 (가독성, 가시성)
1 parent e6a1881 commit 1052e20

File tree

2 files changed

+13
-38
lines changed

2 files changed

+13
-38
lines changed

src/middlewares/__test__/auth.middleware.test.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('인증 미들웨어', () => {
147147
);
148148
});
149149

150-
it('사용자를 찾을 수 없으면 next를 호출해야 한다', async () => {
150+
it('사용자를 찾을 수 없으면 DBError가 발생해야 한다', async () => {
151151
// 유효한 토큰 준비
152152
mockRequest.cookies = {
153153
'access_token': validToken,
@@ -169,9 +169,15 @@ describe('인증 미들웨어', () => {
169169
// 검증
170170
expect(nextFunction).toHaveBeenCalledTimes(1);
171171
expect(mockRequest.user).toBeUndefined();
172+
expect(nextFunction).toHaveBeenCalledWith(
173+
expect.objectContaining({
174+
name: 'DBError',
175+
message: '사용자를 찾을 수 없습니다.'
176+
})
177+
);
172178
});
173179

174-
it('쿠키 대신 요청 본문에서 토큰을 추출해야 한다', async () => {
180+
it('쿠키에 토큰이 없으면 헤더에서 토큰을 가져와야 한다', async () => {
175181
// 요청 본문에 토큰 설정
176182
mockRequest.body = {
177183
accessToken: validToken,
@@ -203,38 +209,5 @@ describe('인증 미들웨어', () => {
203209
expect(nextFunction).not.toHaveBeenCalledWith(expect.any(Error));
204210
expect(mockRequest.user).toEqual(mockUser);
205211
});
206-
207-
it('쿠키와 요청 본문 대신 헤더에서 토큰을 추출해야 한다', async () => {
208-
// 헤더에 토큰 설정
209-
mockRequest.headers = {
210-
'access_token': validToken,
211-
'refresh_token': 'refresh-token'
212-
};
213-
214-
// 사용자 정보 mock
215-
const mockUser = {
216-
id: 1,
217-
username: 'testuser',
218-
email: 'test@example.com',
219-
velog_uuid: 'c7507240-093b-11ea-9aae-a58a86bb0520'
220-
};
221-
222-
// DB 쿼리 결과 모킹
223-
(pool.query as jest.Mock).mockResolvedValueOnce({
224-
rows: [mockUser]
225-
});
226-
227-
// 미들웨어 실행
228-
await authMiddleware.verify(
229-
mockRequest as Request,
230-
mockResponse as Response,
231-
nextFunction
232-
);
233-
234-
// 검증
235-
expect(nextFunction).toHaveBeenCalledTimes(1);
236-
expect(nextFunction).not.toHaveBeenCalledWith(expect.any(Error));
237-
expect(mockRequest.user).toEqual(mockUser);
238-
});
239212
});
240213
});

src/repositories/__test__/qr.repo.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('UserRepository - QR Login Token', () => {
7272
});
7373

7474
it('토큰이 존재하지 않으면 null을 반환해야 한다', async () => {
75+
// 참고로 존재하지 않거나, 만료된 토큰 밖에 없거나, 이미 사용된 토큰은 모두 "null 이 되는 것임"
7576
(mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
7677

7778
const result = await repo.findQRLoginToken('token');
@@ -102,14 +103,15 @@ describe('UserRepository - QR Login Token', () => {
102103

103104
describe('updateQRLoginTokenToUse', () => {
104105
it('유저 ID로 토큰을 사용 처리해야 한다', async () => {
106+
const targetUserId = 1;
105107
(mockPool.query as jest.Mock).mockResolvedValueOnce(undefined);
106108

107-
await expect(repo.updateQRLoginTokenToUse(1)).resolves.not.toThrow();
109+
await expect(repo.updateQRLoginTokenToUse(targetUserId)).resolves.not.toThrow();
108110

109-
expect(mockPool.query).toHaveBeenCalledTimes(1);
111+
expect(mockPool.query).toHaveBeenCalledTimes(targetUserId);
110112
expect(mockPool.query).toHaveBeenCalledWith(
111113
expect.stringContaining('UPDATE users_qrlogintoken'),
112-
[1]
114+
[targetUserId]
113115
);
114116
});
115117

0 commit comments

Comments
 (0)