Files
RaptDramaDump/tests/test_huangdou_coins.py

145 lines
6.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from server.services.huangdou.client import normalize_host
from server.services.huangdou.coins import allow_payment, episode_note, keep_episode, paid_cost, public_lock_reason, summarize
def test_paid_cost_sums_only_unbought_coin_episodes() -> None:
rows = [
{"type": "free", "price": 0, "is_buy": False},
{"type": "vip", "price": 0, "is_buy": False},
{"type": "coin", "price": 10, "is_buy": False},
{"type": "points", "price": 5, "is_buy": False},
{"type": "coin", "price": 8, "is_buy": True},
{"type": "coin", "price": 7, "is_buy": None},
{"type": "coin", "price": 9, "is_buy": "n"},
{"type": "coin", "price": 4, "is_buy": "y"},
]
assert paid_cost(rows[2]) == 10
assert paid_cost(rows[3]) == 5
assert paid_cost(rows[4]) == 0
assert paid_cost(rows[5]) == 0
assert paid_cost(rows[6]) == 9
assert paid_cost(rows[7]) == 0
assert summarize(rows)["coins"] == 24
assert summarize(rows)["coin_count"] == 3
assert summarize(rows)["vip_count"] == 1
def test_payment_and_episode_choice() -> None:
free = {"type": "free", "price": 0, "is_buy": True}
vip = {"type": "vip", "price": 0, "is_buy": False}
coin = {"type": "coin", "price": 12, "is_buy": False}
assert allow_payment(True, True, 20, 12) is True
assert allow_payment(True, True, 11, 12) is False
assert allow_payment(False, True, 20, 12) is False
assert keep_episode(free, allow_paid=False, account_vip=False) is True
assert keep_episode(vip, allow_paid=False, account_vip=False) is False
assert keep_episode(vip, allow_paid=False, account_vip=True) is True
assert keep_episode(coin, allow_paid=False, account_vip=True) is False
assert keep_episode(coin, allow_paid=True, account_vip=True) is True
bought = {"type": "coin", "price": 12, "is_buy": True}
priced_vip = {"type": "vip", "price": 6, "is_buy": False}
assert keep_episode(bought, allow_paid=False, account_vip=False) is True
assert keep_episode(priced_vip, allow_paid=False, account_vip=True) is False
assert episode_note(priced_vip, account_vip=True) == (False, "6 积分", 6)
assert episode_note(bought, account_vip=False)[0] is True
assert public_lock_reason(
{"type": "vip", "price": 6, "is_buy": False},
"创作者作品需单独解锁(会员权益不含创作者作品)",
) == "6 积分"
assert public_lock_reason(bought, "创作者作品需单独解锁(会员权益不含创作者作品)") == "这一集暂时不能播放"
def test_account_login_reuses_the_same_device_fingerprint() -> None:
import tempfile
from pathlib import Path
from unittest.mock import patch
from server.services.huangdou import client as client_module
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "session.json"
with patch.object(client_module, "SESSION_PATH", path):
first = client_module.HuangdouClient()
device_id = first.device_id
session_id = first.session_id
info = dict(first.device_info)
assert device_id
assert session_id
assert info.get("userAgent")
guest_device, guest_session = first._identity("guest", False)[1:3]
assert guest_device
assert guest_device != device_id
assert guest_session != session_id
assert first._identity("account", False)[1] == device_id
again_device, again_session = first._identity("guest", False)[1:3]
assert again_device == guest_device
assert again_session == guest_session
first.roll_guest_fingerprint()
rolled_device = first._identity("guest", False)[1]
assert rolled_device != guest_device
assert first.device_id == device_id
first.roll_account_fingerprint()
assert first.device_id != device_id
assert first.session_id != session_id
assert first._identity("account", False)[1] == first.device_id
account_device = first.device_id
account_session = first.session_id
first.set_host("https://example.com")
assert first.device_id == account_device
second = client_module.HuangdouClient()
assert second.device_id == account_device
assert second.session_id == account_session
assert second._identity("account", True)[1] == account_device
assert second.guest_device_id == rolled_device
def test_saved_accounts_roundtrip() -> None:
import tempfile
from pathlib import Path
from unittest.mock import patch
from server.services.huangdou import accounts as accounts_module
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "accounts.json"
with patch.object(accounts_module, "PATH", path):
saved = accounts_module.replace(
[
{"username": "13800000001", "password": "secret-one", "nickname": "甲"},
{"username": " 13800000001 ", "password": "secret-one", "nickname": "重复"},
{"username": "", "password": "x", "nickname": ""},
]
)
assert [item["username"] for item in saved] == ["13800000001"]
assert accounts_module.load()[0]["nickname"] == "甲"
assert accounts_module.load()[0]["password"] == "secret-one"
def test_sign_only_when_today_is_open() -> None:
from server.services.huangdou.login import _sign_message, _signed_today
assert not _signed_today(None)
assert not _signed_today("n")
assert not _signed_today("0")
assert _signed_today("y")
assert _signed_today("Y")
assert _signed_today(True)
assert _sign_message({"prize_name": "金币", "prize_num": 10}) == "签到成功,获得 金币 × 10"
assert _sign_message({"msg": "已经签过了"}) == "已经签过了"
def test_host_keeps_scheme() -> None:
assert normalize_host("pmihlvee.cc") == "https://pmihlvee.cc"
assert normalize_host("https://example.com/") == "https://example.com"
assert normalize_host("") == ""
if __name__ == "__main__":
test_paid_cost_sums_only_unbought_coin_episodes()
test_payment_and_episode_choice()
test_account_login_reuses_the_same_device_fingerprint()
test_saved_accounts_roundtrip()
test_sign_only_when_today_is_open()
test_host_keeps_scheme()
print("ok")