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.submitDesignChallenge.js
825 lines (757 loc) · 33.2 KB
/
test.submitDesignChallenge.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.1
* @author kurtrips, isv
*/
"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 _ = require('underscore');
var async = require('async');
var usv = require("../common/unifiedSubmissionValidator");
var testHelper = require('./helpers/testHelper');
var SQL_DIR = __dirname + "/sqls/desUploadSubmission/";
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
/**
* Objects and values required for generating the OAuth token
*/
var CLIENT_ID = require('../config/tc-config').tcConfig.oauthClientId;
var SECRET = require('../config/tc-config').tcConfig.oauthClientSecret;
var jwt = require('jsonwebtoken');
/**
* Stores submission ids for testing
*/
var submissionIds = [];
describe('Submit for design challenge', function () {
/**
* Users that we have setup.
*/
var user124764 = 'facebook|fb124764',
user124766 = 'facebook|fb124766';
/**
* The path to a sample submission zip
*/
var sampleSubmissionPath = './test/test_files/des_upload_submission/submission.zip';
/**
* The path to a sample source zip
*/
var sampleSourcePath = './test/test_files/des_upload_submission/source.zip';
/**
* The path to a sample preview file
*/
var samplePreviewPath = './test/test_files/des_upload_submission/preview.jpg';
/**
* The path to an unknown type file
*/
var unknownTypePath = './test/test_files/des_upload_submission/strange.blah';
/**
* Deletes specified folder recursively.
*
* @param {String} path - a path to folder to be deleted.
*/
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
/**
* Return the authentication header to be used for the given user.
* @param {Object} user the user to authenticate
*/
function getAuthHeader(user) {
var authHeader = "Bearer " + jwt.sign({sub: user}, SECRET, {expiresInMinutes: 1000, audience: CLIENT_ID});
return authHeader;
}
/**
* Creates a Request object using the given URL and with the 3 submission files
* 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) {
var req = request(API_ENDPOINT)
.post(url)
.set('Accept', 'application/json')
.set('Authorization', getAuthHeader(user))
.expect('Content-Type', /json/)
.expect(expectedStatusCode)
.attach('submissionFile', sampleSubmissionPath)
.attach('sourceFile', sampleSourcePath)
.attach('previewFile', samplePreviewPath);
return req;
}
/**
* Creates a Request object using the given URL without the 3 submission files
* 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 getBareRequest(url, user, expectedStatusCode) {
var req = request(API_ENDPOINT)
.post(url)
.set('Accept', 'application/json')
.set('Authorization', getAuthHeader(user))
.expect('Content-Type', /json/)
.expect(expectedStatusCode);
return req;
}
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);
}
/**
* Checks if records have been inserted into image, submission_image tables and image files have been generated for
* tested submission.
*
* @param {Number} submissionId - ID for generated submission.
* @param {Function} callback - a callback to be notified when done.
*/
function checkGeneratedImages(submissionId, callback) {
var images = [],
imageFileName,
imageMapped,
submissionDir = usv.createDesignSubmissionPath(432001, 124764, 'hung');
async.waterfall([
function (cb) {
// check image table
var sql = "* from image ORDER BY image_id";
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
if (err) {
cb(err);
} else {
assert.equal(result.length, 21, 'Wrong number of images inserted into image table');
result.forEach(function (row) {
images.push({imageId: row.image_id, typeId: row.image_type_id, fileName: row.file_name});
});
cb();
}
});
}, function (cb) {
// check submission_image table
var sql = "* from submission_image WHERE submission_id = " + submissionId + " ORDER BY image_id";
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
if (err) {
cb(err);
} else {
imageMapped = false;
assert.equal(result.length, 21, 'Wrong number of images inserted into submission_image table');
result.forEach(function (row) {
images.forEach(function (image) {
if (image.imageId === row.image_id) {
imageMapped = true;
}
});
});
assert.isTrue(imageMapped, "Wrong image mapped to submission");
cb();
}
});
}, function (cb) {
// check files
images.forEach(function (image) {
imageFileName = submissionDir + image.fileName;
assert.isTrue(fs.existsSync(imageFileName), "Generated image file is not found");
});
assert.isTrue(fs.existsSync(submissionDir + submissionId + "_image.jpg"), "Preview image file is not found");
assert.isTrue(fs.existsSync(submissionDir + submissionId + "_imagew.png"), "Watermarked preview image file is not found");
assert.isTrue(fs.existsSync(submissionDir + submissionId + "_preview.zip"), "Preview ZIP file is not found");
cb();
}
], callback);
}
/**
* 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);
}
], done);
});
/**
* This function is run before each test.
* Generate tests data.
* @param {Function<err>} done the callback
*/
beforeEach(function (done) {
async.waterfall([
function (cb) {
testHelper.runSqlFile(SQL_DIR + "tcs_catalog__clean.part1", "tcs_catalog", cb);
}, function (cb) {
var submissionDir = usv.createDesignSubmissionPath(432001, 124764, 'hung');
if (fs.existsSync(submissionDir)) {
deleteFolderRecursive(submissionDir);
}
cb();
}
], done);
});
/**
* This function is run after all tests.
* @param {Function<err>} done the callback
*/
after(function (done) {
clearDb(done);
});
/**
* This function is run after each test.
* Cleans up tests data.
* @param {Function<err>} done the callback
*/
afterEach(function (done) {
async.waterfall([
function (cb) {
testHelper.runSqlFile(SQL_DIR + "tcs_catalog__clean.part1", "tcs_catalog", cb);
}, function (cb) {
var submissionDir = usv.createDesignSubmissionPath(432001, 124764, 'hung');
if (fs.existsSync(submissionDir)) {
deleteFolderRecursive(submissionDir);
}
cb();
}
], done);
});
/**
* Test design submission - when user is not authenticated
*/
it('should return 401 when user is not authenticated', function (done) {
var req = request(API_ENDPOINT)
.post('/v2/design/challenges/432001/submit')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.attach('submissionFile', sampleSubmissionPath)
.attach('sourceFile', sampleSourcePath)
.attach('previewFile', samplePreviewPath)
.expect(401);
req.end(done);
});
/**
* Test design submission - when challengeId is not a number
*/
it('should return 400 when challengeId is not a number', function (done) {
var req = getRequest('/v2/design/challenges/abcd/submit', user124764, 400);
req.end(done);
});
/**
* Test design submission - when challengeId is negative
*/
it('should return 400 when challengeId is negative', function (done) {
var req = getRequest('/v2/design/challenges/-1/submit', user124764, 400);
req.end(done);
});
/**
* Test design submission - when challengeId is 0
*/
it('should return 400 when challengeId is 0', function (done) {
var req = getRequest('/v2/design/challenges/0/submit', user124764, 400);
req.end(done);
});
/**
* Test design submission - when challengeId is too big
*/
it('should return 400 when challengeId is too big', function (done) {
var req = getRequest('/v2/design/challenges/2784298347328974932874982374923648762398472893742893742233/submit', user124764, 400);
req.end(done);
});
/**
* Test design submission - when rank is not a number
*/
it('should return 400 when rank is not a number', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('rank', 'abcdef');
req.end(done);
});
/**
* Test design submission - when type is neither submission not checkpoint
*/
it('should return 400 when rank is neither submission not checkpoint', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('type', 'abcdef');
req.end(done);
});
/**
* Test design submission - when challenge does not exist
*/
it('should return 404 when challenge does not exist', function (done) {
var req = getRequest('/v2/design/challenges/432999/submit', user124764, 404);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'No such challenge exists.');
done();
});
});
/**
* Test design submission - when challenge is not a design challenge
*/
it('should return 400 when challenge is not a design challenge', function (done) {
var req = getRequest('/v2/design/challenges/432002/submit', user124764, 400);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Non-design challenges are not supported.');
done();
});
});
/**
* Test design submission - when challenge is not active
*/
it('should return 400 when challenge is not active', function (done) {
var req = getRequest('/v2/design/challenges/432003/submit', user124764, 400);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Challenge is not currently open for submission.');
done();
});
});
/**
* Test design submission - when challenge is active but current timestamp is before registration phase
*/
it('should return 400 when challenge is active but current timestamp is before registration phase', function (done) {
var req = getRequest('/v2/design/challenges/432004/submit', user124764, 400);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Challenge is not currently open for submission.');
done();
});
});
/**
* Test design submission - when challenge is active but current timestamp is after submission phase
*/
it('should return 400 when challenge is active but current timestamp is after submission phase', function (done) {
var req = getRequest('/v2/design/challenges/432005/submit', user124764, 400);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Challenge is not currently open for submission.');
done();
});
});
/**
* Test design submission - when checkpoint submission is made but checkpoint phase is not open
*/
it('should return 400 when checkpoint submission is made but checkpoint phase is not open', function (done) {
var req = getRequest('/v2/design/challenges/432006/submit?type=checkpoint', user124764, 400);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Challenge is not currently open for checkpoint submission.');
done();
});
});
/**
* Test design submission - when user is not allowed to submit for challenge
*/
it('should return 403 when user is not allowed to submit for challenge', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124766, 403);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'You are not authorized to submit for this challenge.');
done();
});
});
/**
* Test design submission - when font data is sent but not of equal length
*/
it('should return 400 when font data is sent but not of equal length', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('fonts', 'font1||font2');
req.field('fontNames', 'arial||calibri');
req.field('fontUrls', 'http://www.google.com');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'font parameters are not all of same length.');
done();
});
});
/**
* Test design submission - when font data has an empty font source
*/
it('should return 400 when font data is sent but has empty font source', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('fonts', 'font1|| ||font3');
req.field('fontNames', 'arial||calibri||droid');
req.field('fontUrls', 'http://www.google.com||http://www.google.co.ru||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Font Source for index: 1');
done();
});
});
/**
* Test design submission - when font data has an empty font name
*/
it('should return 400 when font data is sent but has empty font name', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('fonts', 'font1||font2||font3');
req.field('fontNames', 'arial|| ||droid');
req.field('fontUrls', 'http://www.google.com||http://www.google.co.ru||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Font Name for index: 1');
done();
});
});
/**
* Test design submission - when font url is empty for a non standard font
* Note that in the test error is at index 1 and not 0, because font at index 0 is a standard one
*/
it('should return 400 when font data is sent but has empty font url', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('fonts', 'Studio Standard Fonts list||font2||font3');
req.field('fontNames', 'arial||sans||droid');
req.field('fontUrls', ' || ||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Font URL for index: 1');
done();
});
});
/**
* Test design submission - when stockArt data is sent but not of equal length
*/
it('should return 400 when stockArt data is sent but not of equal length', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('stockArtFileNumbers', '1||2');
req.field('stockArtNames', 'sa1||sa2');
req.field('stockArtUrls', 'http://www.google.com');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'stockArt parameters are not all of same length.');
done();
});
});
/**
* Test design submission - when stockArt data has an empty stockArt file number
*/
it('should return 400 when stockArt data is sent but has empty stockArt file number', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('stockArtFileNumbers', '1|| ||3');
req.field('stockArtNames', 'sa1||sa2||sa3');
req.field('stockArtUrls', 'http://www.google.com||http://www.google.co.ru||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Stock Art file number for index: 1');
done();
});
});
/**
* Test design submission - when stockArt data has an empty stockArt name
*/
it('should return 400 when stockArt data is sent but has an empty stckArt name', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('stockArtFileNumbers', '1||2||3');
req.field('stockArtNames', 'sa1|| ||sa3');
req.field('stockArtUrls', 'http://www.google.com||http://www.google.co.ru||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Stock Art name for index: 1');
done();
});
});
/**
* Test design submission - when stockArt url is empty
*/
it('should return 400 when stockArt data is sent but has an empty url', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.field('stockArtFileNumbers', 'stockArt1||stockArt2||stockArt3');
req.field('stockArtNames', 'sa1||sa2||sa3');
req.field('stockArtUrls', 'http://www.google.com|| ||http://www.google.co.in');
req.end(function (err, resp) {
if (err) {
done(err);
return;
}
assert.equal(resp.body.error.details, 'Missing Stock Art url for index: 1');
done();
});
});
/**
* Test design submission - when unknown file type is sent
*/
it('should return 400 when unknown file type is sent', function (done) {
var req = getBareRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.attach('submissionFile', sampleSubmissionPath)
.attach('sourceFile', sampleSourcePath)
.attach('previewFile', unknownTypePath);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Unknown file type submitted.');
done();
});
});
/**
* Test design submission - when zip is expected but not sent
*/
it('should return 400 when zip is expected but not sent', function (done) {
var req = getBareRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.attach('submissionFile', samplePreviewPath)
.attach('sourceFile', sampleSourcePath)
.attach('previewFile', samplePreviewPath);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Invalid file type submitted.');
done();
});
});
/**
* Test design submission - when image is expected but not sent
*/
it('should return 400 when image is expected but not sent', function (done) {
var req = getBareRequest('/v2/design/challenges/432001/submit', user124764, 400);
req.attach('submissionFile', sampleSubmissionPath)
.attach('sourceFile', sampleSourcePath)
.attach('previewFile', sampleSourcePath);
req.end(function (err, resp) {
assert.equal(resp.body.error.details, 'Invalid file type submitted.');
done();
});
});
/**
* Test design submission for success
* The tables must be populated properly
* The zip pointed by upload.parameter must exist on the system
* Note: Reviewer must examine the zip manually
* Also tests that all optional parameters are handled properly
* Also note that rank is automatically set to 1 even if we send it in as 0 (could even send it as negative)
*/
it('should return 200 when success', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 200),
dirName,
targetFile;
req.field('comment', 'just a test comment');
req.field('fonts', 'font1||font2||font3');
req.field('fontNames', 'arial||sans||droid');
req.field('fontUrls', 'http://www.google.com||http://www.google.co.ru||http://www.google.co.in');
req.field('stockArtFileNumbers', '1||2||3');
req.field('stockArtNames', 'sa1||sa2||sa3');
req.field('stockArtUrls', 'http://www.google.ca||http://www.google.ba||www.google.aa');
req.field('rank', '0');
req.end(function (err, resp) {
var submissionId = resp.body.submissionId;
submissionIds.push(submissionId);
var uploadId = resp.body.uploadId;
var parameter;
assert.isDefined(submissionId, "created submissionId must be returned in response.");
assert.isDefined(uploadId, "created uploadId must be returned in response.");
async.waterfall([
function (cb) {
var sql = "* from upload where upload_id = " + uploadId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
project_phase_id: 432002,
resource_id: 432011,
modify_user: '124764',
project_id: 432001,
upload_type_id: 1,
create_user: '124764',
upload_status_id: 1,
upload_id: uploadId
};
var actual = _.omit(result[0], ['create_date', 'modify_date', 'parameter']);
parameter = result[0].parameter;
assert.deepEqual(actual, expected, 'Actual and Expected of upload table did not match');
cb();
});
},
function (cb) {
var sql = "* from submission where submission_id = " + submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
submission_id: submissionId,
upload_id: uploadId,
submission_status_id: 1,
submission_type_id: 1,
modify_user: '124764',
create_user: '124764',
user_rank: 1
};
var actual = _.omit(result[0], ['create_date', 'modify_date']);
assert.deepEqual(actual, expected, 'Actual and Expected of submission table did not match');
cb();
});
}, function (cb) {
//Check if the row was created properly in resource_submission table
var sql = "* from resource_submission where submission_id = " + submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
resource_id: 432011,
modify_user: '124764',
create_user: '124764',
submission_id: 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();
});
}, function (cb) {
//The unified zip must exist in test/tmp/design_submissions/432001/hung_124764/
dirName = usv.createDesignSubmissionPath(432001, 124764, 'hung');
targetFile = dirName + parameter;
assert.isTrue(fs.existsSync(targetFile), "The unified zip must exist");
cb();
/*Please inspect the zip manually, specially that the declaration.txt inside the submission.zip of submission directory*/
}, function (cb) {
// Verify that the necessary records have been inserted into image, submission_image tables
checkGeneratedImages(submissionId, cb);
}
], done);
});
});
/**
* Test design submission - when rank is changed
* rank is sent in as 1 so the previous submission with rank 1 now becomes rank 2
*/
it('should return 200 when rank is changed', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 200);
req.field('rank', '1');
req.end(function (err, resp) {
var submissionId = resp.body.submissionId;
var previousSubmissionId = submissionIds[0];
async.waterfall([
function (cb) {
var sql = "user_rank, submission_id from submission where submission_id IN (" + previousSubmissionId + "," + submissionId + ") order by submission_id";
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
assert.equal(result[0].user_rank, 2);
assert.equal(result[1].user_rank, 1);
cb();
});
}, function (cb) {
checkGeneratedImages(submissionId, cb);
}
], done);
});
});
/**
* Test design submission - when rank is changed
* rank is sent in as 10 so the rank is set to 3 actually as previous submission have rank 1,2
*/
it('should return 200 when rank is changed', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit', user124764, 200);
req.field('rank', '10');
req.end(function (err, resp) {
var submissionId = resp.body.submissionId;
async.waterfall([
function (cb) {
var sql = "user_rank from submission where submission_id = " + submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
assert.equal(result[0].user_rank, 3);
cb();
});
}, function (cb) {
checkGeneratedImages(submissionId, cb);
}
], done);
});
});
/**
* Test design submission for success when chcekpoint submission is made
* Also tests that all optional parameters, when missing, do not cause the code to break.
*/
it('should return 200 when checkpoint submission is made', function (done) {
var req = getRequest('/v2/design/challenges/432001/submit?type=checkpoint', user124764, 200);
req.end(function (err, resp) {
var submissionId = resp.body.submissionId;
var uploadId = resp.body.uploadId;
assert.isDefined(submissionId, "created submissionId must be returned in response.");
assert.isDefined(uploadId, "created uploadId must be returned in response.");
async.waterfall([
function (cb) {
var sql = "* from upload where upload_id = " + uploadId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
project_phase_id: 432003,
resource_id: 432011,
modify_user: '124764',
project_id: 432001,
upload_type_id: 1,
create_user: '124764',
upload_status_id: 1,
upload_id: uploadId
};
var actual = _.omit(result[0], ['create_date', 'modify_date', 'parameter']);
assert.deepEqual(actual, expected, 'Actual and Expected of upload table did not match');
cb();
});
},
function (cb) {
var sql = "* from submission where submission_id = " + submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
submission_id: submissionId,
upload_id: 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();
});
}, function (cb) {
//Check if the row was created properly in resource_submission table
var sql = "* from resource_submission where submission_id = " + submissionId;
testHelper.runSqlSelectQuery(sql, "tcs_catalog", function (err, result) {
var expected = {
resource_id: 432011,
modify_user: '124764',
create_user: '124764',
submission_id: 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();
});
}, function (cb) {
checkGeneratedImages(submissionId, cb);
}
], done);
});
});
});