-
Notifications
You must be signed in to change notification settings - Fork 534
/
docker-token.test.ts
167 lines (146 loc) · 4.01 KB
/
docker-token.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as tap from 'tap';
import * as cli from '../../src/cli/commands';
import { fakeServer } from './fake-server';
import * as sinon from 'sinon';
const { test } = tap;
const port = (process.env.PORT = process.env.SNYK_PORT = '12345');
const BASE_API = '/api/v1';
process.env.SNYK_API = 'http://localhost:' + port + BASE_API;
process.env.SNYK_HOST = 'http://localhost:' + port;
process.env.LOG_LEVEL = '0';
const apiKey = '123456789';
let oldkey;
let oldendpoint;
const server = fakeServer(BASE_API, apiKey);
// This import needs to come after the server init
// it causes the configured API url to be incorrect.
import * as plugins from '../../src/lib/plugins/index';
test('setup', async (t) => {
t.plan(3);
let key = await cli.config('get', 'api');
oldkey = key;
t.pass('existing user config captured: ' + oldkey);
key = await cli.config('get', 'endpoint');
oldendpoint = key;
t.pass('existing user endpoint captured: ' + oldendpoint);
await new Promise((resolve) => {
server.listen(port, resolve);
});
t.pass('started demo server');
t.end();
});
test('prime config', async (t) => {
await cli.config('unset', 'endpoint');
t.pass('endpoint removed');
await cli.config('unset', 'api');
t.pass('api key removed');
process.env.SNYK_DOCKER_TOKEN = 'docker-jwt-token';
t.pass('docker token set');
t.end();
});
test('`snyk test` with docker flag - docker token and no api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
try {
await cli.test('foo:latest', {
docker: true,
});
const req = server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, 'docker-jwt/test-dep-graph', 'posts to correct url');
} catch (err) {
t.fail('did not expect exception to be thrown ' + err);
}
});
test('`snyk test` with docker flag - docker token and api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
await cli.config('set', 'api=' + apiKey);
try {
await cli.test('foo:latest', {
docker: true,
});
const req = server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.match(req.url, 'test-dep-graph', 'posts to correct url');
} catch (err) {
t.fail('did not expect exception to be thrown ' + err);
}
await cli.config('unset', 'api');
t.end();
});
test('`snyk test` without docker flag - docker token and no api key', async (t) => {
stubDockerPluginResponse(
plugins,
{
plugin: {
packageManager: 'deb',
},
package: {},
},
t,
);
try {
await cli.test('foo:latest', {
docker: false,
});
t.fail('expected MissingApiTokenError');
} catch (err) {
t.equal(err.name, 'MissingApiTokenError', 'should throw if not docker');
}
});
test('teardown', async (t) => {
t.plan(4);
delete process.env.SNYK_API;
delete process.env.SNYK_HOST;
delete process.env.SNYK_PORT;
delete process.env.SNYK_DOCKER_TOKEN;
t.notOk(process.env.SNYK_PORT, 'fake env values cleared');
await new Promise((resolve) => {
server.close(resolve);
});
t.pass('server shutdown');
if (!oldkey) {
await cli.config('unset', 'api');
} else {
await cli.config('set', 'api=' + oldkey);
}
if (oldendpoint) {
await cli.config('set', `endpoint=${oldendpoint}`);
t.pass('user endpoint restored');
} else {
t.pass('no endpoint');
}
t.pass('user config restored');
t.end();
});
function stubDockerPluginResponse(plugins, fixture: string | object, t) {
const plugin = {
async inspect() {
return typeof fixture === 'object' ? fixture : require(fixture);
},
};
const spyPlugin = sinon.spy(plugin, 'inspect');
const loadPlugin = sinon.stub(plugins, 'loadPlugin');
loadPlugin
.withArgs(sinon.match.any, sinon.match({ docker: true }))
.returns(plugin);
t.teardown(loadPlugin.restore);
return spyPlugin;
}