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

Commit 8a39be1

Browse files
committed
enable modify round api for web arena super role
1 parent 0959cb7 commit 8a39be1

File tree

9 files changed

+138
-23
lines changed

9 files changed

+138
-23
lines changed

actions/srmRoundManagement.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
*
77
* Changes in version 1.1 (Module Assembly - Web Arena - Match Configurations):
88
* - Updated ListSRMContestRounds to send UTC time in milliseconds for registration and coding start time
9+
* Changes in version 1.2 (First2Finish - TC API - Modify SRM Round API Update):
10+
* - updated modifySRMContestRound to support web arena super role
911
*
10-
* @version 1.1
12+
* @version 1.2
1113
* @author TCSASSEMBLER
1214
*/
1315
/*jslint node: true, nomen: true */
1416
"use strict";
1517
var async = require('async');
1618
var _ = require('underscore');
1719
var NotFoundError = require('../errors/NotFoundError');
20+
var ForbiddenError = require('../errors/ForbiddenError');
1821

1922
/**
2023
* Max value for integer
@@ -613,13 +616,20 @@ exports.modifySRMContestRound = {
613616
helper.handleNoConnection(api, connection, next);
614617
return;
615618
}
619+
if (_.isUndefined(params.auto_end)) {
620+
params.auto_end = false;
621+
}
616622
async.series([
617623
function (cb) {
618624
console.log("oldRoundId = " + oldRoundId);
619625
var error =
620-
helper.checkIdParameter(oldRoundId, 'oldRoundId') ||
621-
checkContestRound(helper, params) ||
622-
helper.checkAdmin(connection, "You need to be authorized first.", "You are forbidden for this API.");
626+
helper.checkIdParameter(oldRoundId, 'oldRoundId') ||
627+
checkContestRound(helper, params) ||
628+
helper.checkAdminOrWebArenaSuper(
629+
connection,
630+
"You need to be authorized first.",
631+
"You are forbidden for this API."
632+
);
623633
cb(error);
624634
},
625635
// check if modifying round existed.
@@ -630,10 +640,12 @@ exports.modifySRMContestRound = {
630640
}, dbConnectionMap),
631641
function (result, cbx) {
632642
if (result.length === 0) {
633-
cbx(new NotFoundError('modifying round is not existed.'));
634-
} else {
635-
cbx(null);
643+
return cbx(new NotFoundError('modifying round is not existed.'));
644+
}
645+
if (connection.caller.isWebArenaSuper && result[0].creator_id !== connection.caller.userId) {
646+
return cbx(new ForbiddenError('Round was not created by you.'));
636647
}
648+
return cbx();
637649
}], cb);
638650
},
639651
function (cb) {

apiary.apib

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8513,6 +8513,16 @@ Request
85138513
"description":"The request is understood, but it has been refused or access is not allowed."
85148514
}
85158515

8516+
+ Response 403 (application/json)
8517+
8518+
{
8519+
"name":"Forbidden",
8520+
"value":"403",
8521+
"description": "The request is understood, but it has been refused or access is not allowed."
8522+
"details": "Round was not created by you."
8523+
}
8524+
8525+
85168526
+ Response 404 (application/json)
85178527

85188528
{

initializers/helper.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* This module contains helper functions.
88
* @author Sky_, Ghost_141, muzehyun, kurtrips, isv, LazyChild, hesibo, panoptimum, flytoj2ee, TCSASSEMBLER
9-
* @version 1.41
9+
* @version 1.42
1010
* changes in 1.1:
1111
* - add mapProperties
1212
* changes in 1.2:
@@ -110,6 +110,8 @@
110110
* - Update getLowerCaseList method to use map method.
111111
* Changes in 1.41:
112112
* - Update apiName2dbNameMap to add entries for srm schedule API.
113+
* Changes in 1.42:
114+
* - Add checkAdminOrWebArenaSuper to check if user has web arena super role.
113115
*/
114116
"use strict";
115117

@@ -1412,6 +1414,20 @@ helper.checkAdmin = function (connection, unauthorizedErrMsg, forbiddenErrMsg) {
14121414
return new ForbiddenError();
14131415
};
14141416

