2021-06-01 12:20:20 +00:00
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
2018-12-05 10:43:53 +00:00
require 'rails_helper'
RSpec . describe 'Twilio SMS' , type : :request do
describe 'request handling' do
2020-06-19 09:17:18 +00:00
let ( :agent ) do
create ( :agent , groups : Group . all )
2018-12-05 10:43:53 +00:00
end
it 'does basic call' do
# configure twilio channel
group_id = Group . find_by ( name : 'Users' ) . id
UserInfo . current_user_id = 1
channel = create (
:channel ,
2018-12-19 17:31:51 +00:00
area : 'Sms::Account' ,
options : {
adapter : 'sms/twilio' ,
2018-12-05 10:43:53 +00:00
webhook_token : 'f409460e50f76d331fdac8ba7b7963b6' ,
2018-12-19 17:31:51 +00:00
account_id : '111' ,
token : '223' ,
sender : '333' ,
2018-12-05 10:43:53 +00:00
} ,
group_id : nil ,
)
# process inbound sms
post '/api/v1/sms_webhook' , params : read_message ( 'inbound_sms1' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :not_found )
2018-12-05 10:43:53 +00:00
post '/api/v1/sms_webhook/not_existing' , params : read_message ( 'inbound_sms1' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :not_found )
2018-12-05 10:43:53 +00:00
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms1' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :unprocessable_entity )
2018-12-05 10:43:53 +00:00
expect ( 'Can\'t use Channel::Driver::Sms::Twilio: #<Exceptions::UnprocessableEntity: Group needed in channel definition!>' ) . to eq ( json_response [ 'error' ] )
channel . group_id = Group . first . id
channel . save!
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms1' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
xml_response = REXML :: Document . new ( response . body )
expect ( 1 ) . to eq ( xml_response . elements . count )
ticket = Ticket . last
article = Ticket :: Article . last
customer = User . last
expect ( ticket . articles . count ) . to eq ( 1 )
expect ( ticket . title ) . to eq ( 'Ldfhxhcuffufuf. Fifififig. Fifififiif F...' )
expect ( ticket . state . name ) . to eq ( 'new' )
expect ( ticket . group_id ) . to eq ( group_id )
expect ( ticket . customer_id ) . to eq ( customer . id )
expect ( ticket . created_by_id ) . to eq ( customer . id )
expect ( article . from ) . to eq ( '+491710000000' )
expect ( article . to ) . to eq ( '+4915700000000' )
expect ( article . cc ) . to be_nil
expect ( article . subject ) . to be_nil
expect ( article . body ) . to eq ( 'Ldfhxhcuffufuf. Fifififig. Fifififiif Fifififiif Fifififiif Fifififiif Fifififiif' )
expect ( article . created_by_id ) . to eq ( customer . id )
expect ( article . sender . name ) . to eq ( 'Customer' )
expect ( article . type . name ) . to eq ( 'sms' )
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms2' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
xml_response = REXML :: Document . new ( response . body )
expect ( 1 ) . to eq ( xml_response . elements . count )
ticket . reload
expect ( ticket . articles . count ) . to eq ( 2 )
expect ( ticket . state . name ) . to eq ( 'new' )
article = Ticket :: Article . last
expect ( article . from ) . to eq ( '+491710000000' )
expect ( article . to ) . to eq ( '+4915700000000' )
expect ( article . cc ) . to be_nil
expect ( article . subject ) . to be_nil
2019-08-16 15:39:31 +00:00
expect ( article . body ) . to eq ( 'Follow-up' )
2018-12-05 10:43:53 +00:00
expect ( article . sender . name ) . to eq ( 'Customer' )
expect ( article . type . name ) . to eq ( 'sms' )
expect ( article . created_by_id ) . to eq ( customer . id )
# check duplicate callbacks
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms2' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
xml_response = REXML :: Document . new ( response . body )
expect ( 1 ) . to eq ( xml_response . elements . count )
ticket . reload
expect ( ticket . articles . count ) . to eq ( 2 )
expect ( ticket . state . name ) . to eq ( 'new' )
expect ( article . id ) . to eq ( Ticket :: Article . last . id )
# new ticket need to be create
ticket . state = Ticket :: State . find_by ( name : 'closed' )
ticket . save!
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms3' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
xml_response = REXML :: Document . new ( response . body )
expect ( 1 ) . to eq ( xml_response . elements . count )
ticket . reload
expect ( ticket . articles . count ) . to eq ( 2 )
2019-04-15 01:41:17 +00:00
expect ( ticket . id ) . not_to eq ( Ticket . last . id )
2018-12-05 10:43:53 +00:00
expect ( ticket . state . name ) . to eq ( 'closed' )
ticket = Ticket . last
article = Ticket :: Article . last
customer = User . last
expect ( ticket . articles . count ) . to eq ( 1 )
expect ( ticket . title ) . to eq ( 'new 2' )
expect ( ticket . group_id ) . to eq ( group_id )
expect ( ticket . customer_id ) . to eq ( customer . id )
expect ( ticket . created_by_id ) . to eq ( customer . id )
expect ( article . from ) . to eq ( '+491710000000' )
expect ( article . to ) . to eq ( '+4915700000000' )
expect ( article . cc ) . to be_nil
expect ( article . subject ) . to be_nil
expect ( article . body ) . to eq ( 'new 2' )
expect ( article . created_by_id ) . to eq ( customer . id )
expect ( article . sender . name ) . to eq ( 'Customer' )
expect ( article . type . name ) . to eq ( 'sms' )
# reply by agent
params = {
ticket_id : ticket . id ,
2018-12-19 17:31:51 +00:00
body : 'some test' ,
type : 'sms' ,
2018-12-05 10:43:53 +00:00
}
2020-06-19 09:17:18 +00:00
authenticated_as ( agent )
2018-12-05 10:43:53 +00:00
post '/api/v1/ticket_articles' , params : params , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :created )
2018-12-05 10:43:53 +00:00
expect ( json_response ) . to be_a_kind_of ( Hash )
expect ( json_response [ 'subject' ] ) . to be_nil
expect ( json_response [ 'body' ] ) . to eq ( 'some test' )
expect ( json_response [ 'content_type' ] ) . to eq ( 'text/plain' )
2020-06-19 09:17:18 +00:00
expect ( json_response [ 'updated_by_id' ] ) . to eq ( agent . id )
expect ( json_response [ 'created_by_id' ] ) . to eq ( agent . id )
2018-12-05 10:43:53 +00:00
stub_request ( :post , 'https://api.twilio.com/2010-04-01/Accounts/111/Messages.json' )
. with (
2018-12-19 17:31:51 +00:00
body : {
2018-12-05 10:43:53 +00:00
'Body' = > 'some test' ,
'From' = > '333' ,
2018-12-19 17:31:51 +00:00
'To' = > nil ,
2018-12-05 10:43:53 +00:00
} ,
headers : {
2018-12-19 17:31:51 +00:00
'Accept' = > 'application/json' ,
2018-12-05 10:43:53 +00:00
'Accept-Charset' = > 'utf-8' ,
2018-12-19 17:31:51 +00:00
'Authorization' = > 'Basic MTExOjIyMw==' ,
'Content-Type' = > 'application/x-www-form-urlencoded' ,
2018-12-05 10:43:53 +00:00
}
) . to_return ( status : 200 , body : '' , headers : { } )
expect ( article . preferences [ :delivery_retry ] ) . to be_nil
expect ( article . preferences [ :delivery_status ] ) . to be_nil
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2018-12-05 10:43:53 +00:00
Scheduler . worker ( true )
article = Ticket :: Article . find ( json_response [ 'id' ] )
expect ( article . preferences [ :delivery_retry ] ) . to eq ( 1 )
expect ( article . preferences [ :delivery_status ] ) . to eq ( 'success' )
end
it 'does customer based on already existing mobile attibute' do
customer = create (
2020-06-19 09:17:18 +00:00
:customer ,
2018-12-19 17:31:51 +00:00
email : 'me@example.com' ,
2018-12-05 10:43:53 +00:00
mobile : '01710000000' ,
)
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2018-12-05 10:43:53 +00:00
Scheduler . worker ( true )
UserInfo . current_user_id = 1
2019-06-28 11:38:49 +00:00
create (
2018-12-05 10:43:53 +00:00
:channel ,
2018-12-19 17:31:51 +00:00
area : 'Sms::Account' ,
2018-12-05 10:43:53 +00:00
options : {
2018-12-19 17:31:51 +00:00
adapter : 'sms/twilio' ,
2018-12-05 10:43:53 +00:00
webhook_token : 'f409460e50f76d331fdac8ba7b7963b6' ,
2018-12-19 17:31:51 +00:00
account_id : '111' ,
token : '223' ,
sender : '333' ,
2018-12-05 10:43:53 +00:00
} ,
)
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms1' ) , as : :json
2019-04-15 01:41:17 +00:00
expect ( response ) . to have_http_status ( :ok )
2018-12-05 10:43:53 +00:00
xml_response = REXML :: Document . new ( response . body )
expect ( 1 ) . to eq ( xml_response . elements . count )
expect ( customer . id ) . to eq ( User . last . id )
end
2021-08-20 03:36:30 +00:00
it 'does basic call when ticket has a custom attribute' , db_strategy : :reset do
create ( :object_manager_attribute_text , screens : attributes_for ( :required_screen ) )
ObjectManager :: Attribute . migration_execute
UserInfo . current_user_id = 1
create (
:channel ,
area : 'Sms::Account' ,
options : {
adapter : 'sms/twilio' ,
webhook_token : 'f409460e50f76d331fdac8ba7b7963b6' ,
account_id : '111' ,
token : '223' ,
sender : '333' ,
} ,
group_id : Group . first . id ,
)
post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6' , params : read_message ( 'inbound_sms1' ) , as : :json
expect ( response ) . to have_http_status ( :ok )
end
2018-12-05 10:43:53 +00:00
def read_message ( file )
JSON . parse ( File . read ( Rails . root . join ( 'test' , 'data' , 'twilio' , " #{ file } .json " ) ) )
end
end
end