mirror of
https://github.com/azaion/detections.git
synced 2026-04-22 07:06:32 +00:00
[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:
@@ -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", "")
|
return pyjwt.encode(
|
||||||
if secret:
|
{"exp": int(time.time()) + 3600, "sub": sub},
|
||||||
return pyjwt.encode(
|
_TEST_JWT_SECRET,
|
||||||
{"exp": int(time.time()) + 3600, "sub": sub},
|
algorithm="HS256",
|
||||||
secret,
|
)
|
||||||
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),
|
||||||
|
|||||||
@@ -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", "")
|
return pyjwt.encode(
|
||||||
if secret:
|
{"exp": int(time.time()) + 3600, "sub": sub},
|
||||||
return pyjwt.encode(
|
_TEST_JWT_SECRET,
|
||||||
{"exp": int(time.time()) + 3600, "sub": sub},
|
algorithm="HS256",
|
||||||
secret,
|
)
|
||||||
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),
|
||||||
|
|||||||
Reference in New Issue
Block a user