@@ -20,6 +20,19 @@ function interceptAndRun(handler, test) {
20
20
} ;
21
21
}
22
22
23
+ function determineResponse ( req , res , ctx ) {
24
+ let response
25
+ if ( req . headers . get ( "ex-snyk-token" ) === null ) {
26
+ response = res ( ctx . status ( 400 ) ) ;
27
+
28
+ } else if ( req . headers . get ( "ex-snyk-token" ) === "good-dummy-token" ) {
29
+ response = res ( ctx . status ( 200 ) ) ;
30
+ } else {
31
+ response = res ( ctx . status ( 401 ) ) ;
32
+ }
33
+ return response
34
+ }
35
+
23
36
suite ( 'testing the analysis module for sending api requests' , ( ) => {
24
37
let backendUrl = 'http://url.lru' // dummy backend url will be used for fake server
25
38
// fake provided data, in prod will be provided by the provider and used for creating requests
@@ -97,6 +110,54 @@ suite('testing the analysis module for sending api requests', () => {
97
110
}
98
111
) )
99
112
} )
113
+ suite ( 'testing the validateToken function' , ( ) => {
114
+
115
+ test ( 'invoking validateToken function with good token' , interceptAndRun (
116
+ // interception route, will return ok response for our fake content type
117
+ rest . get ( `${ backendUrl } /api/v3/token` , ( req , res , ctx ) => {
118
+ return determineResponse ( req , res , ctx ) ;
119
+
120
+ } ) ,
121
+ async ( ) => {
122
+ let options = {
123
+ 'EXHORT_SNYK_TOKEN' : 'good-dummy-token'
124
+ }
125
+ // verify response as expected
126
+ let res = await analysis . validateToken ( backendUrl , options )
127
+ expect ( res ) . to . equal ( 200 )
128
+ }
129
+ ) )
130
+ test ( 'invoking validateToken function with bad token' , interceptAndRun (
131
+ // interception route, will return ok response for our fake content type
132
+ rest . get ( `${ backendUrl } /api/v3/token` , ( req , res , ctx ) => {
133
+ return determineResponse ( req , res , ctx ) ;
134
+
135
+ } ) ,
136
+ async ( ) => {
137
+ let options = {
138
+ 'EXHORT_SNYK_TOKEN' : 'bad-dummy-token'
139
+ }
140
+ // verify response as expected
141
+ let res = await analysis . validateToken ( backendUrl , options )
142
+ expect ( res ) . to . equal ( 401 )
143
+ }
144
+ ) )
145
+ test ( 'invoking validateToken function without token' , interceptAndRun (
146
+ // interception route, will return ok response for our fake content type
147
+ rest . get ( `${ backendUrl } /api/v3/token` , ( req , res , ctx ) => {
148
+ return determineResponse ( req , res , ctx ) ;
149
+
150
+ } ) ,
151
+ async ( ) => {
152
+ let options = {
153
+ }
154
+ // verify response as expected
155
+ let res = await analysis . validateToken ( backendUrl , options )
156
+ expect ( res ) . to . equal ( 400 )
157
+ }
158
+ ) )
159
+
160
+ } )
100
161
101
162
suite ( 'verify environment variables to token headers mechanism' , ( ) => {
102
163
let fakeManifest = 'fake-file.typ'
0 commit comments