Skip to content

Commit dc0b5c5

Browse files
committed
Refactor tests to use 'API_KEYS' for configuration and update header references for consistency
1 parent 7e1d5bb commit dc0b5c5

4 files changed

Lines changed: 24 additions & 30 deletions

File tree

test/quick_test.py

Whitespace-only changes.

test/test_api.py

Whitespace-only changes.

tests/quick_test.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
from __future__ import annotations
22

3-
import os # <-- DITAMBAHKAN
3+
import os
44
import json
55
from api.services.cevs_aggregator import compute_cevs_for_company
66
from flask import Flask
77
from api.api_server import app as flask_app
88

99

1010
def test_global_edgar_endpoint_smoke(monkeypatch):
11-
# Use Flask test client to avoid needing a running server
11+
# PERBAIKAN FINAL: Memuat daftar kunci valid ke dalam konfigurasi aplikasi
12+
# sebelum membuat test client.
13+
flask_app.config['API_KEYS'] = os.getenv('API_KEYS')
1214
client = flask_app.test_client()
1315

14-
# Ambil API key dari environment variable
15-
# PERBAIKAN: Menggunakan nama secret yang benar 'TEST_API_KEY'
1616
api_key = os.getenv('TEST_API_KEY')
17-
# PENTING: Ganti 'X-API-KEY' jika nama header Anda berbeda
18-
headers = {'X-API-KEY': api_key}
17+
headers = {'X-API-Key': api_key}
1918

20-
resp = client.get("/global/edgar?country=United%20States&pollutant=PM2.5&window=3", headers=headers) # <-- DITAMBAHKAN headers
19+
resp = client.get("/global/edgar?country=United%20States&pollutant=PM2.5&window=3", headers=headers)
2120

22-
# Karena API Key sudah valid, status code tidak akan 401 lagi.
23-
# Tes ini sekarang memeriksa apakah statusnya 200 (sukses) atau 400 (jika ada parameter salah)
2421
assert resp.status_code in (200, 400)
2522
data = resp.get_json()
2623
assert isinstance(data, dict)
@@ -29,12 +26,10 @@ def test_global_edgar_endpoint_smoke(monkeypatch):
2926
assert "series" in data and isinstance(data["series"], list)
3027
assert "trend" in data and isinstance(data["trend"], dict)
3128

32-
# Bagian di bawah ini untuk testing lokal dan tidak dijalankan oleh pytest
33-
# jadi tidak perlu diubah.
29+
# Bagian di bawah ini tidak perlu diubah
3430
"""
3531
Quick test script untuk API tanpa perlu terminal interaktif
3632
"""
37-
3833
import requests
3934

4035

tests/test_global_routes.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
from __future__ import annotations
66

7-
import os # <-- DITAMBAHKAN
7+
import os
88
import pytest
99
from flask import Flask
1010
from api.api_server import app as flask_app
@@ -16,22 +16,22 @@ class TestGlobalRoutes:
1616
@pytest.fixture
1717
def client(self):
1818
"""Flask test client for making API requests."""
19+
# PERBAIKAN FINAL: Memuat daftar kunci valid ke dalam konfigurasi aplikasi
20+
# sebelum membuat test client.
21+
flask_app.config['API_KEYS'] = os.getenv('API_KEYS')
1922
return flask_app.test_client()
2023

2124
@pytest.fixture
2225
def auth_headers(self):
2326
"""Fixture to create authentication headers from environment variable."""
24-
# PERBAIKAN: Menggunakan nama secret yang benar 'TEST_API_KEY'
2527
api_key = os.getenv('TEST_API_KEY')
2628
if not api_key:
27-
# PERBAIKAN: Pesan error disesuaikan
2829
pytest.fail("TEST_API_KEY environment variable not set. Please set it in GitHub Secrets.")
29-
# PENTING: Ganti 'X-API-KEY' jika nama header Anda berbeda
30-
return {'X-API-KEY': api_key}
30+
return {'X-API-Key': api_key}
3131

3232
def test_global_emissions_basic_response(self, client, auth_headers):
3333
"""Test /global/emissions returns 200 and proper structure."""
34-
resp = client.get("/global/emissions?limit=5", headers=auth_headers) # <-- DITAMBAHKAN headers
34+
resp = client.get("/global/emissions?limit=5", headers=auth_headers)
3535
assert resp.status_code == 200
3636
data = resp.get_json()
3737
assert data["status"] == "success"
@@ -41,7 +41,7 @@ def test_global_emissions_basic_response(self, client, auth_headers):
4141

4242
def test_global_emissions_with_filters(self, client, auth_headers):
4343
"""Test /global/emissions with state and year filters."""
44-
resp = client.get("/global/emissions?state=TX&year=2023&limit=3", headers=auth_headers) # <-- DITAMBAHKAN headers
44+
resp = client.get("/global/emissions?state=TX&year=2023&limit=3", headers=auth_headers)
4545
assert resp.status_code == 200
4646
data = resp.get_json()
4747
assert data["status"] == "success"
@@ -50,7 +50,7 @@ def test_global_emissions_with_filters(self, client, auth_headers):
5050

5151
def test_global_emissions_stats(self, client, auth_headers):
5252
"""Test /global/emissions/stats returns aggregated statistics."""
53-
resp = client.get("/global/emissions/stats", headers=auth_headers) # <-- DITAMBAHKAN headers
53+
resp = client.get("/global/emissions/stats", headers=auth_headers)
5454
assert resp.status_code == 200
5555
data = resp.get_json()
5656
assert data["status"] == "success"
@@ -63,7 +63,7 @@ def test_global_emissions_stats(self, client, auth_headers):
6363

