Skip to content

Commit

Permalink
#140 Only allow admins to get another user's boards
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvansson committed Mar 30, 2021
1 parent 0bcbd49 commit 8431c1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/controllers/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ async function listBoard(req, res) {
}

async function getBoardsEmail(req, res) {
const { search = '' } = req.query;
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 boards."
});
}

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

const searchFields = ['name', 'author'];
const query =
search && search.length ? getORQuery(searchFields, search, true) : {};
Expand Down
41 changes: 41 additions & 0 deletions test/controllers/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,45 @@ describe('Board API calls', function () {
done();
});
});

describe('GET /board/byemail/:email', function() {
it("only allows an admin to get another user's boards", 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 boards as a regular user.
// This should fail.
await request(server)
.get(`/board/byemail/${encodeURI(adminEmail)}`)
.set('Authorization', `Bearer ${user.token}`)
.expect({
message: "You are not authorized to get this user's boards.",
})
.expect(403);

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


// Try to get my own boards as a regular user.
// This should succeed.
await request(server)
.get(`/board/byemail/${encodeURI(userEmail)}`)
.set('Authorization', `Bearer ${user.token}`)
.expect(200);
});
});
});

0 comments on commit 8431c1a

Please sign in to comment.