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.srmRoundComponentsAndTerms.js
488 lines (425 loc) · 22.4 KB
/
test.srmRoundComponentsAndTerms.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
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*
* @version 1.0
* @author TCSASSEMBLER
*
* The test cases for srmRoundComponentsAndTerms.js.
*/
"use strict";
/*global describe, it, before, beforeEach, after, afterEach */
/**
* Module dependencies.
*/
var _ = require('underscore'),
async = require('async'),
request = require('supertest'),
chai = require('chai'),
jwt = require('jsonwebtoken');
var assert = chai.assert;
var testHelper = require('./helpers/testHelper');
var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080',
SQL_DIR = __dirname + "/sqls/srmRoundComponentsAndTerms/",
CLIENT_ID = require('../config/tc-config').tcConfig.oauthClientId,
CLIENT_SECRET = require('../config/tc-config').tcConfig.oauthClientSecret,
USER = {
heffan : "ad|132456",
"super" : "ad|132457",
user : "ad|132458",
ksmith : "ad|124861" // web arena super user
};
/**
* Generate an auth header
* @param {String} user the user to generate the header for
* @return {String} the generated string
*/
function generateAuthHeader(user) {
return "Bearer " + jwt.sign({sub: USER[user]}, CLIENT_SECRET, {expiresInMinutes: 1000, audience: CLIENT_ID});
}
/**
* Create put request and return it.
*
* @param queryString - the query string
* @param user - the user handle
* @returns {*} request
*/
function createPostRequest(queryString, user) {
var req = request(API_ENDPOINT)
.post(queryString)
.set("Accept", "application/json")
.expect("Content-Type", /json/);
if (user) {
req.set('Authorization', generateAuthHeader(user));
}
return req;
}
/**
* Create get request and return it.
*
* @param queryString - the query string
* @param user - the user handle
* @returns {*} request
*/
function createGetRequest(queryString, user) {
var req = request(API_ENDPOINT)
.get(queryString)
.set("Accept", "application/json")
.expect("Content-Type", /json/);
if (user) {
req.set('Authorization', generateAuthHeader(user));
}
return req;
}
/**
* Assert post response detail.
*
* @param queryString - the query string
* @param user - the user handle
* @param obj - the JSON object
* @param statusCode - the expected status code
* @param errorDetail - the error detail.
* @param done the callback function
*/
function assertPostError(queryString, user, obj, statusCode, errorDetail, done) {
createPostRequest(queryString, user).expect(statusCode).send(obj).end(function (err, res) {
if (err) {
done(err);
return;
}
if (statusCode === 200) {
assert.equal(res.body.error, errorDetail, "Invalid error detail");
} else {
assert.equal(res.body.error.details, errorDetail, "Invalid error detail");
}
done();
});
}
/**
* Assert Get response detail.
*
* @param queryString - the query string
* @param user - the user handle
* @param obj - the JSON object
* @param statusCode - the expected status code
* @param errorDetail - the error detail.
* @param done the callback function
*/
function assertGetError(queryString, user, obj, statusCode, errorDetail, done) {
createGetRequest(queryString, user).expect(statusCode).send(obj).end(function (err, res) {
if (err) {
done(err);
return;
}
if (statusCode === 200) {
assert.equal(res.body.error, errorDetail, "Invalid error detail");
} else {
assert.equal(res.body.error.details, errorDetail, "Invalid error detail");
}
done();
});
}
describe('SRM Round Components And Terms APIs', function () {
this.timeout(120000); // Wait 2 minutes, remote db might be slow.
/**
* Clear database
* @param {Function<err>} done the callback
*/
function clearDb(done) {
testHelper.runSqlFile(SQL_DIR + "informixoltp__clean", "informixoltp", done);
}
/**
* This function is run before all tests.
*
* @param {Function<err>} done the callback
*/
before(function (done) {
async.waterfall([
clearDb
], done);
});
/**
* This function is run after all tests.
* Clean up all data.
* @param {Function<err>} done the callback
*/
after(function (done) {
clearDb(done);
});
describe('Set Round Problems API invalid test', function () {
var validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
it("No anonymous access.", function (done) {
assertPostError("/v2/data/srm/rounds/13673/components", null, validRequest, 401, "Authorized information needed.", done);
});
it("Admin or web Arena super user only.", function (done) {
assertPostError("/v2/data/srm/rounds/13673/components", 'user', validRequest, 403, "Admin or web Arena super user only.", done);
});
it("roundId should be number.", function (done) {
assertPostError("/v2/data/srm/rounds/13673a/components", 'heffan', validRequest, 400, "roundId should be number.", done);
});
it("roundId should be number (with web Arena super user).", function (done) {
assertPostError("/v2/data/srm/rounds/13673a/components", 'ksmith', validRequest, 400, "roundId should be number.", done);
});
it("roundId should be Integer.", function (done) {
assertPostError("/v2/data/srm/rounds/13673.01/components", 'heffan', validRequest, 400, "roundId should be Integer.", done);
});
it("roundId should be positive.", function (done) {
assertPostError("/v2/data/srm/rounds/-13673/components", 'heffan', validRequest, 400, "roundId should be positive.", done);
});
it("roundId should be less or equal to 2147483647.", function (done) {
assertPostError("/v2/data/srm/rounds/1111111111111111111/components", 'heffan', validRequest, 400,
"roundId should be less or equal to 2147483647.", done);
});
it("components should be Array.", function (done) {
validRequest = {"components": 1};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"components should be Array.", done);
});
it("componentId should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"componentId should be provided", done);
});
it("points should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"points should be provided", done);
});
it("divisionId should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"divisionId should be provided", done);
});
it("difficultyId should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"difficultyId should be provided", done);
});
it("openOrder should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"openOrder should be provided", done);
});
it("submitOrder should be provided", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"submitOrder should be provided", done);
});
it("componentId should be positive.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": -1, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"componentId should be positive.", done);
});
it("points should be greater or equal to 0", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": -1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"points should be greater or equal to 0", done);
});
it("divisionId should be number.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": "a", "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"divisionId should be number.", done);
});
it("difficultyId should be positive.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": -1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"difficultyId should be positive.", done);
});
it("openOrder should be non-negative.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": -1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"openOrder should be non-negative.", done);
});
it("submitOrder should be non-negative.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": -1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"submitOrder should be non-negative.", done);
});
it("The componentId does not exist in database.", function (done) {
validRequest = {"components": [{"componentId": 1, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"The componentId " + 1 + " does not exist in database.", done);
});
it("The divisionId does not exist in database.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 10, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"The divisionId " + 10 + " does not exist in database.", done);
});
it("The difficultyId does not exist in database.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 10, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"The difficultyId " + 10 + " does not exist in database.", done);
});
it("The componentId and divisionId group should be unique.", function (done) {
validRequest = {"components": [{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2020, "points": 1, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1}]};
assertPostError("/v2/data/srm/rounds/13673/components", 'heffan', validRequest, 400,
"The componentId " + 2020 + " and divisionId " + 1 + " group should be unique.", done);
});
});
describe('Set Round Terms API invalid test', function () {
var validRequest = {"terms": "term text"};
it("No anonymous access.", function (done) {
assertPostError("/v2/data/srm/rounds/13673/terms", null, validRequest, 401, "Authorized information needed.", done);
});
it("Admin or web Arena super user only.", function (done) {
assertPostError("/v2/data/srm/rounds/13673/terms", 'user', validRequest, 403, "Admin or web Arena super user only.", done);
});
it("roundId should be number.", function (done) {
assertPostError("/v2/data/srm/rounds/13673a/terms", 'heffan', validRequest, 400, "roundId should be number.", done);
});
it("roundId should be number (with web Arena super user).", function (done) {
assertPostError("/v2/data/srm/rounds/13673a/terms", 'ksmith', validRequest, 400, "roundId should be number.", done);
});
it("roundId should be Integer.", function (done) {
assertPostError("/v2/data/srm/rounds/13673.01/terms", 'heffan', validRequest, 400, "roundId should be Integer.", done);
});
it("roundId should be positive.", function (done) {
assertPostError("/v2/data/srm/rounds/-13673/terms", 'heffan', validRequest, 400, "roundId should be positive.", done);
});
it("roundId should be less or equal to 2147483647.", function (done) {
assertPostError("/v2/data/srm/rounds/1111111111111111111/terms", 'heffan', validRequest, 400,
"roundId should be less or equal to 2147483647.", done);
});
it("The round terms should not be empty.", function (done) {
validRequest.terms = " ";
assertPostError("/v2/data/srm/rounds/13673/terms", 'heffan', validRequest, 400,
"The round terms should not be empty.", done);
});
});
describe('Valid test', function () {
it("Valid set components.", function(done) {
var validRequest = {};
createPostRequest("/v2/data/srm/rounds/13673/components", 'heffan').expect(200).send(validRequest).end(function (err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.body.success, true, "Invalid response detail");
var sql = "* from round_component where round_id = 13673 order by component_id";
testHelper.runSqlSelectQuery(sql, "informixoltp", function (err, result) {
if (err) {
done(err);
return;
}
assert.equal(result.length, 0, "All components should be removed!");
});
done();
});
});
it("Valid set components.", function (done) {
var validRequest = {"components": [{"componentId": 2020, "points": 250, "divisionId": 1, "difficultyId": 1, "openOrder": 1, "submitOrder": 1},
{"componentId": 2021, "points": 500, "divisionId": 2, "difficultyId": 2, "openOrder": 1, "submitOrder": 1}]};
createPostRequest("/v2/data/srm/rounds/13673/components", 'heffan').expect(200).send(validRequest).end(function (err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.body.success, true, "Invalid response detail");
var sql = "* from round_component where round_id = 13673 order by component_id";
testHelper.runSqlSelectQuery(sql, "informixoltp", function (err, result) {
if (err) {
done(err);
return;
}
assert.equal(result.length, 2, "Exactly 2 rows must be returned");
var expected1 = {
round_id: 13673,
component_id: 2020,
submit_order: 1,
division_id: 1,
difficulty_id: 1,
points: 250,
open_order: 1
}, expected2 = {
round_id: 13673,
component_id: 2021,
submit_order: 1,
division_id: 2,
difficulty_id: 2,
points: 500,
open_order: 1
};
assert.deepEqual(result[0], expected1, 'Actual and Expected component did not match.');
assert.deepEqual(result[1], expected2, 'Actual and Expected component did not match.');
});
done();
});
});
it("Valid set terms.", function (done) {
var validRequest = {"terms": "term text"};
createPostRequest("/v2/data/srm/rounds/13673/terms", 'heffan').expect(200).send(validRequest).end(function (err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.body.success, true, "Invalid response detail");
var sql = "* from round_terms where round_id = 13673";
testHelper.runSqlSelectQuery(sql, "informixoltp", function (err, result) {
if (err) {
done(err);
return;
}
assert.equal(result.length, 1, "Exactly 1 row must be returned");
var expected = {
round_id: 13673,
terms_content: "term text"
};
assert.deepEqual(result[0], expected, 'Actual and Expected component did not match.');
});
done();
});
});
describe('Get Round Terms API invalid test', function () {
it("No anonymous access.", function (done) {
assertGetError("/v2/data/srm/rounds/13673/terms", null, null, 401, "Authorized information needed.", done);
});
it("Admin access only.", function (done) {
assertGetError("/v2/data/srm/rounds/13673/terms", 'user', null, 403, "Admin access only.", done);
});
it("roundId should be number.", function (done) {
assertGetError("/v2/data/srm/rounds/13673a/terms", 'heffan', null, 400, "roundId should be number.", done);
});
it("roundId should be Integer.", function (done) {
assertGetError("/v2/data/srm/rounds/13673.01/terms", 'heffan', null, 400, "roundId should be Integer.", done);
});
it("roundId should be positive.", function (done) {
assertGetError("/v2/data/srm/rounds/-13673/terms", 'heffan', null, 400, "roundId should be positive.", done);
});
it("roundId should be less or equal to 2147483647.", function (done) {
assertGetError("/v2/data/srm/rounds/1111111111111111111/terms", 'heffan', null, 400,
"roundId should be less or equal to 2147483647.", done);
});
it("The round terms should not be empty.", function (done) {
var notFoundRoundId = 136733;
assertGetError("/v2/data/srm/rounds/" + notFoundRoundId + "/terms", 'heffan', null, 400,
"The round terms can't be found with such roundId = " + notFoundRoundId, done);
});
it("Valid get round terms.", function (done) {
createGetRequest("/v2/data/srm/rounds/13673/terms", 'heffan').expect(200).send(null).end(function (err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.body.roundTermsContent, "term text", "Invalid response detail");
done();
});
});
});
});
});