Skip to content

Commit 83230bb

Browse files
committed
Refactor test setup to ensure API keys are loaded from environment variables and handle missing keys gracefully
1 parent 5affe97 commit 83230bb

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

tests/quick_test.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
import json
55
from api.services.cevs_aggregator import compute_cevs_for_company
66
from flask import Flask
7-
from api.api_server import app as flask_app, load_api_keys
7+
from api.api_server import app as flask_app
88

99

1010
def test_global_edgar_endpoint_smoke(monkeypatch):
11-
# Langkah 1: Atur konfigurasi dengan string kunci mentah dari environment
12-
flask_app.config['API_KEYS'] = os.getenv('API_KEYS')
13-
# Langkah 2 (KRITIS): Panggil kembali fungsi parsing untuk memperbarui daftar kunci yang valid
14-
load_api_keys(flask_app)
15-
client = flask_app.test_client()
16-
11+
"""
12+
Smoke test for the /global/edgar endpoint.
13+
Uses monkeypatch to ensure API keys are loaded correctly.
14+
"""
15+
# Ambil kunci dari environment yang disediakan oleh CI/workflow
16+
api_keys_str = os.getenv('API_KEYS')
1717
api_key = os.getenv('TEST_API_KEY')
18+
if not api_keys_str or not api_key:
19+
pytest.fail("API_KEYS or TEST_API_KEY environment variables not set in CI.")
20+
21+
# Gunakan monkeypatch untuk mengatur environment variable secara paksa untuk tes ini
22+
monkeypatch.setenv('API_KEYS', api_keys_str)
23+
24+
client = flask_app.test_client()
1825
headers = {'X-API-KEY': api_key}
1926

2027
resp = client.get("/global/edgar?country=United%20States&pollutant=PM2.5&window=3", headers=headers)

tests/test_global_routes.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77
import os
88
import pytest
99
from flask import Flask
10-
from api.api_server import app as flask_app, load_api_keys
10+
from api.api_server import app as flask_app
1111

1212

1313
class TestGlobalRoutes:
1414
"""Test all /global/* endpoints for basic functionality and response structure."""
1515

1616
@pytest.fixture
17-
def client(self):
18-
"""Flask test client for making API requests."""
19-
# Langkah 1: Atur konfigurasi dengan string kunci mentah dari environment
20-
flask_app.config['API_KEYS'] = os.getenv('API_KEYS')
21-
# Langkah 2 (KRITIS): Panggil kembali fungsi parsing untuk memperbarui daftar kunci yang valid
22-
load_api_keys(flask_app)
17+
def client(self, monkeypatch):
18+
"""
19+
Flask test client with patched environment variables.
20+
This ensures the app sees the correct API keys during tests.
21+
"""
22+
# Ambil kunci dari environment yang disediakan oleh CI/workflow
23+
api_keys_str = os.getenv('API_KEYS')
24+
if not api_keys_str:
25+
pytest.fail("API_KEYS environment variable not set in CI.")
26+
27+
# Gunakan monkeypatch untuk mengatur environment variable secara paksa untuk tes ini
28+
monkeypatch.setenv('API_KEYS', api_keys_str)
29+
30+
# Sekarang, aman untuk membuat test client karena environment sudah benar
2331
return flask_app.test_client()
2432

2533
@pytest.fixture

0 commit comments

Comments
 (0)