init commit

This commit is contained in:
2025-09-25 08:05:25 +09:00
commit 4d7551d4f1
56 changed files with 5977 additions and 0 deletions

60
tests/conftest.py Normal file
View File

@@ -0,0 +1,60 @@
import pytest
import asyncio
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from httpx import AsyncClient
from shared.database import Base
from shared.config import settings
from services.user_service.main import app
# Test database URL
TEST_DATABASE_URL = "postgresql+asyncpg://admin:password@localhost:5432/women_safety_test"
# Test engine
test_engine = create_async_engine(TEST_DATABASE_URL, echo=True)
TestAsyncSession = sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
@pytest.fixture(scope="session")
def event_loop():
"""Create an instance of the default event loop for the test session."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
async def setup_database():
"""Set up test database"""
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
@pytest.fixture
async def db_session(setup_database):
"""Create a test database session"""
async with TestAsyncSession() as session:
yield session
await session.rollback()
@pytest.fixture
async def client():
"""Create test client"""
async with AsyncClient(app=app, base_url="http://testserver") as ac:
yield ac
@pytest.fixture
def user_data():
"""Sample user data for testing"""
return {
"email": "test@example.com",
"password": "testpassword123",
"first_name": "Test",
"last_name": "User",
"phone": "+1234567890"
}

View File

@@ -0,0 +1,85 @@
import pytest
from httpx import AsyncClient
class TestUserService:
"""Test cases for User Service"""
async def test_register_user(self, client: AsyncClient, user_data):
"""Test user registration"""
response = await client.post("/api/v1/register", json=user_data)
assert response.status_code == 200
data = response.json()
assert data["email"] == user_data["email"]
assert data["first_name"] == user_data["first_name"]
assert "id" in data
async def test_register_duplicate_email(self, client: AsyncClient, user_data):
"""Test registration with duplicate email"""
# First registration
await client.post("/api/v1/register", json=user_data)
# Second registration with same email
response = await client.post("/api/v1/register", json=user_data)
assert response.status_code == 400
assert "already registered" in response.json()["detail"]
async def test_login(self, client: AsyncClient, user_data):
"""Test user login"""
# Register user first
await client.post("/api/v1/register", json=user_data)
# Login
login_data = {
"email": user_data["email"],
"password": user_data["password"]
}
response = await client.post("/api/v1/login", json=login_data)
assert response.status_code == 200
data = response.json()
assert "access_token" in data
assert data["token_type"] == "bearer"
async def test_login_invalid_credentials(self, client: AsyncClient):
"""Test login with invalid credentials"""
login_data = {
"email": "wrong@example.com",
"password": "wrongpassword"
}
response = await client.post("/api/v1/login", json=login_data)
assert response.status_code == 401
async def test_get_profile(self, client: AsyncClient, user_data):
"""Test getting user profile"""
# Register and login
await client.post("/api/v1/register", json=user_data)
login_response = await client.post("/api/v1/login", json={
"email": user_data["email"],
"password": user_data["password"]
})
token = login_response.json()["access_token"]
# Get profile
headers = {"Authorization": f"Bearer {token}"}
response = await client.get("/api/v1/profile", headers=headers)
assert response.status_code == 200
data = response.json()
assert data["email"] == user_data["email"]
async def test_update_profile(self, client: AsyncClient, user_data):
"""Test updating user profile"""
# Register and login
await client.post("/api/v1/register", json=user_data)
login_response = await client.post("/api/v1/login", json={
"email": user_data["email"],
"password": user_data["password"]
})
token = login_response.json()["access_token"]
# Update profile
update_data = {"bio": "Updated bio text"}
headers = {"Authorization": f"Bearer {token}"}
response = await client.put("/api/v1/profile", json=update_data, headers=headers)
assert response.status_code == 200
data = response.json()
assert data["bio"] == "Updated bio text"