Files
SmartUp/backend/test_upstream_auth_headers.py
T

61 lines
1.8 KiB
Python

import pytest
from app.services.upstream_client import UpstreamClient, UpstreamError
def test_new_api_token_headers_include_authorization_and_new_api_user():
client = UpstreamClient(
base_url="http://newapi.local",
api_prefix="",
auth_type="new_api_token",
auth_config={"token": "access-token", "user_id": "7"},
)
headers = client._headers()
assert headers["Authorization"] == "Bearer access-token"
assert headers["New-Api-User"] == "7"
assert "Nox-Api-User" not in headers
def test_new_api_token_rejects_dashboard_jwt():
client = UpstreamClient(
base_url="http://newapi.local",
api_prefix="",
auth_type="new_api_token",
auth_config={"token": "a" * 1200 + ".payload.signature", "user_id": "7"},
)
with pytest.raises(UpstreamError, match="面板登录 JWT"):
client._headers()
def test_nox_token_headers_include_authorization_and_nox_api_user():
client = UpstreamClient(
base_url="http://nox.local",
api_prefix="",
auth_type="nox_token",
auth_config={"token": "nox-access-token", "user_id": "9"},
)
headers = client._headers()
assert headers["Authorization"] == "Bearer nox-access-token"
assert headers["Nox-Api-User"] == "9"
assert "New-Api-User" not in headers
def test_bearer_headers_do_not_include_new_or_nox_user_headers():
client = UpstreamClient(
base_url="http://sub2api.local",
api_prefix="/api/v1",
auth_type="bearer",
auth_config={"token": "sk-sub2api", "user_id": "11", "new_api_user": "11"},
)
headers = client._headers()
assert headers["Authorization"] == "Bearer sk-sub2api"
assert "New-Api-User" not in headers
assert "Nox-Api-User" not in headers