twilio_sms_controller_test.rb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. require 'test_helper'
  2. require 'rexml/document'
  3. require 'webmock/minitest'
  4. class TwilioSmsControllerTest < ActionDispatch::IntegrationTest
  5. setup do
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. end
  8. test 'basic call' do
  9. # configure twilio channel
  10. bot_id = 123_456_789
  11. group_id = Group.find_by(name: 'Users').id
  12. UserInfo.current_user_id = 1
  13. channel = Channel.create!(
  14. area: 'Sms::Account',
  15. options: {
  16. adapter: 'sms/twilio',
  17. webhook_token: 'f409460e50f76d331fdac8ba7b7963b6',
  18. account_id: '111',
  19. token: '223',
  20. sender: '333',
  21. },
  22. group_id: nil,
  23. active: true,
  24. )
  25. # create agent
  26. agent = User.create!(
  27. login: 'tickets-agent@example.com',
  28. firstname: 'Tickets',
  29. lastname: 'Agent',
  30. email: 'tickets-agent@example.com',
  31. password: 'agentpw',
  32. active: true,
  33. roles: Role.where(name: 'Agent'),
  34. groups: Group.all,
  35. )
  36. # process inbound sms
  37. post '/api/v1/sms_webhook', params: read_messaage('inbound_sms1'), headers: @headers
  38. assert_response(404)
  39. result = JSON.parse(@response.body)
  40. post '/api/v1/sms_webhook/not_existing', params: read_messaage('inbound_sms1'), headers: @headers
  41. assert_response(404)
  42. result = JSON.parse(@response.body)
  43. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms1'), headers: @headers
  44. assert_response(422)
  45. result = JSON.parse(@response.body)
  46. assert_equal(result['error'], 'Can\'t use Channel::Driver::Sms::Twilio: #<Exceptions::UnprocessableEntity: Group needed in channel definition!>')
  47. channel.group_id = Group.first.id
  48. channel.save!
  49. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms1'), headers: @headers
  50. assert_response(200)
  51. response = REXML::Document.new(@response.body)
  52. assert_equal(response.elements.count, 1)
  53. ticket = Ticket.last
  54. article = Ticket::Article.last
  55. customer = User.last
  56. assert_equal(1, ticket.articles.count)
  57. assert_equal('Ldfhxhcuffufuf. Fifififig. Fifififiif F...', ticket.title)
  58. assert_equal('new', ticket.state.name)
  59. assert_equal(group_id, ticket.group_id)
  60. assert_equal(customer.id, ticket.customer_id)
  61. assert_equal(customer.id, ticket.created_by_id)
  62. assert_equal('+491710000000', article.from)
  63. assert_equal('+4915700000000', article.to)
  64. assert_nil(article.cc)
  65. assert_nil(article.subject)
  66. assert_equal('Ldfhxhcuffufuf. Fifififig. Fifififiif Fifififiif Fifififiif Fifififiif Fifififiif', article.body)
  67. assert_equal(customer.id, article.created_by_id)
  68. assert_equal('Customer', article.sender.name)
  69. assert_equal('sms', article.type.name)
  70. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms2'), headers: @headers
  71. assert_response(200)
  72. response = REXML::Document.new(@response.body)
  73. assert_equal(response.elements.count, 1)
  74. ticket.reload
  75. assert_equal(2, ticket.articles.count)
  76. assert_equal('new', ticket.state.name)
  77. article = Ticket::Article.last
  78. assert_equal('+491710000000', article.from)
  79. assert_equal('+4915700000000', article.to)
  80. assert_nil(article.cc)
  81. assert_nil(article.subject)
  82. assert_equal('Follow up', article.body)
  83. assert_equal('Customer', article.sender.name)
  84. assert_equal('sms', article.type.name)
  85. assert_equal(customer.id, article.created_by_id)
  86. # check duplicate callbacks
  87. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms2'), headers: @headers
  88. assert_response(200)
  89. response = REXML::Document.new(@response.body)
  90. assert_equal(response.elements.count, 1)
  91. ticket.reload
  92. assert_equal(2, ticket.articles.count)
  93. assert_equal('new', ticket.state.name)
  94. assert_equal(Ticket::Article.last.id, article.id)
  95. # new ticket need to be create
  96. ticket.state = Ticket::State.find_by(name: 'closed')
  97. ticket.save!
  98. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms3'), headers: @headers
  99. assert_response(200)
  100. response = REXML::Document.new(@response.body)
  101. assert_equal(response.elements.count, 1)
  102. ticket.reload
  103. assert_equal(2, ticket.articles.count)
  104. assert_not_equal(Ticket.last.id, ticket.id)
  105. assert_equal('closed', ticket.state.name)
  106. ticket = Ticket.last
  107. article = Ticket::Article.last
  108. customer = User.last
  109. assert_equal(1, ticket.articles.count)
  110. assert_equal('new 2', ticket.title)
  111. assert_equal(group_id, ticket.group_id)
  112. assert_equal(customer.id, ticket.customer_id)
  113. assert_equal(customer.id, ticket.created_by_id)
  114. assert_equal('+491710000000', article.from)
  115. assert_equal('+4915700000000', article.to)
  116. assert_nil(article.cc)
  117. assert_nil(article.subject)
  118. assert_equal('new 2', article.body)
  119. assert_equal(customer.id, article.created_by_id)
  120. assert_equal('Customer', article.sender.name)
  121. assert_equal('sms', article.type.name)
  122. # reply by agent
  123. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  124. params = {
  125. ticket_id: ticket.id,
  126. body: 'some test',
  127. type: 'sms',
  128. }
  129. post '/api/v1/ticket_articles', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  130. assert_response(201)
  131. result = JSON.parse(@response.body)
  132. assert_equal(Hash, result.class)
  133. assert_nil(result['subject'])
  134. assert_equal('some test', result['body'])
  135. assert_equal('text/plain', result['content_type'])
  136. assert_equal(agent.id, result['updated_by_id'])
  137. assert_equal(agent.id, result['created_by_id'])
  138. stub_request(:post, 'https://api.twilio.com/2010-04-01/Accounts/111/Messages.json')
  139. .with(
  140. body: {
  141. 'Body' => 'some test',
  142. 'From' => '333',
  143. 'To' => nil,
  144. },
  145. headers: {
  146. 'Accept' => 'application/json',
  147. 'Accept-Charset' => 'utf-8',
  148. 'Authorization' => 'Basic MTExOjIyMw==',
  149. 'Content-Type' => 'application/x-www-form-urlencoded',
  150. }
  151. ).to_return(status: 200, body: '', headers: {})
  152. assert_nil(article.preferences[:delivery_retry])
  153. assert_nil(article.preferences[:delivery_status])
  154. Observer::Transaction.commit
  155. Scheduler.worker(true)
  156. article = Ticket::Article.find(result['id'])
  157. assert_equal(1, article.preferences[:delivery_retry])
  158. assert_equal('success', article.preferences[:delivery_status])
  159. end
  160. test 'customer based on already existing mobile attibute' do
  161. customer = User.create!(
  162. firstname: '',
  163. lastname: '',
  164. email: 'me@example.com',
  165. mobile: '01710000000',
  166. note: '',
  167. updated_by_id: 1,
  168. created_by_id: 1,
  169. )
  170. Observer::Transaction.commit
  171. Scheduler.worker(true)
  172. # configure twilio channel
  173. bot_id = 123_456_789
  174. group_id = Group.find_by(name: 'Users').id
  175. UserInfo.current_user_id = 1
  176. channel = Channel.create!(
  177. area: 'Sms::Account',
  178. options: {
  179. adapter: 'sms/twilio',
  180. webhook_token: 'f409460e50f76d331fdac8ba7b7963b6',
  181. account_id: '111',
  182. token: '223',
  183. sender: '333',
  184. },
  185. group_id: group_id,
  186. active: true,
  187. )
  188. post '/api/v1/sms_webhook/f409460e50f76d331fdac8ba7b7963b6', params: read_messaage('inbound_sms1'), headers: @headers
  189. assert_response(200)
  190. response = REXML::Document.new(@response.body)
  191. assert_equal(response.elements.count, 1)
  192. assert_equal(User.last.id, customer.id)
  193. end
  194. def read_messaage(file)
  195. File.read(Rails.root.join('test', 'data', 'twilio', "#{file}.json"))
  196. end
  197. end