integration_cti_controller_test.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. require 'test_helper'
  2. require 'rexml/document'
  3. class IntegrationCtiControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. Cti::Log.destroy_all
  6. Setting.set('cti_integration', true)
  7. Setting.set('cti_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_cti-customer1@example.com',
  50. firstname: 'CallerId',
  51. lastname: 'Customer1',
  52. email: 'ticket-caller_id_cti-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_cti-customer2@example.com',
  64. firstname: 'CallerId',
  65. lastname: 'Customer2',
  66. email: 'ticket-caller_id_cti-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_cti-customer3@example.com',
  75. firstname: 'CallerId',
  76. lastname: 'Customer3',
  77. email: 'ticket-caller_id_cti-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 'token check' do
  87. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&call_id=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  88. post '/api/v1/cti/not_existing_token', params: params
  89. assert_response(401)
  90. result = JSON.parse(@response.body)
  91. assert_equal(Hash, result.class)
  92. assert_equal('Invalid token, please contact your admin!', result['error'])
  93. end
  94. test 'basic call' do
  95. token = Setting.get('cti_token')
  96. # inbound - I
  97. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&call_id=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  98. post "/api/v1/cti/#{token}", params: params
  99. assert_response(200)
  100. result = JSON.parse(@response.body)
  101. assert_equal(Hash, result.class)
  102. assert(result.blank?)
  103. # inbound - II - block caller
  104. params = 'event=newCall&direction=in&from=491715000000&to=4930600000000&call_id=4991155921769858278-2&user%5B%5D=user+1&user%5B%5D=user+2'
  105. post "/api/v1/cti/#{token}", params: params
  106. assert_response(200)
  107. result = JSON.parse(@response.body)
  108. assert_equal(Hash, result.class)
  109. assert_equal('reject', result['action'])
  110. assert_equal('busy', result['reason'])
  111. # outbound - I - set default_caller_id
  112. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&call_id=8621106404543334274-3&user%5B%5D=user+1'
  113. post "/api/v1/cti/#{token}", params: params
  114. assert_response(200)
  115. result = JSON.parse(@response.body)
  116. assert_equal(Hash, result.class)
  117. assert_equal('dial', result['action'])
  118. assert_equal('4912347114711', result['number'])
  119. assert_equal('4930777000000', result['caller_id'])
  120. # outbound - II - set caller_id based on routing_table by explicite number
  121. params = 'event=newCall&direction=out&from=4930600000000&to=491714000000&call_id=8621106404543334274-4&user%5B%5D=user+1'
  122. post "/api/v1/cti/#{token}", params: params
  123. assert_response(200)
  124. result = JSON.parse(@response.body)
  125. assert_equal(Hash, result.class)
  126. assert_equal('dial', result['action'])
  127. assert_equal('491714000000', result['number'])
  128. assert_equal('41715880339000', result['caller_id'])
  129. # outbound - III - set caller_id based on routing_table by 41*
  130. params = 'event=newCall&direction=out&from=4930600000000&to=4147110000000&call_id=8621106404543334274-5&user%5B%5D=user+1'
  131. post "/api/v1/cti/#{token}", params: params
  132. assert_response(200)
  133. result = JSON.parse(@response.body)
  134. assert_equal(Hash, result.class)
  135. assert_equal('dial', result['action'])
  136. assert_equal('4147110000000', result['number'])
  137. assert_equal('41715880339000', result['caller_id'])
  138. # no config
  139. Setting.set('cti_config', {})
  140. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&call_id=4991155921769858278-6&user%5B%5D=user+1&user%5B%5D=user+2'
  141. post "/api/v1/cti/#{token}", params: params
  142. assert_response(422)
  143. result = JSON.parse(@response.body)
  144. assert_equal(Hash, result.class)
  145. assert_equal('Feature not configured, please contact your admin!', result['error'])
  146. end
  147. test 'log call' do
  148. token = Setting.get('cti_token')
  149. # outbound - I - new call
  150. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&call_id=1234567890-1&user%5B%5D=user+1'
  151. post "/api/v1/cti/#{token}", params: params
  152. assert_response(200)
  153. log = Cti::Log.find_by(call_id: '1234567890-1')
  154. assert(log)
  155. assert_equal('4930777000000', log.from)
  156. assert_equal('4912347114711', log.to)
  157. assert_equal('out', log.direction)
  158. assert_equal('user 1', log.from_comment)
  159. assert_equal('CallerId Customer1', log.to_comment)
  160. assert_nil(log.comment)
  161. assert_equal('newCall', log.state)
  162. assert_equal(true, log.done)
  163. assert(log.initialized_at)
  164. assert_nil(log.start_at)
  165. assert_nil(log.end_at)
  166. assert_nil(log.duration_waiting_time)
  167. assert_nil(log.duration_talking_time)
  168. # outbound - I - hangup by agent
  169. params = 'event=hangup&direction=out&call_id=1234567890-1&cause=cancel'
  170. post "/api/v1/cti/#{token}", params: params
  171. assert_response(200)
  172. log = Cti::Log.find_by(call_id: '1234567890-1')
  173. assert(log)
  174. assert_equal('4930777000000', log.from)
  175. assert_equal('4912347114711', log.to)
  176. assert_equal('out', log.direction)
  177. assert_equal('user 1', log.from_comment)
  178. assert_equal('CallerId Customer1', log.to_comment)
  179. assert_equal('cancel', log.comment)
  180. assert_equal('hangup', log.state)
  181. assert_equal(true, log.done)
  182. assert(log.initialized_at)
  183. assert_nil(log.start_at)
  184. assert(log.end_at)
  185. assert(log.duration_waiting_time)
  186. assert_nil(log.duration_talking_time)
  187. # outbound - II - new call
  188. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&call_id=1234567890-2&user%5B%5D=user+1'
  189. post "/api/v1/cti/#{token}", params: params
  190. assert_response(200)
  191. log = Cti::Log.find_by(call_id: '1234567890-2')
  192. assert(log)
  193. assert_equal('4930777000000', log.from)
  194. assert_equal('4912347114711', log.to)
  195. assert_equal('out', log.direction)
  196. assert_equal('user 1', log.from_comment)
  197. assert_equal('CallerId Customer1', log.to_comment)
  198. assert_nil(log.comment)
  199. assert_equal('newCall', log.state)
  200. assert_equal(true, log.done)
  201. assert(log.initialized_at)
  202. assert_nil(log.start_at)
  203. assert_nil(log.end_at)
  204. assert_nil(log.duration_waiting_time)
  205. assert_nil(log.duration_talking_time)
  206. # outbound - II - answer by customer
  207. params = 'event=answer&direction=out&call_id=1234567890-2&from=4930600000000&to=4912347114711'
  208. post "/api/v1/cti/#{token}", params: params
  209. assert_response(200)
  210. log = Cti::Log.find_by(call_id: '1234567890-2')
  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('answer', log.state)
  219. assert_equal(true, log.done)
  220. assert(log.initialized_at)
  221. assert(log.start_at)
  222. assert_nil(log.end_at)
  223. assert(log.duration_waiting_time)
  224. assert_nil(log.duration_talking_time)
  225. # outbound - II - hangup by customer
  226. params = 'event=hangup&direction=out&call_id=1234567890-2&cause=normalClearing&from=4930600000000&to=4912347114711'
  227. post "/api/v1/cti/#{token}", params: params
  228. assert_response(200)
  229. log = Cti::Log.find_by(call_id: '1234567890-2')
  230. assert(log)
  231. assert_equal('4930777000000', log.from)
  232. assert_equal('4912347114711', log.to)
  233. assert_equal('out', log.direction)
  234. assert_equal('user 1', log.from_comment)
  235. assert_equal('CallerId Customer1', log.to_comment)
  236. assert_equal('normalClearing', log.comment)
  237. assert_equal('hangup', log.state)
  238. assert_equal(true, log.done)
  239. assert(log.initialized_at)
  240. assert(log.start_at)
  241. assert(log.end_at)
  242. assert(log.duration_waiting_time)
  243. assert(log.duration_talking_time)
  244. # inbound - I - new call
  245. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&call_id=1234567890-3&user%5B%5D=user+1'
  246. post "/api/v1/cti/#{token}", params: params
  247. assert_response(200)
  248. log = Cti::Log.find_by(call_id: '1234567890-3')
  249. assert(log)
  250. assert_equal('4930600000000', log.to)
  251. assert_equal('4912347114711', log.from)
  252. assert_equal('in', log.direction)
  253. assert_equal('user 1', log.to_comment)
  254. assert_equal('CallerId Customer1', log.from_comment)
  255. assert_nil(log.comment)
  256. assert_equal('newCall', log.state)
  257. assert_equal(false, log.done)
  258. assert(log.initialized_at)
  259. assert_nil(log.start_at)
  260. assert_nil(log.end_at)
  261. assert_nil(log.duration_waiting_time)
  262. assert_nil(log.duration_talking_time)
  263. # inbound - I - answer by customer
  264. params = 'event=answer&direction=in&call_id=1234567890-3&to=4930600000000&from=4912347114711'
  265. post "/api/v1/cti/#{token}", params: params
  266. assert_response(200)
  267. log = Cti::Log.find_by(call_id: '1234567890-3')
  268. assert(log)
  269. assert_equal('4930600000000', log.to)
  270. assert_equal('4912347114711', log.from)
  271. assert_equal('in', log.direction)
  272. assert_equal('user 1', log.to_comment)
  273. assert_equal('CallerId Customer1', log.from_comment)
  274. assert_nil(log.comment)
  275. assert_equal('answer', log.state)
  276. assert_equal(true, log.done)
  277. assert(log.initialized_at)
  278. assert(log.start_at)
  279. assert_nil(log.end_at)
  280. assert(log.duration_waiting_time)
  281. assert_nil(log.duration_talking_time)
  282. # inbound - I - hangup by customer
  283. params = 'event=hangup&direction=in&call_id=1234567890-3&cause=normalClearing&to=4930600000000&from=4912347114711'
  284. post "/api/v1/cti/#{token}", params: params
  285. assert_response(200)
  286. log = Cti::Log.find_by(call_id: '1234567890-3')
  287. assert(log)
  288. assert_equal('4930600000000', log.to)
  289. assert_equal('4912347114711', log.from)
  290. assert_equal('in', log.direction)
  291. assert_equal('user 1', log.to_comment)
  292. assert_equal('CallerId Customer1', log.from_comment)
  293. assert_equal('normalClearing', log.comment)
  294. assert_equal('hangup', log.state)
  295. assert_equal(true, log.done)
  296. assert(log.initialized_at)
  297. assert(log.start_at)
  298. assert(log.end_at)
  299. assert(log.duration_waiting_time)
  300. assert(log.duration_talking_time)
  301. # inbound - II - new call
  302. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&call_id=1234567890-4&user%5B%5D=user+1,user+2'
  303. post "/api/v1/cti/#{token}", params: params
  304. assert_response(200)
  305. log = Cti::Log.find_by(call_id: '1234567890-4')
  306. assert(log)
  307. assert_equal('4930600000000', log.to)
  308. assert_equal('4912347114711', log.from)
  309. assert_equal('in', log.direction)
  310. assert_equal('user 1,user 2', log.to_comment)
  311. assert_equal('CallerId Customer1', log.from_comment)
  312. assert_nil(log.comment)
  313. assert_equal('newCall', log.state)
  314. assert_equal(false, log.done)
  315. assert(log.initialized_at)
  316. assert_nil(log.start_at)
  317. assert_nil(log.end_at)
  318. assert_nil(log.duration_waiting_time)
  319. assert_nil(log.duration_talking_time)
  320. # inbound - II - answer by voicemail
  321. params = 'event=answer&direction=in&call_id=1234567890-4&to=4930600000000&from=4912347114711&user=voicemail'
  322. post "/api/v1/cti/#{token}", params: params
  323. assert_response(200)
  324. log = Cti::Log.find_by(call_id: '1234567890-4')
  325. assert(log)
  326. assert_equal('4930600000000', log.to)
  327. assert_equal('4912347114711', log.from)
  328. assert_equal('in', log.direction)
  329. assert_equal('voicemail', log.to_comment)
  330. assert_equal('CallerId Customer1', log.from_comment)
  331. assert_nil(log.comment)
  332. assert_equal('answer', log.state)
  333. assert_equal(true, log.done)
  334. assert(log.initialized_at)
  335. assert(log.start_at)
  336. assert_nil(log.end_at)
  337. assert(log.duration_waiting_time)
  338. assert_nil(log.duration_talking_time)
  339. # inbound - II - hangup by customer
  340. params = 'event=hangup&direction=in&call_id=1234567890-4&cause=normalClearing&to=4930600000000&from=4912347114711'
  341. post "/api/v1/cti/#{token}", params: params
  342. assert_response(200)
  343. log = Cti::Log.find_by(call_id: '1234567890-4')
  344. assert(log)
  345. assert_equal('4930600000000', log.to)
  346. assert_equal('4912347114711', log.from)
  347. assert_equal('in', log.direction)
  348. assert_equal('voicemail', log.to_comment)
  349. assert_equal('CallerId Customer1', log.from_comment)
  350. assert_equal('normalClearing', log.comment)
  351. assert_equal('hangup', log.state)
  352. assert_equal(false, log.done)
  353. assert(log.initialized_at)
  354. assert(log.start_at)
  355. assert(log.end_at)
  356. assert(log.duration_waiting_time)
  357. assert(log.duration_talking_time)
  358. # inbound - III - new call
  359. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&call_id=1234567890-5&user%5B%5D=user+1,user+2'
  360. post "/api/v1/cti/#{token}", params: params
  361. assert_response(200)
  362. log = Cti::Log.find_by(call_id: '1234567890-5')
  363. assert(log)
  364. assert_equal('4930600000000', log.to)
  365. assert_equal('4912347114711', log.from)
  366. assert_equal('in', log.direction)
  367. assert_equal('user 1,user 2', log.to_comment)
  368. assert_equal('CallerId Customer1', log.from_comment)
  369. assert_nil(log.comment)
  370. assert_equal('newCall', log.state)
  371. assert_equal(false, log.done)
  372. assert(log.initialized_at)
  373. assert_nil(log.start_at)
  374. assert_nil(log.end_at)
  375. assert_nil(log.duration_waiting_time)
  376. assert_nil(log.duration_talking_time)
  377. # inbound - III - hangup by customer
  378. params = 'event=hangup&direction=in&call_id=1234567890-5&cause=normalClearing&to=4930600000000&from=4912347114711'
  379. post "/api/v1/cti/#{token}", params: params
  380. assert_response(200)
  381. log = Cti::Log.find_by(call_id: '1234567890-5')
  382. assert(log)
  383. assert_equal('4930600000000', log.to)
  384. assert_equal('4912347114711', log.from)
  385. assert_equal('in', log.direction)
  386. assert_equal('user 1,user 2', log.to_comment)
  387. assert_equal('CallerId Customer1', log.from_comment)
  388. assert_equal('normalClearing', log.comment)
  389. assert_equal('hangup', log.state)
  390. assert_equal(false, log.done)
  391. assert(log.initialized_at)
  392. assert_nil(log.start_at)
  393. assert(log.end_at)
  394. assert(log.duration_waiting_time)
  395. assert_nil(log.duration_talking_time)
  396. # inbound - IV - new call
  397. params = 'event=newCall&direction=in&to=4930600000000&from=49999992222222&call_id=1234567890-6&user%5B%5D=user+1,user+2'
  398. post "/api/v1/cti/#{token}", params: params
  399. assert_response(200)
  400. log = Cti::Log.find_by(call_id: '1234567890-6')
  401. assert(log)
  402. assert_equal('4930600000000', log.to)
  403. assert_equal('49999992222222', log.from)
  404. assert_equal('in', log.direction)
  405. assert_equal('user 1,user 2', log.to_comment)
  406. assert_equal('CallerId Customer3,CallerId Customer2', log.from_comment)
  407. assert_not(log.preferences['to'])
  408. assert(log.preferences['from'])
  409. assert_nil(log.comment)
  410. assert_equal('newCall', log.state)
  411. assert_equal(false, log.done)
  412. assert(log.initialized_at)
  413. assert_nil(log.start_at)
  414. assert_nil(log.end_at)
  415. assert_nil(log.duration_waiting_time)
  416. assert_nil(log.duration_talking_time)
  417. # inbound - IV - new call
  418. params = 'event=newCall&direction=in&to=4930600000000&from=anonymous&call_id=1234567890-7&user%5B%5D=user+1,user+2'
  419. post "/api/v1/cti/#{token}", params: params
  420. assert_response(200)
  421. log = Cti::Log.find_by(call_id: '1234567890-7')
  422. assert(log)
  423. assert_equal('4930600000000', log.to)
  424. assert_equal('anonymous', log.from)
  425. assert_equal('in', log.direction)
  426. assert_equal('user 1,user 2', log.to_comment)
  427. assert_nil(log.from_comment)
  428. assert_not(log.preferences['to'])
  429. assert_not(log.preferences['from'])
  430. assert_nil(log.comment)
  431. assert_equal('newCall', log.state)
  432. assert_equal(false, log.done)
  433. assert(log.initialized_at)
  434. assert_nil(log.start_at)
  435. assert_nil(log.end_at)
  436. assert_nil(log.duration_waiting_time)
  437. assert_nil(log.duration_talking_time)
  438. # get caller list
  439. get '/api/v1/cti/log'
  440. assert_response(401)
  441. customer2 = User.lookup(login: 'ticket-caller_id_cti-customer2@example.com')
  442. customer3 = User.lookup(login: 'ticket-caller_id_cti-customer3@example.com')
  443. headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  444. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('cti-agent@example.com', 'agentpw')
  445. get '/api/v1/cti/log', headers: headers.merge('Authorization' => credentials)
  446. assert_response(200)
  447. result = JSON.parse(@response.body)
  448. assert_equal(result['list'].class, Array)
  449. assert_equal(7, result['list'].count)
  450. assert(result['assets'])
  451. assert(result['assets']['User'])
  452. assert(result['assets']['User'][customer2.id.to_s])
  453. assert(result['assets']['User'][customer3.id.to_s])
  454. assert_equal('1234567890-7', result['list'][0]['call_id'])
  455. assert_equal('1234567890-6', result['list'][1]['call_id'])
  456. assert_equal('1234567890-5', result['list'][2]['call_id'])
  457. assert_equal('1234567890-4', result['list'][3]['call_id'])
  458. assert_equal('1234567890-3', result['list'][4]['call_id'])
  459. assert_equal('1234567890-2', result['list'][5]['call_id'])
  460. assert_equal('hangup', result['list'][5]['state'])
  461. assert_equal('4930777000000', result['list'][5]['from'])
  462. assert_equal('user 1', result['list'][5]['from_comment'])
  463. assert_equal('4912347114711', result['list'][5]['to'])
  464. assert_equal('CallerId Customer1', result['list'][5]['to_comment'])
  465. assert_equal('normalClearing', result['list'][5]['comment'])
  466. assert_equal('hangup', result['list'][5]['state'])
  467. assert_equal('1234567890-1', result['list'][6]['call_id'])
  468. end
  469. end