Skip to content

Commit

Permalink
[NEW] Create new permission.listAll endpoint to be able to use update…
Browse files Browse the repository at this point in the history
…dSince parameter (#12748)

* Create new permission.listAll endpoint to be able to use updatedSince parameter

* fix lint

* Change deprecation version
  • Loading branch information
MarcosSpessatto authored and rodrigok committed Dec 21, 2018
1 parent fb3e383 commit 41d9305
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/rocketchat-api/server/v1/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,46 @@ RocketChat.API.v1.addRoute('permissions', { authRequired: true }, {
},
});

// DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.85 this should be gone.
RocketChat.API.v1.addRoute('permissions.list', { authRequired: true }, {
get() {
const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get'));

return RocketChat.API.v1.success({
permissions: result,
});
return RocketChat.API.v1.success(this.deprecationWarning({
endpoint: 'permissions.list',
versionWillBeRemove: '0.85',
response: {
permissions: result,
},
}));
},
});

RocketChat.API.v1.addRoute('permissions.listAll', { authRequired: true }, {
get() {
const { updatedSince } = this.queryParams;

let updatedSinceDate;
if (updatedSince) {
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-roomId-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
} else {
updatedSinceDate = new Date(updatedSince);
}
}

let result;
Meteor.runAsUser(this.userId, () => result = Meteor.call('permissions/get', updatedSinceDate));

if (Array.isArray(result)) {
result = {
update: result,
remove: [],
};
}

return RocketChat.API.v1.success(result);
},
});

Expand Down
41 changes: 41 additions & 0 deletions tests/end-to-end/api/11-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe('[Permissions]', function() {
});
});

// DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.85 this should be gone.
describe('[/permissions.list]', () => {
it('should return all permissions that exists on the server, with respective roles', (done) => {
request.get(api('permissions.list'))
Expand All @@ -44,6 +46,45 @@ describe('[Permissions]', function() {
});
});

describe('[/permissions.listAll]', () => {
it('should return an array with update and remove properties', (done) => {
request.get(api('permissions.listAll'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('update').and.to.be.an('array');
expect(res.body).to.have.property('remove').and.to.be.an('array');
})
.end(done);
});

it('should return an array with update and remov properties when search by "updatedSince" query parameter', (done) => {
request.get(api('permissions.listAll?updatedSince=2018-11-27T13:52:01Z'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('update').and.to.be.an('array');
expect(res.body).to.have.property('remove').and.to.be.an('array');
})
.end(done);
});

it('should return an error when updatedSince query parameter is not a valid ISODate string', (done) => {
request.get(api('permissions.listAll?updatedSince=fsafdf'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
});
});

describe('[/permissions.update]', () => {
it('should change the permissions on the server', (done) => {
const permissions = [
Expand Down

0 comments on commit 41d9305

Please sign in to comment.