Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add REST endpoints channels.roles & groups.roles #10607

Merged
merged 11 commits into from
May 11, 2018
11 changes: 11 additions & 0 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,14 @@ RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequire
}
});

RocketChat.API.v1.addRoute('channels.roles', { authRequired: true }, {
get() {
const findResult = findChannelByIdOrName({ params: this.requestParams() });

const roles = Meteor.runAsUser(this.userId, () => Meteor.call('getRoomRoles', findResult._id));

return RocketChat.API.v1.success({
roles
});
}
});
12 changes: 12 additions & 0 deletions packages/rocketchat-api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,15 @@ RocketChat.API.v1.addRoute('groups.unarchive', { authRequired: true }, {
return RocketChat.API.v1.success();
}
});

RocketChat.API.v1.addRoute('groups.roles', { authRequired: true }, {
get() {
const findResult = findPrivateGroupByIdOrName({ params: this.requestParams(), userId: this.userId });

const roles = Meteor.runAsUser(this.userId, () => Meteor.call('getRoomRoles', findResult.rid));

return RocketChat.API.v1.success({
roles
});
}
});
70 changes: 70 additions & 0 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,74 @@ describe('[Channels]', function() {
.end(done);
});
});

describe('/channels.roles', () => {
let testChannel;
it('/channels.create', (done) => {
request.post(api('channels.create'))
.set(credentials)
.send({
name: `channel.roles.test.${ Date.now() }`
})
.end((err, res) => {
testChannel = res.body.channel;
done();
});
});
it('/channels.invite', async(done) => {
request.post(api('channels.invite'))
.set(credentials)
.send({
roomId: testChannel._id,
userId: 'rocket.cat'
})
.end(done);
});
it('/channels.addModerator', (done) => {
request.post(api('channels.addModerator'))
.set(credentials)
.send({
roomId: testChannel._id,
userId: 'rocket.cat'
})
.end(done);
});
it('/channels.addLeader', (done) => {
request.post(api('channels.addLeader'))
.set(credentials)
.send({
roomId: testChannel._id,
userId: 'rocket.cat'
})
.end(done);
});
it('should return an array of role <-> user relationships in a channel', (done) => {
request.get(api('channels.roles'))
.set(credentials)
.query({
roomId: testChannel._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('roles').that.is.an('array').that.has.lengthOf(2);

expect(res.body.roles[0]).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[0]).to.have.a.property('rid').that.is.equal(testChannel._id);
expect(res.body.roles[0]).to.have.a.property('roles').that.is.an('array').that.includes('moderator', 'leader');
expect(res.body.roles[0]).to.have.a.property('u').that.is.an('object');
expect(res.body.roles[0].u).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[0].u).to.have.a.property('username').that.is.a('string');

expect(res.body.roles[1]).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[1]).to.have.a.property('rid').that.is.equal(testChannel._id);
expect(res.body.roles[1]).to.have.a.property('roles').that.is.an('array').that.includes('owner');
expect(res.body.roles[1]).to.have.a.property('u').that.is.an('object');
expect(res.body.roles[1].u).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[1].u).to.have.a.property('username').that.is.a('string');
})
.end(done);
});
});
});
70 changes: 70 additions & 0 deletions tests/end-to-end/api/03-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,74 @@ describe('groups', function() {
.end(done);
});
});

describe('/groups.roles', () => {
let testGroup;
it('/groups.create', (done) => {
request.post(api('groups.create'))
.set(credentials)
.send({
name: `group.roles.test.${ Date.now() }`
})
.end((err, res) => {
testGroup = res.body.group;
done();
});
});
it('/groups.invite', async(done) => {
request.post(api('groups.invite'))
.set(credentials)
.send({
roomId: testGroup._id,
userId: 'rocket.cat'
})
.end(done);
});
it('/groups.addModerator', (done) => {
request.post(api('groups.addModerator'))
.set(credentials)
.send({
roomId: testGroup._id,
userId: 'rocket.cat'
})
.end(done);
});
it('/groups.addLeader', (done) => {
request.post(api('groups.addLeader'))
.set(credentials)
.send({
roomId: testGroup._id,
userId: 'rocket.cat'
})
.end(done);
});
it('should return an array of roles <-> user relationships in a private group', (done) => {
request.get(api('groups.roles'))
.set(credentials)
.query({
roomId: testGroup._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.a.property('success', true);
expect(res.body).to.have.a.property('roles').that.is.an('array').that.has.lengthOf(2);

expect(res.body.roles[0]).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[0]).to.have.a.property('rid').that.is.equal(testGroup._id);
expect(res.body.roles[0]).to.have.a.property('roles').that.is.an('array').that.includes('moderator', 'leader');
expect(res.body.roles[0]).to.have.a.property('u').that.is.an('object');
expect(res.body.roles[0].u).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[0].u).to.have.a.property('username').that.is.a('string');

expect(res.body.roles[1]).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[1]).to.have.a.property('rid').that.is.equal(testGroup._id);
expect(res.body.roles[1]).to.have.a.property('roles').that.is.an('array').that.includes('owner');
expect(res.body.roles[1]).to.have.a.property('u').that.is.an('object');
expect(res.body.roles[1].u).to.have.a.property('_id').that.is.a('string');
expect(res.body.roles[1].u).to.have.a.property('username').that.is.a('string');
})
.end(done);
});
});
});