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.unifiedSubmissionValidator.js
297 lines (264 loc) · 12.7 KB
/
test.unifiedSubmissionValidator.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
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.0
* @author isv
*/
"use strict";
/*global describe, it, before, beforeEach, after, afterEach */
/*jslint node: true, stupid: true, unparam: true */
/**
* Module dependencies.
*/
var supertest = require('supertest');
var assert = require('chai').assert;
var async = require('async');
var path = require('path');
var usv = require("../common/unifiedSubmissionValidator");
var config = require("../config/tc-config").tcConfig;
var unifiedSubmissionValidator = usv.getUnifiedSubmissionValidator(null, null);
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080';
describe('Unified Submission Validator API', function () {
/**
* Tests the Unified Submission Validator against failure test case. Sends a request for calling the desired method
* of the validator and expects the server to respond with HTTP response of specified status providing the specified
* expected error details.
*
* @param {String} methodName - a name of the method to be tested.
* @param {String} queryParams - optional parameters to be passed to tested method.
* @param {Number} expectedStatusCode - status code for HTTP response expected to be returned from server.
* @param {String} expectedErrorMessage - error message expected to be returned from server.
* @param {Function} callback - a callback to be called when test finishes.
*/
function testFailureScenario(methodName, queryParams, expectedStatusCode, expectedErrorMessage, callback) {
supertest(API_ENDPOINT)
.get('/test/usv/' + methodName + '?' + queryParams)
.expect('Content-Type', /json/)
.expect(expectedStatusCode)
.end(function (err, res) {
if (err) {
callback(err);
return;
}
var body = res.body;
if (expectedStatusCode === 200) {
assert.equal(body.error, expectedErrorMessage);
} else {
assert.equal(body.error.details, expectedErrorMessage);
}
callback();
});
}
/**
* Tests the Unified Submission Validator against success test case. Sends a request for calling the desired method
* of the validator and expects the server to respond with HTTP 200 OK response. Passes the response body to
* callback.
*
* @param {String} methodName - a name of the method to be tested.
* @param {String} queryParams - optional parameters to be passed to tested method.
* @param {Function} callback - a callback to be called when test finishes.
*/
function testSuccessScenario(methodName, queryParams, callback) {
supertest(API_ENDPOINT)
.get('/test/usv/' + methodName + '?' + queryParams)
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
callback(err);
return;
}
var body = res.body;
callback(null, body);
});
}
this.timeout(30000);
it("getFileName - Correct file name must be returned", function (done) {
var fileName
= unifiedSubmissionValidator.getFileName(path.sep + 'tmp' + path.sep + 'test_files' + path.sep + 'test.txt');
assert.equal(fileName, 'test.txt', 'Wrong file name returned');
done();
});
it("calcAlternateFileName - Correct alternative file name must be returned", function (done) {
var expected,
fileName
= unifiedSubmissionValidator.calcAlternateFileName(1000, 132456, 'heffan', 2000, 'my_submission.zip', 'tiny');
expected = config.designSubmissionsBasePath + path.sep + '1000' + path.sep + 'heffan_132456' + path.sep + '2000_tiny.zip';
assert.equal(fileName, expected, 'Wrong alternative file name returned');
done();
});
it("getFileType - should return matching file type", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('getFileType', 'fileName=test.txt', cb);
}, function (response, cb) {
assert.ok(response.fileType);
assert.equal(response.fileType.extension, 'txt', 'Wrong file type found');
cb();
}
], done);
});
it("getFileType - should return no matching file type", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('getFileType', 'fileName=test.zzzz', cb);
}, function (response, cb) {
assert.notOk(response.fileType);
cb();
}
], done);
});
it("getBundledFileParser - non-archive file type - should respond with HTTP 400", function (done) {
testFailureScenario('getBundledFileParser', 'filePath=test.txt', 400, 'The file type [2] is not an archive file',
done);
});
it("getBundledFileParser - un-supported file type - should respond with HTTP 400", function (done) {
testFailureScenario('getBundledFileParser', 'filePath=test.ddd', 400, 'Unsupported file type',
done);
});
it("getBundledFileParser - should return matching file parser", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('getBundledFileParser', 'filePath=test.zip', cb);
}, function (response, cb) {
assert.ok(response.fileParser);
cb();
}
], done);
});
it("validate - valid submission (ZIP) - should return SUCCESS result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'valid_submission.zip', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isTrue(response.validationResult.valid);
assert.equal(response.validationResult.message, 'Success');
cb();
}
], done);
});
it("validate - invalid submission (no preview file) (ZIP) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_preview_file_submission.zip', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No preview file provided in the submission');
cb();
}
], done);
});
it("validate - invalid submission (no preview image) (ZIP) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_preview_image_submission.zip', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No preview image provided in the submission');
cb();
}
], done);
});
it("validate - invalid submission (no source) (ZIP) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_source_submission.zip', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No native sources provided in the submission');
cb();
}
], done);
});
it("validate - non-existing file - should respond with HTTP 400", function (done) {
testFailureScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'non_existing.zip', 400,
'Invalid filename', done);
});
it("validate - empty file - should respond with HTTP 400", function (done) {
testFailureScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'empty.zip', 400,
'Submission file is empty', done);
});
it("validate - valid submission (JAR) - should return SUCCESS result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'valid_submission.jar', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isTrue(response.validationResult.valid);
assert.equal(response.validationResult.message, 'Success');
cb();
}
], done);
});
it("validate - invalid submission (no preview file) (JAR) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_preview_file_submission.jar', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No preview file provided in the submission');
cb();
}
], done);
});
it("validate - invalid submission (no preview image) (JAR) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_preview_image_submission.jar', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No preview image provided in the submission');
cb();
}
], done);
});
it("validate - invalid submission (no source) (JAR) - should return FAILURE result", function (done) {
async.waterfall([
function (cb) {
testSuccessScenario('validate', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'no_source_submission.jar', cb);
}, function (response, cb) {
assert.ok(response.validationResult);
assert.isFalse(response.validationResult.valid);
assert.equal(response.validationResult.message, 'No native sources provided in the submission');
cb();
}
], done);
});
it("ZipFileAnalyzer#getFiles - should return files and contents", function (done) {
var expected
= {'dir1/1.txt': [49, 49, 49, 49, 49], 'dir2/2.txt': [50, 50, 50, 50, 50], '3.txt': [51, 51, 51, 51, 51]};
async.waterfall([
function (cb) {
testSuccessScenario('getFiles', 'filePath=test' + path.sep + 'test_files' + path.sep
+ 'unified_submission_validator' + path.sep + 'get_files.zip', cb);
}, function (response, cb) {
assert.ok(response.files);
assert.deepEqual(response.files, expected, 'Wrong files or contents returned');
cb();
}
], done);
});
it("createDesignSubmissionPath - Correct design submission path must be returned", function (done) {
var expected,
designSubmissionPath = usv.createDesignSubmissionPath(1000, 132456, 'heffan');
expected = config.designSubmissionsBasePath + path.sep + '1000' + path.sep + 'heffan_132456' + path.sep;
assert.equal(designSubmissionPath, expected, 'Wrong design submission path returned');
done();
});
});