-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathdatabaseFetcher.test.ts
More file actions
315 lines (280 loc) · 10.2 KB
/
databaseFetcher.test.ts
File metadata and controls
315 lines (280 loc) · 10.2 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import * as sinon from 'sinon';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as tmp from 'tmp';
import { expect } from 'chai';
import { window } from 'vscode';
import {
convertGithubNwoToDatabaseUrl,
convertLgtmUrlToDatabaseUrl,
looksLikeLgtmUrl,
findDirWithFile,
looksLikeGithubRepo,
} from '../../databaseFetcher';
import { ProgressCallback } from '../../commandRunner';
import * as Octokit from '@octokit/rest';
describe('databaseFetcher', function() {
// These tests make API calls and may need extra time to complete.
this.timeout(10000);
describe('convertGithubNwoToDatabaseUrl', () => {
let sandbox: sinon.SinonSandbox;
let quickPickSpy: sinon.SinonStub;
let progressSpy: ProgressCallback;
let mockRequest: sinon.SinonStub;
let octokit: Octokit.Octokit;
beforeEach(() => {
sandbox = sinon.createSandbox();
quickPickSpy = sandbox.stub(window, 'showQuickPick');
progressSpy = sandbox.spy();
mockRequest = sandbox.stub();
octokit = ({
request: mockRequest,
}) as unknown as Octokit.Octokit;
});
afterEach(() => {
sandbox.restore();
});
it('should convert a GitHub nwo to a database url', async () => {
// We can't make the real octokit request (since we need credentials), so we mock the response.
const mockApiResponse = {
data: [
{
id: 1495869,
name: 'csharp-database',
language: 'csharp',
uploader: {},
content_type: 'application/zip',
state: 'uploaded',
size: 55599715,
created_at: '2022-03-24T10:46:24Z',
updated_at: '2022-03-24T10:46:27Z',
url: 'https://api.github.com/repositories/143040428/code-scanning/codeql/databases/csharp',
},
{
id: 1100671,
name: 'database.zip',
language: 'javascript',
uploader: {},
content_type: 'application/zip',
state: 'uploaded',
size: 29294434,
created_at: '2022-03-01T16:00:04Z',
updated_at: '2022-03-01T16:00:06Z',
url: 'https://api.github.com/repositories/143040428/code-scanning/codeql/databases/javascript',
},
{
id: 648738,
name: 'ql-database',
language: 'ql',
uploader: {},
content_type: 'application/json; charset=utf-8',
state: 'uploaded',
size: 39735500,
created_at: '2022-02-02T09:38:50Z',
updated_at: '2022-02-02T09:38:51Z',
url: 'https://api.github.com/repositories/143040428/code-scanning/codeql/databases/ql',
},
],
};
mockRequest.resolves(mockApiResponse);
quickPickSpy.resolves('javascript');
const githubRepo = 'github/codeql';
const result = await convertGithubNwoToDatabaseUrl(
githubRepo,
octokit,
progressSpy
);
expect(result).not.to.be.undefined;
if (result === undefined) {
return;
}
const { databaseUrl, name, owner } = result;
expect(databaseUrl).to.equal(
'https://api.github.com/repos/github/codeql/code-scanning/codeql/databases/javascript'
);
expect(name).to.equal('codeql');
expect(owner).to.equal('github');
expect(quickPickSpy.firstCall.args[0]).to.deep.equal([
'csharp',
'javascript',
'ql',
]);
});
// Repository doesn't exist, or the user has no access to the repository.
it('should fail on an invalid/inaccessible repository', async () => {
const mockApiResponse = {
data: {
message: 'Not Found',
},
status: 404,
};
mockRequest.resolves(mockApiResponse);
const githubRepo = 'foo/bar-not-real';
await expect(
convertGithubNwoToDatabaseUrl(githubRepo, octokit, progressSpy)
).to.be.rejectedWith(/Unable to get database/);
expect(progressSpy).to.have.callCount(0);
});
// User has access to the repository, but there are no databases for any language.
it('should fail on a repository with no databases', async () => {
const mockApiResponse = {
data: [],
};
mockRequest.resolves(mockApiResponse);
const githubRepo = 'foo/bar-with-no-dbs';
await expect(
convertGithubNwoToDatabaseUrl(githubRepo, octokit, progressSpy)
).to.be.rejectedWith(/Unable to get database/);
expect(progressSpy).to.have.been.calledOnce;
});
});
describe('convertLgtmUrlToDatabaseUrl', () => {
let sandbox: sinon.SinonSandbox;
let quickPickSpy: sinon.SinonStub;
let progressSpy: ProgressCallback;
beforeEach(() => {
sandbox = sinon.createSandbox();
quickPickSpy = sandbox.stub(window, 'showQuickPick');
progressSpy = sandbox.spy();
});
afterEach(() => {
sandbox.restore();
});
it('should convert a project url to a database url', async () => {
quickPickSpy.resolves('javascript');
const lgtmUrl = 'https://lgtm.com/projects/g/github/codeql';
const dbUrl = await convertLgtmUrlToDatabaseUrl(lgtmUrl, progressSpy);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/javascript'
);
expect(quickPickSpy.firstCall.args[0]).to.contain('javascript');
expect(quickPickSpy.firstCall.args[0]).to.contain('python');
});
it('should convert a project url to a database url with extra path segments', async () => {
quickPickSpy.resolves('python');
const lgtmUrl =
'https://lgtm.com/projects/g/github/codeql/subpage/subpage2?query=xxx';
const dbUrl = await convertLgtmUrlToDatabaseUrl(lgtmUrl, progressSpy);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/python'
);
expect(progressSpy).to.have.been.calledOnce;
});
it('should convert a raw slug to a database url with extra path segments', async () => {
quickPickSpy.resolves('python');
const lgtmUrl =
'g/github/codeql';
const dbUrl = await convertLgtmUrlToDatabaseUrl(lgtmUrl, progressSpy);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/python'
);
expect(progressSpy).to.have.been.calledOnce;
});
it('should fail on a nonexistent project', async () => {
quickPickSpy.resolves('javascript');
const lgtmUrl = 'https://lgtm.com/projects/g/github/hucairz';
await expect(convertLgtmUrlToDatabaseUrl(lgtmUrl, progressSpy)).to.rejectedWith(/Invalid LGTM URL/);
expect(progressSpy).to.have.callCount(0);
});
});
describe('looksLikeGithubRepo', () => {
it('should handle invalid urls', () => {
expect(looksLikeGithubRepo(''))
.to.be.false;
expect(looksLikeGithubRepo('http://github.com/foo/bar'))
.to.be.false;
expect(looksLikeGithubRepo('https://ww.github.com/foo/bar'))
.to.be.false;
expect(looksLikeGithubRepo('https://ww.github.com/foo'))
.to.be.false;
expect(looksLikeGithubRepo('foo'))
.to.be.false;
});
it('should handle valid urls', () => {
expect(looksLikeGithubRepo('https://github.com/foo/bar'))
.to.be.true;
expect(looksLikeGithubRepo('https://www.github.com/foo/bar'))
.to.be.true;
expect(looksLikeGithubRepo('https://github.com/foo/bar/sub/pages'))
.to.be.true;
expect(looksLikeGithubRepo('foo/bar'))
.to.be.true;
});
});
describe('looksLikeLgtmUrl', () => {
it('should handle invalid urls', () => {
expect(looksLikeLgtmUrl('')).to.be.false;
expect(looksLikeLgtmUrl('http://lgtm.com/projects/g/github/codeql')).to.be
.false;
expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github/codeql'))
.to.be.false;
expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github')).to.be
.false;
expect(looksLikeLgtmUrl('g/github')).to.be
.false;
expect(looksLikeLgtmUrl('ggg/github/myproj')).to.be
.false;
});
it('should handle valid urls', () => {
expect(looksLikeLgtmUrl('https://lgtm.com/projects/g/github/codeql')).to
.be.true;
expect(looksLikeLgtmUrl('https://www.lgtm.com/projects/g/github/codeql'))
.to.be.true;
expect(
looksLikeLgtmUrl('https://lgtm.com/projects/g/github/codeql/sub/pages')
).to.be.true;
expect(
looksLikeLgtmUrl(
'https://lgtm.com/projects/g/github/codeql/sub/pages?query=string'
)
).to.be.true;
expect(looksLikeLgtmUrl('g/github/myproj')).to.be
.true;
expect(looksLikeLgtmUrl('git/github/myproj')).to.be
.true;
});
});
describe('findDirWithFile', () => {
let dir: tmp.DirResult;
beforeEach(() => {
dir = tmp.dirSync({ unsafeCleanup: true });
createFile('a');
createFile('b');
createFile('c');
createDir('dir1');
createFile('dir1', 'd');
createFile('dir1', 'e');
createFile('dir1', 'f');
createDir('dir2');
createFile('dir2', 'g');
createFile('dir2', 'h');
createFile('dir2', 'i');
createDir('dir2', 'dir3');
createFile('dir2', 'dir3', 'j');
createFile('dir2', 'dir3', 'k');
createFile('dir2', 'dir3', 'l');
});
it('should find files', async () => {
expect(await findDirWithFile(dir.name, 'k')).to.equal(
path.join(dir.name, 'dir2', 'dir3')
);
expect(await findDirWithFile(dir.name, 'h')).to.equal(
path.join(dir.name, 'dir2')
);
expect(await findDirWithFile(dir.name, 'z', 'a')).to.equal(dir.name);
// there's some slight indeterminism when more than one name exists
// but in general, this will find files in the current directory before
// finding files in sub-dirs
expect(await findDirWithFile(dir.name, 'k', 'a')).to.equal(dir.name);
});
it('should not find files', async () => {
expect(await findDirWithFile(dir.name, 'x', 'y', 'z')).to.be.undefined;
});
function createFile(...segments: string[]) {
fs.createFileSync(path.join(dir.name, ...segments));
}
function createDir(...segments: string[]) {
fs.mkdirSync(path.join(dir.name, ...segments));
}
});
});