1- const { test } = require ( 'tap' )
1+ const t = require ( 'tap' )
22const requireInject = require ( 'require-inject' )
33const audit = require ( '../../lib/audit.js' )
44
5- test ( 'should audit using Arborist' , t => {
5+ t . test ( 'should audit using Arborist' , t => {
66 let ARB_ARGS = null
77 let AUDIT_CALLED = false
88 let REIFY_OUTPUT_CALLED = false
@@ -29,6 +29,7 @@ test('should audit using Arborist', t => {
2929 ARB_OBJ = this
3030 this . audit = ( ) => {
3131 AUDIT_CALLED = true
32+ this . auditReport = { }
3233 }
3334 } ,
3435 '../../lib/utils/reify-output.js' : arb => {
@@ -62,7 +63,7 @@ test('should audit using Arborist', t => {
6263 t . end ( )
6364} )
6465
65- test ( 'should audit - json' , t => {
66+ t . test ( 'should audit - json' , t => {
6667 const audit = requireInject ( '../../lib/audit.js' , {
6768 '../../lib/npm.js' : {
6869 prefix : 'foo' ,
@@ -75,7 +76,9 @@ test('should audit - json', t => {
7576 exitCode : 0
7677 } ) ,
7778 '@npmcli/arborist' : function ( ) {
78- this . audit = ( ) => { }
79+ this . audit = ( ) => {
80+ this . auditReport = { }
81+ }
7982 } ,
8083 '../../lib/utils/reify-output.js' : ( ) => { } ,
8184 '../../lib/utils/output.js' : ( ) => { }
@@ -87,7 +90,84 @@ test('should audit - json', t => {
8790 } )
8891} )
8992
90- test ( 'completion' , t => {
93+ t . test ( 'report endpoint error' , t => {
94+ for ( const json of [ true , false ] ) {
95+ t . test ( `json=${ json } ` , t => {
96+ const OUTPUT = [ ]
97+ const LOGS = [ ]
98+ const mocks = {
99+ '../../lib/npm.js' : {
100+ prefix : 'foo' ,
101+ command : 'audit' ,
102+ flatOptions : {
103+ json
104+ } ,
105+ log : {
106+ warn : ( ...warning ) => LOGS . push ( warning )
107+ }
108+ } ,
109+ 'npm-audit-report' : ( ) => {
110+ throw new Error ( 'should not call audit report when there are errors' )
111+ } ,
112+ '@npmcli/arborist' : function ( ) {
113+ this . audit = ( ) => {
114+ this . auditReport = {
115+ error : {
116+ message : 'hello, this didnt work' ,
117+ method : 'POST' ,
118+ uri : 'https://example.com/' ,
119+ headers : {
120+ head : [ 'ers' ]
121+ } ,
122+ statusCode : 420 ,
123+ body : json ? { nope : 'lol' }
124+ : Buffer . from ( 'i had a vuln but i eated it lol' )
125+ }
126+ }
127+ }
128+ } ,
129+ '../../lib/utils/reify-output.js' : ( ) => { } ,
130+ '../../lib/utils/output.js' : ( ...msg ) => {
131+ OUTPUT . push ( msg )
132+ }
133+ }
134+ // have to pass mocks to both to get the npm and output set right
135+ const auditError = requireInject ( '../../lib/utils/audit-error.js' , mocks )
136+ const audit = requireInject ( '../../lib/audit.js' , {
137+ ...mocks ,
138+ '../../lib/utils/audit-error.js' : auditError
139+ } )
140+
141+ audit ( [ ] , ( err ) => {
142+ t . equal ( err , 'audit endpoint returned an error' )
143+ t . strictSame ( OUTPUT , [
144+ [
145+ json ? '{\n' +
146+ ' "message": "hello, this didnt work",\n' +
147+ ' "method": "POST",\n' +
148+ ' "uri": "https://example.com/",\n' +
149+ ' "headers": {\n' +
150+ ' "head": [\n' +
151+ ' "ers"\n' +
152+ ' ]\n' +
153+ ' },\n' +
154+ ' "statusCode": 420,\n' +
155+ ' "body": {\n' +
156+ ' "nope": "lol"\n' +
157+ ' }\n' +
158+ '}'
159+ : 'i had a vuln but i eated it lol'
160+ ]
161+ ] )
162+ t . strictSame ( LOGS , [ [ 'audit' , 'hello, this didnt work' ] ] )
163+ t . end ( )
164+ } )
165+ } )
166+ }
167+ t . end ( )
168+ } )
169+
170+ t . test ( 'completion' , t => {
91171 t . test ( 'fix' , t => {
92172 audit . completion ( {
93173 conf : { argv : { remain : [ 'npm' , 'audit' ] } }
@@ -117,4 +197,4 @@ test('completion', t => {
117197 } )
118198
119199 t . end ( )
120- } )
200+ } )
0 commit comments