修复小程序令牌过期登录循环
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
const assert = require('assert')
|
||||
|
||||
const storage = {}
|
||||
const requests = []
|
||||
const navigations = []
|
||||
const scheduled = []
|
||||
let clearedSessions = 0
|
||||
let globalData = { wechatLoginState: { status: 'SUCCESS', accessToken: 'expired-token' } }
|
||||
|
||||
global.wx = {
|
||||
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
|
||||
getDeviceInfo: () => ({ platform: 'devtools' }),
|
||||
getStorageSync: key => storage[key],
|
||||
setStorageSync: (key, value) => { storage[key] = value },
|
||||
removeStorageSync: key => { delete storage[key] },
|
||||
request: options => requests.push(options),
|
||||
showToast: () => {},
|
||||
reLaunch: options => navigations.push(options.url)
|
||||
}
|
||||
global.getApp = () => ({
|
||||
globalData,
|
||||
clearSession() {
|
||||
clearedSessions += 1
|
||||
delete storage['jayucloud-salon_session']
|
||||
globalData.wechatLoginState = null
|
||||
}
|
||||
})
|
||||
global.setTimeout = callback => {
|
||||
scheduled.push(callback)
|
||||
return scheduled.length
|
||||
}
|
||||
|
||||
const auth = require('./auth')
|
||||
const request = require('./request')
|
||||
|
||||
async function rejectWithUnauthorized(options) {
|
||||
const pending = request(options).then(
|
||||
() => { throw new Error('Expected request to reject') },
|
||||
error => error
|
||||
)
|
||||
requests[requests.length - 1].success({
|
||||
statusCode: 401,
|
||||
data: { message: '登录已过期,请重新登录' }
|
||||
})
|
||||
return pending
|
||||
}
|
||||
|
||||
async function run() {
|
||||
auth.setSession({ accessToken: 'expired-token' }, true)
|
||||
|
||||
await rejectWithUnauthorized({ url: '/user/wechat-mini/quick-login' })
|
||||
assert.strictEqual(clearedSessions, 0)
|
||||
assert.deepStrictEqual(navigations, [])
|
||||
|
||||
await rejectWithUnauthorized({ url: '/manager/calendar' })
|
||||
assert.strictEqual(clearedSessions, 1)
|
||||
assert.strictEqual(globalData.wechatLoginState, null)
|
||||
assert.strictEqual(scheduled.length, 1)
|
||||
scheduled.shift()()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index'])
|
||||
|
||||
await rejectWithUnauthorized({ url: '/manager/customers' })
|
||||
assert.strictEqual(clearedSessions, 1)
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index'])
|
||||
|
||||
auth.setSession({ accessToken: 'new-login-token' }, true)
|
||||
await rejectWithUnauthorized({ url: '/manager/calendar' })
|
||||
assert.strictEqual(clearedSessions, 2)
|
||||
assert.strictEqual(scheduled.length, 1)
|
||||
scheduled.shift()()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
Reference in New Issue
Block a user