1417+
/**
1418+
* Check whether given user has web arena super role or not
1419+
* @param connection - the api connection object
1420+
* @param {String} unauthorizedErrMsg - the error message for unauthorized error.
1421+
* @param {String} forbiddenErrMsg - the error message for forbidden error.
1422+
* @return {Error} if user is not admin or does not have web arena super role.
1423+
*/
1424+
helper.checkAdminOrWebArenaSuper = function (connection, unauthorizedErrMsg, forbiddenErrMsg) {
1425+
if (connection.caller.isWebArenaSuper) {
1426+
return null;
1427+
}
1428+
return helper.checkAdmin(connection, unauthorizedErrMsg, forbiddenErrMsg);
1429+
};
1430+
14151431
/**
14161432
* Check if the caller has at least member access level.
14171433
* @param {Object} connection - the connection object.

queries/get_round_onlyid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
select round_id from round where round_id=@round_id@;
1+
select round_id, creator_id from round where round_id=@round_id@;

test/sqls/srmRoundManagement/informixoltp__insert_rounds

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ INSERT INTO contest (contest_id, name, status, group_id) VALUES (30007, "Contest
5555
INSERT INTO contest (contest_id, name, status, group_id) VALUES (30008, "Contest 8", "A", -1);
5656
INSERT INTO contest (contest_id, name, status, group_id) VALUES (30009, "Contest 9", "A", -1);
5757

58-
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational)
59-
VALUES (40055, 30006, "Round 7", "A", 11, 'short name 7', 1, 0, 5);
58+
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational, creator_id)
59+
VALUES (40055, 30006, "Round 7", "A", 11, 'short name 7', 1, 0, 5, 124861);
6060
INSERT INTO SURVEY(survey_id, name, start_date, end_date, status_id, text)
6161
VALUES (40055, 'survey2', '2013-10-09 01:50:00', '2013-10-09 02:50:00', 1, 'text 2');
6262
INSERT INTO round_room_assignment (round_id, coders_per_room, algorithm, by_division, final, by_region, p)
@@ -82,8 +82,8 @@ INSERT INTO round_segment (round_id, segment_id, start_time, end_time, status)
8282
INSERT INTO room (room_id, round_id, name, division_id, room_type_id) VALUES (499154, 40055, "Admin Room", -1, 1);
8383

8484

85-
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational)
86-
VALUES (40060, 30009, "Round 8", "A", 11, 'short name 8', 1, 0, 5);
85+
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational, creator_id)
86+
VALUES (40060, 30009, "Round 8", "A", 11, 'short name 8', 1, 0, 5, 132456);
8787
INSERT INTO SURVEY(survey_id, name, start_date, end_date, status_id, text)
8888
VALUES (40060, 'survey3', '2013-10-09 01:50:00', '2013-10-09 02:50:00', 1, 'text 3');
8989
INSERT INTO round_room_assignment (round_id, coders_per_room, algorithm, by_division, final, by_region, p)
@@ -128,8 +128,8 @@ INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name
128128
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational)
129129
VALUES (41005, 30010, "Round 5", "A", 10, 'short name 5', 1, 512, 4);
130130

131-
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational)
132-
VALUES (41006, 30011, "Round 6", "A", 11, 'short name 6', 1, 0, 5);
131+
INSERT INTO round (round_id, contest_id, name, status, round_type_id, short_name, region_id, registration_limit, invitational, creator_id)
132+
VALUES (41006, 30011, "Round 6", "A", 11, 'short name 6', 1, 0, 5, 124861);
133133
INSERT INTO SURVEY(survey_id, name, start_date, end_date, status_id, text)
134134
VALUES (41006, 'survey1', '2013-10-09 01:50:00', '2013-10-09 02:50:00', 1, 'text 1');
135135
INSERT INTO round_room_assignment (round_id, coders_per_room, algorithm, by_division, final, by_region, p)

test/test.srmRoundManagement.js

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/*
22
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
33
*
4-
* @version 1.0
5-
* @author TCASSEMBLER
4+
* @version 1.1
5+
* @author TCASSEMBLER, TCSFINALFIXER
6+
*
7+
* Changes in 1.1:
8+
* - Add tests 'should 403 if web arena super modifies round not his own',
9+
* 'should modify own round for web arena super role'
610
*/
711
"use strict";
812
/*global describe, it, before, beforeEach, after, afterEach */
@@ -1329,8 +1333,6 @@ describe('SRM Round Management APIs', function () {
13291333
], done);
13301334
});
13311335
});
1332-
1333-
13341336
});
13351337

13361338
describe("Modify SRM Contest Round", function () {
@@ -1445,6 +1447,20 @@ describe('SRM Round Management APIs', function () {
14451447
}, done);
14461448
});
14471449

