From 5476bc22988403a167466737d4d70352eee0d150 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Fri, 13 Apr 2018 10:07:05 -0300 Subject: [PATCH 1/3] Add REST /directory endpoint --- packages/rocketchat-api/server/v1/misc.js | 23 ++++++ tests/end-to-end/api/00-miscellaneous.js | 95 ++++++++++++++++++++++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-api/server/v1/misc.js b/packages/rocketchat-api/server/v1/misc.js index 05fee75aec6b..c3178c70cc15 100644 --- a/packages/rocketchat-api/server/v1/misc.js +++ b/packages/rocketchat-api/server/v1/misc.js @@ -168,3 +168,26 @@ RocketChat.API.v1.addRoute('shield.svg', { authRequired: false }, { }; } }); + +RocketChat.API.v1.addRoute('directory', { authRequired: true }, { + get() { + const { offset, count } = this.getPaginationItems(); + const { sort, query } = this.parseJsonQuery(); + + const { text, type } = query; + const sortDirection = sort && sort === 1 ? 'asc' : 'desc'; + + const result = Meteor.runAsUser(this.userId, () => Meteor.call('browseChannels', { + text, + type, + sort: sortDirection, + page: offset, + limit: count + })); + + if (result) { + return RocketChat.API.v1.success({ result }); + } + return RocketChat.API.v1.failure('Please verify the parameters'); + } +}); diff --git a/tests/end-to-end/api/00-miscellaneous.js b/tests/end-to-end/api/00-miscellaneous.js index d655110d4c74..a21006e7dc5c 100644 --- a/tests/end-to-end/api/00-miscellaneous.js +++ b/tests/end-to-end/api/00-miscellaneous.js @@ -2,8 +2,8 @@ /* globals expect */ /* eslint no-unused-vars: 0 */ -import {getCredentials, api, login, request, credentials} from '../../data/api-data.js'; -import {adminEmail, adminUsername, adminPassword} from '../../data/user.js'; +import { getCredentials, api, login, request, credentials } from '../../data/api-data.js'; +import { adminEmail, adminUsername, adminPassword, password } from '../../data/user.js'; import supertest from 'supertest'; describe('miscellaneous', function() { @@ -12,7 +12,7 @@ describe('miscellaneous', function() { before(done => getCredentials(done)); describe('API default', () => { - // Required by mobile apps + // Required by mobile apps it('/info', (done) => { request.get('/api/info') .expect('Content-Type', 'application/json') @@ -98,4 +98,93 @@ describe('miscellaneous', function() { }); }); + describe('/directory', () => { + let user; + let testChannel; + before((done) => { + const username = `user.test.${ Date.now() }`; + const email = `${ username }@rocket.chat`; + request.post(api('users.create')) + .set(credentials) + .send({ email, name: username, username, password}) + .end((err, res) => { + user = res.body.user; + done(); + }); + }); + after(done => { + request.post(api('users.delete')).set(credentials).send({ + userId: user._id + }).end(done); + user = undefined; + }); + it('create an channel', (done) => { + request.post(api('channels.create')) + .set(credentials) + .send({ + name: `channel.test.${ Date.now() }` + }) + .end((err, res) => { + testChannel = res.body.channel; + done(); + }); + }); + it('should have return an array(result) when search by user and execute succesfully', (done) => { + request.get(api('directory')) + .set(credentials) + .query({ + query: JSON.stringify({ + text: user.username, + type: 'users' + }) + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('result').and.to.be.an('array'); + expect(res.body.result[0]).to.have.property('_id'); + expect(res.body.result[0]).to.have.property('createdAt'); + expect(res.body.result[0]).to.have.property('username'); + expect(res.body.result[0]).to.have.property('emails').and.to.be.an('array'); + expect(res.body.result[0]).to.have.property('name'); + }) + .end(done); + }); + it('should have return an array(result) when search by channel and execute succesfully', (done) => { + request.get(api('directory')) + .set(credentials) + .query({ + query: JSON.stringify({ + text: testChannel.name, + type: 'channels' + }) + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('result').and.to.be.an('array'); + expect(res.body.result[0]).to.have.property('_id'); + expect(res.body.result[0]).to.have.property('name'); + expect(res.body.result[0]).to.have.property('usernames').and.to.be.an('array'); + expect(res.body.result[0]).to.have.property('ts'); + }) + .end(done); + }); + }); + + describe('/settings.oauth', () => { + it('should have return list of available oauth services when user is logged', (done) => { + request.get(api('settings.oauth')) + .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('services').and.to.be.an('array'); + }) + .end(done); + }); + }); }); From 7535f2403422295538bb897edcbeb568cdf13606 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Wed, 18 Apr 2018 17:52:36 -0300 Subject: [PATCH 2/3] Remove duplicate test --- tests/end-to-end/api/00-miscellaneous.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/end-to-end/api/00-miscellaneous.js b/tests/end-to-end/api/00-miscellaneous.js index a21006e7dc5c..a02931f5c33a 100644 --- a/tests/end-to-end/api/00-miscellaneous.js +++ b/tests/end-to-end/api/00-miscellaneous.js @@ -173,18 +173,4 @@ describe('miscellaneous', function() { .end(done); }); }); - - describe('/settings.oauth', () => { - it('should have return list of available oauth services when user is logged', (done) => { - request.get(api('settings.oauth')) - .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('services').and.to.be.an('array'); - }) - .end(done); - }); - }); }); From 57a4fc5233614c85bd161ce9a879c672bf285aa9 Mon Sep 17 00:00:00 2001 From: Marcos Vinicius Spessatto Defendi Date: Thu, 19 Apr 2018 10:28:39 -0300 Subject: [PATCH 3/3] Correct logic, and add one more test case in REST directory endpoint --- packages/rocketchat-api/server/v1/misc.js | 6 ++--- tests/end-to-end/api/00-miscellaneous.js | 28 +++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/rocketchat-api/server/v1/misc.js b/packages/rocketchat-api/server/v1/misc.js index c3178c70cc15..72e2cd4483bb 100644 --- a/packages/rocketchat-api/server/v1/misc.js +++ b/packages/rocketchat-api/server/v1/misc.js @@ -185,9 +185,9 @@ RocketChat.API.v1.addRoute('directory', { authRequired: true }, { limit: count })); - if (result) { - return RocketChat.API.v1.success({ result }); + if (!result) { + return RocketChat.API.v1.failure('Please verify the parameters'); } - return RocketChat.API.v1.failure('Please verify the parameters'); + return RocketChat.API.v1.success({ result }); } }); diff --git a/tests/end-to-end/api/00-miscellaneous.js b/tests/end-to-end/api/00-miscellaneous.js index a02931f5c33a..000bb6c10b08 100644 --- a/tests/end-to-end/api/00-miscellaneous.js +++ b/tests/end-to-end/api/00-miscellaneous.js @@ -2,8 +2,8 @@ /* globals expect */ /* eslint no-unused-vars: 0 */ -import { getCredentials, api, login, request, credentials } from '../../data/api-data.js'; -import { adminEmail, adminUsername, adminPassword, password } from '../../data/user.js'; +import {getCredentials, api, login, request, credentials} from '../../data/api-data.js'; +import {adminEmail, adminUsername, adminPassword, password} from '../../data/user.js'; import supertest from 'supertest'; describe('miscellaneous', function() { @@ -106,7 +106,7 @@ describe('miscellaneous', function() { const email = `${ username }@rocket.chat`; request.post(api('users.create')) .set(credentials) - .send({ email, name: username, username, password}) + .send({email, name: username, username, password}) .end((err, res) => { user = res.body.user; done(); @@ -129,7 +129,7 @@ describe('miscellaneous', function() { done(); }); }); - it('should have return an array(result) when search by user and execute succesfully', (done) => { + it('should return an array(result) when search by user and execute succesfully', (done) => { request.get(api('directory')) .set(credentials) .query({ @@ -151,7 +151,7 @@ describe('miscellaneous', function() { }) .end(done); }); - it('should have return an array(result) when search by channel and execute succesfully', (done) => { + it('should return an array(result) when search by channel and execute succesfully', (done) => { request.get(api('directory')) .set(credentials) .query({ @@ -172,5 +172,23 @@ describe('miscellaneous', function() { }) .end(done); }); + + it('should return an error when send invalid query', (done) => { + request.get(api('directory')) + .set(credentials) + .query({ + query: JSON.stringify({ + text: 'invalid channel', + type: 'invalid' + }) + }) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).to.have.property('success', false); + }) + .end(done); + }); + }); });