|
| 1 | +from datetime import UTC, datetime, timedelta |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from fastapi import HTTPException |
| 5 | +from jose import jwt |
| 6 | + |
| 7 | +from app.core.startup import auto_seed_data, run_startup_tasks, setup_test_users |
| 8 | +from app.modules.auth.models import User |
| 9 | +from app.modules.auth.token import create_access_token |
| 10 | + |
| 11 | + |
| 12 | +def test_create_access_token_dev_long_expiry(): |
| 13 | + """Test that tokens in dev mode have a very long expiry.""" |
| 14 | + mock_settings = MagicMock() |
| 15 | + mock_settings.is_dev = True |
| 16 | + mock_settings.secret_key = "test_secret" |
| 17 | + mock_settings.jwt_algorithm = "HS256" |
| 18 | + |
| 19 | + with patch("app.modules.auth.token.get_settings", return_value=mock_settings): |
| 20 | + token = create_access_token({"sub": "user@example.com"}) |
| 21 | + payload = jwt.decode(token, "test_secret", algorithms=["HS256"]) |
| 22 | + |
| 23 | + exp = payload["exp"] |
| 24 | + expected_min_exp = (datetime.now(UTC) + timedelta(days=365 * 99)).timestamp() |
| 25 | + assert exp > expected_min_exp |
| 26 | + |
| 27 | + |
| 28 | +def test_create_access_token_prod_normal_expiry(): |
| 29 | + """Test that tokens in prod mode have normal expiry.""" |
| 30 | + mock_settings = MagicMock() |
| 31 | + mock_settings.is_dev = False |
| 32 | + mock_settings.access_token_expire_minutes = 60 |
| 33 | + mock_settings.secret_key = "test_secret" |
| 34 | + mock_settings.jwt_algorithm = "HS256" |
| 35 | + |
| 36 | + with patch("app.modules.auth.token.get_settings", return_value=mock_settings): |
| 37 | + token = create_access_token({"sub": "user@example.com"}) |
| 38 | + payload = jwt.decode(token, "test_secret", algorithms=["HS256"]) |
| 39 | + |
| 40 | + exp = payload["exp"] |
| 41 | + # Should be roughly 60 minutes from now |
| 42 | + expected_exp = (datetime.now(UTC) + timedelta(minutes=60)).timestamp() |
| 43 | + assert abs(exp - expected_exp) < 10 # Allow 10s difference |
| 44 | + |
| 45 | + |
| 46 | +def test_setup_test_users(db, test_settings): |
| 47 | + """Test that test users are created if they don't exist.""" |
| 48 | + # Ensure user doesn't exist in the current transaction |
| 49 | + primary_dev_user = test_settings.dev_users[0] |
| 50 | + user = db.query(User).filter(User.email == primary_dev_user["email"]).first() |
| 51 | + if user: |
| 52 | + db.delete(user) |
| 53 | + db.flush() |
| 54 | + |
| 55 | + setup_test_users(db, test_settings.dev_users, test_settings.dev_users_password) |
| 56 | + |
| 57 | + user = db.query(User).filter(User.email == primary_dev_user["email"]).first() |
| 58 | + assert user is not None |
| 59 | + assert user.email == primary_dev_user["email"] |
| 60 | + |
| 61 | + |
| 62 | +@patch("app.core.startup.seed_all") |
| 63 | +def test_auto_seed_data_empty_db(mock_seed_all, db): |
| 64 | + """Test that seeding is called when DB is empty.""" |
| 65 | + mock_seed_all.return_value = None |
| 66 | + auto_seed_data(db) |
| 67 | + mock_seed_all.assert_called_once() |
| 68 | + |
| 69 | + |
| 70 | +@patch("app.core.startup.seed_all") |
| 71 | +def test_auto_seed_data_already_seeded(mock_seed_all, db): |
| 72 | + """Test that seeding is skipped if DB already has data (simulated by HTTPException 405).""" |
| 73 | + mock_seed_all.side_effect = HTTPException( |
| 74 | + status_code=405, detail="Action is only allowed on empty database" |
| 75 | + ) |
| 76 | + |
| 77 | + # This should not raise an exception, just log and return |
| 78 | + auto_seed_data(db) |
| 79 | + mock_seed_all.assert_called_once() |
| 80 | + |
| 81 | + |
| 82 | +def test_run_startup_tasks_dev_calls_subtasks(db): |
| 83 | + """Test that all dev startup tasks are triggered in dev mode.""" |
| 84 | + mock_settings = MagicMock() |
| 85 | + mock_settings.is_dev = True |
| 86 | + mock_settings.is_demo = False |
| 87 | + mock_settings.dev_users = [{"email": "user@example.com"}] |
| 88 | + mock_settings.dev_users_password = "password" |
| 89 | + |
| 90 | + with ( |
| 91 | + patch("app.core.startup.setup_test_users") as mock_setup_users, |
| 92 | + patch("app.core.startup.auto_seed_data") as mock_auto_seed, |
| 93 | + ): |
| 94 | + run_startup_tasks(db, mock_settings) |
| 95 | + mock_setup_users.assert_called_once_with( |
| 96 | + db, mock_settings.dev_users, mock_settings.dev_users_password |
| 97 | + ) |
| 98 | + mock_auto_seed.assert_called_once_with(db) |
0 commit comments