Files
moonsource-qidian/tests/qidian/yuewen_qr_spec.lua
T
GarfieldandCopilot 0956063d06 ✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
2026-11-16 12:08:03 +08:00

336 lines
14 KiB
Lua

--[[--
moonsource_qidian.yuewen_qr:取码 → 轮询 → ywguid/ywkey 或跟跳 Set-Cookie。
过期、关窗、手机取消刷新不把迟到的登录态交给 on_cookie。
浏览器扩展绑定仍指向 book.qq.com。
@module tests.moonsource_qidian.yuewen_qr_spec
--]]
local Assert = require("support.assert")
local Stubs = require("support.stubs")
package.preload["json"] = function()
return { decode = require("support.json_stub").decode, encode = function() return "{}" end }
end
package.preload["http.timeout"] = function()
return { net = function() return 15 end }
end
local script, requested = {}, {}
local function respond(prefix, res) script[#script + 1] = { prefix = prefix, res = res } end
package.preload["http.request"] = function()
return {
request = function(opts, cb)
requested[#requested + 1] = opts
for i, item in ipairs(script) do
if opts.url:sub(1, #item.prefix) == item.prefix then
table.remove(script, i)
require("ui/uimanager"):nextTick(function()
if item.res.hook then item.res.hook() end
cb(item.res)
end)
return { cancel = function() end }
end
end
require("ui/uimanager"):nextTick(function() cb(nil, "no script") end)
return { cancel = function() end }
end,
header = function(res, name) return res and res.headers and res.headers[name] end,
headerAll = function(res, name)
local value = res and res.headers and res.headers[name]
return type(value) == "table" and value or (value and { value } or {})
end,
}
end
local shown = {}
local function widget(kind)
return function()
return { new = function(_, o)
o.kind = kind
function o:show() shown[#shown + 1] = self end
function o:close()
self.closed = true
if self.close_callback then self.close_callback() end
end
return o
end }
end
end
package.preload["ui.themes.widgets.qr"] = widget("qr")
package.preload["ui.themes.widgets.confirm"] = widget("confirm")
package.preload["ui.themes.widgets.toast"] = widget("toast")
package.preload["ui.themes.widgets.progress_dialog"] = widget("busy")
local QRLogin = require("moonsource_qidian.yuewen_qr")
local SPEC = {
app_id = "1450000220", area_id = "1", return_url = "https://book.qq.com/",
referer = "https://login.book.qq.com/login.html",
title = "QQ阅读扫码登录", caption = "用 QQ阅读或起点 App 扫一扫",
}
local QR_PREFIX = "https://ptlogin.yuewen.com/login/qrcode?"
local POLL_PREFIX = "https://ptlogin.yuewen.com/login/qrcodelogin?"
local function reset()
script, requested, shown = {}, {}, {}
end
local function find(kind)
for i = #shown, 1, -1 do if shown[i].kind == kind then return shown[i] end end
end
local function toastCount(text)
local n = 0
for i = 1, #shown do
if shown[i].kind == "toast" and shown[i].text == text then n = n + 1 end
end
return n
end
local function qrcode(session)
return { code = 200, body = '{"code":0,"data":{"sessionKey":"' .. session .. '"}}' }
end
local function poll(body, extra)
local res = { code = 200, body = body }
if extra then
for k, v in pairs(extra) do res[k] = v end
end
return res
end
do -- 扫码 URL 与官方同一组参数,顺序由 formEncode 排定
Assert.eq(QRLogin:new(SPEC, function() end):scanUrl("sk/1"),
"https://ptlogin.yuewen.com/mobile/scanlogin?appid=1450000220&areaid=1"
.. "&bookId=0&chapterId=0&sessionkey=sk%2F1")
end
do -- 等待是字符串 "0",成功后 JSON 里的 ywGuid/ywKey 排成 Cookie
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":-11019,"scanStatus":"0"}'))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","data":{"ywGuid":"g1","ywKey":"k1","ywOpenId":"o1"}}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(find("qr").text,
"https://ptlogin.yuewen.com/mobile/scanlogin?appid=1450000220&areaid=1"
.. "&bookId=0&chapterId=0&sessionkey=sk1")
Assert.eq(find("qr").caption, "用 QQ阅读或起点 App 扫一扫")
Assert.eq(got, "ywguid=g1; ywkey=k1; ywopenid=o1")
Assert.is_true(find("qr").closed)
Assert.eq(toastCount("已取消登录"), 0)
Assert.is_true(requested[1].allow_redirects == false)
Assert.is_true(requested[2].url:find("qrcode=sk1", 1, true) ~= nil)
Assert.len(script, 0)
end
do -- 数字 GUID 要完整十进制;LuaJIT tostring 的科学计数法会毁掉 Fock 身份
reset()
respond(QR_PREFIX, qrcode("long-guid"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":5,"data":{"ywGuid":1234567890123456,"ywKey":"key"}}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=1234567890123456; ywkey=key")
end
do -- 已扫码只提示一次,确认后仍把登录态交出去
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"1"}'))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":1}'))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","ywGuid":"g2","ywKey":"k2"}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=g2; ywkey=k2")
Assert.eq(toastCount("已扫码,请在手机上确认"), 1)
end
do -- 过期:重试框,不交登录态
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"4"}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.is_nil(got)
Assert.eq(find("confirm").text, "二维码已过期")
Assert.eq(find("confirm").ok_text, "重新获取")
Assert.eq(#requested, 2)
end
do -- 用户关掉二维码:迟到的成功不交登录态,也不再轮询
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","data":{"ywGuid":"late","ywKey":"no"}}', {
hook = function() find("qr"):close() end,
}))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.is_nil(got)
Assert.eq(#requested, 2, "关闭后不再轮询")
Assert.eq(toastCount("已取消登录"), 1)
end
do -- 只有 302url:跟跳收 Set-Cookie,相对 Location 拼回原 host;属性不当 cookie
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","data":{"302url":"https://ptlogin.yuewen.com/jump"}}'))
respond("https://ptlogin.yuewen.com/jump", {
code = 302, headers = { Location = "/done", ["Set-Cookie"] = "ywguid=g3; Path=/; HttpOnly" },
})
respond("https://ptlogin.yuewen.com/done", {
code = 200, headers = { ["Set-Cookie"] = "ywkey=k3; Path=/, ywopenid=o3; Secure" },
})
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=g3; ywkey=k3; ywopenid=o3")
Assert.is_nil(got and got:find("Path", 1, true))
Assert.len(script, 0)
end
do -- 非 https 跳转不跟,不交登录态
reset()
respond(QR_PREFIX, qrcode("sk1"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","data":{"302url":"http://evil.example/x"}}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.is_nil(got)
Assert.eq(find("confirm").text, "登录失败:未获取到登录态")
Assert.eq(#requested, 2)
end
do -- 已拿到临时凭据仍须走 302url,不能把跨站身份当本站会话保存
reset()
respond(QR_PREFIX, qrcode("exchange"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":5,"data":{"ywGuid":"temporary","ywKey":"old",'
.. '"302url":"https://ptlogin.yuewen.com/exchange"}}'))
respond("https://ptlogin.yuewen.com/exchange", {
code = 302, headers = { Location = "https://m.qidian.com/finish",
["Set-Cookie"] = { "ywguid=intermediate; Path=/", "ywkey=intermediate; Path=/" } },
})
respond("https://m.qidian.com/finish", {
code = 200, headers = { ["Set-Cookie"] = { "ywguid=321; Path=/", "ywkey=web; Path=/" } },
})
local spec = { app_id = "13", area_id = "31", return_url = "https://m.qidian.com/", ticket = "1" }
local got
QRLogin:new(spec, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=321; ywkey=web")
Assert.len(requested, 4)
Assert.is_nil(requested[3].headers.Cookie, "第三方扫码域不带书源 Cookie")
Assert.matches(requested[1].url, "ticket=1")
end
do -- 官网 ticket 路径必须兑换,本站回包替换扫码 JSON 中的身份
reset()
respond(QR_PREFIX, qrcode("ticket"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":5,"data":{"ticket":"one-use","ywGuid":"temporary","ywKey":"old"}}'))
respond("https://m.qidian.com/?ticket=", {
code = 200, headers = { ["Set-Cookie"] = { "ywguid=456; Path=/", "ywkey=web; Path=/" } },
})
local spec = { app_id = "13", area_id = "31", return_url = "https://m.qidian.com/", ticket = "1" }
local got
QRLogin:new(spec, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=456; ywkey=web")
Assert.len(requested, 3)
end
do -- ticket=1 时忽略裸 302url,强制带 ticket 兑换;否则会吃到空壳首页却「登录成功」
reset()
respond(QR_PREFIX, qrcode("bare-home"))
respond(POLL_PREFIX, poll(
'{"code":0,"scanStatus":5,"data":{"ticket":"t1","ywGuid":"temporary","ywKey":"old",'
.. '"302url":"https://m.qidian.com/"}}'))
respond("https://m.qidian.com/?ticket=", {
code = 200, headers = { ["Set-Cookie"] = { "ywguid=789; Path=/", "ywkey=web; Path=/" } },
})
local spec = { app_id = "13", area_id = "31", return_url = "https://m.qidian.com/", ticket = "1" }
local got
QRLogin:new(spec, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=789; ywkey=web")
Assert.matches(requested[3].url, "ticket=t1")
Assert.is_nil(requested[3].url:match("^https://m%.qidian%.com/?$"))
end
do -- 本站常常不 Set-Cookie ywkey(只有 abPolicies);ticket 落到本站且 JSON 身份齐全就算完成
reset()
respond(QR_PREFIX, qrcode("no-set-cookie"))
respond(POLL_PREFIX, poll(
'{"code":0,"scanStatus":5,"data":{"ticket":"t2","ywGuid":"temporary","ywKey":"old"}}'))
respond("https://m.qidian.com/?ticket=", {
code = 200, body = "<html>shell</html>",
headers = { ["Set-Cookie"] = "abPolicies=%7B%7D; Path=/" },
})
local spec = { app_id = "13", area_id = "31", return_url = "https://m.qidian.com/", ticket = "1" }
local got
QRLogin:new(spec, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=temporary; ywkey=old")
end
do -- 即使临时凭据完整,也不跟陌生 HTTPS 地址
reset()
respond(QR_PREFIX, qrcode("foreign"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":5,"data":{"ywGuid":"g","ywKey":"k",'
.. '"302url":"https://qidian.com.evil.example/steal"}}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.is_nil(got)
Assert.len(requested, 2)
end
do -- 手机取消:关掉旧码不提示已取消,再要一张新码
reset()
respond(QR_PREFIX, qrcode("old"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"3"}'))
respond(QR_PREFIX, qrcode("new"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":"5","data":{"ywGuid":"g4","ywKey":"k4"}}'))
local got
QRLogin:new(SPEC, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(got, "ywguid=g4; ywkey=k4")
Assert.eq(toastCount("已取消登录"), 0)
Assert.eq(requested[1].url:sub(1, #QR_PREFIX), QR_PREFIX)
Assert.eq(requested[3].url:sub(1, #QR_PREFIX), QR_PREFIX)
Assert.is_true(requested[4].url:find("qrcode=new", 1, true) ~= nil)
Assert.len(script, 0)
end
for _idx, spec in ipairs({
{ app_id = "13", area_id = "31", return_url = "https://m.qidian.com/",
referer = "https://passport.qidian.com/mobile/login", title = "起点扫码登录", caption = SPEC.caption },
{ app_id = "57", area_id = "5", return_url = "https://www.xxsypro.com/",
referer = "https://passport.yuewen.com/yuewen.html", title = "潇湘扫码登录", caption = SPEC.caption },
}) do
reset()
respond(QR_PREFIX, qrcode("source-session"))
respond(POLL_PREFIX, poll('{"code":0,"scanStatus":5,"data":{"ywGuid":"source-guid","ywKey":"source-key"}}'))
local got
QRLogin:new(spec, function(cookie) got = cookie end):start()
Stubs.flush()
Assert.eq(find("qr").title, spec.title)
Assert.matches(find("qr").text, "appid=" .. spec.app_id .. "&areaid=" .. spec.area_id)
Assert.matches(requested[1].url, "appId=" .. spec.app_id .. "&areaId=" .. spec.area_id)
Assert.matches(requested[2].url, "appId=" .. spec.app_id .. "&areaId=" .. spec.area_id)
Assert.eq(requested[1].headers.Referer, spec.referer)
Assert.eq(got, "ywguid=source-guid; ywkey=source-key")
end
do -- 浏览器扩展绑定不改到 passport
local Setting = require("moonsource_qqreader.setting")
Assert.eq(Setting.browser.domain, "book.qq.com")
Assert.eq(Setting.browser.login, "https://book.qq.com/book-shelf")
Assert.eq(Setting.browser.check, "https://book.qq.com/book-shelf")
Assert.eq(type(Setting.login), "function")
end