[AZ-180] Fix pre-existing test failures: patch JWT_SECRET in auth-dependent tests

Tests expecting file storage (image/video write) failed because JWT_SECRET was
not set in the test environment, causing require_auth to return "" (falsy),
skipping the storage block. Both tests now patch main.JWT_SECRET and use a
properly signed JWT.

Made-with: Cursor
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-04-02 07:23:10 +03:00
parent 2149cd6c08
commit 2ed9ce3336
2 changed files with 17 additions and 25 deletions
+4 -7
View File
@@ -14,19 +14,15 @@ pytest.importorskip("inference")
import inference as inference_mod import inference as inference_mod
_TEST_JWT_SECRET = "az-test-secret-for-unit-tests-only-32b"
def _access_jwt(sub: str = "u1") -> str: def _access_jwt(sub: str = "u1") -> str:
secret = os.environ.get("JWT_SECRET", "")
if secret:
return pyjwt.encode( return pyjwt.encode(
{"exp": int(time.time()) + 3600, "sub": sub}, {"exp": int(time.time()) + 3600, "sub": sub},
secret, _TEST_JWT_SECRET,
algorithm="HS256", algorithm="HS256",
) )
import base64, json
raw = json.dumps({"exp": int(time.time()) + 3600, "sub": sub}, separators=(",", ":")).encode()
payload = base64.urlsafe_b64encode(raw).decode().rstrip("=")
return f"h.{payload}.s"
def _jpeg_bytes() -> bytes: def _jpeg_bytes() -> bytes:
@@ -73,6 +69,7 @@ def test_auth_image_still_writes_once_before_detect(reset_main_inference):
expected_path = os.path.abspath(os.path.join(idir, f"{content_hash}.jpg")) expected_path = os.path.abspath(os.path.join(idir, f"{content_hash}.jpg"))
client = TestClient(main.app) client = TestClient(main.app)
with ( with (
patch.object(main, "JWT_SECRET", _TEST_JWT_SECRET),
patch.object(builtins, "open", tracking_open), patch.object(builtins, "open", tracking_open),
patch.object(main.http_requests, "post", mock_post), patch.object(main.http_requests, "post", mock_post),
patch.object(main.http_requests, "put", mock_put), patch.object(main.http_requests, "put", mock_put),
+5 -10
View File
@@ -1,6 +1,4 @@
import asyncio import asyncio
import base64
import json
import os import os
import tempfile import tempfile
import threading import threading
@@ -275,20 +273,16 @@ class TestMediaContentHashFromFile:
os.unlink(path) os.unlink(path)
_TEST_JWT_SECRET = "az-test-secret-for-unit-tests-only-32b"
def _access_jwt(sub: str = "u1") -> str: def _access_jwt(sub: str = "u1") -> str:
import jwt as pyjwt import jwt as pyjwt
secret = os.environ.get("JWT_SECRET", "")
if secret:
return pyjwt.encode( return pyjwt.encode(
{"exp": int(time.time()) + 3600, "sub": sub}, {"exp": int(time.time()) + 3600, "sub": sub},
secret, _TEST_JWT_SECRET,
algorithm="HS256", algorithm="HS256",
) )
raw = json.dumps(
{"exp": int(time.time()) + 3600, "sub": sub}, separators=(",", ":")
).encode()
payload = base64.urlsafe_b64encode(raw).decode().rstrip("=")
return f"h.{payload}.s"
class _FakeInfStream: class _FakeInfStream:
@@ -337,6 +331,7 @@ class TestDetectVideoEndpoint:
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
client = TestClient(main.app) client = TestClient(main.app)
with ( with (
patch.object(main, "JWT_SECRET", _TEST_JWT_SECRET),
patch.object(main, "get_inference", return_value=_FakeInfStream()), patch.object(main, "get_inference", return_value=_FakeInfStream()),
patch.object(main.http_requests, "post", mock_post), patch.object(main.http_requests, "post", mock_post),
patch.object(main.http_requests, "put", mock_put), patch.object(main.http_requests, "put", mock_put),