|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import notification from 'ant-design-vue/es/notification' |
| 19 | +import router from '@/router' |
| 20 | +import store from '@/store' |
| 21 | +import { axios } from '@/utils/request' |
| 22 | + |
| 23 | +jest.mock('ant-design-vue/es/notification', () => ({ |
| 24 | + error: jest.fn(), |
| 25 | + warning: jest.fn(), |
| 26 | + warn: jest.fn() |
| 27 | +})) |
| 28 | +jest.mock('@/router', () => ({ |
| 29 | + currentRoute: { value: { path: '/dashboard' } }, |
| 30 | + push: jest.fn() |
| 31 | +})) |
| 32 | +jest.mock('@/store', () => ({ |
| 33 | + getters: { countNotify: 0 }, |
| 34 | + commit: jest.fn(), |
| 35 | + dispatch: jest.fn(() => Promise.resolve()) |
| 36 | +})) |
| 37 | +jest.mock('@/locales', () => ({ |
| 38 | + i18n: { global: { t: (key) => key } } |
| 39 | +})) |
| 40 | +jest.mock('@/vue-app', () => ({ |
| 41 | + vueProps: { $localStorage: { get: () => null } } |
| 42 | +})) |
| 43 | + |
| 44 | +const unauthorizedAdapter = (config) => Promise.reject(Object.assign(new Error('Request failed with status code 401'), { |
| 45 | + config, |
| 46 | + isAxiosError: true, |
| 47 | + response: { |
| 48 | + status: 401, |
| 49 | + config, |
| 50 | + data: { listvirtualmachinesresponse: { errorcode: 401, errortext: 'unable to verify user credentials and/or request signature' } } |
| 51 | + } |
| 52 | +})) |
| 53 | + |
| 54 | +const callApi = () => axios.get('/client/api', { |
| 55 | + params: { command: 'listVirtualMachines' }, |
| 56 | + adapter: unauthorizedAdapter |
| 57 | +}).catch(() => {}) |
| 58 | + |
| 59 | +describe('utils > request.js', () => { |
| 60 | + beforeEach(() => { |
| 61 | + jest.clearAllMocks() |
| 62 | + }) |
| 63 | + |
| 64 | + it('shows a session expired warning when the session times out', async () => { |
| 65 | + router.currentRoute.value.path = '/dashboard' |
| 66 | + await callApi() |
| 67 | + |
| 68 | + expect(notification.error).not.toHaveBeenCalled() |
| 69 | + expect(notification.warning).toHaveBeenCalledTimes(1) |
| 70 | + const shown = notification.warning.mock.calls[0][0] |
| 71 | + expect(shown.message).toBe('label.session.expired') |
| 72 | + expect(shown.description).toBe('message.session.expired') |
| 73 | + expect(shown.duration).toBeUndefined() |
| 74 | + expect(store.dispatch).toHaveBeenCalledWith('Logout') |
| 75 | + }) |
| 76 | + |
| 77 | + it('shows nothing when already on the login page', async () => { |
| 78 | + router.currentRoute.value.path = '/user/login' |
| 79 | + await callApi() |
| 80 | + |
| 81 | + expect(notification.error).not.toHaveBeenCalled() |
| 82 | + expect(notification.warning).not.toHaveBeenCalled() |
| 83 | + }) |
| 84 | +}) |
0 commit comments