@@ -41,101 +41,114 @@ describe('snyk secrets test TOON output', () => {
4141 afterAll ( ( ) => server . closePromise ( ) ) ;
4242
4343 describe . each ( [ 0 , 1 ] ) ( '%i findings' , ( count ) => {
44- test . each ( [ '--toon' , '--toon-file-output=result.toon' ] ) (
45- '%s preserves the result and exit code' ,
46- async ( flag ) => {
47- server . setEndpointResponse ( testPath , {
48- data : {
49- id : testId ,
50- type : 'tests' ,
51- attributes : {
52- state : { execution : 'finished' } ,
53- outcome : { result : count ? 'fail' : 'pass' } ,
54- effective_summary : { count } ,
55- raw_summary : { count } ,
56- config : { scan_config : { secrets : { } } } ,
57- } ,
44+ test . each ( [
45+ [ '--toon' , '' ] ,
46+ [ '--toon' , '--toon=full' ] ,
47+ [ '--toon-file-output=result.toon' , '' ] ,
48+ [ '--toon-file-output=result.toon' , '--toon=full' ] ,
49+ ] ) ( '%s %s preserves the result and exit code' , async ( flag , fullFlag ) => {
50+ server . setEndpointResponse ( testPath , {
51+ data : {
52+ id : testId ,
53+ type : 'tests' ,
54+ attributes : {
55+ state : { execution : 'finished' } ,
56+ outcome : { result : count ? 'fail' : 'pass' } ,
57+ effective_summary : { count } ,
58+ raw_summary : { count } ,
59+ config : { scan_config : { secrets : { } } } ,
5860 } ,
59- } ) ;
60- server . setEndpointResponse ( ` ${ testPath } /findings` , {
61- data : count
62- ? [
63- {
64- id : '00000000-0000-4000-8000-000000000003' ,
65- type : 'findings ' ,
66- attributes : {
67- finding_type : 'secret' ,
68- title : 'Synthetic secret' ,
69- key : 'synthetic- secret' ,
70- cause_of_failure : true ,
71- rating : { severity : 'high' } ,
72- locations : [
73- {
74- type : 'source' ,
75- file_path : 'config.txt ' ,
76- from_line : 1 ,
77- to_line : 1 ,
78- } ,
79- ] ,
80- problems : [
81- {
82- id : 'synthetic-secret' ,
83- source : 'secret' ,
84- name : 'Synthetic secret' ,
85- } ,
86- ] ,
87- evidence : [ ] ,
88- policy_modifications : [ ] ,
89- risk : { } ,
90- } ,
61+ } ,
62+ } ) ;
63+ server . setEndpointResponse ( ` ${ testPath } /findings` , {
64+ data : count
65+ ? [
66+ {
67+ id : '00000000-0000-4000-8000-000000000003 ' ,
68+ type : 'findings' ,
69+ attributes : {
70+ finding_type : 'secret' ,
71+ title : 'Synthetic secret' ,
72+ key : 'synthetic-secret' ,
73+ cause_of_failure : true ,
74+ rating : { severity : 'high' } ,
75+ locations : [
76+ {
77+ type : 'source ' ,
78+ file_path : 'config.txt' ,
79+ from_line : 1 ,
80+ to_line : 1 ,
81+ } ,
82+ ] ,
83+ problems : [
84+ {
85+ id : 'synthetic- secret' ,
86+ source : 'secret' ,
87+ name : 'Synthetic secret' ,
88+ } ,
89+ ] ,
90+ evidence : [ ] ,
91+ policy_modifications : [ ] ,
92+ risk : { } ,
9193 } ,
92- ]
93- : [ ] ,
94+ } ,
95+ ]
96+ : [ ] ,
97+ } ) ;
98+ for ( const endpoint of [ testPath , `${ testPath } /findings` ] ) {
99+ server . setEndpointHeaders ( endpoint , {
100+ 'Content-Type' : 'application/vnd.api+json' ,
94101 } ) ;
95- for ( const endpoint of [ testPath , `${ testPath } /findings` ] ) {
96- server . setEndpointHeaders ( endpoint , {
97- 'Content-Type' : 'application/vnd.api+json' ,
98- } ) ;
99- }
102+ }
100103
101- const { code, stdout, stderr } = await runSnykCLI (
102- `secrets test --org=${ orgId } ${ flag } ` ,
103- {
104- cwd : directory ,
105- env,
106- } ,
107- ) ;
108- expect ( stderr ) . toBe ( '' ) ;
109- expect ( code ) . toBe ( count ? 1 : 0 ) ;
110- const output =
111- flag === '--toon'
112- ? stdout
113- : await fs . readFile ( join ( directory , 'result.toon' ) , 'utf8' ) ;
114- expect ( output ) . toContain ( `org: ${ orgId } ` ) ;
104+ const { code, stdout, stderr } = await runSnykCLI (
105+ `secrets test --org=${ orgId } ${ flag } ${ fullFlag ? ` ${ fullFlag } ` : '' } ` ,
106+ {
107+ cwd : directory ,
108+ env,
109+ } ,
110+ ) ;
111+ expect ( stderr ) . toBe ( '' ) ;
112+ expect ( code ) . toBe ( count ? 1 : 0 ) ;
113+ const full = fullFlag === '--toon=full' ;
114+ const output =
115+ flag === '--toon'
116+ ? stdout
117+ : await fs . readFile ( join ( directory , 'result.toon' ) , 'utf8' ) ;
118+ expect ( output ) . toContain ( `org: ${ orgId } ` ) ;
119+ if ( full ) {
120+ expect ( output ) . not . toContain ( 'hint: add --toon=full for all fields' ) ;
121+ } else {
115122 expect ( output ) . toContain ( 'hint: add --toon=full for all fields' ) ;
116- if ( count ) {
117- expect ( output ) . toContain ( 'secrets[1]{file,line,rule,severity}:' ) ;
118- expect ( output ) . toContain ( 'config.txt,1,synthetic-secret,high' ) ;
123+ }
124+ if ( count ) {
125+ expect ( output ) . toContain ( 'secrets[1]{file,line,rule,severity}:' ) ;
126+ expect ( output ) . toContain ( 'config.txt,1,synthetic-secret,high' ) ;
127+ } else {
128+ expect ( output ) . toMatch ( / ^ s e c r e t s : \[ \] $ / m) ;
129+ }
130+ if ( flag !== '--toon' ) {
131+ if ( full ) {
132+ // `--toon=<mode>` also selects TOON for stdout alongside the file.
133+ expect ( stdout ) . toContain ( `org: ${ orgId } ` ) ;
134+ expect ( stdout ) . toMatch ( / ^ s e c r e t s (?: \[ \d + \] \{ | : ) / m) ;
119135 } else {
120- expect ( output ) . toMatch ( / ^ s e c r e t s : \[ \] $ / m) ;
121- }
122- if ( flag !== '--toon' ) {
123136 expect ( stdout ) . not . toMatch ( / ^ s e c r e t s (?: \[ \d + \] \{ | : ) / m) ;
124137 expect ( stdout ) . toContain ( 'Secret Detection' ) ;
125138 }
126- expect ( server . getRequests ( ) ) . toEqual (
127- expect . arrayContaining ( [
128- expect . objectContaining ( {
129- method : 'POST' ,
130- path : `/rest/orgs/ ${ orgId } /tests` ,
131- } ) ,
132- expect . objectContaining ( {
133- method : 'GET' ,
134- path : ` ${ testPath } /findings` ,
135- } ) ,
136- ] ) ,
137- ) ;
138- } ,
139- ) ;
139+ }
140+ expect ( server . getRequests ( ) ) . toEqual (
141+ expect . arrayContaining ( [
142+ expect . objectContaining ( {
143+ method : 'POST' ,
144+ path : `/rest/orgs/ ${ orgId } /tests` ,
145+ } ) ,
146+ expect . objectContaining ( {
147+ method : 'GET' ,
148+ path : ` ${ testPath } /findings` ,
149+ } ) ,
150+ ] ) ,
151+ ) ;
152+ } ) ;
140153 } ) ;
141154} ) ;
0 commit comments