-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_webapp.js
405 lines (349 loc) · 14.5 KB
/
test_webapp.js
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
'use strict';
/*
Used to test the gitlab webapp
nock will simulate a gitlab server running at
localhost:80, where Strider Tester, a user is
registered with the name "stridertester", and
has been registered with api token - zRtVsmeznn7ySatTrnrp
stridertester is an "owner" of a group named "testunion"
and has admin access to three projects -
testunion / unionproject1
Strider Tester / pubproject1
Strider Tester / privproject1
*/
const expect = require('expect.js');
const webapp = require('../lib/webapp');
const util = require('util');
const nock = require('nock');
function deepClone(sourceObject) {
return JSON.parse(JSON.stringify(sourceObject));
}
const wrongCredentialsConfig = {
api_url: 'http://localhost:80/api/v3',
api_key: 'badkey'
};
const providerConfig = {
whitelist: [],
pull_requests: 'none',
repo: 'http://nodev/stridertester/privproject1',
owner: {
avatar_url: 'http://www.gravatar.com/avatar/3f671ed86ed3d21ed3640c7a016b0997?s=40&d=identicon',
state: 'active',
id: 3,
username: 'stridertester',
name: 'Strider Tester'
},
url: 'git@nodev:stridertester/privproject1.git',
scm: 'git',
auth: {type: 'ssh'}
};
const providerConfigForProjectWithMissingStriderJson =
{
whitelist: [],
pull_requests: 'none',
repo: 'http://nodev/stridertester/priproject2',
owner: {
avatar_url: 'http://www.gravatar.com/avatar/3f671ed86ed3d21ed3640c7a016b0997?s=40&d=identicon',
state: 'active',
id: 3,
username: 'stridertester',
name: 'Strider Tester'
},
url: 'git@nodev:stridertester/priproject2.git',
scm: 'git',
auth: {type: 'ssh'}
};
const repoProject = {
name: 'stridertester/privproject1',
display_name: 'stridertester/privproject1',
display_url: 'http://nodev/stridertester/privproject1',
public: false,
prefetch_config: true,
creator: "55d2ecb5edb0d634165eac37",
provider: {
id: 'gitlab',
account: '0',
repo_id: '5',
config: {
auth: {type: 'ssh'},
scm: 'git',
url: 'git@nodev:stridertester/privproject1.git',
owner: {
name: 'Strider Tester',
username: 'stridertester',
id: 3,
state: 'active',
avatar_url: 'http://www.gravatar.com/avatar/3f671ed86ed3d21ed3640c7a016b0997?s=40&d=identicon'
},
repo: 'http://nodev/stridertester/privproject1',
pull_requests: 'none',
whitelist: []
}
},
branches: [{
name: 'master',
active: true,
mirror_master: false,
deploy_on_green: true,
pubkey: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1j1c3wNyUwFzIhU5ELZb6tH1K+TkQgV0CrYjRvWmeZZr5aNKehSo5ntCoPtjZO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HIt7/V/YKFyCU4v7DkrCJdOcTnLHpOlMm2e3NZInGZdDp8+\n6XFKRKlk/Vv7NZg5m/liPhBRGZalWTT5x20VyTNxVN0WudjPTGbl/Q6l9nG1cu5V\nNtxX5G3o8eOcSQaiTY8IVEN96ApuYhnh2u+463WSaWc5U53Oh+0V4kcCgYEAzS3n\n9o1qmkc+KLmjYI19M5gN3sW1ZyIYa3f0i9FCqnAaKKoBrNVjSaQ5Hp/jyAc1hDtq\nS/HwztUDJNvVB5/IRacpMklrh8ReF/2kTHgM1ChCTkdsoIQOw04Sp8VJLK4ECswn\n7R+17Ff4hW0nffYfQeMeXCZd99HrbYgH564PL+MCgYBzNLzuJbt7GA2J5VGLrfnt\nVz2thtXb+5pzmH8hYGK0yT3wXJbjAtYJ/xXYSZYUzBy8KwjRbEksazvzmYvKDIkj\nhTTZOJJgqHgQWFVE8+fvhO2eokG3i8JGvlTs1YDs0+b9tKQCccW4pQJ5aiStO6CD\nqfsPtKGbfWXIQq9wrYC9rQKBgGUx5f0pJFGxH4wwes7NGdoPfY/JDT4vh8HBaQBo\nliu/nHc/2hGnMRAKCg37p/lo7NEqCLQqQK2SS7mrHrdi7ADGgEAIDBvrEslcVONO\nNm2Q6Zr4CTBl0W4sHHFYkU2TCfVVWb/O6wgFH1KXUmoCsMnrPXcPiTsH8siT1epD\nY8QnAoGAYhPbK1k6sH8bTdU6hI3TtaSZ1MOMDO2eeB0u2dpQSOiQ5a33vLM9AN2s\nHQfwA6yNn9oPf3BnJBOGxpX6W/L3ntYOVp4T/18LCZOX8f9GFwUrAAtbH52YgP/3\njveYyVbjAOkSohdCd2UhiJWoQVHcGGHrkX/bKFfyVYUlKrKpVag=\n-----END RSA PRIVATE KEY-----\n',
plugins: [
{
id: 'node',
enabled: true,
config: {
fork: 'Node.js',
runtime: 'whatever',
caching: 'none',
test: 'npm test',
globals: []
}
}],
runner: {
id: 'simple-runner',
config: {pty: false}
}
},
{name: '*', mirror_master: true}
]
};
const projectWithInvalidRepoID = deepClone(repoProject);
projectWithInvalidRepoID.provider.repo_id = "invalidrepo";
const projectWithInvalidSSHKey = deepClone(repoProject);
projectWithInvalidSSHKey.branches[0].pubkey = "invalid key";
const projectWithInvalidName = deepClone(repoProject);
projectWithInvalidName.name = "nonexistentproject";
describe('gitlab webapp', function() {
//--------------------------------------------------------------------------------------
//Test getting a json file
describe('getFile', function() {
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll();
require('./mocks/gitlab_webapp_getfile.js')();
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll();
});
const filename = "strider.json";
const ref = {
branch: 'master',
};
//getFile only uses account.config
const account = {
api_key: 'zRtVsmeznn7ySatTrnrp',
api_url: 'http://localhost:80/api/v3'
};
//getFile only uses project.provider.repo_id
const project = {
provider: {
repo_id: '5'
}
};
//project with strider.json missing
const projectWithMissingStriderJson = {
provider: {
repo_id: '8'
}
};
it('should get a json file correctly', function(done) {
webapp.getFile(filename, ref, account, providerConfig, project, function(err, text) {
expect(text).to.be.a('string');
done();
});
});
//created a project priproject2 of type node.js, but not having a strider.json for this test
it('should not crash, but return an error if the file could not be found', function(done) {
webapp.getFile(filename, ref, account, providerConfigForProjectWithMissingStriderJson, projectWithMissingStriderJson, function(err, text) {
expect(err).to.be.ok();
expect(text).to.not.be.ok();
done();
});
});
});
//--------------------------------------------------------------------------------------
//Test getting branches from a repository
describe('getBranches', function() {
//takes parameters - account, config, project
/*
NOTE: the account argument in this function is DIFFERENT from
the account argument passed in to webapp.getFile. Here we get passed what is
known elsewhere (in the api tests) to be a config object (i.e. one with
two properties - api_url and api_key. Also note that in the webapp file, config
is used to refer to an object of type project.provider.config
*/
const account = {
api_url: 'http://localhost:80/api/v3',
api_key: 'zRtVsmeznn7ySatTrnrp'
};
const invalidAccount = {
api_url: 'http://localhost:80/api/v3',
api_key: 'badkey'
};
//getBranches only uses project.provider.repo_id
const project = {
provider: {
repo_id: '5',
}
};
const invalidProject = {
provider: {
repo_id: 'invalidrepo',
}
};
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll();
require('./mocks/gitlab_webapp_getbranches.js')();
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll();
});
it('should get a correct list of branches in the project', function(done) {
webapp.getBranches(account, providerConfig, project, function(err, branches) {
//debug("Branches we get are: " + Object.prototype.toString.call(branches) + "Inspected: " + util.inspect(branches, false, null, true));
expect(branches).to.be.an('array');
expect(branches).to.eql(['firstbranch', 'master']);
done();
});
});
it('should complain suitably if account data is empty', function(done) {
webapp.getBranches({}, providerConfig, project, function(err, branches) {
//debug("Branches we get are: " + Object.prototype.toString.call(branches) + "Inspected: " + util.inspect(branches, false, null, true));
//debug("We get ERR as: " + util.inspect(err, false, null, true));
expect(err).to.be.ok();
done();
});
});
it('should complain suitably if account data is invalid', function(done) {
webapp.getBranches(invalidAccount, providerConfig, project, function(err, branches) {
//debug("We get ERR as: " + err);
expect(err).to.be.ok();
done();
});
});
it('should complain suitably if project.provider.repo_id is invalid', function(done) {
webapp.getBranches(account, providerConfig, invalidProject, function(err, branches) {
//debug("We get ERR as: " + util.inspect(err, false, null, true));
expect(err).to.be.ok();
done();
});
});
});
//--------------------------------------------------------------------------------------
describe('listRepos', function() {
//takes parameters - config, callback
//NOTE: what listRepos expects as config, is called account elsewhere in webapp.js
const config = {
api_url: 'http://localhost:80/api/v3',
api_key: 'zRtVsmeznn7ySatTrnrp'
};
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll();
require('./mocks/gitlab_webapp_listrepos.js')();
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll();
});
it('should get a list of repositories accessible to the user correctly', function(done) {
webapp.listRepos(config, function(err, repos) {
expect(err).to.not.be.ok();
expect(repos).to.be.an('array');
expect(repos.length).to.eql(3);
done();
});
});
it('should complain if an invalid config is passed - invalid api_url', function(done) {
webapp.listRepos({api_url: 'invalidurl', api_key: 'zRtVsmeznn7ySatTrnrp'}, function(err, repos) {
expect(err).to.be.ok();
done();
});
});
it('should complain if invalid credentials are passed - wrong api_key', function(done) {
webapp.listRepos(wrongCredentialsConfig, function(err, repos) {
expect(err).to.be.ok();
done();
});
});
});
//--------------------------------------------------------------------------------------
describe('setupRepo', function() {
//takes parameters - account, project, config, callback
const account = {
api_url: 'http://localhost:80/api/v3',
api_key: 'zRtVsmeznn7ySatTrnrp'
};
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll();
require('./mocks/gitlab_webapp_setuprepo.js')();
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll();
});
it('should callback with the same config object that we passed in as the second parameter if repo was set up successfully', function(done) {
webapp.setupRepo(account, providerConfig, repoProject, function(err, receivedConf) {
expect(err).to.not.be.ok();
expect(receivedConf).to.eql(providerConfig);
done();
});
});
it('should callback with an error if invalid credentials are supplied', function(done) {
webapp.setupRepo(wrongCredentialsConfig, providerConfig, repoProject, function(err, receivedConf) {
expect(err).to.be.ok();
done();
});
});
it('should callback with an error if project.provider.repo_id is invalid', function(done) {
webapp.setupRepo(account, providerConfig, projectWithInvalidRepoID, function(err, receivedConf) {
expect(err).to.be.ok();
done();
});
});
it('should callback with an error if project.branches[0].pubkey is not a valid ssh public key', function(done) {
webapp.setupRepo(account, providerConfig, projectWithInvalidSSHKey, function(err, receivedConf) {
expect(err).to.be.ok();
done();
});
});
});
//--------------------------------------------------------------------------------------
describe('tearDownRepo', function() {
//takes parameters - account, project, config, callback
const account = {
api_url: 'http://localhost:80/api/v3',
api_key: 'zRtVsmeznn7ySatTrnrp'
};
before('Setup the mock gitlab server', function setupNock() {
nock.cleanAll();
require('./mocks/gitlab_webapp_teardownrepos.js')();
});
after('Tear down mock Gitlab server', function tearDownNock() {
nock.cleanAll();
});
it('should callback on success with no parameters if the repo was destroyed successfully', function(done) {
webapp.teardownRepo(account, providerConfig, repoProject, function(err) {
expect(err).to.not.be.ok();
done();
});
});
it('should callback with an error if invalid credentials are supplied', function(done) {
webapp.teardownRepo(wrongCredentialsConfig, providerConfig, repoProject, function(err) {
expect(err).to.be.ok();
done();
});
});
it('should callback with an error if project.provider.repo_id is invalid', function(done) {
webapp.teardownRepo(account, providerConfig, projectWithInvalidRepoID, function(err) {
expect(err).to.be.ok();
done();
});
});
it.skip('should callback with an error if an invalid project name is given', function(done) {
webapp.teardownRepo(account, providerConfig, projectWithInvalidName, function(err) {
//TODO: what happens here is that the title does not match that of the key and
//the key is not deleted. In that case, we are silently ignoring the was_deleted flag
//and directly calling done with a null error
expect(err).to.be.ok();
done();
});
});
});
});