Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit d25e025

Browse files
committed
Merge pull request #403 from cheuk-fung/round-apis-update
Enable a bunch of round apis for web arena super user
2 parents 899023c + 03b0100 commit d25e025

16 files changed

+168
-124
lines changed

actions/srmChallenges.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ exports.setRoundLanguages = {
18861886
async.auto(
18871887
{
18881888
admin: function (cb) {
1889-
cb(helper.checkAdmin(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_MESSAGE));
1889+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
18901890
},
18911891
roundId: [
18921892
'admin',

actions/srmProblems.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ var async = require('async');
2222
var _ = require('underscore');
2323
var moment = require('moment');
2424
var IllegalArgumentError = require('../errors/IllegalArgumentError');
25-
var NotFoundError = require('../errors/NotFoundError');
26-
var UnauthorizedError = require('../errors/UnauthorizedError');
27-
var ForbiddenError = require('../errors/ForbiddenError');
25+
26+
/**
27+
* Error messages
28+
*/
29+
var NON_ADMIN_MESSAGE = "Admin access only.",
30+
NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE = "Admin or web Arena super user only.",
31+
UNAUTHORIZED_MESSAGE = "Authorized information needed.";
2832

2933
/**
3034
* Parsed the problem from database query result.
@@ -54,15 +58,15 @@ var listRoundProblems = function (api, connection, dbConnectionMap, next) {
5458

5559
async.waterfall([
5660
function (cb) {
57-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
61+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
5862
}, function (cb) {
5963
cb(helper.checkIdParameter(roundId, "roundId"));
6064
}, function (cb) {
6165
sqlParams.roundId = roundId;
6266
api.dataAccess.executeQuery("get_srm_assigned_problems", sqlParams, dbConnectionMap, cb);
6367
}, function (results, cb) {
6468
if (results.length === 0) {
65-
cb(new NotFoundError("Cannot find records by given roundId."));
69+
cb();
6670
return;
6771
}
6872
_.each(results, function (item) {
@@ -105,7 +109,7 @@ var listRoundProblemComponents = function (api, connection, dbConnectionMap, nex
105109

106110
async.waterfall([
107111
function (cb) {
108-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
112+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
109113
}, function (cb) {
110114
// check parameters
111115
var error = helper.checkIdParameter(roundId, "roundId");
@@ -135,7 +139,7 @@ var listRoundProblemComponents = function (api, connection, dbConnectionMap, nex
135139
}
136140
}, function (results, cb) {
137141
if (results.length === 0) {
138-
cb(new NotFoundError("Cannot find records by given roundId."));
142+
cb();
139143
return;
140144
}
141145
_.each(results, function (item) {
@@ -231,16 +235,7 @@ var listSRMProblems = function (api, connection, dbConnectionMap, next) {
231235

232236
async.waterfall([
233237
function (cb) {
234-
if (!helper.isAdmin(caller) && !caller.isWebArenaSuper) {
235-
if (!helper.isMember(caller)) {
236-
cb(new UnauthorizedError("Authorized information needed."));
237-
} else {
238-
cb(new ForbiddenError("Admin or web Arena super user only."));
239-
}
240-
} else {
241-
cb();
242-
}
243-
//cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
238+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
244239
}, function (cb) {
245240
if (caller.isWebArenaSuper) {
246241
async.waterfall([

actions/srmRoundComponentsAndTerms.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ var _ = require('underscore');
1414
var moment = require('moment');
1515
var IllegalArgumentError = require('../errors/IllegalArgumentError');
1616

17+
/**
18+
* Error messages
19+
*/
20+
var NON_ADMIN_MESSAGE = "Admin access only.",
21+
NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE = "Admin or web Arena super user only.",
22+
UNAUTHORIZED_MESSAGE = "Authorized information needed.";
23+
1724
/**
1825
* Check whether the given value is defined and id parameter.
1926
* @param value - the given value.
@@ -206,7 +213,7 @@ var setRoundComponents = function (api, connection, dbConnectionMap, next) {
206213

207214
async.waterfall([
208215
function (cb) {
209-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
216+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
210217
}, function (cb) {
211218
checkRoundId(api, dbConnectionMap, roundId, cb);
212219
}, function (error, cb) {
@@ -278,7 +285,7 @@ var setRoundTerms = function (api, connection, dbConnectionMap, next) {
278285

279286
async.waterfall([
280287
function (cb) {
281-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
288+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
282289
}, function (cb) {
283290
checkRoundId(api, dbConnectionMap, roundId, cb);
284291
}, function (error, cb) {

actions/srmRoundQuestions.js

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ var _ = require('underscore');
1919
var moment = require('moment');
2020
var IllegalArgumentError = require('../errors/IllegalArgumentError');
2121
var NotFoundError = require('../errors/NotFoundError');
22-
var UnauthorizedError = require('../errors/UnauthorizedError');
23-
var ForbiddenError = require('../errors/ForbiddenError');
2422

2523
var DATE_FORMAT = "YYYY-MM-DD HH:mm";
2624

25+
/**
26+
* Error messages
27+
*/
28+
var NON_ADMIN_MESSAGE = "Admin access only.",
29+
NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE = "Admin or web Arena super user only.",
30+
UNAUTHORIZED_MESSAGE = "Authorized information needed.";
31+
2732
/**
2833
* Get Round Question Answers.
2934
*
@@ -40,15 +45,7 @@ var getRoundQuestionAnswers = function (api, connection, dbConnectionMap, next)
4045

4146
async.waterfall([
4247
function (cb) {
43-
if (!helper.isAdmin(caller) && !caller.isWebArenaSuper) {
44-
if (!helper.isMember(caller)) {
45-
cb(new UnauthorizedError("Authorized information needed."));
46-
} else {
47-
cb(new ForbiddenError("Admin or web Arena super user only."));
48-
}
49-
} else {
50-
cb();
51-
}
48+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
5249
}, function (cb) {
5350
cb(helper.checkIdParameter(questionId, "questionId"));
5451
}, function (cb) {
@@ -88,7 +85,7 @@ var getRoundQuestions = function (api, connection, dbConnectionMap, next) {
8885

8986
async.waterfall([
9087
function (cb) {
91-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
88+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
9289
}, function (cb) {
9390
cb(helper.checkIdParameter(roundId, "roundId"));
9491
}, function (cb) {
@@ -287,28 +284,6 @@ var setRoundSurvey = function (api, connection, dbConnectionMap, next) {
287284
});
288285
};
289286

290-
/**
291-
* Check if user authorized and is admin or web Arena super user
292-
*
293-
* @param api the api instance.
294-
* @param the connection instance
295-
* @param callback the callback method
296-
*/
297-
function checkAuthorization(api, connection, callback) {
298-
var helper = api.helper,
299-
caller = connection.caller;
300-
301-
if (!helper.isAdmin(caller) && !caller.isWebArenaSuper) {
302-
if (!helper.isMember(caller)) {
303-
callback(new UnauthorizedError("Authorized information needed."));
304-
} else {
305-
callback(new ForbiddenError("Admin or web Arena super user only."));
306-
}
307-
} else {
308-
callback();
309-
}
310-
}
311-
312287
/**
313288
* Check question id.
314289
*
@@ -403,15 +378,7 @@ var addRoundQuestionAnswer = function (api, connection, dbConnectionMap, next) {
403378

404379
async.waterfall([
405380
function (cb) {
406-
if (!helper.isAdmin(caller) && !caller.isWebArenaSuper) {
407-
if (!helper.isMember(caller)) {
408-
cb(new UnauthorizedError("Authorized information needed."));
409-
} else {
410-
cb(new ForbiddenError("Admin or web Arena super user only."));
411-
}
412-
} else {
413-
cb();
414-
}
381+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
415382
}, function (cb) {
416383
checkQuestionId(api, dbConnectionMap, questionId, cb);
417384
}, function (error, cb) {
@@ -556,7 +523,7 @@ var addRoundQuestion = function (api, connection, dbConnectionMap, next) {
556523

557524
async.waterfall([
558525
function (cb) {
559-
checkAuthorization(api, connection, cb);
526+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
560527
}, function (cb) {
561528
checkRoundId(api, dbConnectionMap, roundId, cb);
562529
}, function (error, cb) {
@@ -620,7 +587,7 @@ var modifyRoundQuestion = function (api, connection, dbConnectionMap, next) {
620587

621588
async.waterfall([
622589
function (cb) {
623-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
590+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
624591
}, function (cb) {
625592
checkQuestionId(api, dbConnectionMap, questionId, cb);
626593
}, function (error, cb) {
@@ -659,7 +626,7 @@ var deleteRoundQuestion = function (api, connection, dbConnectionMap, next) {
659626

660627
async.waterfall([
661628
function (cb) {
662-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
629+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
663630
}, function (cb) {
664631
checkQuestionId(api, dbConnectionMap, questionId, cb);
665632
}, function (error, cb) {
@@ -731,7 +698,7 @@ var modifyRoundQuestionAnswer = function (api, connection, dbConnectionMap, next
731698

732699
async.waterfall([
733700
function (cb) {
734-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
701+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
735702
}, function (cb) {
736703
checkAnswerValues(api, text, sortOrder, correct, cb);
737704
}, function (error, cb) {
@@ -768,7 +735,7 @@ var deleteRoundQuestionAnswer = function (api, connection, dbConnectionMap, next
768735

769736
async.waterfall([
770737
function (cb) {
771-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
738+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
772739
}, function (cb) {
773740
cb(helper.checkIdParameter(answerId, 'answerId'));
774741
}, function (cb) {

actions/srmRoundSegments.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ var IllegalArgumentError = require('../errors/IllegalArgumentError');
2020

2121
var DATE_FORMAT = "YYYY-MM-DD HH:mm:ssZZ";
2222
var DB_DATE_FORMAT = "YYYY-MM-DD HH:mm:ss";
23+
24+
/**
25+
* Error messages
26+
*/
27+
var NON_ADMIN_MESSAGE = "Admin access only.",
28+
NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE = "Admin or web Arena super user only.",
29+
UNAUTHORIZED_MESSAGE = "Authorized information needed.";
30+
2331
/**
2432
* Check round id.
2533
*
@@ -155,7 +163,7 @@ var setRoundSegments = function (api, connection, dbConnectionMap, next) {
155163

156164
async.waterfall([
157165
function (cb) {
158-
cb(helper.checkAdmin(connection, 'Authorized information needed.', 'Admin access only.'));
166+
cb(helper.checkAdminOrWebArenaSuper(connection, UNAUTHORIZED_MESSAGE, NON_ADMIN_OR_WEB_ARENA_SUPER_MESSAGE));
159167
}, function (cb) {
160168
checkRoundId(api, dbConnectionMap, roundId, cb);
161169
}, function (error, cb) {

test/test.srmProblems.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,18 @@ describe('SRM Round Problem APIs', function () {
172172
assertError("/v2/data/srm/rounds/13672/problems", null, 401, "Authorized information needed.", done);
173173
});
174174

175-
it("Admin access only.", function (done) {
176-
assertError("/v2/data/srm/rounds/13672/problems", 'user', 403, "Admin access only.", done);
175+
it("Admin or web Arena super user only.", function (done) {
176+
assertError("/v2/data/srm/rounds/13672/problems", 'user', 403, "Admin or web Arena super user only.", done);
177177
});
178178

179179
it("roundId should be number.", function (done) {
180180
assertError("/v2/data/srm/rounds/aaa/problems", 'heffan', 400, "roundId should be number.", done);
181181
});
182182

183+
it("roundId should be number (with web Arena super user).", function (done) {
184+
assertError("/v2/data/srm/rounds/aaa/problems", 'ksmith', 400, "roundId should be number.", done);
185+
});
186+
183187
it("roundId should be Integer.", function (done) {
184188
assertError("/v2/data/srm/rounds/1.01/problems", 'heffan', 400, "roundId should be Integer.", done);
185189
});
@@ -193,8 +197,9 @@ describe('SRM Round Problem APIs', function () {
193197
'heffan', 400, "roundId should be less or equal to 2147483647.", done);
194198
});
195199

196-
it("Cannot find records by given roundId.", function (done) {
197-
assertError("/v2/data/srm/rounds/100/problems", 'heffan', 404, "Cannot find records by given roundId.", done);
200+
it("Valid empty SRM round problems.", function (done) {
201+
validateResult("/v2/data/srm/rounds/100/problems", 'heffan',
202+
"./test_files/srm_problems/list_round_problems_empty.json", done);
198203
});
199204

200205
it("Valid request.", function (done) {
@@ -210,14 +215,18 @@ describe('SRM Round Problem APIs', function () {
210215
assertError("/v2/data/srm/rounds/13672/components", null, 401, "Authorized information needed.", done);
211216
});
212217

213-
it("Admin access only.", function (done) {
214-
assertError("/v2/data/srm/rounds/13672/components", 'user', 403, "Admin access only.", done);
218+
it("Admin or web Arena super user only.", function (done) {
219+
assertError("/v2/data/srm/rounds/13672/components", 'user', 403, "Admin or web Arena super user only.", done);
215220
});
216221

217222
it("roundId should be number.", function (done) {
218223
assertError("/v2/data/srm/rounds/aaa/components", 'heffan', 400, "roundId should be number.", done);
219224
});
220225

226+
it("roundId should be number (with web Arena super user).", function (done) {
227+
assertError("/v2/data/srm/rounds/aaa/components", 'ksmith', 400, "roundId should be number.", done);
228+
});
229+
221230
it("roundId should be Integer.", function (done) {
222231
assertError("/v2/data/srm/rounds/1.01/components", 'heffan', 400, "roundId should be Integer.", done);
223232
});
@@ -287,5 +296,11 @@ describe('SRM Round Problem APIs', function () {
287296
validateResult("/v2/data/srm/rounds/13672/components", 'heffan',
288297
"./test_files/srm_problems/list_round_problem_components_global.json", done);
289298
});
299+
300+
it("Valid empty SRM round components.", function (done) {
301+
validateResult("/v2/data/srm/rounds/1000000/components", 'heffan',
302+
"./test_files/srm_problems/list_round_problem_components_empty.json", done);
303+
});
304+
290305
});
291306
});

0 commit comments

Comments
 (0)