Skip to content

Commit 21ef0ea

Browse files
committed
Fix analytics Python tests: add INTERNAL_JWT_SECRET env var + auth token fixture
1 parent 50b2598 commit 21ef0ea

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ jobs:
140140
elif [[ "${{ matrix.service }}" == "analytics-python-service" ]]; then
141141
echo "POSTGRES_USER=test" >> $GITHUB_ENV
142142
echo "POSTGRES_PASSWORD=test" >> $GITHUB_ENV
143+
echo "INTERNAL_JWT_SECRET=test-secret" >> $GITHUB_ENV
143144
fi
144145
- name: Test Python
145146
run: pytest -v

services/analytics-python-service/test_main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
"""Tests for the Analytics Service API using FastAPI TestClient."""
22

3+
import base64
4+
import hashlib
5+
import hmac
6+
import json
7+
import time
8+
39
import pytest
410
from httpx import ASGITransport, AsyncClient
511
from main import app
612

13+
INTERNAL_JWT_SECRET = "test-secret"
14+
15+
16+
def _make_internal_token(secret: str) -> str:
17+
header = base64.urlsafe_b64encode(json.dumps({"alg": "HS256", "typ": "JWT"}).encode()).rstrip(b"=").decode()
18+
payload = base64.urlsafe_b64encode(json.dumps({"sub": "test", "exp": int(time.time()) + 3600}).encode()).rstrip(b"=").decode()
19+
sig = base64.urlsafe_b64encode(
20+
hmac.new(secret.encode(), f"{header}.{payload}".encode(), hashlib.sha256).digest()
21+
).rstrip(b"=").decode()
22+
return f"{header}.{payload}.{sig}"
23+
724

825
@pytest.fixture
926
def client():
1027
transport = ASGITransport(app=app)
11-
return AsyncClient(transport=transport, base_url="http://test")
28+
token = _make_internal_token(INTERNAL_JWT_SECRET)
29+
return AsyncClient(transport=transport, base_url="http://test", headers={"x-internal-auth": token})
1230

1331

1432
@pytest.mark.asyncio

0 commit comments

Comments
 (0)