127 lines
4.5 KiB
Python
127 lines
4.5 KiB
Python
import pytest
|
|
from unittest.mock import MagicMock
|
|
from app.dependencies import get_auth_service
|
|
|
|
class TestAuthAPI:
|
|
"""인증 관련 API 테스트"""
|
|
|
|
def test_join_success(self, client, mock_auth_service):
|
|
"""회원가입 성공 테스트"""
|
|
# Mock 설정
|
|
client.app.dependency_overrides[get_auth_service] = lambda: mock_auth_service
|
|
mock_auth_service.join.return_value = {
|
|
"user_id": 1,
|
|
"email": "test@example.com",
|
|
"message": "회원가입 성공"
|
|
}
|
|
|
|
# 테스트 실행
|
|
response = client.post("/api/v1/auth/join", json={
|
|
"email": "test@example.com",
|
|
"password": "Test1234!",
|
|
"password_confirm": "Test1234!",
|
|
"name": "Test User"
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "success"
|
|
assert data["data"]["email"] == "test@example.com"
|
|
assert mock_auth_service.join.called
|
|
|
|
def test_join_duplicate_email(self, client, mock_auth_service):
|
|
"""중복 이메일로 회원가입 시도 테스트"""
|
|
# Mock 설정
|
|
client.app.dependency_overrides[get_auth_service] = lambda: mock_auth_service
|
|
mock_auth_service.join.side_effect = ValueError("이미 존재하는 이메일입니다")
|
|
|
|
# 테스트 실행
|
|
response = client.post("/api/v1/auth/join", json={
|
|
"email": "existing@example.com",
|
|
"password": "Test1234!",
|
|
"password_confirm": "Test1234!",
|
|
"name": "Test User"
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 400
|
|
data = response.json()
|
|
assert data["status"] == "error"
|
|
assert "이미 존재하는 이메일" in data["message"]
|
|
|
|
def test_join_password_mismatch(self, client):
|
|
"""비밀번호 불일치 테스트"""
|
|
response = client.post("/api/v1/auth/join", json={
|
|
"email": "test@example.com",
|
|
"password": "Test1234!",
|
|
"password_confirm": "Different1234!",
|
|
"name": "Test User"
|
|
})
|
|
|
|
# 검증 - 422는 FastAPI의 validation error
|
|
assert response.status_code == 422
|
|
|
|
def test_login_success(self, client, mock_auth_service):
|
|
"""로그인 성공 테스트"""
|
|
# Mock 설정
|
|
client.app.dependency_overrides[get_auth_service] = lambda: mock_auth_service
|
|
mock_auth_service.login.return_value = {
|
|
"access_token": "test_token_123",
|
|
"token_type": "bearer",
|
|
"user_id": 1,
|
|
"email": "test@example.com"
|
|
}
|
|
|
|
# 테스트 실행
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "test@example.com",
|
|
"password": "Test1234!"
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "success"
|
|
assert data["data"]["access_token"] == "test_token_123"
|
|
assert data["data"]["token_type"] == "bearer"
|
|
assert mock_auth_service.login.called
|
|
|
|
def test_login_invalid_credentials(self, client, mock_auth_service):
|
|
"""잘못된 인증정보로 로그인 시도 테스트"""
|
|
# Mock 설정
|
|
client.app.dependency_overrides[get_auth_service] = lambda: mock_auth_service
|
|
mock_auth_service.login.side_effect = ValueError("이메일 또는 비밀번호가 일치하지 않습니다")
|
|
|
|
# 테스트 실행
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "test@example.com",
|
|
"password": "WrongPassword!"
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 400
|
|
data = response.json()
|
|
assert data["status"] == "error"
|
|
assert "이메일 또는 비밀번호가 일치하지 않습니다" in data["message"]
|
|
|
|
def test_login_empty_email(self, client):
|
|
"""빈 이메일로 로그인 시도 테스트"""
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "",
|
|
"password": "Test1234!"
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 422
|
|
|
|
def test_login_empty_password(self, client):
|
|
"""빈 비밀번호로 로그인 시도 테스트"""
|
|
response = client.post("/api/v1/auth/login", json={
|
|
"email": "test@example.com",
|
|
"password": ""
|
|
})
|
|
|
|
# 검증
|
|
assert response.status_code == 422
|