|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import {NoopReporter} from '../../src/reporters/index.js'; |
| 4 | +import {run as buildRun} from './_helpers.js'; |
| 5 | +import {run as audit} from '../../src/cli/commands/audit.js'; |
| 6 | +import {promisify} from '../../src/util/promise.js'; |
| 7 | + |
| 8 | +const path = require('path'); |
| 9 | +const zlib = require('zlib'); |
| 10 | +const gunzip = promisify(zlib.gunzip); |
| 11 | + |
| 12 | +const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'audit'); |
| 13 | + |
| 14 | +const setupMockRequestManager = function(config) { |
| 15 | + const apiResponse = JSON.stringify(getAuditResponse(config), null, 2); |
| 16 | + // $FlowFixMe |
| 17 | + config.requestManager.request = jest.fn(); |
| 18 | + config.requestManager.request.mockReturnValue( |
| 19 | + new Promise(resolve => { |
| 20 | + resolve(apiResponse); |
| 21 | + }), |
| 22 | + ); |
| 23 | +}; |
| 24 | + |
| 25 | +const setupMockReporter = function(reporter) { |
| 26 | + // $FlowFixMe |
| 27 | + reporter.auditAdvisory = jest.fn(); |
| 28 | + // $FlowFixMe |
| 29 | + reporter.auditAction = jest.fn(); |
| 30 | + // $FlowFixMe |
| 31 | + reporter.auditSummary = jest.fn(); |
| 32 | +}; |
| 33 | + |
| 34 | +const getAuditResponse = function(config): Object { |
| 35 | + // $FlowFixMe |
| 36 | + return require(path.join(config.cwd, 'audit-api-response.json')); |
| 37 | +}; |
| 38 | + |
| 39 | +const runAudit = buildRun.bind( |
| 40 | + null, |
| 41 | + NoopReporter, |
| 42 | + fixturesLoc, |
| 43 | + async (args, flags, config, reporter, lockfile, getStdout): Promise<string> => { |
| 44 | + setupMockRequestManager(config); |
| 45 | + setupMockReporter(reporter); |
| 46 | + await audit(config, reporter, flags, args); |
| 47 | + return getStdout(); |
| 48 | + }, |
| 49 | +); |
| 50 | + |
| 51 | +test.concurrent('sends correct dependency map to audit api for single dependency.', () => { |
| 52 | + const expectedApiPost = { |
| 53 | + name: 'yarn-test', |
| 54 | + install: [], |
| 55 | + remove: [], |
| 56 | + metadata: {}, |
| 57 | + requires: { |
| 58 | + minimatch: '^3.0.0', |
| 59 | + }, |
| 60 | + dependencies: { |
| 61 | + minimatch: { |
| 62 | + version: '3.0.0', |
| 63 | + integrity: 'sha1-UjYVelHk8ATBd/s8Un/33Xjw74M=', |
| 64 | + requires: { |
| 65 | + 'brace-expansion': '^1.0.0', |
| 66 | + }, |
| 67 | + dependencies: {}, |
| 68 | + }, |
| 69 | + 'brace-expansion': { |
| 70 | + version: '1.1.11', |
| 71 | + integrity: 'sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==', |
| 72 | + requires: { |
| 73 | + 'balanced-match': '^1.0.0', |
| 74 | + 'concat-map': '0.0.1', |
| 75 | + }, |
| 76 | + dependencies: {}, |
| 77 | + }, |
| 78 | + 'balanced-match': { |
| 79 | + version: '1.0.0', |
| 80 | + integrity: 'sha1-ibTRmasr7kneFk6gK4nORi1xt2c=', |
| 81 | + requires: {}, |
| 82 | + dependencies: {}, |
| 83 | + }, |
| 84 | + 'concat-map': { |
| 85 | + version: '0.0.1', |
| 86 | + integrity: 'sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=', |
| 87 | + requires: {}, |
| 88 | + dependencies: {}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + version: '0.0.0', |
| 92 | + }; |
| 93 | + |
| 94 | + return runAudit([], {}, 'single-vulnerable-dep-installed', async config => { |
| 95 | + const calledWithPipe = config.requestManager.request.mock.calls[0][0].body; |
| 96 | + const calledWith = JSON.parse(await gunzip(calledWithPipe)); |
| 97 | + expect(calledWith).toEqual(expectedApiPost); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +test('calls reporter auditAdvisory with correct data', () => { |
| 102 | + return runAudit([], {}, 'single-vulnerable-dep-installed', (config, reporter) => { |
| 103 | + const apiResponse = getAuditResponse(config); |
| 104 | + expect(reporter.auditAdvisory).toBeCalledWith(apiResponse.actions[0].resolves[0], apiResponse.advisories['118']); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +// *** Test temporarily removed due to inability to correctly puggest actions to the user. |
| 109 | +// test('calls reporter auditAction with correct data', () => { |
| 110 | +// return runAudit([], {}, 'single-vulnerable-dep-installed', (config, reporter) => { |
| 111 | +// const apiResponse = getAuditResponse(config); |
| 112 | +// expect(reporter.auditAction).toBeCalledWith({ |
| 113 | +// cmd: 'yarn upgrade minimatch@3.0.4', |
| 114 | +// isBreaking: false, |
| 115 | +// action: apiResponse.actions[0], |
| 116 | +// }); |
| 117 | +// }); |
| 118 | +// }); |
| 119 | + |
| 120 | +test('calls reporter auditSummary with correct data', () => { |
| 121 | + return runAudit([], {}, 'single-vulnerable-dep-installed', (config, reporter) => { |
| 122 | + const apiResponse = getAuditResponse(config); |
| 123 | + expect(reporter.auditSummary).toBeCalledWith(apiResponse.metadata); |
| 124 | + }); |
| 125 | +}); |
0 commit comments