fix: harden redeem link flow
This commit is contained in:
1 parent
3ab15e69f9
commit
cf2b71f831
5 files changed
+174
-10
No files matched your search
@@ -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"
|
||||
@@ -155,3 +155,34 @@ def test_license_server_redeem_token_rejects_expired_token(tmp_path, monkeypatch
|
||||
assert result.ok is False
|
||||
assert result.reason == "expired"
|
||||
assert service.stats()["total"] == 0
|
||||
|
||||
|
||||
def test_license_server_redeem_token_is_single_use_under_concurrency(tmp_path, monkeypatch):
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import time
|
||||
|
||||
from license_server import core
|
||||
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
|
||||
|
||||
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
|
||||
token = service.create_redeem_token(
|
||||
days=7,
|
||||
max_devices=1,
|
||||
customer="race-order",
|
||||
platform="xianyu",
|
||||
external_order_id="ORDER-RACE",
|
||||
)
|
||||
original_insert_card = service._insert_card
|
||||
|
||||
def slow_insert(*args, **kwargs):
|
||||
time.sleep(0.2)
|
||||
return original_insert_card(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(service, "_insert_card", slow_insert)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(lambda _idx: service.claim_redeem_token(token.token), range(2)))
|
||||
|
||||
assert sorted(result.reason for result in results) == ["already_redeemed", "ok"]
|
||||
assert sum(1 for result in results if result.ok) == 1
|
||||
assert service.stats()["total"] == 1
|
||||
Reference in new issue
Block a user