@ -5,6 +5,7 @@ import { updatePermission } from '../../data/permissions.helper';
import { createIntegration , removeIntegration } from '../../data/integration.helper' ;
import { createUser , login } from '../../data/users.helper' ;
import { password } from '../../data/user' ;
import { createRoom } from '../../data/rooms.helper.js' ;
describe ( '[Incoming Integrations]' , function ( ) {
this . retries ( 0 ) ;
@ -13,6 +14,8 @@ describe('[Incoming Integrations]', function () {
let integrationCreatedByAnUser ;
let user ;
let userCredentials ;
let channel ;
let testChannelName ;
before ( ( done ) => getCredentials ( done ) ) ;
@ -20,8 +23,15 @@ describe('[Incoming Integrations]', function () {
updatePermission ( 'manage-incoming-integrations' , [ ] )
. then ( ( ) => updatePermission ( 'manage-own-incoming-integrations' , [ ] ) )
. then ( ( ) => updatePermission ( 'manage-own-outgoing-integrations' , [ ] ) )
. then ( ( ) => updatePermission ( 'manage-outgoing-integrations' , [ ] ) )
. then ( done ) ;
. then ( ( ) => updatePermission ( 'manage-outgoing-integrations' , [ ] ) ) ;
testChannelName = ` channel.test. ${ Date . now ( ) } - ${ Math . random ( ) } ` ;
createRoom ( { type : 'c' , name : testChannelName } ) . end ( ( err , res ) => {
channel = res . body . channel ;
return done ( ) ;
} ) ;
} ) ;
after ( ( done ) => {
@ -45,6 +55,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : true ,
channel : '#general' ,
} )
. expect ( 'Content-Type' , 'application/json' )
@ -69,6 +80,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : true ,
channel : '#general' ,
} )
. expect ( 'Content-Type' , 'application/json' )
@ -92,6 +104,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : true ,
channel : '#general' ,
} )
. expect ( 'Content-Type' , 'application/json' )
@ -116,6 +129,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : false ,
channel : '#general' ,
} )
. expect ( 'Content-Type' , 'application/json' )
@ -142,6 +156,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test2' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : false ,
channel : '#general' ,
} )
. expect ( 'Content-Type' , 'application/json' )
@ -165,6 +180,105 @@ describe('[Incoming Integrations]', function () {
. expect ( 200 )
. end ( done ) ;
} ) ;
it ( "should return an error when sending 'channel' field telling its configuration is disabled" , ( done ) => {
request
. post ( ` /hooks/ ${ integration . _id } / ${ integration . token } ` )
. send ( {
text : 'Example message' ,
channel : [ testChannelName ] ,
} )
. expect ( 400 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , false ) ;
expect ( res . body ) . to . have . property ( 'error' , 'overriding destination channel is disabled for this integration' ) ;
} )
. end ( done ) ;
} ) ;
it ( "should return an error when sending 'roomId' field telling its configuration is disabled" , ( done ) => {
request
. post ( ` /hooks/ ${ integration . _id } / ${ integration . token } ` )
. send ( {
text : 'Example message' ,
roomId : channel . _id ,
} )
. expect ( 400 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , false ) ;
expect ( res . body ) . to . have . property ( 'error' , 'overriding destination channel is disabled for this integration' ) ;
} )
. end ( done ) ;
} ) ;
it ( 'should send a message for a channel that is specified in the webhooks configuration' , ( done ) => {
const successfulMesssage = ` Message sent successfully at # ${ Date . now ( ) } ` ;
request
. post ( ` /hooks/ ${ integration . _id } / ${ integration . token } ` )
. send ( {
text : successfulMesssage ,
} )
. expect ( 200 )
. end ( ( ) => {
return request
. get ( api ( 'channels.messages' ) )
. set ( credentials )
. query ( {
roomId : 'GENERAL' ,
} )
. expect ( 'Content-Type' , 'application/json' )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'messages' ) . and . to . be . an ( 'array' ) ;
expect ( ! ! res . body . messages . find ( ( m ) => m . msg === successfulMesssage ) ) . to . be . true ;
} )
. end ( done ) ;
} ) ;
} ) ;
it ( 'should send a message for a channel that is not specified in the webhooks configuration' , async ( ) => {
await request
. put ( api ( 'integrations.update' ) )
. set ( credentials )
. send ( {
type : 'webhook-incoming' ,
overrideDestinationChannelEnabled : true ,
integrationId : integration . _id ,
username : 'rocket.cat' ,
channel : '#general' ,
scriptEnabled : true ,
enabled : true ,
name : integration . name ,
} )
. expect ( 'Content-Type' , 'application/json' )
. expect ( 200 )
. expect ( ( res ) => {
expect ( res . body ) . to . have . property ( 'success' , true ) ;
expect ( res . body ) . to . have . property ( 'integration' ) ;
expect ( res . body . integration . overrideDestinationChannelEnabled ) . to . be . equal ( true ) ;
} ) ;
const successfulMesssage = ` Message sent successfully at # ${ Date . now ( ) } ` ;
await request
. post ( ` /hooks/ ${ integration . _id } / ${ integration . token } ` )
. send ( {
text : successfulMesssage ,
channel : [ testChannelName ] ,
} )
. expect ( 200 ) ;
return request
. get ( api ( 'channels.messages' ) )
. 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 ( 'messages' ) . and . to . be . an ( 'array' ) ;
expect ( ! ! res . body . messages . find ( ( m ) => m . msg === successfulMesssage ) ) . to . be . true ;
} ) ;
} ) ;
} ) ;
describe ( '[/integrations.history]' , ( ) => {
@ -200,6 +314,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test' ,
username : 'rocket.cat' ,
scriptEnabled : false ,
overrideDestinationChannelEnabled : true ,
channel : '#general' ,
} ,
userCredentials ,
@ -381,6 +496,7 @@ describe('[Incoming Integrations]', function () {
alias : 'test updated' ,
username : 'rocket.cat' ,
scriptEnabled : true ,
overrideDestinationChannelEnabled : true ,
channel : '#general' ,
integrationId : integration . _id ,
} )