1
1
import { runSnykCLI } from '../../util/runSnykCLI' ;
2
+ import { FakeServer , fakeServer } from '../../../acceptance/fake-server' ;
3
+ import { createProjectFromWorkspace } from '../../util/createProject' ;
4
+ import { isCLIV2 } from '../../util/isCLIV2' ;
2
5
3
6
jest . setTimeout ( 1000 * 60 ) ;
4
7
@@ -14,3 +17,87 @@ test('returns value in one line', async () => {
14
17
expect ( code ) . toEqual ( 0 ) ;
15
18
expect ( stdout ) . toEqual ( expectedToken + '\n' ) ;
16
19
} ) ;
20
+
21
+ describe ( 'snyk config set endpoint' , ( ) => {
22
+ let server : FakeServer ;
23
+ const port = process . env . PORT || process . env . SNYK_PORT || '12345' ;
24
+ const baseApi = '/api' ;
25
+ const token = '123456789' ;
26
+
27
+ beforeAll ( ( done ) => {
28
+ server = fakeServer ( baseApi + '/v1' , token ) ;
29
+ server . listen ( port , ( ) => {
30
+ done ( ) ;
31
+ } ) ;
32
+ } ) ;
33
+
34
+ afterEach ( ( ) => {
35
+ jest . resetAllMocks ( ) ;
36
+ server . restore ( ) ;
37
+ } ) ;
38
+
39
+ afterAll ( ( done ) => {
40
+ server . close ( ( ) => {
41
+ done ( ) ;
42
+ } ) ;
43
+ } ) ;
44
+
45
+ it ( 'set and use endpoint' , async ( ) => {
46
+ const env = {
47
+ ...process . env ,
48
+ SNYK_TOKEN : token ,
49
+ SNYK_HTTP_PROTOCOL_UPGRADE : '0' ,
50
+ } ;
51
+
52
+ delete env [ 'SNYK_API' ] ;
53
+
54
+ // ensure that we start from scratch
55
+ const requestCount0 = server . getRequests ( ) . length ;
56
+ expect ( requestCount0 ) . toEqual ( 0 ) ;
57
+
58
+ // set endpoint
59
+ const endpoint = 'http://127.0.0.1:' + server . getPort ( ) + baseApi ;
60
+ const resultconfigSet = await runSnykCLI (
61
+ 'config set endpoint=' + endpoint + ' -d' ,
62
+ {
63
+ env : env ,
64
+ } ,
65
+ ) ;
66
+ expect ( resultconfigSet . code ) . toEqual ( 0 ) ;
67
+
68
+ // run a tests against the endpoint
69
+ const project = await createProjectFromWorkspace ( 'npm-package' ) ;
70
+
71
+ const resultTest = await runSnykCLI (
72
+ 'test --debug --org=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' ,
73
+ {
74
+ cwd : project . path ( ) ,
75
+ env : env ,
76
+ } ,
77
+ ) ;
78
+ expect ( resultTest . code ) . toEqual ( 0 ) ;
79
+ expect ( resultTest . stderr ) . toContain ( endpoint ) ;
80
+ expect ( resultTest . stderr ) . not . toContain ( 'snyk.io' ) ;
81
+
82
+ const requestCount1 = server . getRequests ( ) . length ;
83
+ expect ( requestCount1 ) . toBeGreaterThan ( requestCount0 ) ;
84
+
85
+ if ( isCLIV2 ( ) ) {
86
+ // generate an sbom against the endpoint
87
+ const resultSBOM = await runSnykCLI (
88
+ `sbom --experimental --debug --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee` ,
89
+ {
90
+ env : env ,
91
+ } ,
92
+ ) ;
93
+ expect ( resultSBOM . code ) . toEqual ( 0 ) ;
94
+ expect ( resultSBOM . stderr ) . toContain ( endpoint ) ;
95
+ expect ( resultSBOM . stderr ) . not . toContain ( 'snyk.io' ) ;
96
+
97
+ const requestCount2 = server . getRequests ( ) . length ;
98
+ expect ( requestCount2 ) . toBeGreaterThan ( requestCount1 ) ;
99
+ }
100
+
101
+ await runSnykCLI ( 'config unset endpoint' ) ;
102
+ } ) ;
103
+ } ) ;
0 commit comments