Skip to content

Commit

Permalink
fix(body): only show deleted bodies to people with permission. Fixes …
Browse files Browse the repository at this point in the history
…HELP-1873 (#386)

* fix(body): only show deleted bodies to people with permission

* chore(test): add tests
  • Loading branch information
WikiRik authored Nov 11, 2021
1 parent 6ffeb7d commit c3f38eb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions middlewares/bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ exports.listAllBodies = async (req, res) => {
};

exports.getBody = async (req, res) => {
if (req.currentBody.status === 'deleted' && (!req.user || !req.permissions.hasPermission('view_deleted:body'))) {
return errors.makeForbiddenError(res, 'Permission view_deleted:body is required, but not present.');
}

return res.json({
success: true,
data: req.currentBody
Expand Down
53 changes: 53 additions & 0 deletions test/api/bodies-details.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,57 @@ describe('Body details', () => {
expect(res.body).not.toHaveProperty('errors');
expect(res.body.data.id).toEqual(body.id);
});

test('should return 403 if not logged in on a deleted body', async () => {
const body = await generator.createBody({ status: 'deleted' });

const res = await request({
uri: '/bodies/' + body.id,
method: 'GET'
});

expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should return 403 if no permissions on a deleted body', async () => {
const user = await generator.createUser();
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody({ status: 'deleted' });

const res = await request({
uri: '/bodies/' + body.id,
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(403);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should work for authorized user on a deleted body', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

await generator.createPermission({ scope: 'global', action: 'view_deleted', object: 'body' });

const body = await generator.createBody({ status: 'deleted' });

const res = await request({
uri: '/bodies/' + body.id,
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');
expect(res.body.data.id).toEqual(body.id);
});
});

0 comments on commit c3f38eb

Please sign in to comment.