2021-02-14 13:09:20 -05:00
|
|
|
import { GlModal } from '@gitlab/ui';
|
2020-04-01 20:08:11 -04:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-01-25 10:12:32 -05:00
|
|
|
import { nextTick } from 'vue';
|
2020-04-01 20:08:11 -04:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-08-17 17:09:56 -04:00
|
|
|
import ResetKey from '~/prometheus_alerts/components/reset_key.vue';
|
2020-04-01 20:08:11 -04:00
|
|
|
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
|
|
|
|
|
|
|
|
describe('ResetKey', () => {
|
|
|
|
let mock;
|
|
|
|
let vm;
|
|
|
|
|
|
|
|
const propsData = {
|
|
|
|
initialAuthorizationKey: 'abcd1234',
|
|
|
|
changeKeyUrl: '/updateKeyUrl',
|
|
|
|
notifyUrl: '/root/autodevops-deploy/prometheus/alerts/notify.json',
|
|
|
|
learnMoreUrl: '/learnMore',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
setFixtures('<div class="flash-container"></div><div id="reset-key"></div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
vm.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authorization key exists', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
propsData.initialAuthorizationKey = 'abcd1234';
|
|
|
|
vm = shallowMount(ResetKey, {
|
|
|
|
propsData,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows fields and buttons', () => {
|
|
|
|
expect(vm.find('#notify-url').attributes('value')).toEqual(propsData.notifyUrl);
|
|
|
|
expect(vm.find('#authorization-key').attributes('value')).toEqual(
|
|
|
|
propsData.initialAuthorizationKey,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(vm.findAll(ClipboardButton).length).toBe(2);
|
|
|
|
expect(vm.find('.js-reset-auth-key').text()).toEqual('Reset key');
|
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('reset updates key', async () => {
|
2020-04-01 20:08:11 -04:00
|
|
|
mock.onPost(propsData.changeKeyUrl).replyOnce(200, { token: 'newToken' });
|
|
|
|
|
|
|
|
vm.find(GlModal).vm.$emit('ok');
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
await waitForPromises();
|
|
|
|
expect(vm.vm.authorizationKey).toEqual('newToken');
|
|
|
|
expect(vm.find('#authorization-key').attributes('value')).toEqual('newToken');
|
2020-04-01 20:08:11 -04:00
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('reset key failure shows error', async () => {
|
2020-04-01 20:08:11 -04:00
|
|
|
mock.onPost(propsData.changeKeyUrl).replyOnce(500);
|
|
|
|
|
|
|
|
vm.find(GlModal).vm.$emit('ok');
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
await waitForPromises();
|
|
|
|
expect(vm.find('#authorization-key').attributes('value')).toEqual(
|
|
|
|
propsData.initialAuthorizationKey,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(document.querySelector('.flash-container').innerText.trim()).toEqual(
|
|
|
|
'Failed to reset key. Please try again.',
|
|
|
|
);
|
2020-04-01 20:08:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authorization key has not been set', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
propsData.initialAuthorizationKey = '';
|
|
|
|
vm = shallowMount(ResetKey, {
|
|
|
|
propsData,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows Generate Key button', () => {
|
|
|
|
expect(vm.find('.js-reset-auth-key').text()).toEqual('Generate key');
|
|
|
|
expect(vm.find('#authorization-key').attributes('value')).toEqual('');
|
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('Generate key button triggers key change', async () => {
|
2020-04-01 20:08:11 -04:00
|
|
|
mock.onPost(propsData.changeKeyUrl).replyOnce(200, { token: 'newToken' });
|
|
|
|
|
|
|
|
vm.find('.js-reset-auth-key').vm.$emit('click');
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await waitForPromises();
|
|
|
|
expect(vm.find('#authorization-key').attributes('value')).toEqual('newToken');
|
2020-04-01 20:08:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|