4.2 KiB
4.2 KiB
인증 시스템 테스트 가이드
테스트 계정
- 관리자: admin / admin123
- 일반 사용자: user / user123
1. Terminal에서 테스트
로그인 테스트
# 관리자로 로그인
curl -X POST http://localhost:8011/api/auth/login \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=admin123"
# 응답 예시:
# {"access_token":"eyJhbGci...","token_type":"bearer"}
토큰 저장 및 사용
# 토큰을 변수에 저장
export TOKEN="eyJhbGci..." # 위에서 받은 토큰
# 인증된 요청 - 사용자 정보 조회
curl -X GET http://localhost:8011/api/auth/me \
-H "Authorization: Bearer $TOKEN"
# 인증된 요청 - 보호된 엔드포인트
curl -X GET http://localhost:8011/api/protected \
-H "Authorization: Bearer $TOKEN"
# 인증된 요청 - Users 서비스 접근
curl -X GET http://localhost:8011/api/users/ \
-H "Authorization: Bearer $TOKEN"
로그아웃
curl -X POST http://localhost:8011/api/auth/logout \
-H "Authorization: Bearer $TOKEN"
2. Postman/Insomnia에서 테스트
Postman 설정
-
로그인 요청
- Method: POST
- URL:
http://localhost:8011/api/auth/login - Body: x-www-form-urlencoded
- username: admin
- password: admin123
-
토큰 사용
- Authorization 탭에서 Type: Bearer Token 선택
- Token 필드에 받은 토큰 붙여넣기
3. Python 스크립트로 테스트
import requests
# 로그인
login_response = requests.post(
"http://localhost:8011/api/auth/login",
data={"username": "admin", "password": "admin123"}
)
token = login_response.json()["access_token"]
# 인증된 요청
headers = {"Authorization": f"Bearer {token}"}
me_response = requests.get(
"http://localhost:8011/api/auth/me",
headers=headers
)
print(me_response.json())
# Users 서비스 접근
users_response = requests.get(
"http://localhost:8011/api/users/",
headers=headers
)
print(users_response.json())
4. JavaScript (브라우저 콘솔)에서 테스트
// 로그인
const loginResponse = await fetch('http://localhost:8011/api/auth/login', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'username=admin&password=admin123'
});
const { access_token } = await loginResponse.json();
console.log('Token:', access_token);
// 인증된 요청
const meResponse = await fetch('http://localhost:8011/api/auth/me', {
headers: {'Authorization': `Bearer ${access_token}`}
});
const userData = await meResponse.json();
console.log('User:', userData);
5. Frontend에서 테스트 (React)
브라우저에서 http://localhost:3000 접속 후 개발자 도구 콘솔에서:
// 로그인 함수
async function testLogin() {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'username=admin&password=admin123'
});
const data = await response.json();
localStorage.setItem('token', data.access_token);
console.log('Logged in!', data);
return data.access_token;
}
// 인증 테스트
async function testAuth() {
const token = localStorage.getItem('token');
const response = await fetch('/api/auth/me', {
headers: {'Authorization': `Bearer ${token}`}
});
const data = await response.json();
console.log('User info:', data);
}
// 실행
await testLogin();
await testAuth();
오류 테스트
잘못된 비밀번호
curl -X POST http://localhost:8011/api/auth/login \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=wrong"
# 응답: 401 Unauthorized
토큰 없이 보호된 엔드포인트 접근
curl -X GET http://localhost:8011/api/auth/me
# 응답: 401 Unauthorized
잘못된 토큰
curl -X GET http://localhost:8011/api/auth/me \
-H "Authorization: Bearer invalid_token"
# 응답: 401 Unauthorized
토큰 정보
- 유효 기간: 30분 (환경 변수 ACCESS_TOKEN_EXPIRE_MINUTES로 설정 가능)
- 알고리즘: HS256
- 페이로드: username 정보 포함
다음 단계
Frontend에 로그인 페이지를 추가하면 UI에서 직접 테스트 가능합니다.