This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathtest.uploadDevelopChallenge.js
642 lines (592 loc) · 26.5 KB
/
test.uploadDevelopChallenge.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.0
* @author TCSASSEMBLER
*/
"use strict";
/*global describe, it, before, beforeEach, after, afterEach */
/*jslint node: true, stupid: true, unparam: true, nomen: true, vars: true */
/**
* Module dependencies.
*/
var fs = require('fs');
var request = require('supertest');
var assert = require('chai').assert;
var async = require("async");
var http = require('http');
var _ = require('underscore');
var testHelper = require('./helpers/testHelper');
var config = require('../config/tc-config').tcConfig;
var SQL_DIR = __dirname + "/sqls/devUploadSubmission/";
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
/**
* Objects and values required for generating the OAuth token
*/
var CLIENT_ID = config.oauthClientId;
var SECRET = config.oauthClientSecret;
var jwt = require('jsonwebtoken');
describe('Submit for develop challenge, using direct file upload', function () {
/**
* Users that we have setup.
*/
var user124764 = 'facebook|fb124764',
user124834 = 'facebook|fb124834';
/**
* The mock thrugood server for testing
*/
var mockThurgoodServer;
/**
* The path to a sample zipped submission
*/
var sampleSubmissionPath = './test/test_files/dev_upload_submission/sample_submission.zip';
/**
* The path to a sample zipped submission which is too large
*/
var sampleSubmissionPathTooLarge = './test/test_files/dev_upload_submission/sample_submission_too_large.zip';
/**
* Return the authentication header to be used for the given user.
* @param {Object} user the user to authenticate
*/
function getAuthHeader(user) {
return "Bearer " + jwt.sign({sub: user}, SECRET, {expiresInMinutes: 1000, audience: CLIENT_ID});
}
/**
* Creates a Request object using the given URL.
* Sets the Authorization header for the given user.
* Sets the expected response code using the expectedStatusCode parameter
* @param {String} url the url to connect
* @param {Object} user the user to authenticate
* @param {Number} expectedStatusCode the expected status code of the response
*/
function getRequest(url, user, expectedStatusCode) {
return request(API_ENDPOINT)
.post(url)
.set('Accept', 'application/json')
.set('Authorization', getAuthHeader(user))
.expect('Content-Type', /json/)
.expect(expectedStatusCode)
.attach('submissionFile', sampleSubmissionPath);
}
this.timeout(120000); // The api with testing remote db could be quit slow
/**
* Clear database
* @param {Function<err>} done the callback
*/
function clearDb(done) {
async.waterfall([
function (cb) {
testHelper.runSqlFile(SQL_DIR + "common_oltp__clean", "common_oltp", cb);
},
function (cb) {
testHelper.runSqlFile(SQL_DIR + "tcs_catalog__clean", "tcs_catalog", cb);
}
], done);
}
/**
* This function is run before all tests.
* Generate tests data.
* @param {Function<err>} done the callback
*/
before(function (done) {
async.waterfall([
clearDb,
function (cb) {
testHelper.runSqlFile(SQL_DIR + "common_oltp__insert_test_data", "common_oltp", cb);
},
function (cb) {
testHelper.runSqlFile(SQL_DIR + "tcs_catalog__insert_test_data", "tcs_catalog", cb);
}, function (cb) {
// This is the mock Thurgood server
// It returns success always with data._id = 123456
mockThurgoodServer = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "applcation/json"});
var res = {
success: 'true',
data: {
_id: 123456
}
};
response.end(JSON.stringify(res));
});
// Listen on port 8090, IP defaults to 127.0.0.1
mockThurgoodServer.listen(8090);
// Put a friendly message on the terminal
console.log("Mock Thurgood Server running at http://127.0.0.1:8090/");
cb();
}
], done);
});
/**
* This function is run after all tests.
* Close the mock thurgood server.
* @param {Function<err>} done the callback
*/
after(function (done) {
mockThurgoodServer.close();
clearDb(done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload for success
* should return json containing uploadId and submissionId
* respective entries in upload, submission and resource_submission tables must be present
* the file must be present in the submissions folder
* the thurgood id must be set in submission table
*/
it('should submit successfully1', function (done) {
var req = getRequest('/v2/develop/challenges/77701/upload', user124764, 200);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
async.series([
function (cb) {
//Check if the row was created properly in upload table
var sql = "* from upload where upload_id = " + resp.body.uploadId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
project_phase_id: 77702,
resource_id: 77711,
parameter: resp.body.uploadId + '_sample_submission.zip',
modify_user: '124764',
project_id: 77701,
upload_type_id: 1,
create_user: '124764',
upload_status_id: 1,
upload_id: resp.body.uploadId
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of upload table did not match');
cb(err, result);
});
}, function (cb) {
//Check if the row was created properly in submission table
var sql = "* from submission where submission_id = " + resp.body.submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
submission_id: resp.body.submissionId,
upload_id: resp.body.uploadId,
submission_status_id: 1,
submission_type_id: 1,
modify_user: '124764',
create_user: '124764',
thurgood_job_id: '123456'
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of submission table did not match');
cb(err, result);
});
}, function (cb) {
//Check if the row was created properly in resource_submission table
var sql = "* from resource_submission where submission_id = " + resp.body.submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
resource_id: 77711,
modify_user: '124764',
create_user: '124764',
submission_id: resp.body.submissionId
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of resource_submission table did not match');
cb(err, result);
});
}
], done);
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload for success when checkpoint submission is made
* should return json containing uploadId and submissionId
* respective entries in upload, submission and resource_submission tables must be present
* the file must be present in the submissions folder
* the thurgood id must NOT be set in submission table because it applies only to final submissions
*/
it('should submit checkpoint successfully', function (done) {
var req = getRequest('/v2/develop/challenges/77701/upload?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
async.series([
function (cb) {
//Check if the row was created properly in upload table
var sql = "* from upload where upload_id = " + resp.body.uploadId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
project_phase_id: 77705,
resource_id: 77711,
parameter: resp.body.uploadId + '_sample_submission.zip',
modify_user: '124764',
project_id: 77701,
upload_type_id: 1,
create_user: '124764',
upload_status_id: 1,
upload_id: resp.body.uploadId
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of upload table did not match');
cb(err, result);
});
}, function (cb) {
//Check if the row was created properly in submission table. No thurgood_job_id must be present
var sql = "* from submission where submission_id = " + resp.body.submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
submission_id: resp.body.submissionId,
upload_id: resp.body.uploadId,
submission_status_id: 1,
submission_type_id: 3,
modify_user: '124764',
create_user: '124764'
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of submission table did not match');
cb(err, result);
});
}, function (cb) {
//Check if the row was created properly in resource_submission table
var sql = "* from resource_submission where submission_id = " + resp.body.submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
resource_id: 77711,
modify_user: '124764',
create_user: '124764',
submission_id: resp.body.submissionId
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of resource_submission table did not match');
cb(err, result);
});
}
], done);
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload for success
* the project has no thurgood properties
* the thurgood id in the submission table must be null
*/
it('should submit successfully for project with no thurgood properties', function (done) {
var req = getRequest('/v2/develop/challenges/77707/upload', user124764, 200);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
//Check if the thurgoodJobId in submission table is null
var sql = "thurgood_job_id from submission where submission_id = " + resp.body.submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
assert.deepEqual(result[0], {}, "The thurgood_job_id must be null in DB");
done(err);
});
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload for success when multiple submissions are made but are not allowed
* The submissions are made in this order checkpoint1, checkpoint2, submission1, submission2
* checkpoint1 must become inactive when checkpoint2 is made
* checkpoint2 must remain active when submission1 is made
* submission1 must become inactive after submission2 is made
*/
it('should submit successfully when multiple submissions are made but are not allowed', function (done) {
var c1uid,
c1sid,
c2uid,
c2sid,
s1uid,
s1sid,
s2uid,
s2sid;
async.series([
function (cb) {
var req = getRequest('/v2/develop/challenges/77708/upload?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
c1uid = resp.body.uploadId;
c1sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77708/upload?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
c2uid = resp.body.uploadId;
c2sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77708/upload', user124764, 200);
req.end(function (err, resp) {
s1uid = resp.body.uploadId;
s1sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77708/upload', user124764, 200);
req.end(function (err, resp) {
s2uid = resp.body.uploadId;
s2sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
//Now we test for correctness
var sql = "u.upload_id, u.upload_status_id, s.submission_id, s.submission_status_id from upload u, submission s where " +
"u.upload_id = s.upload_id AND s.submission_id IN (" + c1sid + "," + c2sid + "," + s1sid + "," + s2sid + ")"
+ " AND u.upload_id IN (" + c1uid + "," + c2uid + "," + s1uid + "," + s2uid + ")";
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
//The checkpoint1 must be deleted
var c1rec = _.findWhere(result, {upload_id: c1uid});
assert.equal(c1rec.upload_status_id, 2);
assert.equal(c1rec.submission_status_id, 5);
//The checkpoint2 must still be active
var c2rec = _.findWhere(result, {upload_id: c2uid});
assert.equal(c2rec.upload_status_id, 1);
assert.equal(c2rec.submission_status_id, 1);
//The submission1 must be deleted
var s1rec = _.findWhere(result, {upload_id: s1uid});
assert.equal(s1rec.upload_status_id, 2);
assert.equal(s1rec.submission_status_id, 5);
//The submission2 must be active
var s2rec = _.findWhere(result, {upload_id: s2uid});
assert.equal(s2rec.upload_status_id, 1);
assert.equal(s2rec.submission_status_id, 1);
cb(err);
});
}
], done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload for success when multiple submissions are made and are allowed
* The submissions are made in this order checkpoint1, checkpoint2, submission1, submission2
* All submissions and uploads must remain active.
*/
it('should submit successfully when multiple submissions are made and are allowed', function (done) {
var c1uid,
c1sid,
c2uid,
c2sid,
s1uid,
s1sid,
s2uid,
s2sid;
async.series([
function (cb) {
var req = getRequest('/v2/develop/challenges/77709/upload?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
c1uid = resp.body.uploadId;
c1sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77709/upload?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
c2uid = resp.body.uploadId;
c2sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77709/upload', user124764, 200);
req.end(function (err, resp) {
s1uid = resp.body.uploadId;
s1sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
var req = getRequest('/v2/develop/challenges/77709/upload', user124764, 200);
req.end(function (err, resp) {
s2uid = resp.body.uploadId;
s2sid = resp.body.submissionId;
cb(err);
});
}, function (cb) {
//Now we test for correctness
var sql = "u.upload_id, u.upload_status_id, s.submission_id, s.submission_status_id from upload u, submission s where " +
"u.upload_id = s.upload_id AND s.submission_id IN (" + c1sid + "," + c2sid + "," + s1sid + "," + s2sid + ")"
+ " AND u.upload_id IN (" + c1uid + "," + c2uid + "," + s1uid + "," + s2uid + ")";
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
//The checkpoint1 must be active
var c1rec = _.findWhere(result, {upload_id: c1uid});
assert.equal(c1rec.upload_status_id, 1);
assert.equal(c1rec.submission_status_id, 1);
//The checkpoint2 must be active
var c2rec = _.findWhere(result, {upload_id: c2uid});
assert.equal(c2rec.upload_status_id, 1);
assert.equal(c2rec.submission_status_id, 1);
//The submission1 must be active
var s1rec = _.findWhere(result, {upload_id: s1uid});
assert.equal(s1rec.upload_status_id, 1);
assert.equal(s1rec.submission_status_id, 1);
//The submission2 must be active
var s2rec = _.findWhere(result, {upload_id: s2uid});
assert.equal(s2rec.upload_status_id, 1);
assert.equal(s2rec.submission_status_id, 1);
cb(err);
});
}
], done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload when user is not logged-in
* should return 401 error
*/
it('should return 401 error when not logged-in', function (done) {
var req = request(API_ENDPOINT)
.post('/v2/develop/challenges/77701/upload')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.attach('submissionFile', sampleSubmissionPath)
.expect(401);
req.end(done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload when challenge id is not a number
* should return 400 error
*/
it('should return 400 error challenge id is not a number', function (done) {
var req = getRequest('/v2/develop/challenges/blah/upload', user124764, 400);
req.end(done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload when challenge id is too large
* should return 400 error
*/
it('should return 400 error challenge id is too large', function (done) {
var req = getRequest('/v2/develop/challenges/2893473289749283749237489327498273497238947/upload', user124764, 400);
req.end(done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload when type is not allowed
* should return 400 error
*/
it('should return 400 error when type is not allowed', function (done) {
var req = getRequest('/v2/develop/challenges/77701/upload?type=nono', user124764, 400);
req.end(done);
});
/**
* Test /v2/develop/challenges/:challengeId/upload when contest does not exist
* should return 404 error
*/
it('should return 404 error when contest does not exist', function (done) {
var req = getRequest('/v2/develop/challenges/77799/upload', user124764, 404);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "No such challenge exists.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when contest is not a develop challenge
* should return 400 error
*/
it('should return 400 error when contest is not a develop challenge', function (done) {
var req = getRequest('/v2/develop/challenges/77702/upload', user124764, 400);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "Non-develop challenges are not supported.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when submission phase is not open
* should return 400 error
*/
it('should return 400 error when contest submission phase is not open', function (done) {
var req = getRequest('/v2/develop/challenges/77703/upload', user124764, 400);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "Submission phase for this challenge is not open.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when type is checkpoint and checkpoint submission phase is not open
* should return 400 error
*/
it('should return 400 error when type is checkpoint and checkpoint submission phase is not open', function (done) {
var req = getRequest('/v2/develop/challenges/77704/upload?type=checkpoint', user124764, 400);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "Checkpoint submission phase for this challenge is not open.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when contest type is MM
* should return 400 error
*/
it('should return 400 error when contest type is MM', function (done) {
var req = getRequest('/v2/develop/challenges/77705/upload', user124764, 400);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "Submission to Marathon Matches and Spec Reviews are not supported.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when contest type is Spec Review
* should return 400 error
*/
it('should return 400 error when contest type is Spec Review', function (done) {
var req = getRequest('/v2/develop/challenges/77706/upload', user124764, 400);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "Submission to Marathon Matches and Spec Reviews are not supported.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when user does not have Submitter role
* should return 403 error
*/
it('should return 403 error when user does not have Submitter role', function (done) {
var req = getRequest('/v2/develop/challenges/77701/upload', user124834, 403);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, "You cannot submit for this challenge as you are not a Submitter.");
done();
});
});
/**
* Test /v2/develop/challenges/:challengeId/upload when user submits a file which is larger than what we have configured
* should return 413 error
*/
it('should return 413 error user submits a file which is larger than what we have configured', function (done) {
var req = request(API_ENDPOINT)
.post('/v2/develop/challenges/77701/upload')
.set('Accept', 'application/json')
.set('Authorization', getAuthHeader(user124764))
.expect('Content-Type', /json/)
.attach('submissionFile', sampleSubmissionPathTooLarge)
.expect(413);
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details,
"The submission file size is greater than the max allowed size: " + (config.submissionMaxSizeBytes / 1024) + " KB.");
done();
});
});
});