Commit efc90e21 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '220601-Prevent-Download-For-Failed-Vuln-Export-Jobs' into 'master'

Prevent Download For Failed Vulnerability Export Jobs

Closes #220601

See merge request gitlab-org/gitlab!40656
parents 77c218a4 9001fc63
...@@ -57,6 +57,9 @@ export default { ...@@ -57,6 +57,9 @@ export default {
.post(this.vulnerabilitiesExportEndpoint) .post(this.vulnerabilitiesExportEndpoint)
.then(({ data }) => pollUntilComplete(data._links.self)) .then(({ data }) => pollUntilComplete(data._links.self))
.then(({ data }) => { .then(({ data }) => {
if (data.status !== 'finished') {
throw new Error();
}
download({ download({
fileName: `csv-export-${formatDate(new Date(), 'isoDateTime')}.csv`, fileName: `csv-export-${formatDate(new Date(), 'isoDateTime')}.csv`,
url: data._links.download, url: data._links.download,
......
---
title: Prevent Download For Failed Vulnerability Export Jobs
merge_request: 40656
author: Kev @KevSlashNull
type: fixed
...@@ -19,6 +19,7 @@ const vulnerabilitiesExportEndpoint = `${TEST_HOST}/vulnerability_findings.csv`; ...@@ -19,6 +19,7 @@ const vulnerabilitiesExportEndpoint = `${TEST_HOST}/vulnerability_findings.csv`;
describe('Csv Button Export', () => { describe('Csv Button Export', () => {
let mock; let mock;
let wrapper; let wrapper;
let spy;
const issueUrl = 'https://gitlab.com/gitlab-org/gitlab/issues/197111'; const issueUrl = 'https://gitlab.com/gitlab-org/gitlab/issues/197111';
const findPopoverExternalLink = () => wrapper.find({ ref: 'popoverExternalLink' }); const findPopoverExternalLink = () => wrapper.find({ ref: 'popoverExternalLink' });
...@@ -38,6 +39,23 @@ describe('Csv Button Export', () => { ...@@ -38,6 +39,23 @@ describe('Csv Button Export', () => {
}); });
}; };
const setupMocksAndSpy = (statusLink, downloadLink, downloadAnchor, status = 'finished') => {
mock
.onPost(vulnerabilitiesExportEndpoint)
.reply(statusCodes.ACCEPTED, { _links: { self: statusLink } });
mock.onGet(statusLink).reply(() => {
// We need to mock it at this stage because vue internally uses
// document.createElement to mount the elements.
spy = jest.spyOn(document, 'createElement').mockImplementationOnce(() => {
// eslint-disable-next-line no-param-reassign
downloadAnchor.click = jest.fn();
return downloadAnchor;
});
return [statusCodes.OK, { _links: { download: downloadLink }, status }];
});
};
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
localStorage.removeItem(STORAGE_KEY); localStorage.removeItem(STORAGE_KEY);
...@@ -61,21 +79,8 @@ describe('Csv Button Export', () => { ...@@ -61,21 +79,8 @@ describe('Csv Button Export', () => {
const downloadAnchor = document.createElement('a'); const downloadAnchor = document.createElement('a');
const statusLink = '/poll/until/complete'; const statusLink = '/poll/until/complete';
const downloadLink = '/link/to/download'; const downloadLink = '/link/to/download';
let spy;
setupMocksAndSpy(statusLink, downloadLink, downloadAnchor);
mock
.onPost(vulnerabilitiesExportEndpoint)
.reply(statusCodes.ACCEPTED, { _links: { self: statusLink } });
mock.onGet(statusLink).reply(() => {
// We need to mock it at this stage because vue internally uses
// document.createElement to mount the elements.
spy = jest.spyOn(document, 'createElement').mockImplementationOnce(() => {
downloadAnchor.click = jest.fn();
return downloadAnchor;
});
return [statusCodes.OK, { _links: { download: downloadLink } }];
});
findCsvExportButton().vm.$emit('click'); findCsvExportButton().vm.$emit('click');
...@@ -89,6 +94,22 @@ describe('Csv Button Export', () => { ...@@ -89,6 +94,22 @@ describe('Csv Button Export', () => {
}); });
}); });
it(`shows the flash error when the export job status is 'failed'`, () => {
setupMocksAndSpy(
'/poll/until/complete',
'/link/to/download',
document.createElement('a'),
'failed',
);
findCsvExportButton().vm.$emit('click');
return axios.waitForAll().then(() => {
expect(spy).not.toHaveBeenCalled();
expect(createFlash).toHaveBeenCalledWith('There was an error while generating the report.');
});
});
it('shows the flash error when backend fails to generate the export', () => { it('shows the flash error when backend fails to generate the export', () => {
mock.onPost(vulnerabilitiesExportEndpoint).reply(statusCodes.NOT_FOUND, {}); mock.onPost(vulnerabilitiesExportEndpoint).reply(statusCodes.NOT_FOUND, {});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment