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

Commit 79aec49

Browse files
committed
enable round question apis for web arena super role
1 parent 899023c commit 79aec49

File tree

2 files changed

+40
-61
lines changed

2 files changed

+40
-61
lines changed

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) {

test/test.srmRoundQuestions.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:8080',
3030
heffan : "ad|132456",
3131
"super" : "ad|132457",
3232
user : "ad|132458",
33-
ksmith : "ad|124861"
33+
ksmith : "ad|124861" // web arena super user
3434
};
3535

3636

@@ -70,7 +70,7 @@ function createRequest(queryString, user) {
7070
*/
7171
function createPostRequest(queryString, user) {
7272
var req = request(API_ENDPOINT)
73-
.put(queryString)
73+
.post(queryString)
7474
.set("Accept", "application/json")
7575
.expect("Content-Type", /json/);
7676
if (user) {
@@ -222,14 +222,18 @@ describe('SRM Round Questions APIs', function () {
222222
assertError("/v2/data/srm/rounds/13673/questions", null, 401, "Authorized information needed.", done);
223223
});
224224

225-
it("Admin access only.", function (done) {
226-
assertError("/v2/data/srm/rounds/13673/questions", 'user', 403, "Admin access only.", done);
225+
it("Admin or web Arena super user only.", function (done) {
226+
assertError("/v2/data/srm/rounds/13673/questions", 'user', 403, "Admin or web Arena super user only.", done);
227227
});
228228

229229
it("roundId should be number.", function (done) {
230230
assertError("/v2/data/srm/rounds/roundId/questions", 'heffan', 400, "roundId should be number.", done);
231231
});
232232

233+
it("roundId should be number (with web Arena super user).", function (done) {
234+
assertError("/v2/data/srm/rounds/roundId/questions", 'ksmith', 400, "roundId should be number.", done);
235+
});
236+
233237
it("roundId should be Integer.", function (done) {
234238
assertError("/v2/data/srm/rounds/13673.01/questions", 'heffan', 400, "roundId should be Integer.", done);
235239
});
@@ -255,12 +259,11 @@ describe('SRM Round Questions APIs', function () {
255259
assertError("/v2/data/srm/rounds/1000000/answers", 'user', 403, "Admin or web Arena super user only.", done);
256260
});
257261

258-
// Only admin or web arena super user can get into this step
259262
it("questionId should be number.", function (done) {
260263
assertError("/v2/data/srm/rounds/aaa/answers", 'heffan', 400, "questionId should be number.", done);
261264
});
262265

263-
it("questionId should be number.", function (done) {
266+
it("questionId should be number (with web Arena super user).", function (done) {
264267
assertError("/v2/data/srm/rounds/aaa/answers", 'ksmith', 400, "questionId should be number.", done);
265268
});
266269

@@ -399,12 +402,11 @@ describe('SRM Round Questions APIs', function () {
399402
assertPostError("/v2/data/srm/questions/306/answers", 'user', validRequest, 403, "Admin or web Arena super user only.", done);
400403
});
401404

402-
// Only admin or web arena super user can get into this step
403405
it("questionId should be number.", function (done) {
404406
assertPostError("/v2/data/srm/questions/aaa/answers", 'heffan', validRequest, 400, "questionId should be number.", done);
405407
});
406408

407-
it("questionId should be number.", function (done) {
409+
it("questionId should be number (with web Arena super user).", function (done) {
408410
assertPostError("/v2/data/srm/questions/aaa/answers", 'ksmith', validRequest, 400, "questionId should be number.", done);
409411
});
410412

@@ -469,9 +471,11 @@ describe('SRM Round Questions APIs', function () {
469471
assertPostError("/v2/data/srm/rounds/13673/questions", 'user', validRequest, 403, "Admin or web Arena super user only.", done);
470472
});
471473

472-
// Only admin or web arena super user can get into this step
473474
it("roundId should be number.", function (done) {
474475
assertPostError("/v2/data/srm/rounds/aaa/questions", 'heffan', validRequest, 400, "roundId should be number.", done);
476+
});
477+
478+
it("roundId should be number (with web Arena super user).", function (done) {
475479
assertPostError("/v2/data/srm/rounds/aaa/questions", 'ksmith', validRequest, 400, "roundId should be number.", done);
476480
});
477481

@@ -587,14 +591,18 @@ describe('SRM Round Questions APIs', function () {
587591
assertPostError("/v2/data/srm/rounds/306/question", null, validRequest, 401, "Authorized information needed.", done);
588592
});
589593

590-
it("Admin access only.", function (done) {
591-
assertPostError("/v2/data/srm/rounds/306/question", 'user', validRequest, 403, "Admin access only.", done);
594+
it("Admin or web Arena super user only.", function (done) {
595+
assertPostError("/v2/data/srm/rounds/306/question", 'user', validRequest, 403, "Admin or web Arena super user only.", done);
592596
});
593597

594598
it("questionId should be number.", function (done) {
595599
assertPostError("/v2/data/srm/rounds/aaa/question", 'heffan', validRequest, 400, "questionId should be number.", done);
596600
});
597601

602+
it("questionId should be number (with web Arena super user).", function (done) {
603+
assertPostError("/v2/data/srm/rounds/aaa/question", 'ksmith', validRequest, 400, "questionId should be number.", done);
604+
});
605+
598606
it("questionId should be Integer.", function (done) {
599607
assertPostError("/v2/data/srm/rounds/30.6/question", 'heffan', validRequest, 400, "questionId should be Integer.", done);
600608
});
@@ -697,14 +705,18 @@ describe('SRM Round Questions APIs', function () {
697705
assertDeleteError("/v2/data/srm/rounds/306/question", null, 401, "Authorized information needed.", done);
698706
});
699707

700-
it("Admin access only.", function (done) {
701-
assertDeleteError("/v2/data/srm/rounds/306/question", 'user', 403, "Admin access only.", done);
708+
it("Admin or web Arena super user only.", function (done) {
709+
assertDeleteError("/v2/data/srm/rounds/306/question", 'user', 403, "Admin or web Arena super user only.", done);
702710
});
703711

704712
it("questionId should be number.", function (done) {
705713
assertDeleteError("/v2/data/srm/rounds/aaa/question", 'heffan', 400, "questionId should be number.", done);
706714
});
707715

716+
it("questionId should be number (with web Arena super user).", function (done) {
717+
assertDeleteError("/v2/data/srm/rounds/aaa/question", 'ksmith', 400, "questionId should be number.", done);
718+
});
719+
708720
it("questionId should be Integer.", function (done) {
709721
assertDeleteError("/v2/data/srm/rounds/30.6/question", 'heffan', 400, "questionId should be Integer.", done);
710722
});

0 commit comments

Comments
 (0)