6464
def test_global_iso_basic_response(self, client, auth_headers):
6565
"""Test /global/iso returns 200 and proper structure."""
66-
resp = client.get("/global/iso?limit=10", headers=auth_headers) # <-- DITAMBAHKAN headers
66+
resp = client.get("/global/iso?limit=10", headers=auth_headers)
6767
assert resp.status_code == 200
6868
data = resp.get_json()
6969
assert data["status"] == "success"
@@ -72,15 +72,15 @@ def test_global_iso_basic_response(self, client, auth_headers):
7272

7373
def test_global_iso_with_country_filter(self, client, auth_headers):
7474
"""Test /global/iso with country filter."""
75-
resp = client.get("/global/iso?country=Germany&limit=5", headers=auth_headers) # <-- DITAMBAHKAN headers
75+
resp = client.get("/global/iso?country=Germany&limit=5", headers=auth_headers)
7676
assert resp.status_code == 200
7777
data = resp.get_json()
7878
assert data["status"] == "success"
7979
assert data["filters"]["country"] == "Germany"
8080

8181
def test_global_eea_basic_response(self, client, auth_headers):
8282
"""Test /global/eea returns 200 and proper structure."""
83-
resp = client.get("/global/eea?limit=10", headers=auth_headers) # <-- DITAMBAHKAN headers
83+
resp = client.get("/global/eea?limit=10", headers=auth_headers)
8484
assert resp.status_code == 200
8585
data = resp.get_json()
8686
assert data["status"] == "success"
@@ -89,7 +89,7 @@ def test_global_eea_basic_response(self, client, auth_headers):
8989

9090
def test_global_eea_with_filters(self, client, auth_headers):
9191
"""Test /global/eea with indicator, country, and year filters."""
92-
resp = client.get("/global/eea?country=Sweden&indicator=GHG&year=2023&limit=5", headers=auth_headers) # <-- DITAMBAHKAN headers
92+
resp = client.get("/global/eea?country=Sweden&indicator=GHG&year=2023&limit=5", headers=auth_headers)
9393
assert resp.status_code == 200
9494
data = resp.get_json()
9595
assert data["status"] == "success"
@@ -100,7 +100,7 @@ def test_global_eea_with_filters(self, client, auth_headers):
100100

101101
def test_global_cevs_basic_company(self, client, auth_headers):
102102
"""Test /global/cevs/{company} returns proper CEVS score structure."""
103-
resp = client.get("/global/cevs/Green%20Energy%20Corp", headers=auth_headers) # <-- DITAMBAHKAN headers
103+
resp = client.get("/global/cevs/Green%20Energy%20Corp", headers=auth_headers)
104104
assert resp.status_code == 200
105105
data = resp.get_json()
106106
assert data["status"] == "success"
@@ -116,7 +116,7 @@ def test_global_cevs_basic_company(self, client, auth_headers):
116116

117117
def test_global_cevs_with_country(self, client, auth_headers):
118118
"""Test /global/cevs/{company} with country parameter for enhanced scoring."""
119-
resp = client.get("/global/cevs/Swedish%20Wind%20Power?country=Sweden", headers=auth_headers) # <-- DITAMBAHKAN headers
119+
resp = client.get("/global/cevs/Swedish%20Wind%20Power?country=Sweden", headers=auth_headers)
120120
assert resp.status_code == 200
121121
data = resp.get_json()
122122
assert data["status"] == "success"
@@ -128,7 +128,7 @@ def test_global_cevs_with_country(self, client, auth_headers):
128128

129129
def test_global_edgar_basic_response(self, client, auth_headers):
130130
"""Test /global/edgar returns proper structure (may have empty data if file missing)."""
131-
resp = client.get("/global/edgar?country=United%20States&pollutant=PM2.5", headers=auth_headers) # <-- DITAMBAHKAN headers
131+
resp = client.get("/global/edgar?country=United%20States&pollutant=PM2.5", headers=auth_headers)
132132
assert resp.status_code == 200
133133
data = resp.get_json()
134134
assert data["status"] == "success"
@@ -141,16 +141,15 @@ def test_global_edgar_basic_response(self, client, auth_headers):
141141

142142
def test_global_edgar_missing_country(self, client, auth_headers):
143143
"""Test /global/edgar returns 400 when country parameter is missing."""
144-
resp = client.get("/global/edgar?pollutant=NOx", headers=auth_headers) # <-- DITAMBAHKAN headers
145-
# API Key valid, jadi errornya bukan 401, melainkan 400 karena parameter tidak lengkap
144+
resp = client.get("/global/edgar?pollutant=NOx", headers=auth_headers)
146145
assert resp.status_code == 400
147146
data = resp.get_json()
148147
assert data["status"] == "error"
149148
assert "country is required" in data["message"]
150149

151150
def test_global_edgar_with_window_parameter(self, client, auth_headers):
152151
"""Test /global/edgar with custom trend window parameter."""
153-
resp = client.get("/global/edgar?country=Germany&pollutant=NOx&window=5", headers=auth_headers) # <-- DITAMBAHKAN headers
152+
resp = client.get("/global/edgar?country=Germany&pollutant=NOx&window=5", headers=auth_headers)
154153
assert resp.status_code == 200
155154
data = resp.get_json()
156155
assert data["status"] == "success"

0 commit comments

Comments
 (0)