Spaces:
Sleeping
Sleeping
File size: 13,640 Bytes
bbeedce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from src.main import app
from src.database import get_db, Base
import os
# Use in-memory SQLite for testing
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
"""Override database dependency for testing"""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
@pytest.fixture(scope="function")
def test_db():
"""Create test database tables before each test and drop after"""
Base.metadata.create_all(bind=engine)
yield
Base.metadata.drop_all(bind=engine)
class TestAuthEndpoints:
"""Integration tests for authentication endpoints"""
def test_register_user_success(self, test_db):
"""Test successful user registration"""
response = client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
assert response.status_code == 201
data = response.json()
assert data["email"] == "test@example.com"
assert data["username"] == "testuser"
assert "id" in data
def test_register_duplicate_email(self, test_db):
"""Test registration with duplicate email"""
# First registration
client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser1",
"password": "SecurePass123!"
}
)
# Second registration with same email
response = client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser2",
"password": "SecurePass123!"
}
)
assert response.status_code == 409
assert "Email already registered" in response.json()["detail"]
def test_register_duplicate_username(self, test_db):
"""Test registration with duplicate username"""
# First registration
client.post(
"/api/v1/auth/register",
json={
"email": "test1@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
# Second registration with same username
response = client.post(
"/api/v1/auth/register",
json={
"email": "test2@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
assert response.status_code == 409
assert "Username already taken" in response.json()["detail"]
def test_login_success(self, test_db):
"""Test successful login"""
# Register user first
client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
# Login
response = client.post(
"/api/v1/auth/login",
params={
"email": "test@example.com",
"password": "SecurePass123!"
}
)
assert response.status_code == 200
data = response.json()
assert "access_token" in data
assert data["token_type"] == "bearer"
assert "user_id" in data
def test_login_wrong_password(self, test_db):
"""Test login with wrong password"""
# Register user first
client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
# Login with wrong password
response = client.post(
"/api/v1/auth/login",
params={
"email": "test@example.com",
"password": "WrongPassword"
}
)
assert response.status_code == 401
assert "Incorrect email or password" in response.json()["detail"]
def test_login_nonexistent_user(self, test_db):
"""Test login with non-existent user"""
response = client.post(
"/api/v1/auth/login",
params={
"email": "nonexistent@example.com",
"password": "SecurePass123!"
}
)
assert response.status_code == 401
def test_get_current_user(self, test_db):
"""Test getting current user information"""
# Register and login
client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
login_response = client.post(
"/api/v1/auth/login",
params={
"email": "test@example.com",
"password": "SecurePass123!"
}
)
token = login_response.json()["access_token"]
# Get current user
response = client.get(
"/api/v1/auth/me",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
data = response.json()
assert data["email"] == "test@example.com"
assert data["username"] == "testuser"
def test_get_current_user_invalid_token(self, test_db):
"""Test getting current user with invalid token"""
response = client.get(
"/api/v1/auth/me",
headers={"Authorization": "Bearer invalid_token"}
)
assert response.status_code == 401
class TestTaskEndpoints:
"""Integration tests for task endpoints"""
def get_auth_token(self):
"""Helper method to register and login a user"""
client.post(
"/api/v1/auth/register",
json={
"email": "test@example.com",
"username": "testuser",
"password": "SecurePass123!"
}
)
login_response = client.post(
"/api/v1/auth/login",
params={
"email": "test@example.com",
"password": "SecurePass123!"
}
)
return login_response.json()["access_token"]
def test_create_task_success(self, test_db):
"""Test successful task creation"""
token = self.get_auth_token()
response = client.post(
"/api/v1/tasks/",
json={
"title": "Test Task",
"description": "Test Description",
"status": "pending"
},
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 201
data = response.json()
assert data["title"] == "Test Task"
assert data["description"] == "Test Description"
assert data["status"] == "pending"
assert "id" in data
def test_create_task_invalid_status(self, test_db):
"""Test task creation with invalid status"""
token = self.get_auth_token()
response = client.post(
"/api/v1/tasks/",
json={
"title": "Test Task",
"description": "Test Description",
"status": "invalid_status"
},
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 400
def test_create_task_unauthorized(self, test_db):
"""Test task creation without authentication"""
response = client.post(
"/api/v1/tasks/",
json={
"title": "Test Task",
"description": "Test Description",
"status": "pending"
}
)
assert response.status_code == 403
def test_get_tasks(self, test_db):
"""Test retrieving all tasks for a user"""
token = self.get_auth_token()
# Create multiple tasks
for i in range(3):
client.post(
"/api/v1/tasks/",
json={
"title": f"Task {i}",
"description": f"Description {i}",
"status": "pending"
},
headers={"Authorization": f"Bearer {token}"}
)
# Get all tasks
response = client.get(
"/api/v1/tasks/",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
data = response.json()
assert len(data) == 3
def test_get_task_by_id(self, test_db):
"""Test retrieving a specific task"""
token = self.get_auth_token()
# Create a task
create_response = client.post(
"/api/v1/tasks/",
json={
"title": "Test Task",
"description": "Test Description",
"status": "pending"
},
headers={"Authorization": f"Bearer {token}"}
)
task_id = create_response.json()["id"]
# Get the task
response = client.get(
f"/api/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
data = response.json()
assert data["id"] == task_id
assert data["title"] == "Test Task"
def test_get_task_not_found(self, test_db):
"""Test retrieving a non-existent task"""
token = self.get_auth_token()
response = client.get(
"/api/v1/tasks/999",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 404
def test_update_task(self, test_db):
"""Test updating a task"""
token = self.get_auth_token()
# Create a task
create_response = client.post(
"/api/v1/tasks/",
json={
"title": "Original Title",
"description": "Original Description",
"status": "pending"
},
headers={"Authorization": f"Bearer {token}"}
)
task_id = create_response.json()["id"]
# Update the task
response = client.put(
f"/api/v1/tasks/{task_id}",
json={
"title": "Updated Title",
"status": "completed"
},
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
data = response.json()
assert data["title"] == "Updated Title"
assert data["status"] == "completed"
def test_delete_task(self, test_db):
"""Test deleting a task"""
token = self.get_auth_token()
# Create a task
create_response = client.post(
"/api/v1/tasks/",
json={
"title": "Test Task",
"description": "Test Description",
"status": "pending"
},
headers={"Authorization": f"Bearer {token}"}
)
task_id = create_response.json()["id"]
# Delete the task
response = client.delete(
f"/api/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 204
# Verify task is deleted
get_response = client.get(
f"/api/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {token}"}
)
assert get_response.status_code == 404
def test_user_isolation(self, test_db):
"""Test that users can only access their own tasks"""
# Create first user and task
client.post(
"/api/v1/auth/register",
json={
"email": "user1@example.com",
"username": "user1",
"password": "SecurePass123!"
}
)
login1 = client.post(
"/api/v1/auth/login",
params={"email": "user1@example.com", "password": "SecurePass123!"}
)
token1 = login1.json()["access_token"]
create_response = client.post(
"/api/v1/tasks/",
json={"title": "User 1 Task", "status": "pending"},
headers={"Authorization": f"Bearer {token1}"}
)
task_id = create_response.json()["id"]
# Create second user
client.post(
"/api/v1/auth/register",
json={
"email": "user2@example.com",
"username": "user2",
"password": "SecurePass123!"
}
)
login2 = client.post(
"/api/v1/auth/login",
params={"email": "user2@example.com", "password": "SecurePass123!"}
)
token2 = login2.json()["access_token"]
# Try to access user1's task with user2's token
response = client.get(
f"/api/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {token2}"}
)
assert response.status_code == 404 # Should not find the task
|