fix: auth 测试改用 monkeypatch.setattr,MarkdownSplitter 跳过代码块内标题识别
This commit is contained in:
+4
-7
@@ -15,35 +15,32 @@ class TestVerifyApiKey:
|
||||
"""未设置环境变量时跳过认证."""
|
||||
monkeypatch.setenv("MD_VECTOR_API_KEY", "")
|
||||
import src.server.auth as auth
|
||||
auth._get_expected_api_key = lambda: ""
|
||||
monkeypatch.setattr(auth, "_get_expected_api_key", lambda: "")
|
||||
result = verify_api_key(x_api_key=None)
|
||||
assert result is True
|
||||
|
||||
def test_rejects_when_key_required_but_not_provided(self, monkeypatch):
|
||||
"""已设置密钥但请求未提供."""
|
||||
import src.server.auth as auth
|
||||
auth._get_expected_api_key = lambda: "secret123"
|
||||
monkeypatch.setattr(auth, "_get_expected_api_key", lambda: "secret123")
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
verify_api_key(x_api_key=None)
|
||||
assert exc.value.status_code == 401
|
||||
auth._get_expected_api_key = auth._make_get_api_key()
|
||||
|
||||
def test_rejects_wrong_key(self, monkeypatch):
|
||||
"""错误的密钥被拒绝."""
|
||||
import src.server.auth as auth
|
||||
auth._get_expected_api_key = lambda: "secret123"
|
||||
monkeypatch.setattr(auth, "_get_expected_api_key", lambda: "secret123")
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
verify_api_key(x_api_key="wrong-key")
|
||||
assert exc.value.status_code == 401
|
||||
auth._get_expected_api_key = auth._make_get_api_key()
|
||||
|
||||
def test_accepts_correct_key(self, monkeypatch):
|
||||
"""正确的密钥通过认证."""
|
||||
import src.server.auth as auth
|
||||
auth._get_expected_api_key = lambda: "secret123"
|
||||
monkeypatch.setattr(auth, "_get_expected_api_key", lambda: "secret123")
|
||||
result = verify_api_key(x_api_key="secret123")
|
||||
assert result is True
|
||||
auth._get_expected_api_key = auth._make_get_api_key()
|
||||
|
||||
|
||||
class TestRateLimiter:
|
||||
|
||||
Reference in New Issue
Block a user