Skip to content

Commit a71c114

Browse files
committed
Allow delete of own submissions by members
1 parent 48886d6 commit a71c114

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/controllers/SubmissionController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function * patchSubmission (req, res) {
7474
* @param res the http response
7575
*/
7676
function * deleteSubmission (req, res) {
77-
yield SubmissionService.deleteSubmission(req.params.submissionId)
77+
yield SubmissionService.deleteSubmission(req.authUser, req.params.submissionId)
7878
res.status(204).send()
7979
}
8080

src/routes/SubmissionRoutes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
controller: 'SubmissionController',
5151
method: 'deleteSubmission',
5252
auth: 'jwt',
53-
access: ['Administrator'],
53+
access: ['Topcoder User', 'Administrator', 'Copilot'],
5454
scopes: ['delete:submission', 'all:submission'],
5555
blockByIp: true
5656
}

src/services/SubmissionService.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,20 @@ patchSubmission.schema = {
568568

569569
/**
570570
* Function to delete submission
571+
* @param {Object} authUser Authenticated User
571572
* @param {String} submissionId submissionId which need to be deleted
572573
* @return {Promise}
573574
*/
574-
function * deleteSubmission (submissionId) {
575+
function * deleteSubmission (authUser, submissionId) {
575576
const exist = yield _getSubmission(submissionId)
576577
if (!exist) {
577578
throw new errors.HttpStatusError(404, `Submission with ID = ${submissionId} is not found`)
578579
}
579580

581+
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && exist.memberId !== authUser.uerId) {
582+
throw new errors.HttpStatusError(403, 'You are now allowed to delete this submission.')
583+
}
584+
580585
// Filter used to delete the record
581586
const filter = {
582587
TableName: table,

test/unit/SubmissionService.test.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,15 @@ describe('Submission Service tests', () => {
470470
})
471471
})
472472

473-
it('Deleting submission with user token should throw 403', (done) => {
473+
it('Deleting submission with User token should get succeeded', (done) => {
474474
chai.request(app)
475475
.delete(`${config.API_VERSION}/submissions/${testSubmission.Item.id}`)
476476
.set('Authorization', `Bearer ${config.USER_TOKEN}`)
477477
.end((err, res) => {
478-
res.should.have.status(403)
479-
res.body.message.should.be.eql('You are not allowed to perform this action!')
478+
res.should.have.status(204)
480479
done()
481480
})
482-
})
481+
}).timeout(10000)
483482

484483
it('Deleting non-existent submission should throw 404', (done) => {
485484
chai.request(app)
@@ -492,6 +491,16 @@ describe('Submission Service tests', () => {
492491
})
493492
})
494493

494+
it('Deleting submission with Copilot token should get succeeded', (done) => {
495+
chai.request(app)
496+
.delete(`${config.API_VERSION}/submissions/${testSubmission.Item.id}`)
497+
.set('Authorization', `Bearer ${config.COPILOT_TOKEN}`)
498+
.end((err, res) => {
499+
res.should.have.status(204)
500+
done()
501+
})
502+
}).timeout(10000)
503+
495504
it('Deleting submission with Admin token should get succeeded', (done) => {
496505
chai.request(app)
497506
.delete(`${config.API_VERSION}/submissions/${testSubmission.Item.id}`)

0 commit comments

Comments
 (0)