summaryrefslogtreecommitdiff
path: root/spec/frontend/prometheus_alerts/components/reset_key_spec.js
blob: dc5fdb1dffce8043e48881dd8a834892c5a52698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { GlModal } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import ResetKey from '~/prometheus_alerts/components/reset_key.vue';
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');
    });

    it('reset updates key', async () => {
      mock.onPost(propsData.changeKeyUrl).replyOnce(200, { token: 'newToken' });

      vm.find(GlModal).vm.$emit('ok');

      await nextTick();
      await waitForPromises();
      expect(vm.vm.authorizationKey).toEqual('newToken');
      expect(vm.find('#authorization-key').attributes('value')).toEqual('newToken');
    });

    it('reset key failure shows error', async () => {
      mock.onPost(propsData.changeKeyUrl).replyOnce(500);

      vm.find(GlModal).vm.$emit('ok');

      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.',
      );
    });
  });

  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('');
    });

    it('Generate key button triggers key change', async () => {
      mock.onPost(propsData.changeKeyUrl).replyOnce(200, { token: 'newToken' });

      vm.find('.js-reset-auth-key').vm.$emit('click');

      await waitForPromises();
      expect(vm.find('#authorization-key').attributes('value')).toEqual('newToken');
    });
  });
});