diff --git a/tests/api-client.test.js b/tests/api-client.test.js index ad44997..03e9362 100644 --- a/tests/api-client.test.js +++ b/tests/api-client.test.js @@ -28,7 +28,7 @@ async function run() { assert.strictEqual(GenealogyApi.DEFAULT_BASE_URL, undefined); const client = GenealogyApi.createClient({ - baseUrl: 'https://test-genealogy-api.ddxcjp.cn', + baseUrl: 'http://182.61.18.23:8080', clientId: 'client-x', tenantId: 'tenant-x', tokenKey: 'token-key', @@ -136,7 +136,7 @@ async function run() { 'token-key': 'existing-token' }); const failedRegisterClient = GenealogyApi.createClient({ - baseUrl: 'https://test-genealogy-api.ddxcjp.cn', + baseUrl: 'http://182.61.18.23:8080', clientId: 'client-x', tenantId: 'tenant-x', tokenKey: 'token-key', @@ -158,16 +158,59 @@ async function run() { ); assert.strictEqual(failedRegisterStore.getItem('token-key'), 'existing-token'); + const missingTokenStore = createStore(); + const missingTokenClient = GenealogyApi.createClient({ + baseUrl: 'http://182.61.18.23:8080', + clientId: 'client-x', + tenantId: 'tenant-x', + tokenKey: 'token-key', + tokenStore: missingTokenStore, + axiosInstance: { + request: async () => ({ + status: 200, + data: { code: 200, data: {} } + }) + } + }); + await assert.rejects( + missingTokenClient.login({ phone: '13800000000', password: 'md5' }), + (error) => error.message === '登录响应缺少 token' + ); + assert.strictEqual(missingTokenStore.getItem('token-key'), null); + + store.setItem('token-key', 'logout-token'); + await client.logout(); + assert.strictEqual(calls[9].url, '/genealogy/pc/auth/logout'); + assert.strictEqual(store.getItem('token-key'), null); + + const failedLogoutStore = createStore({ + 'token-key': 'logout-token' + }); + const failedLogoutClient = GenealogyApi.createClient({ + baseUrl: 'http://182.61.18.23:8080', + clientId: 'client-x', + tenantId: 'tenant-x', + tokenKey: 'token-key', + tokenStore: failedLogoutStore, + axiosInstance: { + request: async () => { + throw new Error('logout failed'); + } + } + }); + await assert.rejects(failedLogoutClient.logout(), /logout failed/); + assert.strictEqual(failedLogoutStore.getItem('token-key'), null); + await client.sendSmsCode({ sceneCode: 'WEB_H5_LOGIN', phone: '13800000000', validToken: 'captcha-ticket' }); - assert.strictEqual(calls[9].url, '/genealogy/pc/auth/sms/code'); - assert.strictEqual(calls[9].headers.Authorization, undefined); - assert.strictEqual(calls[9].data.grantType, 'sms'); - assert.strictEqual(calls[9].data.sceneCode, 'WEB_H5_LOGIN'); - assert.strictEqual(calls[9].data.validToken, 'captcha-ticket'); + assert.strictEqual(calls[10].url, '/genealogy/pc/auth/sms/code'); + assert.strictEqual(calls[10].headers.Authorization, undefined); + assert.strictEqual(calls[10].data.grantType, 'sms'); + assert.strictEqual(calls[10].data.sceneCode, 'WEB_H5_LOGIN'); + assert.strictEqual(calls[10].data.validToken, 'captcha-ticket'); assert.throws( () => client.listGenealogies(), @@ -177,7 +220,7 @@ async function run() { () => client.publicGenealogies(), (error) => error.code === 'PC_API_NOT_AVAILABLE' ); - assert.strictEqual(calls.length, 10); + assert.strictEqual(calls.length, 11); console.log('api-client tests passed'); } diff --git a/utils/ApiClient.js b/utils/ApiClient.js index 1e9f50b..4a772b4 100644 --- a/utils/ApiClient.js +++ b/utils/ApiClient.js @@ -55,6 +55,13 @@ return data.token || data.accessToken || data.tokenValue || ''; } + function requireLoginToken(data) { + var token = getTokenFromResponse(data); + + if (!token) throw new ApiError('登录响应缺少 token'); + return token; + } + function ApiError(message, options) { this.name = 'ApiError'; this.message = message || '接口请求失败'; @@ -169,7 +176,7 @@ auth: false, body: Object.assign({ grantType: 'password' }, withTenant(body)) }); - setToken(getTokenFromResponse(data)); + setToken(requireLoginToken(data)); return data; } @@ -187,7 +194,7 @@ auth: false, body: Object.assign({ grantType: 'sms' }, withTenant(body)) }); - setToken(getTokenFromResponse(data)); + setToken(requireLoginToken(data)); return data; } @@ -267,9 +274,11 @@ return data; }, logout: async function () { - var data = await request('DELETE', '/genealogy/pc/auth/logout'); - clearToken(); - return data; + try { + return await request('DELETE', '/genealogy/pc/auth/logout'); + } finally { + clearToken(); + } }, uploadFile: function (fileOrFormData) { return request('POST', '/genealogy/pc/files/upload', { body: createFormData(fileOrFormData) });