fix: harden redeem link flow

This commit is contained in:
Your Name committed 2026-06-24 22:03:04 +08:00
1 parent 3ab15e69f9
commit cf2b71f831
5 files changed
+174 -10

No files matched your search

+50 -1
View File
@@ -431,7 +431,7 @@ def test_external_redeem_create_returns_claim_url_without_card_key(tmp_path):
assert resp.status_code == 200
assert body["ok"] is True
assert body["claim_url"].startswith("https://dy.absfan.cn/redeem/")
assert body["claim_url"].startswith("https://dy.absfan.cn/redeem#")
assert "card_key" not in body
assert service.stats()["total"] == 0
@@ -461,3 +461,52 @@ def test_redeem_claim_exchanges_token_for_card_once(tmp_path):
assert first.json()["card_key"].startswith("DG-")
assert second.status_code == 409
assert second.json()["reason"] == "already_redeemed"
def test_redeem_claim_is_rate_limited(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
monkeypatch.setattr(api, "REDEEM_CLAIM_RATE_LIMIT_MAX_REQUESTS", 2)
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service), base_url="https://dy.absfan.cn")
for _ in range(2):
assert client.post("/api/v1/redeem/claim", json={"token": "missing"}).status_code == 404
limited = client.post("/api/v1/redeem/claim", json={"token": "missing"})
assert limited.status_code == 429
assert limited.json()["reason"] == "rate_limited"
def test_external_redeem_auth_failures_are_rate_limited(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
monkeypatch.setattr(api, "EXTERNAL_REDEEM_AUTH_RATE_LIMIT_MAX_REQUESTS", 2)
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(
api.create_app(service=service, external_api_token="external-secret"),
base_url="https://dy.absfan.cn",
)
for _ in range(2):
assert client.post(
"/api/v1/redeem/external/create",
json={"days": 1},
).status_code == 403
limited = client.post(
"/api/v1/redeem/external/create",
json={"days": 1},
)
assert limited.status_code == 429
assert limited.json()["reason"] == "rate_limited"