11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst mockLogs = require('../../fixtures/mock-logs') 31cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm') 41cb0ef41Sopenharmony_ciconst tmock = require('../../fixtures/tmock') 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst auditError = async (t, { command, error, ...config } = {}) => { 71cb0ef41Sopenharmony_ci const { logs, logMocks } = mockLogs() 81cb0ef41Sopenharmony_ci const mockAuditError = tmock(t, '{LIB}/utils/audit-error', logMocks) 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci const mock = await mockNpm(t, { 111cb0ef41Sopenharmony_ci command, 121cb0ef41Sopenharmony_ci config, 131cb0ef41Sopenharmony_ci exec: true, 141cb0ef41Sopenharmony_ci prefixDir: { 'package.json': '{}', 'package-lock.json': '{}' }, 151cb0ef41Sopenharmony_ci }) 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci const res = {} 181cb0ef41Sopenharmony_ci try { 191cb0ef41Sopenharmony_ci res.result = mockAuditError(mock.npm, error ? { error } : {}) 201cb0ef41Sopenharmony_ci } catch (err) { 211cb0ef41Sopenharmony_ci res.error = err 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci return { 251cb0ef41Sopenharmony_ci ...res, 261cb0ef41Sopenharmony_ci logs: logs.warn.filter((l) => l[0] === 'audit'), 271cb0ef41Sopenharmony_ci output: mock.joinedOutput(), 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cit.test('no error, not audit command', async t => { 321cb0ef41Sopenharmony_ci const { result, error, logs, output } = await auditError(t, { command: 'install' }) 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci t.equal(result, false, 'no error') 351cb0ef41Sopenharmony_ci t.notOk(error, 'no error') 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci t.match(output.trim(), /up to date/, 'install output') 381cb0ef41Sopenharmony_ci t.match(output.trim(), /found 0 vulnerabilities/, 'install output') 391cb0ef41Sopenharmony_ci t.strictSame(logs, [], 'no warnings') 401cb0ef41Sopenharmony_ci}) 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cit.test('error, not audit command', async t => { 431cb0ef41Sopenharmony_ci const { result, error, logs, output } = await auditError(t, { 441cb0ef41Sopenharmony_ci command: 'install', 451cb0ef41Sopenharmony_ci error: { 461cb0ef41Sopenharmony_ci message: 'message', 471cb0ef41Sopenharmony_ci body: Buffer.from('body'), 481cb0ef41Sopenharmony_ci method: 'POST', 491cb0ef41Sopenharmony_ci uri: 'https://example.com/not/a/registry', 501cb0ef41Sopenharmony_ci headers: { 511cb0ef41Sopenharmony_ci head: ['ers'], 521cb0ef41Sopenharmony_ci }, 531cb0ef41Sopenharmony_ci statusCode: '420', 541cb0ef41Sopenharmony_ci }, 551cb0ef41Sopenharmony_ci }) 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci t.equal(result, true, 'had error') 581cb0ef41Sopenharmony_ci t.notOk(error, 'no error') 591cb0ef41Sopenharmony_ci t.match(output.trim(), /up to date/, 'install output') 601cb0ef41Sopenharmony_ci t.match(output.trim(), /found 0 vulnerabilities/, 'install output') 611cb0ef41Sopenharmony_ci t.strictSame(logs, [], 'no warnings') 621cb0ef41Sopenharmony_ci}) 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_cit.test('error, audit command, not json', async t => { 651cb0ef41Sopenharmony_ci const { result, error, logs, output } = await auditError(t, { 661cb0ef41Sopenharmony_ci command: 'audit', 671cb0ef41Sopenharmony_ci error: { 681cb0ef41Sopenharmony_ci message: 'message', 691cb0ef41Sopenharmony_ci body: Buffer.from('body error text'), 701cb0ef41Sopenharmony_ci method: 'POST', 711cb0ef41Sopenharmony_ci uri: 'https://example.com/not/a/registry', 721cb0ef41Sopenharmony_ci headers: { 731cb0ef41Sopenharmony_ci head: ['ers'], 741cb0ef41Sopenharmony_ci }, 751cb0ef41Sopenharmony_ci statusCode: '420', 761cb0ef41Sopenharmony_ci }, 771cb0ef41Sopenharmony_ci }) 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci t.equal(result, undefined) 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci t.ok(error, 'throws error') 821cb0ef41Sopenharmony_ci t.match(output, 'body error text', 'some output') 831cb0ef41Sopenharmony_ci t.strictSame(logs, [['audit', 'message']], 'some warnings') 841cb0ef41Sopenharmony_ci}) 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_cit.test('error, audit command, json', async t => { 871cb0ef41Sopenharmony_ci const { result, error, logs, output } = await auditError(t, { 881cb0ef41Sopenharmony_ci json: true, 891cb0ef41Sopenharmony_ci command: 'audit', 901cb0ef41Sopenharmony_ci error: { 911cb0ef41Sopenharmony_ci message: 'message', 921cb0ef41Sopenharmony_ci body: { response: 'body' }, 931cb0ef41Sopenharmony_ci method: 'POST', 941cb0ef41Sopenharmony_ci uri: 'https://username:password@example.com/not/a/registry', 951cb0ef41Sopenharmony_ci headers: { 961cb0ef41Sopenharmony_ci head: ['ers'], 971cb0ef41Sopenharmony_ci }, 981cb0ef41Sopenharmony_ci statusCode: '420', 991cb0ef41Sopenharmony_ci }, 1001cb0ef41Sopenharmony_ci }) 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci t.equal(result, undefined) 1031cb0ef41Sopenharmony_ci t.ok(error, 'throws error') 1041cb0ef41Sopenharmony_ci t.match(output, 1051cb0ef41Sopenharmony_ci '{\n' + 1061cb0ef41Sopenharmony_ci ' "message": "message",\n' + 1071cb0ef41Sopenharmony_ci ' "method": "POST",\n' + 1081cb0ef41Sopenharmony_ci ' "uri": "https://username:***@example.com/not/a/registry",\n' + 1091cb0ef41Sopenharmony_ci ' "headers": {\n' + 1101cb0ef41Sopenharmony_ci ' "head": [\n' + 1111cb0ef41Sopenharmony_ci ' "ers"\n' + 1121cb0ef41Sopenharmony_ci ' ]\n' + 1131cb0ef41Sopenharmony_ci ' },\n' + 1141cb0ef41Sopenharmony_ci ' "statusCode": "420",\n' + 1151cb0ef41Sopenharmony_ci ' "body": {\n' + 1161cb0ef41Sopenharmony_ci ' "response": "body"\n' + 1171cb0ef41Sopenharmony_ci ' }\n' + 1181cb0ef41Sopenharmony_ci '}' 1191cb0ef41Sopenharmony_ci , 'some output') 1201cb0ef41Sopenharmony_ci t.strictSame(logs, [['audit', 'message']], 'some warnings') 1211cb0ef41Sopenharmony_ci}) 122