File tree Expand file tree Collapse file tree
services/analytics-python-service Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+
39import pytest
410from httpx import ASGITransport , AsyncClient
511from 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
926def 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
You can’t perform that action at this time.
0 commit comments