summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/security_reports/security_reports_app_spec.js
blob: ab87d80b291edc5387ea413c1041193e503dbb96 (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
import { mount } from '@vue/test-utils';
import Api from '~/api';
import Flash from '~/flash';
import SecurityReportsApp from '~/vue_shared/security_reports/security_reports_app.vue';

jest.mock('~/flash');

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

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

  const createComponent = () => {
    wrapper = mount(SecurityReportsApp, {
      propsData: { ...props },
    });
  };

  const anyParams = expect.any(Object);

  const findPipelinesTabAnchor = () => wrapper.find('[data-testid="show-pipelines"]');
  const findHelpLink = () => wrapper.find('[data-testid="help"]');
  const setupMrTabsMock = () => {
    mrTabsMock = { tabShown: jest.fn() };
    window.mrTabs = mrTabsMock;
  };
  const setupMockJobArtifact = reportType => {
    jest
      .spyOn(Api, 'pipelineJobs')
      .mockResolvedValue({ data: [{ artifacts: [{ file_type: reportType }] }] });
  };

  afterEach(() => {
    wrapper.destroy();
    delete window.mrTabs;
  });

  describe.each(SecurityReportsApp.reportTypes)('given a report type %p', reportType => {
    beforeEach(() => {
      window.mrTabs = { tabShown: jest.fn() };
      setupMockJobArtifact(reportType);
      createComponent();
      return wrapper.vm.$nextTick();
    });

    it('calls the pipelineJobs API correctly', () => {
      expect(Api.pipelineJobs).toHaveBeenCalledTimes(1);
      expect(Api.pipelineJobs).toHaveBeenCalledWith(props.projectId, props.pipelineId, anyParams);
    });

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

    describe('clicking the anchor to the pipelines tab', () => {
      beforeEach(() => {
        setupMrTabsMock();
        findPipelinesTabAnchor().trigger('click');
      });

      it('calls the mrTabs.tabShown global', () => {
        expect(mrTabsMock.tabShown.mock.calls).toEqual([['pipelines']]);
      });
    });

    it('renders a help link', () => {
      expect(findHelpLink().attributes()).toMatchObject({
        href: props.securityReportsDocsPath,
      });
    });
  });

  describe('given a report type "foo"', () => {
    beforeEach(() => {
      setupMockJobArtifact('foo');
      createComponent();
      return wrapper.vm.$nextTick();
    });

    it('calls the pipelineJobs API correctly', () => {
      expect(Api.pipelineJobs).toHaveBeenCalledTimes(1);
      expect(Api.pipelineJobs).toHaveBeenCalledWith(props.projectId, props.pipelineId, anyParams);
    });

    it('renders nothing', () => {
      expect(wrapper.html()).toBe('');
    });
  });

  describe('security artifacts on last page of multi-page response', () => {
    const numPages = 3;

    beforeEach(() => {
      jest
        .spyOn(Api, 'pipelineJobs')
        .mockImplementation(async (projectId, pipelineId, { page }) => {
          const requestedPage = parseInt(page, 10);
          if (requestedPage < numPages) {
            return {
              // Some jobs with no relevant artifacts
              data: [{}, {}],
              headers: { 'x-next-page': String(requestedPage + 1) },
            };
          } else if (requestedPage === numPages) {
            return {
              data: [{ artifacts: [{ file_type: SecurityReportsApp.reportTypes[0] }] }],
            };
          }

          throw new Error('Test failed due to request of non-existent jobs page');
        });

      createComponent();
      return wrapper.vm.$nextTick();
    });

    it('fetches all pages', () => {
      expect(Api.pipelineJobs).toHaveBeenCalledTimes(numPages);
    });

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

  describe('given an error from the API', () => {
    let error;

    beforeEach(() => {
      error = new Error('an error');
      jest.spyOn(Api, 'pipelineJobs').mockRejectedValue(error);
      createComponent();
      return wrapper.vm.$nextTick();
    });

    it('calls the pipelineJobs API correctly', () => {
      expect(Api.pipelineJobs).toHaveBeenCalledTimes(1);
      expect(Api.pipelineJobs).toHaveBeenCalledWith(props.projectId, props.pipelineId, anyParams);
    });

    it('renders nothing', () => {
      expect(wrapper.html()).toBe('');
    });

    it('calls Flash correctly', () => {
      expect(Flash.mock.calls).toEqual([
        [
          {
            message: SecurityReportsApp.i18n.apiError,
            captureError: true,
            error,
          },
        ],
      ]);
    });
  });
});