Skip to content

Commit

Permalink
Merge pull request #6734 from RocketChat/hotfix/master-incoming-webhook
Browse files Browse the repository at this point in the history
[Fix] Bug with incoming integration (0.55.1)
  • Loading branch information
engelgabriel authored and rodrigok committed Apr 19, 2017
1 parent 0db74e3 commit bfb6942
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 75 deletions.
23 changes: 21 additions & 2 deletions packages/rocketchat-api/server/v1/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ RocketChat.API.v1.addRoute('integrations.create', { authRequired: true }, {
name: String,
enabled: Boolean,
username: String,
urls: [String],
urls: Match.Maybe([String]),
channel: String,
event: String,
event: Match.Maybe(String),
triggerWords: Match.Maybe([String]),
alias: Match.Maybe(String),
avatar: Match.Maybe(String),
Expand All @@ -26,6 +26,11 @@ RocketChat.API.v1.addRoute('integrations.create', { authRequired: true }, {
integration = Meteor.call('addOutgoingIntegration', this.bodyParams);
});
break;
case 'webhook-incoming':
Meteor.runAsUser(this.userId, () => {
integration = Meteor.call('addIncomingIntegration', this.bodyParams);
});
break;
default:
return RocketChat.API.v1.failure('Invalid integration type.');
}
Expand Down Expand Up @@ -121,6 +126,20 @@ RocketChat.API.v1.addRoute('integrations.remove', { authRequired: true }, {
Meteor.call('deleteOutgoingIntegration', integration._id);
});

return RocketChat.API.v1.success({
integration
});
case 'webhook-incoming':
integration = RocketChat.models.Integrations.findOne({ _id: this.bodyParams.integrationId });

if (!integration) {
return RocketChat.API.v1.failure('No integration found.');
}

Meteor.runAsUser(this.userId, () => {
Meteor.call('deleteIncomingIntegration', integration._id);
});

return RocketChat.API.v1.success({
integration
});
Expand Down
8 changes: 4 additions & 4 deletions packages/rocketchat-integrations/server/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function executeIntegrationRest() {
}
this.bodyParams = result && result.content;
if (typeof result !== 'undefined') {
this.response = result.response;
this.scriptResponse = result.response;
if (result.user) {
this.user = result.user;
}
Expand All @@ -235,10 +235,10 @@ function executeIntegrationRest() {
if (_.isEmpty(message)) {
return RocketChat.API.v1.failure('unknown-error');
}
if (this.response) {
logger.incoming.debug('response', this.response);
if (this.scriptResponse) {
logger.incoming.debug('response', this.scriptResponse);
}
return RocketChat.API.v1.success(this.response);
return RocketChat.API.v1.success(this.scriptResponse);
} catch ({error}) {
return RocketChat.API.v1.failure(error);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/data/api-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ export function log(res) {
});
}

export function getCredentials() {
export function getCredentials(done = function() {}) {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
});
})
.end(done);
}

12 changes: 1 addition & 11 deletions tests/end-to-end/api/00-miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ import supertest from 'supertest';
describe('miscellaneous', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

describe('API default', () => {
// Required by mobile apps
Expand Down
12 changes: 1 addition & 11 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ import supertest from 'supertest';
describe('Users', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

it('/users.create', (done) => {
request.post(api('users.create'))
Expand Down
12 changes: 1 addition & 11 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ function getRoomInfo(roomId) {
describe('channels', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

it('/channels.create', (done) => {
request.post(api('channels.create'))
Expand Down
12 changes: 1 addition & 11 deletions tests/end-to-end/api/03-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ function getRoomInfo(roomId) {
describe('groups', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

it('/groups.create', (done) => {
request.post(api('groups.create'))
Expand Down
13 changes: 2 additions & 11 deletions tests/end-to-end/api/04-direct-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@ import supertest from 'supertest';
describe('direct messages', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

it('/chat.postMessage', (done) => {
request.post(api('chat.postMessage'))
.set(credentials)
Expand Down
12 changes: 1 addition & 11 deletions tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ import supertest from 'supertest';
describe('chat', function() {
this.retries(0);

before((done) => {
request.post(api('login'))
.send(login)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
credentials['X-Auth-Token'] = res.body.data.authToken;
credentials['X-User-Id'] = res.body.data.userId;
})
.end(done);
});
before(done => getCredentials(done));

it('/chat.postMessage', (done) => {
request.post(api('chat.postMessage'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {getCredentials, api, login, request, credentials, integration, log } fro
import {adminEmail, password} from '../../data/user.js';
import supertest from 'supertest';

describe('integrations', function() {
describe('Outgoing Integrations', function() {
this.retries(0);

it('/integrations.create', (done) => {
Expand Down
51 changes: 51 additions & 0 deletions tests/end-to-end/api/07-incoming-integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-env mocha */

import {getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('Incoming Integrations', function() {
this.retries(0);

let integration;

before(done => getCredentials(done));

before((done) => {
request.post(api('integrations.create'))
.set(credentials)
.send({
type: 'webhook-incoming',
name: 'Incoming test',
enabled: true,
username: 'rocket.cat',
scriptEnabled: false,
channel: '#general'
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
integration = res.body.integration;
})
.end(done);
});

after((done) => {
request.post(api('integrations.remove'))
.set(credentials)
.send({
type: 'webhook-incoming',
integrationId: integration._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.end(done);
});

it('should execute the incoming integration', (done) => {
request.post(`/hooks/${ integration._id }/${ integration.token }`)
.send({
text: 'Example message'
})
.expect(200)
.end(done);
});
});

0 comments on commit bfb6942

Please sign in to comment.