Skip to content

Commit

Permalink
#140 Only allow admins to get another user's communicators
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvansson committed Mar 30, 2021
1 parent 97a839a commit 0bcbd49
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
7 changes: 7 additions & 0 deletions api/controllers/communicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ async function listCommunicators(req, res) {

async function getCommunicatorsEmail(req, res) {
const email = req.swagger.params.email.value;

if (!req.user.isAdmin && req.user.email !== email) {
return res.status(403).json({
message: "You are not authorized to get this user's communicators."
});
}

const { search = '' } = req.query;

const searchFields = ['name', 'author', 'description'];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js",
"test": "swagger project test",
"test": "NODE_ENV=test swagger project test",
"precommit": "lint-staged",
"snyk-protect": "snyk protect",
"prepare": "npm run snyk-protect"
Expand Down
46 changes: 46 additions & 0 deletions test/controllers/communicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const request = require('supertest');
const server = require('../../app');
const helper = require('../helper');

describe('Communicator API calls', function () {
describe('GET /communicator/byemail/:email', function() {
it("only allows an admin to get another user's communicators", async function() {
const adminEmail = helper.generateEmail();
const admin = await helper.prepareUser(server, {
role: 'admin',
email: adminEmail,
});

const userEmail = helper.generateEmail();
const user = await helper.prepareUser(server, {
role: 'user',
email: userEmail,
});

// Try to get another user's communicators as a regular user.
// This should fail.
await request(server)
.get(`/communicator/byemail/${encodeURI(adminEmail)}`)
.set('Authorization', `Bearer ${user.token}`)
.expect({
message: "You are not authorized to get this user's communicators.",
})
.expect(403);

// Try to get another user's communicators as an admin user.
// This should succeed.
await request(server)
.get(`/communicator/byemail/${encodeURI(userEmail)}`)
.set('Authorization', `Bearer ${admin.token}`)
.expect(200);


// Try to get my own communicators as a regular user.
// This should succeed.
await request(server)
.get(`/communicator/byemail/${encodeURI(userEmail)}`)
.set('Authorization', `Bearer ${user.token}`)
.expect(200);
});
});
});
3 changes: 2 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { token } = require('morgan');
var request = require('supertest');
const user = require('../api/controllers/user');
const should = chai.should();
const uuid = require('uuid');

const User = require('../api/models/User');

Expand Down Expand Up @@ -96,7 +97,7 @@ function prepareDb() {
}

function generateEmail() {
return `test${Date.now()}@example.com`;
return `test.${uuid.v4()}@example.com`;
}

/**
Expand Down

0 comments on commit 0bcbd49

Please sign in to comment.