|
|
@@ -25,7 +25,6 @@ function mockFail(err) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-/** Helper: make uni.request return a sequence of responses (one per call) */
|
|
|
function mockSequence(responses) {
|
|
|
var i = 0
|
|
|
global.uni.request.mockImplementation((opts) => {
|
|
|
@@ -167,6 +166,38 @@ describe('auth API', () => {
|
|
|
jest.useRealTimers()
|
|
|
})
|
|
|
|
|
|
+ test('401 with cached openid silently re-logins and retries original request', async () => {
|
|
|
+ global.uni.reLaunch.mockReset()
|
|
|
+ global.uni.showToast.mockReset()
|
|
|
+ global.uni._storage.openid = 'openid_abc'
|
|
|
+ global.uni._storage.token = 'expired-token'
|
|
|
+ mockSequence([
|
|
|
+ { data: { code: 401, message: 'Token无效或已过期', data: null } },
|
|
|
+ { data: { code: 200, data: { token: 'new-jwt', userId: 42, role: 'parent', familyId: 1, openid: 'openid_abc' } } },
|
|
|
+ { data: { code: 200, data: { userId: 42, role: 'parent' } } }
|
|
|
+ ])
|
|
|
+ const res = await api.verifyToken()
|
|
|
+ expect(res.data.userId).toBe(42)
|
|
|
+ expect(global.uni._storage.token).toBe('new-jwt')
|
|
|
+ expect(global.uni.reLaunch).not.toHaveBeenCalled()
|
|
|
+ expect(global.uni.showToast).not.toHaveBeenCalled()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('401 with cached openid falls back to login when auto-login fails', async () => {
|
|
|
+ jest.useFakeTimers()
|
|
|
+ global.uni._storage.openid = 'openid_abc'
|
|
|
+ global.uni._storage.token = 'expired-token'
|
|
|
+ mockSequence([
|
|
|
+ { data: { code: 401, message: 'Token无效或已过期', data: null } },
|
|
|
+ { data: { code: 401, message: '用户不存在', data: null } }
|
|
|
+ ])
|
|
|
+ await expect(api.verifyToken()).rejects.toEqual({ code: 401, message: 'Token无效或已过期', data: null })
|
|
|
+ jest.runAllTimers()
|
|
|
+ expect(global.uni.reLaunch).toHaveBeenCalledWith({ url: '/pages/login/login' })
|
|
|
+ expect(global.uni.showToast).toHaveBeenCalled()
|
|
|
+ jest.useRealTimers()
|
|
|
+ })
|
|
|
+
|
|
|
test('Authorization header includes Bearer token', async () => {
|
|
|
mockSuccess({ code: 200 })
|
|
|
global.uni._storage.token = 'my-jwt-token'
|
|
|
@@ -306,10 +337,10 @@ describe('user & family API', () => {
|
|
|
expect(lastRequest().url).toMatch(/\/api\/user\/switch-back-to-parent$/)
|
|
|
})
|
|
|
|
|
|
- test('getChildren sends POST', async () => {
|
|
|
+ test('getChildren sends POST to unified member list', async () => {
|
|
|
mockSuccess({ code: 200 })
|
|
|
await api.getChildren()
|
|
|
- expect(lastRequest().url).toMatch(/\/api\/family\/user\/children\/list$/)
|
|
|
+ expect(lastRequest().url).toMatch(/\/api\/family\/member\/list$/)
|
|
|
})
|
|
|
|
|
|
test('createChild sends POST with data', async () => {
|
|
|
@@ -413,11 +444,12 @@ describe('tasks API', () => {
|
|
|
expect(lastRequest().data.size).toBe(20)
|
|
|
})
|
|
|
|
|
|
- test('deleteTask sends DELETE', async () => {
|
|
|
+ test('deleteTask sends POST with _method DELETE', async () => {
|
|
|
mockSuccess({ code: 200 })
|
|
|
await api.deleteTask(10)
|
|
|
- expect(lastRequest().method).toBe('DELETE')
|
|
|
+ expect(lastRequest().method).toBe('POST')
|
|
|
expect(lastRequest().url).toMatch(/\/api\/tasks\/10$/)
|
|
|
+ expect(lastRequest().data._method).toBe('DELETE')
|
|
|
})
|
|
|
|
|
|
test('batchCompleteTasks sends POST with array', async () => {
|
|
|
@@ -549,10 +581,11 @@ describe('identity parameter hardening', () => {
|
|
|
// --------------- guide / packages module ---------------
|
|
|
|
|
|
describe('guide & packages API', () => {
|
|
|
- test('getGuidePackages sends POST to list', async () => {
|
|
|
+ test('getGuidePackages sends POST to my list', async () => {
|
|
|
mockSuccess({ code: 200 })
|
|
|
await api.getGuidePackages(1)
|
|
|
- expect(lastRequest().url).toMatch(/\/api\/guide\/packages$/)
|
|
|
+ expect(lastRequest().url).toMatch(/\/api\/guide\/packages\/my$/)
|
|
|
+ expect(lastRequest().data.guideId).toBe(1)
|
|
|
})
|
|
|
|
|
|
test('registerGuide sends POST', async () => {
|
|
|
@@ -963,8 +996,8 @@ describe('health report API', () => {
|
|
|
|
|
|
test('getSurveyStatus sends POST with reportId', async () => {
|
|
|
mockSuccess({ code: 200 })
|
|
|
- await api.getSurveyStatus(10)
|
|
|
- expect(lastRequest().url).toMatch(/\/api\/health\/survey\/status$/)
|
|
|
+ await api.getSurveyStatus({ reportId: 10 })
|
|
|
+ expect(lastRequest().url).toMatch(/\/api\/survey\/status$/)
|
|
|
expect(lastRequest().data.reportId).toBe(10)
|
|
|
})
|
|
|
})
|