From 3e14f14df5b7d077ee892744a474bff749496369 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 27 Apr 2018 12:51:59 -0300 Subject: [PATCH 1/7] Add /api/v1/channels.roles --- packages/rocketchat-api/server/v1/channels.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index eb76ca5a2038..d1aa55331c2e 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -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 + }); + } +}); From e80d585163f03a470ad4269cbebb6ee264da4372 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 27 Apr 2018 12:57:27 -0300 Subject: [PATCH 2/7] Add /api/v1/groups.roles --- packages/rocketchat-api/server/v1/groups.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/rocketchat-api/server/v1/groups.js b/packages/rocketchat-api/server/v1/groups.js index 3826a38bca9d..562d7bb4da65 100644 --- a/packages/rocketchat-api/server/v1/groups.js +++ b/packages/rocketchat-api/server/v1/groups.js @@ -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 + }); + } +}); From 0886b6c95a93bebfd2a5959ec59bf318c3dcb5c3 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 27 Apr 2018 13:11:37 -0300 Subject: [PATCH 3/7] Add /api/v1/channels.roles tests --- tests/end-to-end/api/02-channels.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index a241a76be432..e2720083539e 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -627,4 +627,21 @@ describe('[Channels]', function() { .end(done); }); }); + + describe('/channels.roles', () => { + it('should return an array of roles by channel', (done) => { + request.get(api('channels.roles')) + .set(credentials) + .query({ + roomId: channel._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('roles').and.to.be.an('array'); + }) + .end(done); + }); + }); }); From 361ffe164318006d06f54fa52ed630c0af0ff468 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Fri, 27 Apr 2018 13:12:17 -0300 Subject: [PATCH 4/7] Add /api/v1/groups.roles tests --- tests/end-to-end/api/03-groups.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/end-to-end/api/03-groups.js b/tests/end-to-end/api/03-groups.js index 8ce141a0ac32..1bc5b4a93c47 100644 --- a/tests/end-to-end/api/03-groups.js +++ b/tests/end-to-end/api/03-groups.js @@ -465,4 +465,21 @@ describe('groups', function() { .end(done); }); }); + + describe('/groups.roles', () => { + it('should return an array of roles for a private group', (done) => { + request.get(api('groups.roles')) + .set(credentials) + .query({ + roomId: group._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('roles').and.to.be.an('array'); + }) + .end(done); + }); + }); }); From 436cc6cbd944e406589a100240aa7ea976975bea Mon Sep 17 00:00:00 2001 From: cardoso Date: Fri, 27 Apr 2018 16:30:03 -0300 Subject: [PATCH 5/7] Improve and fix /api/v1/(channels/groups).roles tests --- tests/end-to-end/api/02-channels.js | 63 +++++++++++++++++++++++++++-- tests/end-to-end/api/03-groups.js | 63 +++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 8 deletions(-) diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index e2720083539e..67c75ee78566 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -629,17 +629,72 @@ describe('[Channels]', function() { }); describe('/channels.roles', () => { - it('should return an array of roles by channel', (done) => { + 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: channel._id + roomId: testChannel._id }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('roles').and.to.be.an('array'); + 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[0].u).to.have.a.property('name'); + + 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'); + expect(res.body.roles[1].u).to.have.a.property('name'); }) .end(done); }); diff --git a/tests/end-to-end/api/03-groups.js b/tests/end-to-end/api/03-groups.js index 1bc5b4a93c47..4f121ac0445a 100644 --- a/tests/end-to-end/api/03-groups.js +++ b/tests/end-to-end/api/03-groups.js @@ -467,17 +467,72 @@ describe('groups', function() { }); describe('/groups.roles', () => { - it('should return an array of roles for a private group', (done) => { + 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: group._id + roomId: testGroup._id }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('roles').and.to.be.an('array'); + 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[0].u).to.have.a.property('name'); + + 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'); + expect(res.body.roles[1].u).to.have.a.property('name'); }) .end(done); }); From 93afde8419221ebf7fdabf318949b1d4e2ac7c7d Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Thu, 10 May 2018 13:50:59 -0300 Subject: [PATCH 6/7] Fix tests-without-oplog --- tests/end-to-end/api/03-groups.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/end-to-end/api/03-groups.js b/tests/end-to-end/api/03-groups.js index 4f121ac0445a..d1c75b7f59bd 100644 --- a/tests/end-to-end/api/03-groups.js +++ b/tests/end-to-end/api/03-groups.js @@ -524,7 +524,6 @@ describe('groups', function() { 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[0].u).to.have.a.property('name'); 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); @@ -532,7 +531,6 @@ describe('groups', function() { 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'); - expect(res.body.roles[1].u).to.have.a.property('name'); }) .end(done); }); From ad82e88564c7f526c2040567f1350a282d35aa66 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Thu, 10 May 2018 13:53:47 -0300 Subject: [PATCH 7/7] Fix tests-without-oplog --- tests/end-to-end/api/02-channels.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 67c75ee78566..e8b035332ef2 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -686,7 +686,6 @@ describe('[Channels]', function() { 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[0].u).to.have.a.property('name'); 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); @@ -694,7 +693,6 @@ describe('[Channels]', function() { 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'); - expect(res.body.roles[1].u).to.have.a.property('name'); }) .end(done); });