✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
commit
7273cef5e7
20 files changed
+1987
No files matched your search
@@ -0,0 +1,130 @@
|
||||
--[[--
|
||||
七猫手机号 / 短信码留在本机;仅发送短信前的拼图桥接,保留 token 导入。
|
||||
|
||||
@module tests.moonsource_qimao.login_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Themed = require("support.theme_stubs")
|
||||
require("source.logout").replaceLibrary = function(id, commit)
|
||||
Assert.eq(id, "ext.qimao")
|
||||
commit()
|
||||
return true
|
||||
end
|
||||
Themed.install()
|
||||
package.preload["ui.themes.widgets.row"] = function()
|
||||
return { widget = function(spec) return spec end }
|
||||
end
|
||||
local Progress = require("ui.themes.widgets.progress_dialog")
|
||||
local newProgress = Progress.new
|
||||
function Progress:new(spec)
|
||||
local dialog = newProgress(self, spec)
|
||||
dialog.show = function(self) self.shown = true end
|
||||
return dialog
|
||||
end
|
||||
local cfg = { token = "old-token" }
|
||||
local saved, reloaded, authorized
|
||||
package.preload["utils.settings"] = function()
|
||||
return {
|
||||
getSource = function() return cfg end,
|
||||
saveSource = function(_id, value) saved = value.token end,
|
||||
}
|
||||
end
|
||||
package.preload["source.auth"] = function()
|
||||
return { ok = function(id) authorized = id end, expired = function() return false end }
|
||||
end
|
||||
package.preload["source.registry"] = function() return { reload = function() reloaded = true end } end
|
||||
local pending_network, defer_network
|
||||
package.preload["ui/network/manager"] = function()
|
||||
return { runWhenConnected = function(_self, fn)
|
||||
if defer_network then pending_network = fn else fn() end
|
||||
end }
|
||||
end
|
||||
local sessions = {}
|
||||
package.preload["moonsource_qimao.verify"] = function()
|
||||
return { new = function(phone, cb)
|
||||
local session = { phone = phone, callback = cb }
|
||||
session.cancel = function() session.cancelled = true end
|
||||
sessions[#sessions + 1] = session
|
||||
return session
|
||||
end }
|
||||
end
|
||||
local login_phone, login_code, checked_token
|
||||
local login_error
|
||||
package.preload["moonsource_qimao.client"] = function()
|
||||
return {
|
||||
token = function() return cfg.token end,
|
||||
loginAsync = function(phone, code, cb)
|
||||
login_phone, login_code = phone, code
|
||||
cb(login_error and nil or "new-token", login_error)
|
||||
end,
|
||||
userInfoAsync = function(token, cb) checked_token = token; cb({}) end,
|
||||
}
|
||||
end
|
||||
local Setting = require("moonsource_qimao.setting")
|
||||
Setting.login()
|
||||
local input = Themed.last("input")
|
||||
Assert.len(input.fields, 2)
|
||||
Assert.eq(input.fields[1].label, "手机号")
|
||||
Assert.eq(input.fields[2].label, "验证码")
|
||||
Assert.len(input.actions, 1, "手机号登录只留发码动作,避免四个按钮挤在一行")
|
||||
input.actions[1].callback("invalid")
|
||||
Assert.len(sessions, 0)
|
||||
Assert.eq(Themed.last("toast").text, "请填写正确的手机号")
|
||||
input.actions[1].callback(" 13800000000 ")
|
||||
Assert.len(sessions, 0, "用户同意协议之前不启动发短信验证")
|
||||
Themed.last("confirm").ok_callback()
|
||||
Assert.len(sessions, 1)
|
||||
Assert.eq(sessions[1].phone, "13800000000")
|
||||
sessions[1].callback(true)
|
||||
Assert.eq(Themed.last("toast").text, "短信已发送,请在设备上填写验证码")
|
||||
Assert.is_nil(input.closed, "滑块完成后仍在设备填写短信码")
|
||||
input.actions[1].callback("13800000000")
|
||||
Assert.is_true(sessions[1].cancelled, "重复点击只保留最新一次发送")
|
||||
Assert.len(sessions, 2)
|
||||
input:close()
|
||||
input.callback("13800000000", "001234")
|
||||
Assert.is_true(sessions[2].cancelled)
|
||||
Assert.eq(login_phone, "13800000000")
|
||||
Assert.eq(login_code, "001234")
|
||||
Assert.eq(checked_token, "new-token", "登录返回的 token 仍先校验")
|
||||
Assert.eq(saved, "new-token")
|
||||
Assert.eq(authorized, "ext.qimao")
|
||||
Assert.is_true(reloaded)
|
||||
Assert.is_nil(cfg.phone, "不持久化手机号或短信码")
|
||||
Assert.is_nil(cfg.code)
|
||||
Assert.is_true(Themed.last("progress_dialog").closed)
|
||||
|
||||
local token_row
|
||||
for _idx, make_row in ipairs(Setting.rows()) do
|
||||
local row = make_row(400)
|
||||
if row.title == "用 token 登录" then
|
||||
token_row = row
|
||||
break
|
||||
end
|
||||
end
|
||||
Assert.not_nil(token_row, "保留 token 登录入口,不依赖账号页行顺序")
|
||||
token_row.callback()
|
||||
local imported = Themed.last("input")
|
||||
Assert.eq(imported.title, "七猫登录 token")
|
||||
Assert.len(imported.fields, 1)
|
||||
imported.callback("manual-token")
|
||||
Assert.eq(saved, "manual-token", "保留旧 token 登录入口")
|
||||
|
||||
Setting.login()
|
||||
input = Themed.last("input")
|
||||
defer_network = true
|
||||
input.actions[1].callback("13800000000")
|
||||
Themed.last("confirm").ok_callback()
|
||||
Assert.not_nil(pending_network)
|
||||
input:close()
|
||||
pending_network()
|
||||
Assert.len(sessions, 2, "等待联网时关闭输入窗不能再弹出二维码")
|
||||
|
||||
defer_network = false
|
||||
Setting.login()
|
||||
input = Themed.last("input")
|
||||
input:close()
|
||||
input.callback("13800000000", "")
|
||||
Assert.eq(Themed.last("toast").text, "请先填写验证码")
|
||||
@@ -0,0 +1,64 @@
|
||||
--[[--
|
||||
七猫的多阶段进度请求必须保留当前阶段的取消句柄。
|
||||
|
||||
@module tests.moonsource_qimao.progress_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
|
||||
local client = { token = function() return "token" end }
|
||||
package.preload["moonsource_qimao.client"] = function() return client end
|
||||
package.preload["moonsource_qimao.mapper"] = function()
|
||||
return {
|
||||
readChapter = function(row)
|
||||
return row and { uid = row.latest_read_chapter_id, title = row.latest_read_chapter_name }
|
||||
end,
|
||||
progressItem = function(row, chapter) return { row = row, chapter = chapter } end,
|
||||
}
|
||||
end
|
||||
package.preload["source.base"] = function()
|
||||
return { loadMeta = function() return { id = "ext.qimao", name = "七猫", type = "chapter" } end }
|
||||
end
|
||||
package.preload["source.browse"] = function() return {} end
|
||||
package.preload["source.shelf"] = function() return {} end
|
||||
package.preload["source.toc"] = function() return { index = function() return 3 end } end
|
||||
package.loaded["moonsource_qimao.source"] = nil
|
||||
|
||||
local Source = require("moonsource_qimao.source")
|
||||
local identity = { stable_id = "book" }
|
||||
local source = Source.new()
|
||||
local toc_cancelled, shelf_cancelled = false, false
|
||||
local shelf_job = { cancel = function() shelf_cancelled = true end }
|
||||
local toc_job = { cancel = function() toc_cancelled = true end }
|
||||
local toc_callback
|
||||
|
||||
client.shelfSyncAsync = function(cb)
|
||||
cb({ { book_id = "book", latest_read_chapter_id = "chapter", latest_read_chapter_name = "第3章" } })
|
||||
return shelf_job
|
||||
end
|
||||
source.loadTocAsync = function(_self, _identity, cb)
|
||||
toc_callback = cb
|
||||
return toc_job
|
||||
end
|
||||
local result_called = false
|
||||
local get_job = source:getProgressAsync(identity, function() result_called = true end)
|
||||
get_job.cancel()
|
||||
Assert.is_true(toc_cancelled, "cancel targets the in-flight TOC request")
|
||||
Assert.is_false(shelf_cancelled, "cancel does not retain the completed shelf request")
|
||||
toc_callback({ { uid = "chapter", title = "第3章" } })
|
||||
Assert.is_false(result_called, "a cancelled request ignores a late callback")
|
||||
|
||||
local push_cancelled = false
|
||||
local push_job = { cancel = function() push_cancelled = true end }
|
||||
toc_cancelled, shelf_cancelled = false, false
|
||||
source.loadTocAsync = function(_self, _identity, cb)
|
||||
cb({ { uid = "chapter", title = "第3章" } })
|
||||
return toc_job
|
||||
end
|
||||
client.shelfPushAsync = function(_items, _cb) return push_job end
|
||||
local put_job = source:putProgressAsync(identity, { chapter_idx = 1 }, function() end)
|
||||
put_job.cancel()
|
||||
Assert.is_true(push_cancelled, "cancel targets the in-flight progress upload")
|
||||
Assert.is_false(toc_cancelled)
|
||||
Assert.is_false(shelf_cancelled)
|
||||
@@ -0,0 +1,271 @@
|
||||
--[[--
|
||||
七猫登录 token、书架分页 / 增删上行与映射离线用例。
|
||||
|
||||
@module tests.source.qimao_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local JSONStub = require("support.json_stub")
|
||||
|
||||
package.preload["json"] = function()
|
||||
return { decode = JSONStub.decode, encode = JSONStub.encode }
|
||||
end
|
||||
|
||||
local cfg = { token = "tk-1" }
|
||||
package.preload["utils.settings"] = function()
|
||||
return { getSource = function() return cfg end }
|
||||
end
|
||||
|
||||
local expired
|
||||
package.preload["source.auth"] = function()
|
||||
return { expire = function(_, msg) expired = msg; return msg end, ok = function() end }
|
||||
end
|
||||
|
||||
local requests, replies = {}, {}
|
||||
package.preload["http.request"] = function()
|
||||
return {
|
||||
get = function(url, opts, cb)
|
||||
requests[#requests + 1] = { url = url, opts = opts }
|
||||
cb(table.remove(replies, 1))
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
post = function(url, body, opts, cb)
|
||||
requests[#requests + 1] = { url = url, body = body, opts = opts }
|
||||
cb(table.remove(replies, 1))
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
package.loaded["moonsource_qimao.client"] = nil
|
||||
package.loaded["moonsource_qimao.mapper"] = nil
|
||||
package.loaded["moonsource_qimao.setting"] = nil
|
||||
local Client = require("moonsource_qimao.client")
|
||||
local Mapper = require("moonsource_qimao.mapper")
|
||||
local Setting = require("moonsource_qimao.setting")
|
||||
|
||||
-- 滑块只交 rid;设备发短信、再用短信码登录,不提前覆盖已有账号。
|
||||
do
|
||||
requests, replies = {}, { '{"data":""}', '{"data":{"token":"new-token"}}' }
|
||||
local sent, token, err
|
||||
Client.sendCodeAsync("13800000000", "slider-proof", function(ok, message) sent, err = ok, message end)
|
||||
Assert.is_true(sent)
|
||||
Assert.is_nil(err)
|
||||
Assert.matches(requests[1].url, "/api/pc/v1/login/send%-code$")
|
||||
Assert.matches(requests[1].body, "phone=13800000000")
|
||||
Assert.matches(requests[1].body, "rid=slider%-proof")
|
||||
Assert.is_nil(requests[1].opts.headers.Authorization, "登录请求不携带已登录账号的 token")
|
||||
Client.loginAsync("13800000000", "001234", function(value, message) token, err = value, message end)
|
||||
Assert.eq(token, "new-token")
|
||||
Assert.is_nil(err)
|
||||
Assert.matches(requests[2].url, "/api/pc/v1/login/index$")
|
||||
Assert.matches(requests[2].body, "code=001234", "短信码保留前导零")
|
||||
Assert.eq(cfg.token, "tk-1", "新 token 经设置页校验后才保存")
|
||||
|
||||
expired, replies = nil, { '{"errors":{"code":"40001002","title":"短信验证码错误"}}', '{"data":{}}' }
|
||||
Client.loginAsync("13800000000", "wrong", function(value, message) token, err = value, message end)
|
||||
Assert.is_nil(token)
|
||||
Assert.eq(err, "短信验证码错误")
|
||||
Assert.is_nil(expired, "尝试新登录不能把原账号标记失效")
|
||||
Client.loginAsync("13800000000", "001234", function(value, message) token, err = value, message end)
|
||||
Assert.is_nil(token)
|
||||
Assert.eq(err, "七猫登录响应缺少 token")
|
||||
requests = {}
|
||||
end
|
||||
|
||||
-- 书架分页:按 meta.total_pages 翻完,请求带 Authorization。
|
||||
do
|
||||
replies = {
|
||||
'{"data":{"books":[{"book_id":"1"}],"meta":{"total_pages":"2"}}}',
|
||||
'{"data":{"books":[{"book_id":"2"}],"meta":{"total_pages":"2"}}}',
|
||||
}
|
||||
local rows, err
|
||||
Client.shelfSyncAsync(function(v, e) rows, err = v, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.len(rows, 2)
|
||||
Assert.eq(rows[2].book_id, "2")
|
||||
Assert.len(requests, 2)
|
||||
Assert.eq(requests[1].opts.headers.Authorization, "tk-1")
|
||||
Assert.matches(requests[1].url, "api%-bs%.wtzw%.com/pcapi/v1/user/local$")
|
||||
Assert.matches(requests[2].body, "page=2")
|
||||
end
|
||||
|
||||
-- 书库列表:分页字段决定是否还有下一页,书取书库行字段。
|
||||
do
|
||||
requests, replies = {}, { '{"data":{"book_list":[{"book_id":"9","title":"甲","author":"乙","image_link":"http://c/x.jpg"}],'
|
||||
.. '"page_data":{"count":"31","page":"2","page_size":"15"}}}' }
|
||||
local wire
|
||||
Client.classifyAsync({
|
||||
channel = "0", category1 = "202", category2 = "a", order = "click",
|
||||
words = "3", update_time = "2", is_over = "1", page = 2,
|
||||
},
|
||||
function(v) wire = v end)
|
||||
Assert.matches(requests[1].url, "^https://www%.qimao%.com/api/classify/book%-list%?")
|
||||
Assert.matches(requests[1].url, "category1=202")
|
||||
Assert.matches(requests[1].url, "words=3")
|
||||
Assert.matches(requests[1].url, "update_time=2")
|
||||
Assert.matches(requests[1].url, "is_over=1")
|
||||
local books, more = Mapper.classify(wire)
|
||||
Assert.eq(books[1].stable_id, "9")
|
||||
Assert.eq(books[1].authors, "乙")
|
||||
Assert.is_true(more)
|
||||
wire.data.page_data.page = "3"
|
||||
Assert.is_true(not select(2, Mapper.classify(wire)))
|
||||
end
|
||||
|
||||
-- 登录态被拒:标失效;弹窗用人话,不原样塞服务端 title。
|
||||
do
|
||||
requests, expired = {}, nil
|
||||
replies = { '{"errors":{"code":"40001002","title":"请先登录"}}' }
|
||||
local wire, err
|
||||
Client.shelfPushAsync({ { book_id = "1" } }, function(v, e) wire, err = v, e end)
|
||||
Assert.is_nil(wire)
|
||||
Assert.matches(err, "moon%-cookie%-storage")
|
||||
Assert.eq(expired, err)
|
||||
end
|
||||
|
||||
-- 没 token 不发请求。
|
||||
do
|
||||
requests, cfg.token = {}, nil
|
||||
local queue = {}
|
||||
package.preload["ui/uimanager"] = function() return { nextTick = function(_, f) queue[#queue + 1] = f end } end
|
||||
package.loaded["source.base"] = nil
|
||||
package.loaded["ui.guard"] = nil
|
||||
package.preload["ui.guard"] = function() return { call = function(_, f, ...) return f(...) end } end
|
||||
local err
|
||||
Client.shelfPushAsync({}, function(_, e) err = e end)
|
||||
for _idx, f in ipairs(queue) do f() end
|
||||
Assert.eq(err, "未登录七猫")
|
||||
Assert.len(requests, 0)
|
||||
cfg.token = "tk-1"
|
||||
end
|
||||
|
||||
-- 校验新 token 的失败不能把已有登录态标失效。
|
||||
do
|
||||
expired = nil
|
||||
replies = { '{"errors":{"code":"40001002","title":"请先登录"}}' }
|
||||
local user, err
|
||||
Client.userInfoAsync("other-token", function(v, e) user, err = v, e end)
|
||||
Assert.is_nil(user)
|
||||
Assert.eq(err, "请先登录")
|
||||
Assert.is_nil(expired)
|
||||
end
|
||||
|
||||
-- 映射:书架行用 book_*,封面补 CDN;上行条目只留 /bookimg/ 路径。
|
||||
do
|
||||
local books = Mapper.shelfList({ { book_id = 7, book_title = "书", author = "作者", image_link = "/bookimg/a.jpg" } })
|
||||
Assert.eq(books[1].stable_id, "7")
|
||||
Assert.eq(books[1].title, "书")
|
||||
Assert.eq(books[1].cover, "https://cdn.wtzw.com/bookimg/a.jpg")
|
||||
local item = Mapper.shelfItem("7", books[1], "1")
|
||||
Assert.eq(item.image_link, "/bookimg/a.jpg")
|
||||
Assert.eq(item.push_type, "1")
|
||||
Assert.is_nil(Mapper.readChapter({ latest_read_chapter_id = "" }))
|
||||
local row = {
|
||||
book_id = "7", book_title = "书", group_name = "在读",
|
||||
latest_read_chapter_id = "19", latest_read_chapter_name = "第19章",
|
||||
page_offset = "12", paragraph_index = "3",
|
||||
}
|
||||
Assert.eq(Mapper.readChapter(row).uid, "19")
|
||||
local same = Mapper.progressItem(row, { uid = "19", title = "第19章", idx = 19 })
|
||||
Assert.eq(same.book_title, "书")
|
||||
Assert.eq(same.group_name, "在读")
|
||||
Assert.eq(same.page_offset, "12")
|
||||
Assert.eq(same.paragraph_index, "3")
|
||||
Assert.eq(same.push_type, "1")
|
||||
local moved = Mapper.progressItem(row, { uid = "20", title = "第20章", idx = 20 })
|
||||
Assert.eq(moved.latest_read_chapter_id, "20")
|
||||
Assert.eq(moved.latest_read_chapter_index, "20")
|
||||
Assert.eq(moved.page_offset, "")
|
||||
Assert.eq(moved.paragraph_index, "0")
|
||||
Assert.eq(moved.book_title, "书")
|
||||
end
|
||||
|
||||
-- 登录输入:裸 token、Bearer 前缀、浏览器 userinfo JSON。
|
||||
do
|
||||
Assert.eq(Setting.parseToken(" abc "), "abc")
|
||||
Assert.eq(Setting.parseToken("Bearer abc"), "abc")
|
||||
Assert.eq(Setting.parseToken('{"token":"xyz","is_vip":"0"}'), "xyz")
|
||||
Assert.eq(Setting.parseToken("{坏"), "")
|
||||
end
|
||||
|
||||
-- 书城浏览:默认节点是「频道:分类:子分类:排序」四段;三段正则会让 cat2/order 为 nil 拼崩。
|
||||
do
|
||||
local classify_q
|
||||
package.loaded["moonsource_qimao.client"] = nil
|
||||
package.preload["moonsource_qimao.client"] = function()
|
||||
return {
|
||||
classifyOptionsAsync = function(cb)
|
||||
cb({
|
||||
data = {
|
||||
filters = {
|
||||
order = { { value = "click", label = "点击" }, { value = "a", label = "全部" } },
|
||||
channel = { { value = "0", label = "男生" }, { value = "a", label = "全部" } },
|
||||
words = { { value = "a", label = "全部" }, { value = "3", label = "50万-100万" } },
|
||||
update_time = { { value = "a", label = "全部" }, { value = "2", label = "7天内" } },
|
||||
is_over = { { value = "a", label = "全部" }, { value = "1", label = "已完结" } },
|
||||
},
|
||||
category = {
|
||||
{ id = "202", name = "玄幻奇幻", type = "0", children = {
|
||||
{ id = "37", name = "东方玄幻", type = "0" },
|
||||
} },
|
||||
},
|
||||
},
|
||||
})
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
classifyAsync = function(q, cb)
|
||||
classify_q = q
|
||||
cb({ data = { book_list = {}, page_data = { page = "1", page_size = "15", count = "0" } } })
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
package.loaded["source.browse"] = nil
|
||||
package.preload["source.browse"] = function()
|
||||
return {
|
||||
group = function(items, selected) return { items = items, selected = selected } end,
|
||||
pages = function(fetch, _limit, groups, cb)
|
||||
fetch(1, function(books, err, more)
|
||||
cb({ data = books or {}, groups = groups, more = more }, err)
|
||||
end)
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
package.loaded["moonsource_qimao.source"] = nil
|
||||
package.loaded["moonsource_qimao.init"] = nil
|
||||
local Source = require("moonsource_qimao.source").new()
|
||||
local res, err
|
||||
Source:browseAsync({}, function(r, e) res, err = r, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(res ~= nil)
|
||||
Assert.eq(classify_q.channel, "0")
|
||||
Assert.eq(classify_q.category1, "a")
|
||||
Assert.eq(classify_q.category2, "a")
|
||||
Assert.eq(classify_q.order, "click")
|
||||
Assert.eq(classify_q.words, "a")
|
||||
Assert.eq(classify_q.update_time, "a")
|
||||
Assert.eq(classify_q.is_over, "a")
|
||||
Assert.eq(res.groups[1].selected, "0:a:a:click:a:a:a")
|
||||
Assert.eq(res.groups[4].title, "字数")
|
||||
Assert.eq(res.groups[4].items[1].id, "0:a:a:click:a:a:a")
|
||||
|
||||
Source:browseAsync({ node = "0:202:37:words_num:3:2:1" }, function(r, e) res, err = r, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(classify_q.order, "words_num", "排序值包含下划线")
|
||||
Assert.eq(classify_q.category1, "202")
|
||||
Assert.eq(classify_q.category2, "37")
|
||||
Assert.eq(classify_q.words, "3")
|
||||
Assert.eq(classify_q.update_time, "2")
|
||||
Assert.eq(classify_q.is_over, "1")
|
||||
Assert.eq(res.groups[1].selected, "0:202:37:words_num:3:2:1")
|
||||
Assert.eq(res.groups[5].title, "字数")
|
||||
|
||||
Source:browseAsync({ node = "0:202:37:words_num" }, function(r, e) res, err = r, e end)
|
||||
Assert.eq(classify_q.order, "words_num", "兼容已有四段筛选节点")
|
||||
Assert.eq(classify_q.words, "a")
|
||||
Assert.eq(classify_q.update_time, "a")
|
||||
Assert.eq(classify_q.is_over, "a")
|
||||
end
|
||||
@@ -0,0 +1,121 @@
|
||||
--[[--
|
||||
七猫滑块仅提交 rid;手机号隔离、一次性票据、异步取消与过期。
|
||||
|
||||
@module tests.moonsource_qimao.verify_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Themed = require("support.theme_stubs")
|
||||
Themed.install()
|
||||
local Ext = require("utils.extension")
|
||||
Ext.reset()
|
||||
local token
|
||||
local submit, submitted_phone, submitted_rid, cancelled
|
||||
package.preload["moonsource_qimao.client"] = function()
|
||||
return {
|
||||
token = function() return token end,
|
||||
sendCodeAsync = function(phone, rid, cb)
|
||||
submitted_phone, submitted_rid, submit = phone, rid, cb
|
||||
return { cancel = function() cancelled = true end }
|
||||
end,
|
||||
}
|
||||
end
|
||||
local timers = {}
|
||||
package.preload["ui/uimanager"] = function()
|
||||
return {
|
||||
scheduleIn = function(_self, _delay, fn) timers[fn] = true end,
|
||||
unschedule = function(_self, fn) timers[fn] = nil end,
|
||||
}
|
||||
end
|
||||
package.preload["ui.guard"] = function() return { wrap = function(_label, fn) return fn end } end
|
||||
local path
|
||||
Ext.add("web.open_page", function(url, _title, _text, opts)
|
||||
path = url
|
||||
return require("ui.themes.widgets.qr"):new{ close_callback = opts.close_callback }:show()
|
||||
end)
|
||||
local Verification = require("moonsource_qimao.verify")
|
||||
local route = Ext.list("web.routes")[1]
|
||||
local status, response, headers
|
||||
local function request(action, params, method)
|
||||
route.handle(method or "POST", action, params, function(code, body, extra)
|
||||
status, response, headers = code, body, extra
|
||||
end)
|
||||
end
|
||||
local finished, finish_err
|
||||
local function newSession()
|
||||
finished, finish_err = nil, nil
|
||||
return Verification.new("13800000000", function(ok, err) finished, finish_err = ok, err end)
|
||||
end
|
||||
local session = newSession()
|
||||
local ticket = path:match("#(.+)$")
|
||||
Assert.matches(path, "^/qimao_verify%.html#%x+$")
|
||||
request("config", { ticket = ticket })
|
||||
Assert.eq(status, 200)
|
||||
Assert.is_nil(response.phone, "网页不能读到设备手机号")
|
||||
Assert.is_nil(response.token, "网页不能读到登录 token")
|
||||
Assert.is_nil(response.fields, "不是远程账号 / 短信输入表单")
|
||||
Assert.eq(headers[1], "Cache-Control: no-store")
|
||||
request("verify", { ticket = "wrong", rid = "proof" })
|
||||
Assert.eq(status, 403)
|
||||
request("verify", { ticket = ticket, rid = {} })
|
||||
Assert.eq(status, 400)
|
||||
request("verify", { ticket = ticket, rid = string.rep("a", 1025) })
|
||||
Assert.eq(status, 400)
|
||||
request("config", { ticket = ticket }, "GET")
|
||||
Assert.eq(status, 405)
|
||||
request("other", { ticket = ticket })
|
||||
Assert.eq(status, 404)
|
||||
|
||||
request("verify", { ticket = ticket, rid = "proof", phone = "another-phone", code = "browser-code" })
|
||||
Assert.eq(submitted_phone, "13800000000", "浏览器不能换掉目标手机号")
|
||||
Assert.eq(submitted_rid, "proof")
|
||||
request("verify", { ticket = ticket, rid = "another-proof" })
|
||||
Assert.eq(status, 409)
|
||||
submit(false, "验证码发送频繁")
|
||||
Assert.eq(status, 400)
|
||||
Assert.eq(response.error, "验证码发送频繁")
|
||||
Assert.is_nil(finished, "发短信失败不能报告验证成功")
|
||||
request("verify", { ticket = ticket, rid = "retry-proof" })
|
||||
submit(true)
|
||||
Assert.eq(status, 200)
|
||||
Assert.is_true(finished)
|
||||
Assert.is_true(session.dialog.closed)
|
||||
Assert.is_nil(next(timers))
|
||||
request("verify", { ticket = ticket, rid = "replay" })
|
||||
Assert.eq(status, 403, "一次验证只允许成功发送一次短信")
|
||||
|
||||
session = newSession()
|
||||
ticket = path:match("#(.+)$")
|
||||
request("verify", { ticket = ticket, rid = "pending" })
|
||||
session.dialog:close()
|
||||
Assert.is_false(finished)
|
||||
Assert.eq(finish_err, "验证已取消")
|
||||
Assert.is_true(cancelled)
|
||||
submit(true)
|
||||
Assert.eq(status, 403)
|
||||
Assert.is_false(finished, "关窗后不能接收迟到的成功")
|
||||
|
||||
session = newSession()
|
||||
ticket = path:match("#(.+)$")
|
||||
session.expires = os.time() - 1
|
||||
request("config", { ticket = ticket })
|
||||
Assert.eq(status, 403)
|
||||
session.timeout()
|
||||
Assert.eq(finish_err, "验证已过期,请重新发起验证")
|
||||
Assert.is_nil(next(timers))
|
||||
|
||||
session = newSession()
|
||||
ticket = path:match("#(.+)$")
|
||||
request("verify", { ticket = ticket, rid = "pending" })
|
||||
token = "new-account"
|
||||
submit(true)
|
||||
Assert.eq(status, 403)
|
||||
Assert.is_false(finished, "换账号后不能把旧手机号的验证报为成功")
|
||||
|
||||
Ext.reset()
|
||||
session = newSession()
|
||||
Assert.is_true(session.done)
|
||||
Assert.is_false(finished)
|
||||
Assert.eq(finish_err, "无法打开远程验证,请先连接 Wi-Fi 并启用远程管理功能")
|
||||
Assert.is_nil(next(timers))
|
||||
Reference in new issue
Block a user