1450+
it(
1451+
'should 403 if web arena super modifies round not his own',
1452+
function (done) {
1453+
var rrequest = requestClone(goodRequest);
1454+
assertFail({
1455+
oldRoundId: 40060,
1456+
json: rrequest,
1457+
status: 403,
1458+
auth: testHelper.generateAuthHeader({sub: 'ad|124861'}).substring(7),
1459+
message: 'Round was not created by you.'
1460+
}, done);
1461+
}
1462+
);
1463+
14481464
it("should return 400 when oldRoundId not a number", function (done) {
14491465
var rrequest = requestClone(goodRequest);
14501466
assertFail({
@@ -2219,7 +2235,7 @@ describe('SRM Round Management APIs', function () {
22192235
short_name: 'modified short name'
22202236
},
22212237
fields,
2222-
touchingFields = ['invitational', 'region_id', 'registration_limit', 'round_type_id',
2238+
touchingFields = ['creator_id', 'invitational', 'region_id', 'registration_limit', 'round_type_id',
22232239
'short_name', 'round_id', 'contest_id', 'status'];
22242240

22252241

@@ -2257,6 +2273,61 @@ describe('SRM Round Management APIs', function () {
22572273
}
22582274
], done);
22592275
});
2276+
2277+
2278+
it('should modify own round for web arena super role', function (done) {
2279+
async.waterfall([
2280+
_.bind(testHelper.runSqlSelectQuery, testHelper, GET_ROUND_SEQ_SQL, "informixoltp"),
2281+
function (results, cb) {
2282+
var oldRoundId = 41006,
2283+
req = _.clone(goodRequest),
2284+
jwt = testHelper.generateAuthHeader({sub: 'ad|124861'});
2285+
req.id = oldRoundId;
2286+
req.type.id = 24;
2287+
req.name = "modify round";
2288+
request(API_ENDPOINT)
2289+
.put('/v2/data/srm/rounds/' + oldRoundId)
2290+
.set('Accept', 'application/json')
2291+
.set('Content-Type', 'application/json')
2292+
.set('Authorization', jwt)
2293+
.expect('Content-Type', /json/)
2294+
.expect(200)
2295+
.send(req)
2296+
.end(function (err, res) {
2297+
if (err) {
2298+
return cb(err);
2299+
}
2300+
testHelper.runSqlSelectQuery(
2301+
"round_id, creator_id, round_type_id, name from round where round_id = " + req.id,
2302+
"informixoltp",
2303+
function (error, results) {
2304+
if (error) {
2305+
return cb(error);
2306+
}
2307+
fs.writeFileSync("/tmp/test.log", JSON.stringify(results));
2308+
assert.ok(results && results.length === 1, "query should have succeeded.");
2309+
assert.equal(
2310+
results[0].creator_id,
2311+
124861,
2312+
"creator should be ksmith"
2313+
);
2314+
assert.equal(
2315+
results[0].round_type_id,
2316+
req.type.id,
2317+
"type should be 'Instant Match'"
2318+
);
2319+
assert.equal(
2320+
results[0].name,
2321+
req.name,
2322+
"name should be modified"
2323+
);
2324+
return cb();
2325+
}
2326+
);
2327+
});
2328+
}
2329+
], done);
2330+
});
22602331
});
22612332

22622333
});

test/test_files/srmRoundManagement/contest30002.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"id": 11,
1616
"desc": "Weakest Link Round"
1717
},
18-
"segments": {
18+
"segments": {
19+
"codingStartTime": 1383958200000,
20+
"registrationStartTime": 1381276200000,
1921
"registrationStart": "Oct 08, 2013 21:50 EDT",
2022
"registrationLength": 60,
2123
"registrationStatus": "A",
@@ -78,4 +80,4 @@
7880
}
7981
}
8082
]
81-
}
83+
}

test/test_files/srmRoundManagement/contest30007.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"challengeStatus": "A",
5050
"codingLength": 60,
5151
"codingStart": "Nov 08, 2013 20:50 EST",
52+
"codingStartTime": 1383958200000,
53+
"registrationStartTime": 1381276200000,
5254
"codingStatus": "A",
5355
"intermissionLength": 60,
5456
"intermissionStatus": "A",

test/test_files/srmRoundManagement/contest30011.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"challengeStatus": "A",
5151
"codingLength": 60,
5252
"codingStart": "Nov 08, 2013 20:50 EST",
53+
"codingStartTime": 1383958200000,
54+
"registrationStartTime":1381276200000,
5355
"codingStatus": "A",
5456
"intermissionLength": 60,
5557
"intermissionStatus": "A",
@@ -79,4 +81,4 @@
7981
}
8082

8183
]
82-
}
84+
}

0 commit comments

Comments
 (0)