修复登录状态接口遗漏并解除验证码提交阻塞
This commit is contained in:
1 parent
949ce8d25b
commit
50e1b795de
6 files changed
+90
-6
No files matched your search
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
import { compileFunction } from 'node:vm'
|
||||
import { computed, ref } from './vue.mjs'
|
||||
import { computed, reactive, ref } from './vue.mjs'
|
||||
|
||||
const source = readFileSync(new URL('../../frontend/src/views/LoginVerification/LoginVerification.js', import.meta.url), 'utf8')
|
||||
const createPage = compileFunction(
|
||||
@@ -10,9 +10,9 @@ const createPage = compileFunction(
|
||||
['computed', 'onBeforeUnmount', 'onMounted', 'ref', 'ElMessage', 'useRouter', 'useGlobalStatusStore', 'http', 'getSafeLoginRedirect', 'window']
|
||||
)
|
||||
|
||||
function fixture() {
|
||||
function fixture({ statusFails = false } = {}) {
|
||||
const calls = []
|
||||
const pending = { challengeId: 'pending-id', account: 'alice', methods: ['email', 'totp'], deliveryStatus: 'idle', expiresAt: new Date(Date.now() + 600000).toISOString() }
|
||||
const pending = reactive({ challengeId: 'pending-id', account: 'alice', methods: ['email', 'totp'], deliveryStatus: 'idle', expiresAt: new Date(Date.now() + 600000).toISOString() })
|
||||
const store = {
|
||||
pendingLogin: pending,
|
||||
setPendingLogin: data => Object.assign(pending, { deliveryStatus: data.delivery_status, retryAt: data.retry_at }),
|
||||
@@ -20,17 +20,49 @@ function fixture() {
|
||||
setUser: () => calls.push('session')
|
||||
}
|
||||
let mount
|
||||
let tick
|
||||
const page = createPage(computed, () => {}, callback => { mount = callback }, ref,
|
||||
{ error() {}, warning() {} }, () => ({ replace: async () => {} }), () => store,
|
||||
{ post: async (url, input, options) => {
|
||||
calls.push({ url, input, options })
|
||||
if (url.endsWith('/status')) {
|
||||
if (statusFails) throw new Error('status unavailable')
|
||||
return { errorNo: 0, data: { pending: true, delivery_status: 'sent' } }
|
||||
}
|
||||
if (url.endsWith('/resend')) return { errorNo: 0, data: { pending: true, delivery_status: 'sending', retry_at: new Date(Date.now() + 60000).toISOString() } }
|
||||
return { errorNo: 0, data: { account: 'alice' } }
|
||||
} }, () => '/', { setInterval: () => 1, clearInterval() {} })
|
||||
} }, () => '/', { setInterval: callback => { tick = callback; return 1 }, clearInterval() {} })
|
||||
mount()
|
||||
return { page, calls }
|
||||
return { page, calls, tick: async () => { tick(); await new Promise(resolve => setImmediate(resolve)) } }
|
||||
}
|
||||
|
||||
test('email code remains submittable when delivery status polling fails', async () => {
|
||||
const { page, calls, tick } = fixture({ statusFails: true })
|
||||
page.chooseMethod('email')
|
||||
assert.equal(page.canSubmit.value, false)
|
||||
await page.resendCode()
|
||||
await tick()
|
||||
assert.ok(page.errorMessage.value)
|
||||
assert.equal(page.sending.value, true)
|
||||
page.code.value = '12345678'
|
||||
assert.equal(page.canSubmit.value, true)
|
||||
await page.verifyCode()
|
||||
assert.equal(calls.filter(call => call.url?.endsWith('/resend')).length, 1)
|
||||
assert.ok(calls.includes('session'))
|
||||
assert.equal(calls.find(call => call.url?.endsWith('/verify')).input.method, 'email')
|
||||
})
|
||||
|
||||
test('polling advances email delivery to sent without sending again', async () => {
|
||||
const { page, calls, tick } = fixture()
|
||||
page.chooseMethod('email')
|
||||
await page.resendCode()
|
||||
await tick()
|
||||
assert.equal(page.sending.value, false)
|
||||
assert.equal(page.pending.value.deliveryStatus, 'sent')
|
||||
assert.equal(page.errorMessage.value, '')
|
||||
assert.equal(calls.filter(call => call.url?.endsWith('/resend')).length, 1)
|
||||
})
|
||||
|
||||
test('进入选择页和切换验证方式均不发信,只有点击发送按钮才请求一次', async () => {
|
||||
const { page, calls } = fixture()
|
||||
assert.deepEqual(calls, [])
|
||||
|
||||
Reference in new issue
Block a user