Spaces:
Sleeping
Sleeping
Restore local changes after git object corruption
Browse files- run_tests.sh +13 -0
- tests/test_db_service.py +83 -14
run_tests.sh
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Test runner for DB Service
|
| 3 |
+
|
| 4 |
+
export ADMIN_EMAILS="admin@example.com"
|
| 5 |
+
export PYTHONPATH="$(pwd):$PYTHONPATH"
|
| 6 |
+
|
| 7 |
+
echo "Running DB Service Tests..."
|
| 8 |
+
echo "=============================="
|
| 9 |
+
|
| 10 |
+
python -m pytest tests/test_db_service.py -v --tb=short
|
| 11 |
+
|
| 12 |
+
echo ""
|
| 13 |
+
echo "Test run complete!"
|
tests/test_db_service.py
CHANGED
|
@@ -12,9 +12,8 @@ Comprehensive tests for the plug-and-play DB Service including:
|
|
| 12 |
import pytest
|
| 13 |
import os
|
| 14 |
from datetime import datetime
|
| 15 |
-
from sqlalchemy import
|
| 16 |
-
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
| 17 |
-
from sqlalchemy.orm import declarative_base
|
| 18 |
|
| 19 |
from services.db_service import (
|
| 20 |
DBServiceConfig,
|
|
@@ -91,7 +90,9 @@ async def setup_db(engine):
|
|
| 91 |
@pytest.fixture
|
| 92 |
async def regular_user(session):
|
| 93 |
"""Create a regular test user."""
|
|
|
|
| 94 |
user = User(
|
|
|
|
| 95 |
email="user@example.com",
|
| 96 |
name="Test User",
|
| 97 |
credits=100
|
|
@@ -105,7 +106,9 @@ async def regular_user(session):
|
|
| 105 |
@pytest.fixture
|
| 106 |
async def admin_user(session):
|
| 107 |
"""Create an admin test user."""
|
|
|
|
| 108 |
user = User(
|
|
|
|
| 109 |
email=os.getenv("ADMIN_EMAILS", "admin@example.com").split(",")[0],
|
| 110 |
name="Admin User",
|
| 111 |
credits=1000
|
|
@@ -119,7 +122,9 @@ async def admin_user(session):
|
|
| 119 |
@pytest.fixture
|
| 120 |
async def other_user(session):
|
| 121 |
"""Create another test user."""
|
|
|
|
| 122 |
user = User(
|
|
|
|
| 123 |
email="other@example.com",
|
| 124 |
name="Other User",
|
| 125 |
credits=50
|
|
@@ -166,7 +171,14 @@ class TestPermissions:
|
|
| 166 |
|
| 167 |
async def test_user_can_read_own_data(self, session, regular_user):
|
| 168 |
"""Test that users can read their own data."""
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
session.add(job)
|
| 171 |
await session.commit()
|
| 172 |
|
|
@@ -179,7 +191,14 @@ class TestPermissions:
|
|
| 179 |
async def test_user_cannot_read_others_data(self, session, regular_user, other_user):
|
| 180 |
"""Test that users cannot read other users' data."""
|
| 181 |
# Create job for other user
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
session.add(job)
|
| 184 |
await session.commit()
|
| 185 |
|
|
@@ -192,8 +211,21 @@ class TestPermissions:
|
|
| 192 |
async def test_admin_can_read_all_data(self, session, admin_user, regular_user):
|
| 193 |
"""Test that admins can read all users' data."""
|
| 194 |
# Create jobs for different users
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
session.add_all([job1, job2])
|
| 198 |
await session.commit()
|
| 199 |
|
|
@@ -213,7 +245,15 @@ class TestPermissions:
|
|
| 213 |
|
| 214 |
async def test_admin_can_access_admin_only_models(self, session, admin_user):
|
| 215 |
"""Test that admins can access admin-only models."""
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
session.add(rate_limit)
|
| 218 |
await session.commit()
|
| 219 |
|
|
@@ -228,7 +268,7 @@ class TestPermissions:
|
|
| 228 |
|
| 229 |
# System should be able to bypass permissions
|
| 230 |
# (actual create would use direct SQLAlchemy, but permission check passes)
|
| 231 |
-
assert qs.
|
| 232 |
|
| 233 |
|
| 234 |
# ============================================================================
|
|
@@ -240,7 +280,14 @@ class TestSoftDeletes:
|
|
| 240 |
|
| 241 |
async def test_soft_delete_marks_record(self, session, regular_user):
|
| 242 |
"""Test that soft delete sets deleted_at."""
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
session.add(job)
|
| 245 |
await session.commit()
|
| 246 |
|
|
@@ -251,7 +298,14 @@ class TestSoftDeletes:
|
|
| 251 |
|
| 252 |
async def test_soft_deleted_not_in_query(self, session, regular_user):
|
| 253 |
"""Test that soft-deleted records don't appear in queries."""
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
session.add(job)
|
| 256 |
await session.commit()
|
| 257 |
|
|
@@ -268,7 +322,14 @@ class TestSoftDeletes:
|
|
| 268 |
|
| 269 |
async def test_admin_can_restore(self, session, admin_user, regular_user):
|
| 270 |
"""Test that admins can restore deleted records."""
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
session.add(job)
|
| 273 |
await session.commit()
|
| 274 |
job_id = job.id
|
|
@@ -285,7 +346,14 @@ class TestSoftDeletes:
|
|
| 285 |
|
| 286 |
async def test_user_cannot_restore(self, session, regular_user):
|
| 287 |
"""Test that regular users cannot restore records."""
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
session.add(job)
|
| 290 |
await session.commit()
|
| 291 |
|
|
@@ -316,8 +384,9 @@ class TestDatabaseInitialization:
|
|
| 316 |
|
| 317 |
async def test_reset_database_clears_data(self, engine, session, regular_user):
|
| 318 |
"""Test that reset_database clears all data."""
|
|
|
|
| 319 |
# Add some data
|
| 320 |
-
user = User(email="test@example.com", name="Test", credits=10)
|
| 321 |
session.add(user)
|
| 322 |
await session.commit()
|
| 323 |
|
|
|
|
| 12 |
import pytest
|
| 13 |
import os
|
| 14 |
from datetime import datetime
|
| 15 |
+
from sqlalchemy import select
|
| 16 |
+
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
|
|
| 17 |
|
| 18 |
from services.db_service import (
|
| 19 |
DBServiceConfig,
|
|
|
|
| 90 |
@pytest.fixture
|
| 91 |
async def regular_user(session):
|
| 92 |
"""Create a regular test user."""
|
| 93 |
+
import uuid
|
| 94 |
user = User(
|
| 95 |
+
user_id=str(uuid.uuid4()),
|
| 96 |
email="user@example.com",
|
| 97 |
name="Test User",
|
| 98 |
credits=100
|
|
|
|
| 106 |
@pytest.fixture
|
| 107 |
async def admin_user(session):
|
| 108 |
"""Create an admin test user."""
|
| 109 |
+
import uuid
|
| 110 |
user = User(
|
| 111 |
+
user_id=str(uuid.uuid4()),
|
| 112 |
email=os.getenv("ADMIN_EMAILS", "admin@example.com").split(",")[0],
|
| 113 |
name="Admin User",
|
| 114 |
credits=1000
|
|
|
|
| 122 |
@pytest.fixture
|
| 123 |
async def other_user(session):
|
| 124 |
"""Create another test user."""
|
| 125 |
+
import uuid
|
| 126 |
user = User(
|
| 127 |
+
user_id=str(uuid.uuid4()),
|
| 128 |
email="other@example.com",
|
| 129 |
name="Other User",
|
| 130 |
credits=50
|
|
|
|
| 171 |
|
| 172 |
async def test_user_can_read_own_data(self, session, regular_user):
|
| 173 |
"""Test that users can read their own data."""
|
| 174 |
+
import uuid
|
| 175 |
+
job = GeminiJob(
|
| 176 |
+
job_id=str(uuid.uuid4()),
|
| 177 |
+
user_id=regular_user.id,
|
| 178 |
+
job_type="text",
|
| 179 |
+
input_data={"prompt": "Test"},
|
| 180 |
+
status="queued"
|
| 181 |
+
)
|
| 182 |
session.add(job)
|
| 183 |
await session.commit()
|
| 184 |
|
|
|
|
| 191 |
async def test_user_cannot_read_others_data(self, session, regular_user, other_user):
|
| 192 |
"""Test that users cannot read other users' data."""
|
| 193 |
# Create job for other user
|
| 194 |
+
import uuid
|
| 195 |
+
job = GeminiJob(
|
| 196 |
+
job_id=str(uuid.uuid4()),
|
| 197 |
+
user_id=other_user.id,
|
| 198 |
+
job_type="text",
|
| 199 |
+
input_data={"prompt": "Other"},
|
| 200 |
+
status="queued"
|
| 201 |
+
)
|
| 202 |
session.add(job)
|
| 203 |
await session.commit()
|
| 204 |
|
|
|
|
| 211 |
async def test_admin_can_read_all_data(self, session, admin_user, regular_user):
|
| 212 |
"""Test that admins can read all users' data."""
|
| 213 |
# Create jobs for different users
|
| 214 |
+
import uuid
|
| 215 |
+
job1 = GeminiJob(
|
| 216 |
+
job_id=str(uuid.uuid4()),
|
| 217 |
+
user_id=regular_user.id,
|
| 218 |
+
job_type="text",
|
| 219 |
+
input_data={"prompt": "User Job"},
|
| 220 |
+
status="queued"
|
| 221 |
+
)
|
| 222 |
+
job2 = GeminiJob(
|
| 223 |
+
job_id=str(uuid.uuid4()),
|
| 224 |
+
user_id=admin_user.id,
|
| 225 |
+
job_type="text",
|
| 226 |
+
input_data={"prompt": "Admin Job"},
|
| 227 |
+
status="queued"
|
| 228 |
+
)
|
| 229 |
session.add_all([job1, job2])
|
| 230 |
await session.commit()
|
| 231 |
|
|
|
|
| 245 |
|
| 246 |
async def test_admin_can_access_admin_only_models(self, session, admin_user):
|
| 247 |
"""Test that admins can access admin-only models."""
|
| 248 |
+
from datetime import datetime, timedelta
|
| 249 |
+
now = datetime.now()
|
| 250 |
+
rate_limit = RateLimit(
|
| 251 |
+
identifier="test",
|
| 252 |
+
endpoint="/api/test",
|
| 253 |
+
attempts=10,
|
| 254 |
+
window_start=now,
|
| 255 |
+
expires_at=now + timedelta(hours=1)
|
| 256 |
+
)
|
| 257 |
session.add(rate_limit)
|
| 258 |
await session.commit()
|
| 259 |
|
|
|
|
| 268 |
|
| 269 |
# System should be able to bypass permissions
|
| 270 |
# (actual create would use direct SQLAlchemy, but permission check passes)
|
| 271 |
+
assert qs.is_system is True
|
| 272 |
|
| 273 |
|
| 274 |
# ============================================================================
|
|
|
|
| 280 |
|
| 281 |
async def test_soft_delete_marks_record(self, session, regular_user):
|
| 282 |
"""Test that soft delete sets deleted_at."""
|
| 283 |
+
import uuid
|
| 284 |
+
job = GeminiJob(
|
| 285 |
+
job_id=str(uuid.uuid4()),
|
| 286 |
+
user_id=regular_user.id,
|
| 287 |
+
job_type="text",
|
| 288 |
+
input_data={"prompt": "Delete Me"},
|
| 289 |
+
status="queued"
|
| 290 |
+
)
|
| 291 |
session.add(job)
|
| 292 |
await session.commit()
|
| 293 |
|
|
|
|
| 298 |
|
| 299 |
async def test_soft_deleted_not_in_query(self, session, regular_user):
|
| 300 |
"""Test that soft-deleted records don't appear in queries."""
|
| 301 |
+
import uuid
|
| 302 |
+
job = GeminiJob(
|
| 303 |
+
job_id=str(uuid.uuid4()),
|
| 304 |
+
user_id=regular_user.id,
|
| 305 |
+
job_type="text",
|
| 306 |
+
input_data={"prompt": "Delete Me"},
|
| 307 |
+
status="queued"
|
| 308 |
+
)
|
| 309 |
session.add(job)
|
| 310 |
await session.commit()
|
| 311 |
|
|
|
|
| 322 |
|
| 323 |
async def test_admin_can_restore(self, session, admin_user, regular_user):
|
| 324 |
"""Test that admins can restore deleted records."""
|
| 325 |
+
import uuid
|
| 326 |
+
job = GeminiJob(
|
| 327 |
+
job_id=str(uuid.uuid4()),
|
| 328 |
+
user_id=regular_user.id,
|
| 329 |
+
job_type="text",
|
| 330 |
+
input_data={"prompt": "Restore Me"},
|
| 331 |
+
status="queued"
|
| 332 |
+
)
|
| 333 |
session.add(job)
|
| 334 |
await session.commit()
|
| 335 |
job_id = job.id
|
|
|
|
| 346 |
|
| 347 |
async def test_user_cannot_restore(self, session, regular_user):
|
| 348 |
"""Test that regular users cannot restore records."""
|
| 349 |
+
import uuid
|
| 350 |
+
job = GeminiJob(
|
| 351 |
+
job_id=str(uuid.uuid4()),
|
| 352 |
+
user_id=regular_user.id,
|
| 353 |
+
job_type="text",
|
| 354 |
+
input_data={"prompt": "Deleted"},
|
| 355 |
+
status="queued"
|
| 356 |
+
)
|
| 357 |
session.add(job)
|
| 358 |
await session.commit()
|
| 359 |
|
|
|
|
| 384 |
|
| 385 |
async def test_reset_database_clears_data(self, engine, session, regular_user):
|
| 386 |
"""Test that reset_database clears all data."""
|
| 387 |
+
import uuid
|
| 388 |
# Add some data
|
| 389 |
+
user = User(user_id=str(uuid.uuid4()), email="test@example.com", name="Test", credits=10)
|
| 390 |
session.add(user)
|
| 391 |
await session.commit()
|
| 392 |
|