Skip to content

Commit 3b6adaa

Browse files
committed
hotfix: 코드래빗 리뷰 반영
1 parent 6d3b0ec commit 3b6adaa

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import dotenv from 'dotenv';
2+
import { Pool } from 'pg';
3+
import pg from 'pg';
4+
import { QRLoginTokenRepository } from '@/repositories/qr.repository';
5+
import { v4 as uuidv4 } from 'uuid';
6+
7+
dotenv.config();
8+
jest.setTimeout(30000);
9+
10+
describe('QRLoginTokenRepository Integration Test', () => {
11+
let pool: Pool;
12+
let repo: QRLoginTokenRepository;
13+
14+
beforeAll(async () => {
15+
try {
16+
const testPoolConfig: pg.PoolConfig = {
17+
user: process.env.POSTGRES_USER,
18+
host: process.env.POSTGRES_HOST,
19+
database: process.env.DATABASE_NAME,
20+
password: process.env.POSTGRES_PASSWORD,
21+
port: Number(process.env.POSTGRES_PORT),
22+
max: 1,
23+
idleTimeoutMillis: 30000,
24+
connectionTimeoutMillis: 5000,
25+
statement_timeout: 30000,
26+
};
27+
28+
if (process.env.POSTGRES_HOST !== 'localhost') {
29+
testPoolConfig.ssl = {
30+
rejectUnauthorized: false,
31+
};
32+
}
33+
34+
pool = new Pool(testPoolConfig);
35+
await pool.query('SELECT 1'); // 연결 확인
36+
console.info('QR 테스트 DB 연결 성공');
37+
38+
// 필요한 테이블 및 유저 존재 확인 (선택 사항)
39+
const tableCheck = await pool.query(`SELECT to_regclass('qr_login_tokens')`);
40+
if (!tableCheck.rows[0].to_regclass) {
41+
throw new Error('qr_login_tokens 테이블이 존재하지 않습니다.');
42+
}
43+
44+
const userCheck = await pool.query(`SELECT COUNT(*) FROM users WHERE id = $1`, [1]);
45+
if (parseInt(userCheck.rows[0].count) === 0) {
46+
throw new Error('user_id = 1 유저가 존재하지 않습니다.');
47+
}
48+
49+
repo = new QRLoginTokenRepository(pool);
50+
} catch (error) {
51+
console.error('QR 테스트 설정 중 오류:', error);
52+
throw error;
53+
}
54+
});
55+
56+
afterAll(async () => {
57+
await pool.end();
58+
});
59+
60+
it('should insert and retrieve QR token', async () => {
61+
const token = uuidv4();
62+
const userId = 1;
63+
const ip = '127.0.0.1';
64+
const userAgent = 'test-agent';
65+
66+
await repo.createQRLoginToken(token, userId, ip, userAgent);
67+
const result = await repo.findQRLoginToken(token);
68+
69+
expect(result).not.toBeNull();
70+
expect(result?.token).toBe(token);
71+
});
72+
73+
it('should return null for expired or used token', async () => {
74+
const result = await repo.findQRLoginToken('invalid-token');
75+
expect(result).toBeNull();
76+
});
77+
});

src/repositories/qr.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { QRLoginToken } from '@/types/models/QRLoginToken.type';
66
export class QRLoginTokenRepository {
77
constructor(private pool: Pool) { }
88

9-
async createQRLoginToken(token: String, userId: number, ip: string, userAgent: string): Promise<void> {
9+
async createQRLoginToken(token: string, userId: number, ip: string, userAgent: string): Promise<void> {
1010
try {
1111
const query = `
1212
INSERT INTO qr_login_tokens (token, user_id, created_at, expires_at, is_used, ip_address, user_agent)

src/routes/qr.router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const qrController = new QRLoginController(qrService);
2121
* security:
2222
* - bearerAuth: []
2323
* responses:
24-
* 201:
24+
* 200:
2525
* description: QR 로그인 토큰 생성 성공
2626
*/
2727
router.post('/qr-login', authMiddleware.login, qrController.createToken);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// // src/types/dto/requests/qrLoginRequest.dto.ts
2+
// import { IsString, IsNotEmpty } from 'class-validator';
3+
4+
// /**
5+
// * @swagger
6+
// * components:
7+
// * schemas:
8+
// * QRLoginRequestDto:
9+
// * type: object
10+
// * required:
11+
// * - userId
12+
// * properties:
13+
// * userId:
14+
// * type: integer
15+
// * description: 로그인 요청한 사용자 ID
16+
// */
17+
// export class QRLoginRequestDto {
18+
// @IsNotEmpty()
19+
// userId: number;
20+
// }

0 commit comments

Comments
 (0)