feat: Complete Phase 1.5 - AuthService Raw Query migration
Phase 1.5 완료: 인증 서비스 Raw Query 전환 및 테스트 완료 ✅ AuthService 전환 완료 (5개 Prisma 호출 제거): - loginPwdCheck(): Raw Query로 사용자 비밀번호 조회 - insertLoginAccessLog(): Raw Query로 로그인 로그 기록 - getUserInfo(): Raw Query로 사용자/권한/회사 정보 조회 - authority_sub_user ↔ authority_master JOIN (master_objid ↔ objid) - 3개 쿼리로 분리 (사용자, 권한, 회사) - processLogin(): 전체 로그인 플로우 통합 - processLogout(): 로그아웃 로그 기록 🧪 테스트 완료: - 단위 테스트: 30개 테스트 모두 통과 ✅ - 로그인 검증 (6개) - 사용자 정보 조회 (5개) - 로그인 로그 기록 (4개) - 전체 로그인 프로세스 (5개) - 로그아웃 (2개) - 토큰 검증 (3개) - Raw Query 전환 검증 (3개) - 성능 테스트 (2개) - 통합 테스트: 작성 완료 (auth.integration.test.ts) - 로그인 → 토큰 발급 → 인증 → 로그아웃 플로우 🔧 주요 변경사항: - Prisma import 제거 → Raw Query (query from db.ts) - authority 테이블 JOIN 수정 (auth_code → master_objid/objid) - 파라미터 바인딩으로 SQL Injection 방지 - 타입 안전성 유지 (TypeScript Generic 사용) 📊 성능: - 로그인 프로세스: < 1초 - 사용자 정보 조회: < 500ms - 모든 테스트 실행 시간: 2.016초 🎯 다음 단계: - Phase 2: 핵심 서비스 전환 (ScreenManagement, TableManagement 등) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
426
backend-node/src/tests/authService.test.ts
Normal file
426
backend-node/src/tests/authService.test.ts
Normal file
@@ -0,0 +1,426 @@
|
||||
/**
|
||||
* AuthService Raw Query 전환 단위 테스트
|
||||
* Phase 1.5: 인증 서비스 테스트
|
||||
*/
|
||||
|
||||
import { AuthService } from "../services/authService";
|
||||
import { query } from "../database/db";
|
||||
import { EncryptUtil } from "../utils/encryptUtil";
|
||||
|
||||
// 테스트 데이터
|
||||
const TEST_USER = {
|
||||
userId: "testuser",
|
||||
password: "testpass123",
|
||||
hashedPassword: "", // 테스트 전에 생성
|
||||
};
|
||||
|
||||
describe("AuthService Raw Query 전환 테스트", () => {
|
||||
// 테스트 전 준비
|
||||
beforeAll(async () => {
|
||||
// 테스트용 비밀번호 해시 생성
|
||||
TEST_USER.hashedPassword = EncryptUtil.encrypt(TEST_USER.password);
|
||||
|
||||
// 테스트 사용자 생성 (이미 있으면 스킵)
|
||||
try {
|
||||
const existing = await query(
|
||||
"SELECT user_id FROM user_info WHERE user_id = $1",
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
if (existing.length === 0) {
|
||||
await query(
|
||||
`INSERT INTO user_info (
|
||||
user_id, user_name, user_password, company_code, locale
|
||||
) VALUES ($1, $2, $3, $4, $5)`,
|
||||
[
|
||||
TEST_USER.userId,
|
||||
"테스트 사용자",
|
||||
TEST_USER.hashedPassword,
|
||||
"ILSHIN",
|
||||
"KR",
|
||||
]
|
||||
);
|
||||
} else {
|
||||
// 비밀번호 업데이트
|
||||
await query(
|
||||
"UPDATE user_info SET user_password = $1 WHERE user_id = $2",
|
||||
[TEST_USER.hashedPassword, TEST_USER.userId]
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("테스트 사용자 생성 실패:", error);
|
||||
}
|
||||
});
|
||||
|
||||
// 테스트 후 정리
|
||||
afterAll(async () => {
|
||||
// 테스트 사용자 삭제 (선택적)
|
||||
// await query("DELETE FROM user_info WHERE user_id = $1", [TEST_USER.userId]);
|
||||
});
|
||||
|
||||
describe("loginPwdCheck - 로그인 비밀번호 검증", () => {
|
||||
test("존재하는 사용자 로그인 성공", async () => {
|
||||
const result = await AuthService.loginPwdCheck(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password
|
||||
);
|
||||
|
||||
expect(result.loginResult).toBe(true);
|
||||
expect(result.errorReason).toBeUndefined();
|
||||
});
|
||||
|
||||
test("존재하지 않는 사용자 로그인 실패", async () => {
|
||||
const result = await AuthService.loginPwdCheck(
|
||||
"nonexistent_user_12345",
|
||||
"anypassword"
|
||||
);
|
||||
|
||||
expect(result.loginResult).toBe(false);
|
||||
expect(result.errorReason).toContain("존재하지 않습니다");
|
||||
});
|
||||
|
||||
test("잘못된 비밀번호 로그인 실패", async () => {
|
||||
const result = await AuthService.loginPwdCheck(
|
||||
TEST_USER.userId,
|
||||
"wrongpassword123"
|
||||
);
|
||||
|
||||
expect(result.loginResult).toBe(false);
|
||||
expect(result.errorReason).toContain("일치하지 않습니다");
|
||||
});
|
||||
|
||||
test("마스터 패스워드 로그인 성공", async () => {
|
||||
const result = await AuthService.loginPwdCheck(
|
||||
TEST_USER.userId,
|
||||
"qlalfqjsgh11"
|
||||
);
|
||||
|
||||
expect(result.loginResult).toBe(true);
|
||||
});
|
||||
|
||||
test("빈 사용자 ID 처리", async () => {
|
||||
const result = await AuthService.loginPwdCheck("", TEST_USER.password);
|
||||
|
||||
expect(result.loginResult).toBe(false);
|
||||
});
|
||||
|
||||
test("빈 비밀번호 처리", async () => {
|
||||
const result = await AuthService.loginPwdCheck(TEST_USER.userId, "");
|
||||
|
||||
expect(result.loginResult).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserInfo - 사용자 정보 조회", () => {
|
||||
test("사용자 정보 조회 성공", async () => {
|
||||
const userInfo = await AuthService.getUserInfo(TEST_USER.userId);
|
||||
|
||||
expect(userInfo).not.toBeNull();
|
||||
expect(userInfo?.userId).toBe(TEST_USER.userId);
|
||||
expect(userInfo?.userName).toBeDefined();
|
||||
expect(userInfo?.companyCode).toBeDefined();
|
||||
expect(userInfo?.locale).toBeDefined();
|
||||
});
|
||||
|
||||
test("사용자 정보 필드 타입 확인", async () => {
|
||||
const userInfo = await AuthService.getUserInfo(TEST_USER.userId);
|
||||
|
||||
expect(userInfo).not.toBeNull();
|
||||
expect(typeof userInfo?.userId).toBe("string");
|
||||
expect(typeof userInfo?.userName).toBe("string");
|
||||
expect(typeof userInfo?.companyCode).toBe("string");
|
||||
expect(typeof userInfo?.locale).toBe("string");
|
||||
});
|
||||
|
||||
test("권한 정보 조회 (있는 경우)", async () => {
|
||||
const userInfo = await AuthService.getUserInfo(TEST_USER.userId);
|
||||
|
||||
// 권한이 없으면 authName은 빈 문자열
|
||||
expect(userInfo).not.toBeNull();
|
||||
if (userInfo) {
|
||||
expect(typeof userInfo.authName === 'string' || userInfo.authName === undefined).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("존재하지 않는 사용자 조회 실패", async () => {
|
||||
const userInfo = await AuthService.getUserInfo("nonexistent_user_12345");
|
||||
|
||||
expect(userInfo).toBeNull();
|
||||
});
|
||||
|
||||
test("회사 정보 기본값 확인", async () => {
|
||||
const userInfo = await AuthService.getUserInfo(TEST_USER.userId);
|
||||
|
||||
// company_code가 없으면 기본값 "ILSHIN"
|
||||
expect(userInfo?.companyCode).toBeDefined();
|
||||
expect(typeof userInfo?.companyCode).toBe("string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("insertLoginAccessLog - 로그인 로그 기록", () => {
|
||||
test("로그인 성공 로그 기록", async () => {
|
||||
await expect(
|
||||
AuthService.insertLoginAccessLog({
|
||||
systemName: "PMS",
|
||||
userId: TEST_USER.userId,
|
||||
loginResult: true,
|
||||
remoteAddr: "127.0.0.1",
|
||||
})
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
test("로그인 실패 로그 기록", async () => {
|
||||
await expect(
|
||||
AuthService.insertLoginAccessLog({
|
||||
systemName: "PMS",
|
||||
userId: TEST_USER.userId,
|
||||
loginResult: false,
|
||||
errorMessage: "비밀번호 불일치",
|
||||
remoteAddr: "127.0.0.1",
|
||||
})
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
test("로그인 로그 기록 후 DB 확인", async () => {
|
||||
await AuthService.insertLoginAccessLog({
|
||||
systemName: "PMS",
|
||||
userId: TEST_USER.userId,
|
||||
loginResult: true,
|
||||
remoteAddr: "127.0.0.1",
|
||||
});
|
||||
|
||||
// 로그가 기록되었는지 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
expect(logs[0].user_id).toBe(TEST_USER.userId.toUpperCase());
|
||||
// login_result는 문자열 또는 불리언일 수 있음
|
||||
expect(logs[0].login_result).toBeTruthy();
|
||||
});
|
||||
|
||||
test("로그 기록 실패해도 예외 던지지 않음", async () => {
|
||||
// 잘못된 데이터로 로그 기록 시도 (에러 발생하지만 프로세스 중단 안됨)
|
||||
await expect(
|
||||
AuthService.insertLoginAccessLog({
|
||||
systemName: "PMS",
|
||||
userId: TEST_USER.userId,
|
||||
loginResult: true,
|
||||
remoteAddr: "127.0.0.1",
|
||||
})
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("processLogin - 전체 로그인 프로세스", () => {
|
||||
test("전체 로그인 프로세스 성공", async () => {
|
||||
const result = await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password,
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.token).toBeDefined();
|
||||
expect(result.userInfo).toBeDefined();
|
||||
expect(result.userInfo?.userId).toBe(TEST_USER.userId);
|
||||
expect(result.errorReason).toBeUndefined();
|
||||
});
|
||||
|
||||
test("로그인 실패 시 토큰 없음", async () => {
|
||||
const result = await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
"wrongpassword",
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.token).toBeUndefined();
|
||||
expect(result.userInfo).toBeUndefined();
|
||||
expect(result.errorReason).toBeDefined();
|
||||
});
|
||||
|
||||
test("존재하지 않는 사용자 로그인 실패", async () => {
|
||||
const result = await AuthService.processLogin(
|
||||
"nonexistent_user",
|
||||
"anypassword",
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorReason).toContain("존재하지 않습니다");
|
||||
});
|
||||
|
||||
test("JWT 토큰 형식 확인", async () => {
|
||||
const result = await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password,
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
if (result.success && result.token) {
|
||||
// JWT 토큰은 3개 파트로 구성 (header.payload.signature)
|
||||
const parts = result.token.split(".");
|
||||
expect(parts.length).toBe(3);
|
||||
}
|
||||
});
|
||||
|
||||
test("로그인 프로세스 로그 기록 확인", async () => {
|
||||
await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password,
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
// 로그인 로그가 기록되었는지 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("processLogout - 로그아웃 프로세스", () => {
|
||||
test("로그아웃 프로세스 성공", async () => {
|
||||
await expect(
|
||||
AuthService.processLogout(TEST_USER.userId, "127.0.0.1")
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
test("로그아웃 로그 기록 확인", async () => {
|
||||
await AuthService.processLogout(TEST_USER.userId, "127.0.0.1");
|
||||
|
||||
// 로그아웃 로그가 기록되었는지 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
AND ERROR_MESSAGE = '로그아웃'
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
expect(logs[0].error_message).toBe("로그아웃");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserInfoFromToken - 토큰으로 사용자 정보 조회", () => {
|
||||
test("유효한 토큰으로 사용자 정보 조회", async () => {
|
||||
// 먼저 로그인해서 토큰 획득
|
||||
const loginResult = await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password,
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
expect(loginResult.success).toBe(true);
|
||||
expect(loginResult.token).toBeDefined();
|
||||
|
||||
// 토큰으로 사용자 정보 조회
|
||||
const userInfo = await AuthService.getUserInfoFromToken(
|
||||
loginResult.token!
|
||||
);
|
||||
|
||||
expect(userInfo).not.toBeNull();
|
||||
expect(userInfo?.userId).toBe(TEST_USER.userId);
|
||||
});
|
||||
|
||||
test("잘못된 토큰으로 조회 실패", async () => {
|
||||
const userInfo = await AuthService.getUserInfoFromToken("invalid_token");
|
||||
|
||||
expect(userInfo).toBeNull();
|
||||
});
|
||||
|
||||
test("만료된 토큰으로 조회 실패", async () => {
|
||||
// 만료된 토큰 시뮬레이션 (실제로는 만료 시간이 필요하므로 단순히 잘못된 토큰 사용)
|
||||
const expiredToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.expired.token";
|
||||
const userInfo = await AuthService.getUserInfoFromToken(expiredToken);
|
||||
|
||||
expect(userInfo).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Raw Query 전환 검증", () => {
|
||||
test("Prisma import가 없는지 확인", async () => {
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const authServicePath = path.join(
|
||||
__dirname,
|
||||
"../services/authService.ts"
|
||||
);
|
||||
const content = fs.readFileSync(authServicePath, "utf8");
|
||||
|
||||
// Prisma import가 없어야 함
|
||||
expect(content).not.toContain('import prisma from "../config/database"');
|
||||
expect(content).not.toContain("import { PrismaClient }");
|
||||
expect(content).not.toContain("prisma.user_info");
|
||||
expect(content).not.toContain("prisma.$executeRaw");
|
||||
});
|
||||
|
||||
test("Raw Query import 확인", async () => {
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const authServicePath = path.join(
|
||||
__dirname,
|
||||
"../services/authService.ts"
|
||||
);
|
||||
const content = fs.readFileSync(authServicePath, "utf8");
|
||||
|
||||
// Raw Query import가 있어야 함
|
||||
expect(content).toContain('import { query } from "../database/db"');
|
||||
});
|
||||
|
||||
test("모든 메서드가 Raw Query 사용 확인", async () => {
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const authServicePath = path.join(
|
||||
__dirname,
|
||||
"../services/authService.ts"
|
||||
);
|
||||
const content = fs.readFileSync(authServicePath, "utf8");
|
||||
|
||||
// query() 함수 호출이 있어야 함
|
||||
expect(content).toContain("await query<");
|
||||
expect(content).toContain("await query(");
|
||||
});
|
||||
});
|
||||
|
||||
describe("성능 테스트", () => {
|
||||
test("로그인 프로세스 성능 (응답 시간 < 1초)", async () => {
|
||||
const startTime = Date.now();
|
||||
|
||||
await AuthService.processLogin(
|
||||
TEST_USER.userId,
|
||||
TEST_USER.password,
|
||||
"127.0.0.1"
|
||||
);
|
||||
|
||||
const endTime = Date.now();
|
||||
const elapsedTime = endTime - startTime;
|
||||
|
||||
expect(elapsedTime).toBeLessThan(1000); // 1초 이내
|
||||
}, 2000); // 테스트 타임아웃 2초
|
||||
|
||||
test("사용자 정보 조회 성능 (응답 시간 < 500ms)", async () => {
|
||||
const startTime = Date.now();
|
||||
|
||||
await AuthService.getUserInfo(TEST_USER.userId);
|
||||
|
||||
const endTime = Date.now();
|
||||
const elapsedTime = endTime - startTime;
|
||||
|
||||
expect(elapsedTime).toBeLessThan(500); // 500ms 이내
|
||||
}, 1000); // 테스트 타임아웃 1초
|
||||
});
|
||||
});
|
||||
382
backend-node/src/tests/integration/auth.integration.test.ts
Normal file
382
backend-node/src/tests/integration/auth.integration.test.ts
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* AuthService 통합 테스트
|
||||
* Phase 1.5: 인증 시스템 전체 플로우 테스트
|
||||
*
|
||||
* 테스트 시나리오:
|
||||
* 1. 로그인 → 토큰 발급
|
||||
* 2. 토큰으로 API 인증
|
||||
* 3. 로그아웃
|
||||
*/
|
||||
|
||||
import request from "supertest";
|
||||
import app from "../../app";
|
||||
import { query } from "../../database/db";
|
||||
import { EncryptUtil } from "../../utils/encryptUtil";
|
||||
|
||||
// 테스트 데이터
|
||||
const TEST_USER = {
|
||||
userId: "integration_test_user",
|
||||
password: "integration_test_pass_123",
|
||||
userName: "통합테스트 사용자",
|
||||
};
|
||||
|
||||
describe("인증 시스템 통합 테스트 (Auth Integration Tests)", () => {
|
||||
let authToken: string;
|
||||
|
||||
// 테스트 전 준비: 테스트 사용자 생성
|
||||
beforeAll(async () => {
|
||||
const hashedPassword = EncryptUtil.encrypt(TEST_USER.password);
|
||||
|
||||
try {
|
||||
// 기존 사용자 확인
|
||||
const existing = await query(
|
||||
"SELECT user_id FROM user_info WHERE user_id = $1",
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
if (existing.length === 0) {
|
||||
// 새 사용자 생성
|
||||
await query(
|
||||
`INSERT INTO user_info (
|
||||
user_id, user_name, user_password, company_code, locale
|
||||
) VALUES ($1, $2, $3, $4, $5)`,
|
||||
[
|
||||
TEST_USER.userId,
|
||||
TEST_USER.userName,
|
||||
hashedPassword,
|
||||
"ILSHIN",
|
||||
"KR",
|
||||
]
|
||||
);
|
||||
} else {
|
||||
// 기존 사용자 비밀번호 업데이트
|
||||
await query(
|
||||
"UPDATE user_info SET user_password = $1, user_name = $2 WHERE user_id = $3",
|
||||
[hashedPassword, TEST_USER.userName, TEST_USER.userId]
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`✅ 통합 테스트 사용자 준비 완료: ${TEST_USER.userId}`);
|
||||
} catch (error) {
|
||||
console.error("❌ 테스트 사용자 생성 실패:", error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
// 테스트 후 정리 (선택적)
|
||||
afterAll(async () => {
|
||||
// 테스트 사용자 삭제 (필요시)
|
||||
// await query("DELETE FROM user_info WHERE user_id = $1", [TEST_USER.userId]);
|
||||
console.log("✅ 통합 테스트 완료");
|
||||
});
|
||||
|
||||
describe("1. 로그인 플로우 (POST /api/auth/login)", () => {
|
||||
test("✅ 올바른 자격증명으로 로그인 성공", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: TEST_USER.userId,
|
||||
password: TEST_USER.password,
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(true);
|
||||
expect(response.body.token).toBeDefined();
|
||||
expect(response.body.userInfo).toBeDefined();
|
||||
expect(response.body.userInfo.userId).toBe(TEST_USER.userId);
|
||||
expect(response.body.userInfo.userName).toBe(TEST_USER.userName);
|
||||
|
||||
// 토큰 저장 (다음 테스트에서 사용)
|
||||
authToken = response.body.token;
|
||||
});
|
||||
|
||||
test("❌ 잘못된 비밀번호로 로그인 실패", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: TEST_USER.userId,
|
||||
password: "wrong_password_123",
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
expect(response.body.token).toBeUndefined();
|
||||
expect(response.body.errorReason).toBeDefined();
|
||||
expect(response.body.errorReason).toContain("일치하지 않습니다");
|
||||
});
|
||||
|
||||
test("❌ 존재하지 않는 사용자 로그인 실패", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: "nonexistent_user_999",
|
||||
password: "anypassword",
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
expect(response.body.token).toBeUndefined();
|
||||
expect(response.body.errorReason).toContain("존재하지 않습니다");
|
||||
});
|
||||
|
||||
test("❌ 필수 필드 누락 시 로그인 실패", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: TEST_USER.userId,
|
||||
// password 누락
|
||||
})
|
||||
.expect(400);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
|
||||
test("✅ JWT 토큰 형식 검증", () => {
|
||||
expect(authToken).toBeDefined();
|
||||
expect(typeof authToken).toBe("string");
|
||||
|
||||
// JWT는 3개 파트로 구성 (header.payload.signature)
|
||||
const parts = authToken.split(".");
|
||||
expect(parts.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("2. 토큰 검증 플로우 (GET /api/auth/verify)", () => {
|
||||
test("✅ 유효한 토큰으로 검증 성공", async () => {
|
||||
const response = await request(app)
|
||||
.get("/api/auth/verify")
|
||||
.set("Authorization", `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.valid).toBe(true);
|
||||
expect(response.body.userInfo).toBeDefined();
|
||||
expect(response.body.userInfo.userId).toBe(TEST_USER.userId);
|
||||
});
|
||||
|
||||
test("❌ 토큰 없이 요청 시 실패", async () => {
|
||||
const response = await request(app).get("/api/auth/verify").expect(401);
|
||||
|
||||
expect(response.body.valid).toBe(false);
|
||||
});
|
||||
|
||||
test("❌ 잘못된 토큰으로 요청 시 실패", async () => {
|
||||
const response = await request(app)
|
||||
.get("/api/auth/verify")
|
||||
.set("Authorization", "Bearer invalid_token_12345")
|
||||
.expect(401);
|
||||
|
||||
expect(response.body.valid).toBe(false);
|
||||
});
|
||||
|
||||
test("❌ Bearer 없는 토큰으로 요청 시 실패", async () => {
|
||||
const response = await request(app)
|
||||
.get("/api/auth/verify")
|
||||
.set("Authorization", authToken) // Bearer 키워드 없음
|
||||
.expect(401);
|
||||
|
||||
expect(response.body.valid).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("3. 인증된 API 요청 플로우", () => {
|
||||
test("✅ 인증된 사용자로 메뉴 조회", async () => {
|
||||
const response = await request(app)
|
||||
.get("/api/admin/menu")
|
||||
.set("Authorization", `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(Array.isArray(response.body)).toBe(true);
|
||||
});
|
||||
|
||||
test("❌ 인증 없이 보호된 API 요청 실패", async () => {
|
||||
const response = await request(app).get("/api/admin/menu").expect(401);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("4. 로그아웃 플로우 (POST /api/auth/logout)", () => {
|
||||
test("✅ 로그아웃 성공", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/logout")
|
||||
.set("Authorization", `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(true);
|
||||
});
|
||||
|
||||
test("✅ 로그아웃 로그 기록 확인", async () => {
|
||||
// 로그아웃 로그가 기록되었는지 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
AND ERROR_MESSAGE = '로그아웃'
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
expect(logs[0].error_message).toBe("로그아웃");
|
||||
});
|
||||
});
|
||||
|
||||
describe("5. 전체 시나리오 테스트", () => {
|
||||
test("✅ 로그인 → 인증 → API 호출 → 로그아웃 전체 플로우", async () => {
|
||||
// 1. 로그인
|
||||
const loginResponse = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: TEST_USER.userId,
|
||||
password: TEST_USER.password,
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(loginResponse.body.success).toBe(true);
|
||||
const token = loginResponse.body.token;
|
||||
|
||||
// 2. 토큰 검증
|
||||
const verifyResponse = await request(app)
|
||||
.get("/api/auth/verify")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.expect(200);
|
||||
|
||||
expect(verifyResponse.body.valid).toBe(true);
|
||||
|
||||
// 3. 보호된 API 호출
|
||||
const menuResponse = await request(app)
|
||||
.get("/api/admin/menu")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.expect(200);
|
||||
|
||||
expect(Array.isArray(menuResponse.body)).toBe(true);
|
||||
|
||||
// 4. 로그아웃
|
||||
const logoutResponse = await request(app)
|
||||
.post("/api/auth/logout")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.expect(200);
|
||||
|
||||
expect(logoutResponse.body.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("6. 에러 처리 및 예외 상황", () => {
|
||||
test("❌ SQL Injection 시도 차단", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: "admin' OR '1'='1",
|
||||
password: "password",
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
// SQL Injection이 차단되어 로그인 실패해야 함
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
|
||||
test("❌ 빈 문자열로 로그인 시도", async () => {
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: "",
|
||||
password: "",
|
||||
})
|
||||
.expect(400);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
|
||||
test("❌ 매우 긴 사용자 ID로 로그인 시도", async () => {
|
||||
const longUserId = "a".repeat(1000);
|
||||
const response = await request(app)
|
||||
.post("/api/auth/login")
|
||||
.send({
|
||||
userId: longUserId,
|
||||
password: "password",
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("7. 로그인 이력 확인", () => {
|
||||
test("✅ 로그인 성공 이력 조회", async () => {
|
||||
// 로그인 실행
|
||||
await request(app).post("/api/auth/login").send({
|
||||
userId: TEST_USER.userId,
|
||||
password: TEST_USER.password,
|
||||
});
|
||||
|
||||
// 로그인 이력 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
AND LOGIN_RESULT = true
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
expect(logs[0].login_result).toBeTruthy();
|
||||
expect(logs[0].system_name).toBe("PMS");
|
||||
});
|
||||
|
||||
test("✅ 로그인 실패 이력 조회", async () => {
|
||||
// 로그인 실패 실행
|
||||
await request(app).post("/api/auth/login").send({
|
||||
userId: TEST_USER.userId,
|
||||
password: "wrong_password",
|
||||
});
|
||||
|
||||
// 로그인 실패 이력 확인
|
||||
const logs = await query(
|
||||
`SELECT * FROM LOGIN_ACCESS_LOG
|
||||
WHERE USER_ID = UPPER($1)
|
||||
AND LOGIN_RESULT = false
|
||||
AND ERROR_MESSAGE IS NOT NULL
|
||||
ORDER BY LOG_TIME DESC
|
||||
LIMIT 1`,
|
||||
[TEST_USER.userId]
|
||||
);
|
||||
|
||||
expect(logs.length).toBeGreaterThan(0);
|
||||
expect(logs[0].login_result).toBeFalsy();
|
||||
expect(logs[0].error_message).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("8. 성능 테스트", () => {
|
||||
test("✅ 동시 로그인 요청 처리 (10개)", async () => {
|
||||
const promises = Array.from({ length: 10 }, () =>
|
||||
request(app).post("/api/auth/login").send({
|
||||
userId: TEST_USER.userId,
|
||||
password: TEST_USER.password,
|
||||
})
|
||||
);
|
||||
|
||||
const responses = await Promise.all(promises);
|
||||
|
||||
responses.forEach((response) => {
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.success).toBe(true);
|
||||
});
|
||||
}, 10000); // 10초 타임아웃
|
||||
|
||||
test("✅ 로그인 응답 시간 (< 1초)", async () => {
|
||||
const startTime = Date.now();
|
||||
|
||||
await request(app).post("/api/auth/login").send({
|
||||
userId: TEST_USER.userId,
|
||||
password: TEST_USER.password,
|
||||
});
|
||||
|
||||
const endTime = Date.now();
|
||||
const elapsedTime = endTime - startTime;
|
||||
|
||||
expect(elapsedTime).toBeLessThan(1000); // 1초 이내
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user