44"""
55from __future__ import annotations
66
7- import os # <-- DITAMBAHKAN
7+ import os
88import pytest
99from flask import Flask
1010from 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