summaryrefslogtreecommitdiff
path: root/spec/frontend/alert_settings/alert_settings_form_spec.js
blob: 5a04d7686454c4a203b98dddc6d56b055ab5d647 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { shallowMount } from '@vue/test-utils';
import { GlModal, GlAlert } from '@gitlab/ui';
import AlertsSettingsForm from '~/alerts_settings/components/alerts_settings_form.vue';
import ToggleButton from '~/vue_shared/components/toggle_button.vue';

const PROMETHEUS_URL = '/prometheus/alerts/notify.json';
const GENERIC_URL = '/alerts/notify.json';
const KEY = 'abcedfg123';
const INVALID_URL = 'http://invalid';
const ACTIVATED = false;

const defaultProps = {
  generic: {
    initialAuthorizationKey: KEY,
    formPath: INVALID_URL,
    url: GENERIC_URL,
    alertsSetupUrl: INVALID_URL,
    alertsUsageUrl: INVALID_URL,
    activated: ACTIVATED,
  },
  prometheus: {
    prometheusAuthorizationKey: KEY,
    prometheusFormPath: INVALID_URL,
    prometheusUrl: PROMETHEUS_URL,
    activated: ACTIVATED,
  },
  opsgenie: {
    opsgenieMvcIsAvailable: true,
    formPath: INVALID_URL,
    activated: ACTIVATED,
    opsgenieMvcTargetUrl: GENERIC_URL,
  },
};

describe('AlertsSettingsForm', () => {
  let wrapper;
  let mockAxios;

  const createComponent = (props = defaultProps, { methods } = {}, data) => {
    wrapper = shallowMount(AlertsSettingsForm, {
      data() {
        return { ...data };
      },
      propsData: {
        ...defaultProps,
        ...props,
      },
      methods,
    });
  };

  const findSelect = () => wrapper.find('[data-testid="alert-settings-select"]');
  const findJsonInput = () => wrapper.find('#alert-json');
  const findUrl = () => wrapper.find('#url');
  const findAuthorizationKey = () => wrapper.find('#authorization-key');
  const findApiUrl = () => wrapper.find('#api-url');

  beforeEach(() => {
    mockAxios = new MockAdapter(axios);
    setFixtures(`
    <div>
      <span class="js-service-active-status fa fa-circle" data-value="true"></span>
      <span class="js-service-active-status fa fa-power-off" data-value="false"></span>
    </div>`);
  });

  afterEach(() => {
    wrapper.destroy();
    mockAxios.restore();
  });

  describe('with default values', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders the initial template', () => {
      expect(wrapper.html()).toMatchSnapshot();
    });
  });

  describe('reset key', () => {
    it('triggers resetKey method', () => {
      const resetGenericKey = jest.fn();
      const methods = { resetGenericKey };
      createComponent(defaultProps, { methods });

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

      expect(resetGenericKey).toHaveBeenCalled();
    });

    it('updates the authorization key on success', () => {
      const formPath = 'some/path';
      mockAxios.onPut(formPath, { service: { token: '' } }).replyOnce(200, { token: 'newToken' });
      createComponent({ generic: { ...defaultProps.generic, formPath } });

      return wrapper.vm.resetGenericKey().then(() => {
        expect(findAuthorizationKey().attributes('value')).toBe('newToken');
      });
    });

    it('shows a alert message on error', () => {
      const formPath = 'some/path';
      mockAxios.onPut(formPath).replyOnce(404);

      createComponent({ generic: { ...defaultProps.generic, formPath } });

      return wrapper.vm.resetGenericKey().then(() => {
        expect(wrapper.find(GlAlert).exists()).toBe(true);
      });
    });
  });

  describe('activate toggle', () => {
    it('triggers toggleActivated method', () => {
      const toggleService = jest.fn();
      const methods = { toggleService };
      createComponent(defaultProps, { methods });

      wrapper.find(ToggleButton).vm.$emit('change', true);

      expect(toggleService).toHaveBeenCalled();
    });

    describe('error is encountered', () => {
      beforeEach(() => {
        const formPath = 'some/path';
        mockAxios.onPut(formPath).replyOnce(500);
      });

      it('restores previous value', () => {
        createComponent({ generic: { ...defaultProps.generic, initialActivated: false } });
        return wrapper.vm.resetGenericKey().then(() => {
          expect(wrapper.find(ToggleButton).props('value')).toBe(false);
        });
      });
    });
  });

  describe('prometheus is active', () => {
    beforeEach(() => {
      createComponent(
        { prometheus: { ...defaultProps.prometheus, prometheusIsActivated: true } },
        {},
        {
          selectedEndpoint: 'prometheus',
        },
      );
    });

    it('renders a valid "select"', () => {
      expect(findSelect().exists()).toBe(true);
    });

    it('shows the API URL input', () => {
      expect(findApiUrl().exists()).toBe(true);
    });

    it('shows the correct default API URL', () => {
      expect(findUrl().attributes('value')).toBe(PROMETHEUS_URL);
    });
  });

  describe('opsgenie is active', () => {
    beforeEach(() => {
      createComponent(
        { opsgenie: { ...defaultProps.opsgenie, opsgenieMvcActivated: true } },
        {},
        {
          selectedEndpoint: 'opsgenie',
        },
      );
    });

    it('shows a input for the opsgenie target URL', () => {
      expect(findApiUrl().exists()).toBe(true);
      expect(findSelect().attributes('value')).toBe('opsgenie');
    });
  });

  describe('trigger test alert', () => {
    beforeEach(() => {
      createComponent({ generic: { ...defaultProps.generic, initialActivated: true } }, {}, true);
    });

    it('should enable the JSON input', () => {
      expect(findJsonInput().exists()).toBe(true);
      expect(findJsonInput().props('value')).toBe(null);
    });

    it('should validate JSON input', () => {
      createComponent({ generic: { ...defaultProps.generic } }, true, {
        testAlertJson: '{ "value": "test" }',
      });

      findJsonInput().vm.$emit('change');
      return wrapper.vm.$nextTick().then(() => {
        expect(findJsonInput().attributes('state')).toBe('true');
      });
    });

    describe('alert service is toggled', () => {
      it('should show a info alert if successful', () => {
        const formPath = 'some/path';
        const toggleService = true;
        mockAxios.onPut(formPath).replyOnce(200);

        createComponent({ generic: { ...defaultProps.generic, formPath } });

        return wrapper.vm.toggleActivated(toggleService).then(() => {
          expect(wrapper.find(GlAlert).attributes('variant')).toBe('info');
        });
      });

      it('should show a error alert if failed', () => {
        const formPath = 'some/path';
        const toggleService = true;
        mockAxios.onPut(formPath).replyOnce(422, {
          errors: 'Error message to display',
        });

        createComponent({ generic: { ...defaultProps.generic, formPath } });

        return wrapper.vm.toggleActivated(toggleService).then(() => {
          expect(wrapper.find(GlAlert).attributes('variant')).toBe('danger');
        });
      });
    });
  });
});