summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/security_reports/security_reports_app_spec.js
blob: 221da35de3d73863ad8d428f2953da8cad896e0a (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { merge } from 'lodash';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import Vuex from 'vuex';
import createMockApollo from 'helpers/mock_apollo_helper';
import { trimText } from 'helpers/text_helper';
import waitForPromises from 'helpers/wait_for_promises';
import {
  expectedDownloadDropdownPropsWithText,
  securityReportMergeRequestDownloadPathsQueryNoArtifactsResponse,
  securityReportMergeRequestDownloadPathsQueryResponse,
  sastDiffSuccessMock,
  secretDetectionDiffSuccessMock,
} from 'jest/vue_shared/security_reports/mock_data';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import HelpIcon from '~/vue_shared/security_reports/components/help_icon.vue';
import SecurityReportDownloadDropdown from '~/vue_shared/security_reports/components/security_report_download_dropdown.vue';
import {
  REPORT_TYPE_SAST,
  REPORT_TYPE_SECRET_DETECTION,
} from '~/vue_shared/security_reports/constants';
import securityReportMergeRequestDownloadPathsQuery from '~/vue_shared/security_reports/graphql/queries/security_report_merge_request_download_paths.query.graphql';
import SecurityReportsApp from '~/vue_shared/security_reports/security_reports_app.vue';

jest.mock('~/flash');

Vue.use(VueApollo);
Vue.use(Vuex);

const SAST_COMPARISON_PATH = '/sast.json';
const SECRET_DETECTION_COMPARISON_PATH = '/secret_detection.json';

describe('Security reports app', () => {
  let wrapper;

  const props = {
    pipelineId: 123,
    projectId: 456,
    securityReportsDocsPath: '/docs',
    discoverProjectSecurityPath: '/discoverProjectSecurityPath',
  };

  const createComponent = (options) => {
    wrapper = mount(
      SecurityReportsApp,
      merge(
        {
          propsData: { ...props },
          stubs: {
            HelpIcon: true,
          },
        },
        options,
      ),
    );
  };

  const pendingHandler = () => new Promise(() => {});
  const successHandler = () =>
    Promise.resolve({ data: securityReportMergeRequestDownloadPathsQueryResponse });
  const successEmptyHandler = () =>
    Promise.resolve({ data: securityReportMergeRequestDownloadPathsQueryNoArtifactsResponse });
  const failureHandler = () => Promise.resolve({ errors: [{ message: 'some error' }] });
  const createMockApolloProvider = (handler) => {
    const requestHandlers = [[securityReportMergeRequestDownloadPathsQuery, handler]];

    return createMockApollo(requestHandlers);
  };

  const findDownloadDropdown = () => wrapper.findComponent(SecurityReportDownloadDropdown);
  const findHelpIconComponent = () => wrapper.findComponent(HelpIcon);

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

  describe('given the artifacts query is loading', () => {
    beforeEach(() => {
      createComponent({
        apolloProvider: createMockApolloProvider(pendingHandler),
      });
    });

    // TODO: Remove this assertion as part of
    // https://gitlab.com/gitlab-org/gitlab/-/issues/273431
    it('initially renders nothing', () => {
      expect(wrapper.html()).toBe('');
    });
  });

  describe('given the artifacts query loads successfully', () => {
    beforeEach(() => {
      createComponent({
        apolloProvider: createMockApolloProvider(successHandler),
      });
    });

    it('renders the download dropdown', () => {
      expect(findDownloadDropdown().props()).toEqual(expectedDownloadDropdownPropsWithText);
    });

    it('renders the expected message', () => {
      expect(wrapper.text()).toContain(SecurityReportsApp.i18n.scansHaveRun);
    });

    it('renders a help link', () => {
      expect(findHelpIconComponent().props()).toEqual({
        helpPath: props.securityReportsDocsPath,
        discoverProjectSecurityPath: props.discoverProjectSecurityPath,
      });
    });
  });

  describe('given the artifacts query loads successfully with no artifacts', () => {
    beforeEach(() => {
      createComponent({
        apolloProvider: createMockApolloProvider(successEmptyHandler),
      });
    });

    // TODO: Remove this assertion as part of
    // https://gitlab.com/gitlab-org/gitlab/-/issues/273431
    it('initially renders nothing', () => {
      expect(wrapper.html()).toBe('');
    });
  });

  describe('given the artifacts query fails', () => {
    beforeEach(() => {
      createComponent({
        apolloProvider: createMockApolloProvider(failureHandler),
      });
    });

    it('calls createAlert correctly', () => {
      expect(createAlert).toHaveBeenCalledWith({
        message: SecurityReportsApp.i18n.apiError,
        captureError: true,
        error: expect.any(Error),
      });
    });

    // TODO: Remove this assertion as part of
    // https://gitlab.com/gitlab-org/gitlab/-/issues/273431
    it('renders nothing', () => {
      expect(wrapper.html()).toBe('');
    });
  });

  describe('given the coreSecurityMrWidgetCounts feature flag is enabled', () => {
    let mock;

    const createComponentWithFlagEnabled = (options) =>
      createComponent(
        merge(options, {
          provide: {
            glFeatures: {
              coreSecurityMrWidgetCounts: true,
            },
          },
          apolloProvider: createMockApolloProvider(successHandler),
        }),
      );

    beforeEach(() => {
      mock = new MockAdapter(axios);
    });

    afterEach(() => {
      mock.restore();
    });

    const SAST_SUCCESS_MESSAGE =
      'Security scanning detected 1 potential vulnerability 1 Critical 0 High and 0 Others';
    const SECRET_DETECTION_SUCCESS_MESSAGE =
      'Security scanning detected 2 potential vulnerabilities 1 Critical 1 High and 0 Others';
    describe.each`
      reportType                      | pathProp                           | path                                | successResponse                   | successMessage
      ${REPORT_TYPE_SAST}             | ${'sastComparisonPath'}            | ${SAST_COMPARISON_PATH}             | ${sastDiffSuccessMock}            | ${SAST_SUCCESS_MESSAGE}
      ${REPORT_TYPE_SECRET_DETECTION} | ${'secretDetectionComparisonPath'} | ${SECRET_DETECTION_COMPARISON_PATH} | ${secretDetectionDiffSuccessMock} | ${SECRET_DETECTION_SUCCESS_MESSAGE}
    `(
      'given a $pathProp and $reportType artifact',
      ({ pathProp, path, successResponse, successMessage }) => {
        describe('when loading', () => {
          beforeEach(() => {
            mock = new MockAdapter(axios, { delayResponse: 1 });
            mock.onGet(path).replyOnce(HTTP_STATUS_OK, successResponse);

            createComponentWithFlagEnabled({
              propsData: {
                [pathProp]: path,
              },
            });

            return waitForPromises();
          });

          it('should have loading message', () => {
            expect(wrapper.text()).toContain('Security scanning is loading');
          });

          it('renders the download dropdown', () => {
            expect(findDownloadDropdown().props()).toEqual(expectedDownloadDropdownPropsWithText);
          });
        });

        describe('when successfully loaded', () => {
          beforeEach(() => {
            mock.onGet(path).replyOnce(HTTP_STATUS_OK, successResponse);

            createComponentWithFlagEnabled({
              propsData: {
                [pathProp]: path,
              },
            });

            return waitForPromises();
          });

          it('should show counts', () => {
            expect(trimText(wrapper.text())).toContain(successMessage);
          });

          it('renders the download dropdown', () => {
            expect(findDownloadDropdown().props()).toEqual(expectedDownloadDropdownPropsWithText);
          });
        });

        describe('when an error occurs', () => {
          beforeEach(() => {
            mock.onGet(path).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR);

            createComponentWithFlagEnabled({
              propsData: {
                [pathProp]: path,
              },
            });

            return waitForPromises();
          });

          it('should show error message', () => {
            expect(trimText(wrapper.text())).toContain('Loading resulted in an error');
          });

          it('renders the download dropdown', () => {
            expect(findDownloadDropdown().props()).toEqual(expectedDownloadDropdownPropsWithText);
          });
        });

        describe('when the comparison endpoint is not provided', () => {
          beforeEach(() => {
            mock.onGet(path).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR);

            createComponentWithFlagEnabled();

            return waitForPromises();
          });

          it('renders the basic scansHaveRun  message', () => {
            expect(wrapper.text()).toContain(SecurityReportsApp.i18n.scansHaveRun);
          });
        });
      },
    );
  });
});