|
1 | 1 | import os
|
2 | 2 | from pathlib import Path
|
3 |
| -from unittest.mock import patch |
| 3 | +from unittest import mock |
4 | 4 |
|
5 | 5 | import pytest
|
6 | 6 | from fastapi.testclient import TestClient
|
7 | 7 | from sqlalchemy.ext.asyncio import async_sessionmaker
|
8 | 8 |
|
9 | 9 | from fastapi_app import create_app
|
10 | 10 | from fastapi_app.globals import global_storage
|
| 11 | +from tests.mocks import MockAzureCredential |
11 | 12 |
|
12 | 13 | POSTGRES_HOST = "localhost"
|
13 | 14 | POSTGRES_USERNAME = "admin"
|
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | @pytest.fixture(scope="session")
|
23 |
| -def setup_env(): |
24 |
| - os.environ["POSTGRES_HOST"] = POSTGRES_HOST |
25 |
| - os.environ["POSTGRES_USERNAME"] = POSTGRES_USERNAME |
26 |
| - os.environ["POSTGRES_DATABASE"] = POSTGRES_DATABASE |
27 |
| - os.environ["POSTGRES_PASSWORD"] = POSTGRES_PASSWORD |
28 |
| - os.environ["POSTGRES_SSL"] = POSTGRES_SSL |
29 |
| - os.environ["POSTGRESQL_DATABASE_URL"] = POSTGRESQL_DATABASE_URL |
30 |
| - os.environ["RUNNING_IN_PRODUCTION"] = "False" |
31 |
| - os.environ["OPENAI_API_KEY"] = "fakekey" |
| 24 | +def monkeypatch_session(): |
| 25 | + with pytest.MonkeyPatch.context() as monkeypatch_session: |
| 26 | + yield monkeypatch_session |
32 | 27 |
|
33 | 28 |
|
34 | 29 | @pytest.fixture(scope="session")
|
35 |
| -def mock_azure_credential(): |
36 |
| - """Mock the Azure credential for testing.""" |
37 |
| - with patch("azure.identity.DefaultAzureCredential", return_value=None): |
| 30 | +def mock_session_env(monkeypatch_session): |
| 31 | + """Mock the environment variables for testing.""" |
| 32 | + with mock.patch.dict(os.environ, clear=True): |
| 33 | + # Database |
| 34 | + monkeypatch_session.setenv("POSTGRES_HOST", POSTGRES_HOST) |
| 35 | + monkeypatch_session.setenv("POSTGRES_USERNAME", POSTGRES_USERNAME) |
| 36 | + monkeypatch_session.setenv("POSTGRES_DATABASE", POSTGRES_DATABASE) |
| 37 | + monkeypatch_session.setenv("POSTGRES_PASSWORD", POSTGRES_PASSWORD) |
| 38 | + monkeypatch_session.setenv("POSTGRES_SSL", POSTGRES_SSL) |
| 39 | + monkeypatch_session.setenv("POSTGRESQL_DATABASE_URL", POSTGRESQL_DATABASE_URL) |
| 40 | + monkeypatch_session.setenv("RUNNING_IN_PRODUCTION", "False") |
| 41 | + # Azure Subscription |
| 42 | + monkeypatch_session.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid") |
| 43 | + # OpenAI |
| 44 | + monkeypatch_session.setenv("AZURE_OPENAI_CHATGPT_MODEL", "gpt-35-turbo") |
| 45 | + monkeypatch_session.setenv("OPENAI_API_KEY", "fakekey") |
| 46 | + # Allowed Origin |
| 47 | + monkeypatch_session.setenv("ALLOWED_ORIGIN", "https://frontend.com") |
| 48 | + |
| 49 | + if os.getenv("AZURE_USE_AUTHENTICATION") is not None: |
| 50 | + monkeypatch_session.delenv("AZURE_USE_AUTHENTICATION") |
38 | 51 | yield
|
39 | 52 |
|
40 | 53 |
|
41 | 54 | @pytest.fixture(scope="session")
|
42 |
| -def app(setup_env, mock_azure_credential): |
| 55 | +def app(mock_session_env): |
43 | 56 | """Create a FastAPI app."""
|
44 | 57 | if not Path("src/static/").exists():
|
45 | 58 | pytest.skip("Please generate frontend files first!")
|
46 |
| - return create_app(is_testing=True) |
| 59 | + return create_app(testing=True) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="function") |
| 63 | +def mock_default_azure_credential(mock_session_env): |
| 64 | + """Mock the Azure credential for testing.""" |
| 65 | + with mock.patch("azure.identity.DefaultAzureCredential") as mock_default_azure_credential: |
| 66 | + mock_default_azure_credential.return_value = MockAzureCredential() |
| 67 | + yield mock_default_azure_credential |
47 | 68 |
|
48 | 69 |
|
49 | 70 | @pytest.fixture(scope="function")
|
50 |
| -def test_client(app): |
| 71 | +def test_client(monkeypatch, app, mock_default_azure_credential): |
51 | 72 | """Create a test client."""
|
52 | 73 |
|
53 | 74 | with TestClient(app) as test_client:
|
|
0 commit comments