integration_sipgate_controller_test.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. require 'test_helper'
  2. require 'rexml/document'
  3. class IntegrationSipgateControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. Cti::Log.destroy_all
  6. Setting.set('sipgate_integration', true)
  7. Setting.set('sipgate_config', {
  8. outbound: {
  9. routing_table: [
  10. {
  11. dest: '41*',
  12. caller_id: '41715880339000',
  13. },
  14. {
  15. dest: '491714000000',
  16. caller_id: '41715880339000',
  17. },
  18. ],
  19. default_caller_id: '4930777000000',
  20. },
  21. inbound: {
  22. block_caller_ids: [
  23. {
  24. caller_id: '491715000000',
  25. note: 'some note',
  26. }
  27. ],
  28. notify_user_ids: {
  29. 2 => true,
  30. 4 => false,
  31. },
  32. }
  33. },)
  34. groups = Group.where(name: 'Users')
  35. roles = Role.where(name: %w[Agent])
  36. agent = User.create_or_update(
  37. login: 'cti-agent@example.com',
  38. firstname: 'E',
  39. lastname: 'S',
  40. email: 'cti-agent@example.com',
  41. password: 'agentpw',
  42. active: true,
  43. roles: roles,
  44. groups: groups,
  45. updated_by_id: 1,
  46. created_by_id: 1,
  47. )
  48. customer1 = User.create_or_update(
  49. login: 'ticket-caller_id_sipgate-customer1@example.com',
  50. firstname: 'CallerId',
  51. lastname: 'Customer1',
  52. email: 'ticket-caller_id_sipgate-customer1@example.com',
  53. password: 'customerpw',
  54. active: true,
  55. phone: '+49 99999 222222',
  56. fax: '+49 99999 222223',
  57. mobile: '+4912347114711',
  58. note: 'Phone at home: +49 99999 222224',
  59. updated_by_id: 1,
  60. created_by_id: 1,
  61. )
  62. customer2 = User.create_or_update(
  63. login: 'ticket-caller_id_sipgate-customer2@example.com',
  64. firstname: 'CallerId',
  65. lastname: 'Customer2',
  66. email: 'ticket-caller_id_sipgate-customer2@example.com',
  67. password: 'customerpw',
  68. active: true,
  69. phone: '+49 99999 222222 2',
  70. updated_by_id: 1,
  71. created_by_id: 1,
  72. )
  73. customer3 = User.create_or_update(
  74. login: 'ticket-caller_id_sipgate-customer3@example.com',
  75. firstname: 'CallerId',
  76. lastname: 'Customer3',
  77. email: 'ticket-caller_id_sipgate-customer3@example.com',
  78. password: 'customerpw',
  79. active: true,
  80. phone: '+49 99999 222222 2',
  81. updated_by_id: 1,
  82. created_by_id: 1,
  83. )
  84. Cti::CallerId.rebuild
  85. end
  86. test 'basic call' do
  87. # inbound - I
  88. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  89. post '/api/v1/sipgate/in', params: params
  90. assert_response(200)
  91. on_hangup = nil
  92. on_answer = nil
  93. content = @response.body
  94. response = REXML::Document.new(content)
  95. response.elements.each('Response') do |element|
  96. on_hangup = element.attributes['onHangup']
  97. on_answer = element.attributes['onAnswer']
  98. end
  99. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup)
  100. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer)
  101. # inbound - II - block caller
  102. params = 'event=newCall&direction=in&from=491715000000&to=4930600000000&callId=4991155921769858278-2&user%5B%5D=user+1&user%5B%5D=user+2'
  103. post '/api/v1/sipgate/in', params: params
  104. assert_response(200)
  105. on_hangup = nil
  106. on_answer = nil
  107. content = @response.body
  108. response = REXML::Document.new(content)
  109. response.elements.each('Response') do |element|
  110. on_hangup = element.attributes['onHangup']
  111. on_answer = element.attributes['onAnswer']
  112. end
  113. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup)
  114. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer)
  115. reason = nil
  116. response.elements.each('Response/Reject') do |element|
  117. reason = element.attributes['reason']
  118. end
  119. assert_equal('busy', reason)
  120. # outbound - I - set default_caller_id
  121. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=8621106404543334274-3&user%5B%5D=user+1'
  122. post '/api/v1/sipgate/out', params: params
  123. assert_response(200)
  124. on_hangup = nil
  125. on_answer = nil
  126. caller_id = nil
  127. number_to_dail = nil
  128. content = @response.body
  129. response = REXML::Document.new(content)
  130. response.elements.each('Response') do |element|
  131. on_hangup = element.attributes['onHangup']
  132. on_answer = element.attributes['onAnswer']
  133. end
  134. response.elements.each('Response/Dial') do |element|
  135. caller_id = element.attributes['callerId']
  136. end
  137. response.elements.each('Response/Dial/Number') do |element|
  138. number_to_dail = element.text
  139. end
  140. assert_equal('4930777000000', caller_id)
  141. assert_equal('4912347114711', number_to_dail)
  142. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  143. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  144. # outbound - II - set caller_id based on routing_table by explicite number
  145. params = 'event=newCall&direction=out&from=4930600000000&to=491714000000&callId=8621106404543334274-4&user%5B%5D=user+1'
  146. post '/api/v1/sipgate/out', params: params
  147. assert_response(200)
  148. on_hangup = nil
  149. on_answer = nil
  150. caller_id = nil
  151. number_to_dail = nil
  152. content = @response.body
  153. response = REXML::Document.new(content)
  154. response.elements.each('Response') do |element|
  155. on_hangup = element.attributes['onHangup']
  156. on_answer = element.attributes['onAnswer']
  157. end
  158. response.elements.each('Response/Dial') do |element|
  159. caller_id = element.attributes['callerId']
  160. end
  161. response.elements.each('Response/Dial/Number') do |element|
  162. number_to_dail = element.text
  163. end
  164. assert_equal('41715880339000', caller_id)
  165. assert_equal('491714000000', number_to_dail)
  166. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  167. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  168. # outbound - III - set caller_id based on routing_table by 41*
  169. params = 'event=newCall&direction=out&from=4930600000000&to=4147110000000&callId=8621106404543334274-5&user%5B%5D=user+1'
  170. post '/api/v1/sipgate/out', params: params
  171. assert_response(200)
  172. on_hangup = nil
  173. on_answer = nil
  174. caller_id = nil
  175. number_to_dail = nil
  176. content = @response.body
  177. response = REXML::Document.new(content)
  178. response.elements.each('Response') do |element|
  179. on_hangup = element.attributes['onHangup']
  180. on_answer = element.attributes['onAnswer']
  181. end
  182. response.elements.each('Response/Dial') do |element|
  183. caller_id = element.attributes['callerId']
  184. end
  185. response.elements.each('Response/Dial/Number') do |element|
  186. number_to_dail = element.text
  187. end
  188. assert_equal('41715880339000', caller_id)
  189. assert_equal('4147110000000', number_to_dail)
  190. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  191. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  192. # no config
  193. Setting.set('sipgate_config', {})
  194. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-6&user%5B%5D=user+1&user%5B%5D=user+2'
  195. post '/api/v1/sipgate/in', params: params
  196. assert_response(422)
  197. error = nil
  198. content = @response.body
  199. response = REXML::Document.new(content)
  200. response.elements.each('Response/Error') do |element|
  201. error = element.text
  202. end
  203. assert_equal('Feature not configured, please contact your admin!', error)
  204. end
  205. test 'log call' do
  206. # outbound - I - new call
  207. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-1&user%5B%5D=user+1'
  208. post '/api/v1/sipgate/out', params: params
  209. assert_response(200)
  210. log = Cti::Log.find_by(call_id: '1234567890-1')
  211. assert(log)
  212. assert_equal('4930777000000', log.from)
  213. assert_equal('4912347114711', log.to)
  214. assert_equal('out', log.direction)
  215. assert_equal('user 1', log.from_comment)
  216. assert_equal('CallerId Customer1', log.to_comment)
  217. assert_nil(log.comment)
  218. assert_equal('newCall', log.state)
  219. assert_equal(true, log.done)
  220. # outbound - I - hangup by agent
  221. params = 'event=hangup&direction=out&callId=1234567890-1&cause=cancel'
  222. post '/api/v1/sipgate/out', params: params
  223. assert_response(200)
  224. log = Cti::Log.find_by(call_id: '1234567890-1')
  225. assert(log)
  226. assert_equal('4930777000000', log.from)
  227. assert_equal('4912347114711', log.to)
  228. assert_equal('out', log.direction)
  229. assert_equal('user 1', log.from_comment)
  230. assert_equal('CallerId Customer1', log.to_comment)
  231. assert_equal('cancel', log.comment)
  232. assert_equal('hangup', log.state)
  233. assert_equal(true, log.done)
  234. # outbound - II - new call
  235. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-2&user%5B%5D=user+1'
  236. post '/api/v1/sipgate/out', params: params
  237. assert_response(200)
  238. log = Cti::Log.find_by(call_id: '1234567890-2')
  239. assert(log)
  240. assert_equal('4930777000000', log.from)
  241. assert_equal('4912347114711', log.to)
  242. assert_equal('out', log.direction)
  243. assert_equal('user 1', log.from_comment)
  244. assert_equal('CallerId Customer1', log.to_comment)
  245. assert_nil(log.comment)
  246. assert_equal('newCall', log.state)
  247. assert_equal(true, log.done)
  248. # outbound - II - answer by customer
  249. params = 'event=answer&direction=out&callId=1234567890-2&from=4930600000000&to=4912347114711'
  250. post '/api/v1/sipgate/out', params: params
  251. assert_response(200)
  252. log = Cti::Log.find_by(call_id: '1234567890-2')
  253. assert(log)
  254. assert_equal('4930777000000', log.from)
  255. assert_equal('4912347114711', log.to)
  256. assert_equal('out', log.direction)
  257. assert_equal('user 1', log.from_comment)
  258. assert_equal('CallerId Customer1', log.to_comment)
  259. assert_nil(log.comment)
  260. assert_equal('answer', log.state)
  261. assert_equal(true, log.done)
  262. # outbound - II - hangup by customer
  263. params = 'event=hangup&direction=out&callId=1234567890-2&cause=normalClearing&from=4930600000000&to=4912347114711'
  264. post '/api/v1/sipgate/out', params: params
  265. assert_response(200)
  266. log = Cti::Log.find_by(call_id: '1234567890-2')
  267. assert(log)
  268. assert_equal('4930777000000', log.from)
  269. assert_equal('4912347114711', log.to)
  270. assert_equal('out', log.direction)
  271. assert_equal('user 1', log.from_comment)
  272. assert_equal('CallerId Customer1', log.to_comment)
  273. assert_equal('normalClearing', log.comment)
  274. assert_equal('hangup', log.state)
  275. assert_equal(true, log.done)
  276. # inbound - I - new call
  277. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-3&user%5B%5D=user+1'
  278. post '/api/v1/sipgate/in', params: params
  279. assert_response(200)
  280. log = Cti::Log.find_by(call_id: '1234567890-3')
  281. assert(log)
  282. assert_equal('4930600000000', log.to)
  283. assert_equal('4912347114711', log.from)
  284. assert_equal('in', log.direction)
  285. assert_equal('user 1', log.to_comment)
  286. assert_equal('CallerId Customer1', log.from_comment)
  287. assert_nil(log.comment)
  288. assert_equal('newCall', log.state)
  289. assert_equal(false, log.done)
  290. # inbound - I - answer by customer
  291. params = 'event=answer&direction=in&callId=1234567890-3&to=4930600000000&from=4912347114711'
  292. post '/api/v1/sipgate/in', params: params
  293. assert_response(200)
  294. log = Cti::Log.find_by(call_id: '1234567890-3')
  295. assert(log)
  296. assert_equal('4930600000000', log.to)
  297. assert_equal('4912347114711', log.from)
  298. assert_equal('in', log.direction)
  299. assert_equal('user 1', log.to_comment)
  300. assert_equal('CallerId Customer1', log.from_comment)
  301. assert_nil(log.comment)
  302. assert_equal('answer', log.state)
  303. assert_equal(true, log.done)
  304. # inbound - I - hangup by customer
  305. params = 'event=hangup&direction=in&callId=1234567890-3&cause=normalClearing&to=4930600000000&from=4912347114711'
  306. post '/api/v1/sipgate/in', params: params
  307. assert_response(200)
  308. log = Cti::Log.find_by(call_id: '1234567890-3')
  309. assert(log)
  310. assert_equal('4930600000000', log.to)
  311. assert_equal('4912347114711', log.from)
  312. assert_equal('in', log.direction)
  313. assert_equal('user 1', log.to_comment)
  314. assert_equal('CallerId Customer1', log.from_comment)
  315. assert_equal('normalClearing', log.comment)
  316. assert_equal('hangup', log.state)
  317. assert_equal(true, log.done)
  318. # inbound - II - new call
  319. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-4&user%5B%5D=user+1,user+2'
  320. post '/api/v1/sipgate/in', params: params
  321. assert_response(200)
  322. log = Cti::Log.find_by(call_id: '1234567890-4')
  323. assert(log)
  324. assert_equal('4930600000000', log.to)
  325. assert_equal('4912347114711', log.from)
  326. assert_equal('in', log.direction)
  327. assert_equal('user 1,user 2', log.to_comment)
  328. assert_equal('CallerId Customer1', log.from_comment)
  329. assert_nil(log.comment)
  330. assert_equal('newCall', log.state)
  331. assert_equal(false, log.done)
  332. # inbound - II - answer by voicemail
  333. params = 'event=answer&direction=in&callId=1234567890-4&to=4930600000000&from=4912347114711&user=voicemail'
  334. post '/api/v1/sipgate/in', params: params
  335. assert_response(200)
  336. log = Cti::Log.find_by(call_id: '1234567890-4')
  337. assert(log)
  338. assert_equal('4930600000000', log.to)
  339. assert_equal('4912347114711', log.from)
  340. assert_equal('in', log.direction)
  341. assert_equal('voicemail', log.to_comment)
  342. assert_equal('CallerId Customer1', log.from_comment)
  343. assert_nil(log.comment)
  344. assert_equal('answer', log.state)
  345. assert_equal(true, log.done)
  346. # inbound - II - hangup by customer
  347. params = 'event=hangup&direction=in&callId=1234567890-4&cause=normalClearing&to=4930600000000&from=4912347114711'
  348. post '/api/v1/sipgate/in', params: params
  349. assert_response(200)
  350. log = Cti::Log.find_by(call_id: '1234567890-4')
  351. assert(log)
  352. assert_equal('4930600000000', log.to)
  353. assert_equal('4912347114711', log.from)
  354. assert_equal('in', log.direction)
  355. assert_equal('voicemail', log.to_comment)
  356. assert_equal('CallerId Customer1', log.from_comment)
  357. assert_equal('normalClearing', log.comment)
  358. assert_equal('hangup', log.state)
  359. assert_equal(false, log.done)
  360. # inbound - III - new call
  361. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-5&user%5B%5D=user+1,user+2'
  362. post '/api/v1/sipgate/in', params: params
  363. assert_response(200)
  364. log = Cti::Log.find_by(call_id: '1234567890-5')
  365. assert(log)
  366. assert_equal('4930600000000', log.to)
  367. assert_equal('4912347114711', log.from)
  368. assert_equal('in', log.direction)
  369. assert_equal('user 1,user 2', log.to_comment)
  370. assert_equal('CallerId Customer1', log.from_comment)
  371. assert_nil(log.comment)
  372. assert_equal('newCall', log.state)
  373. assert_equal(false, log.done)
  374. # inbound - III - hangup by customer
  375. params = 'event=hangup&direction=in&callId=1234567890-5&cause=normalClearing&to=4930600000000&from=4912347114711'
  376. post '/api/v1/sipgate/in', params: params
  377. assert_response(200)
  378. log = Cti::Log.find_by(call_id: '1234567890-5')
  379. assert(log)
  380. assert_equal('4930600000000', log.to)
  381. assert_equal('4912347114711', log.from)
  382. assert_equal('in', log.direction)
  383. assert_equal('user 1,user 2', log.to_comment)
  384. assert_equal('CallerId Customer1', log.from_comment)
  385. assert_equal('normalClearing', log.comment)
  386. assert_equal('hangup', log.state)
  387. assert_equal(false, log.done)
  388. # inbound - IV - new call
  389. params = 'event=newCall&direction=in&to=4930600000000&from=49999992222222&callId=1234567890-6&user%5B%5D=user+1,user+2'
  390. post '/api/v1/sipgate/in', params: params
  391. assert_response(200)
  392. log = Cti::Log.find_by(call_id: '1234567890-6')
  393. assert(log)
  394. assert_equal('4930600000000', log.to)
  395. assert_equal('49999992222222', log.from)
  396. assert_equal('in', log.direction)
  397. assert_equal('user 1,user 2', log.to_comment)
  398. assert_equal('CallerId Customer3,CallerId Customer2', log.from_comment)
  399. assert_not(log.preferences['to'])
  400. assert(log.preferences['from'])
  401. assert_nil(log.comment)
  402. assert_equal('newCall', log.state)
  403. assert_equal(false, log.done)
  404. # get caller list
  405. get '/api/v1/cti/log'
  406. assert_response(401)
  407. customer2 = User.lookup(login: 'ticket-caller_id_sipgate-customer2@example.com')
  408. customer3 = User.lookup(login: 'ticket-caller_id_sipgate-customer3@example.com')
  409. headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  410. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('cti-agent@example.com', 'agentpw')
  411. get '/api/v1/cti/log', headers: headers.merge('Authorization' => credentials)
  412. assert_response(200)
  413. result = JSON.parse(@response.body)
  414. assert_equal(result['list'].class, Array)
  415. assert_equal(6, result['list'].count)
  416. assert(result['assets'])
  417. assert(result['assets']['User'])
  418. assert(result['assets']['User'][customer2.id.to_s])
  419. assert(result['assets']['User'][customer3.id.to_s])
  420. assert_equal('1234567890-6', result['list'][0]['call_id'])
  421. assert_equal('1234567890-5', result['list'][1]['call_id'])
  422. assert_equal('1234567890-4', result['list'][2]['call_id'])
  423. assert_equal('1234567890-3', result['list'][3]['call_id'])
  424. assert_equal('1234567890-2', result['list'][4]['call_id'])
  425. assert_equal('hangup', result['list'][4]['state'])
  426. assert_equal('4930777000000', result['list'][4]['from'])
  427. assert_equal('user 1', result['list'][4]['from_comment'])
  428. assert_equal('4912347114711', result['list'][4]['to'])
  429. assert_equal('CallerId Customer1', result['list'][4]['to_comment'])
  430. assert_equal('normalClearing', result['list'][4]['comment'])
  431. assert_equal('hangup', result['list'][4]['state'])
  432. assert_equal('1234567890-1', result['list'][5]['call_id'])
  433. end
  434. end