cti_spec.rb 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Integration CTI', type: :request do
  4. let(:agent) do
  5. create(:agent)
  6. end
  7. let!(:customer1) do
  8. create(
  9. :customer,
  10. login: 'ticket-caller_id_cti-customer1@example.com',
  11. firstname: 'CallerId',
  12. lastname: 'Customer1',
  13. phone: '+49 99999 222222',
  14. fax: '+49 99999 222223',
  15. mobile: '+4912347114711',
  16. note: 'Phone at home: +49 99999 222224',
  17. )
  18. end
  19. let!(:customer2) do
  20. create(
  21. :customer,
  22. login: 'ticket-caller_id_cti-customer2@example.com',
  23. firstname: 'CallerId',
  24. lastname: 'Customer2',
  25. phone: '+49 99999 222222 2',
  26. )
  27. end
  28. let!(:customer3) do
  29. create(
  30. :customer,
  31. login: 'ticket-caller_id_cti-customer3@example.com',
  32. firstname: 'CallerId',
  33. lastname: 'Customer3',
  34. phone: '+49 99999 222222 2',
  35. )
  36. end
  37. before do
  38. Cti::Log.destroy_all
  39. Setting.set('cti_integration', true)
  40. Setting.set('cti_config', {
  41. outbound: {
  42. routing_table: [
  43. {
  44. dest: '41*',
  45. caller_id: '41715880339000',
  46. },
  47. {
  48. dest: '491714000000',
  49. caller_id: '41715880339000',
  50. },
  51. ],
  52. default_caller_id: '4930777000000',
  53. },
  54. inbound: {
  55. block_caller_ids: [
  56. {
  57. caller_id: '491715000000',
  58. note: 'some note',
  59. }
  60. ],
  61. }
  62. })
  63. Cti::CallerId.rebuild
  64. end
  65. describe 'request handling' do
  66. let!(:token) { Setting.get('cti_token') }
  67. it 'does token check' do
  68. post '/api/v1/cti/not_existing_token', params: {
  69. event: 'newCall',
  70. direction: 'in',
  71. from: '4912347114711',
  72. to: '4930600000000',
  73. call_id: '4991155921769858278-1',
  74. user: 'user 1',
  75. }
  76. expect(response).to have_http_status(:unauthorized)
  77. expect(json_response).to be_a(Hash)
  78. expect(json_response['error']).to eq('Invalid token, please contact your admin!')
  79. end
  80. it 'does basic call' do
  81. # inbound - I
  82. post "/api/v1/cti/#{token}", params: {
  83. event: 'newCall',
  84. direction: 'in',
  85. from: '4912347114711',
  86. to: '4930600000000',
  87. call_id: '4991155921769858278-1',
  88. user: ['user+1', 'user+2'],
  89. }
  90. expect(response).to have_http_status(:ok)
  91. expect(json_response).to be_a(Hash)
  92. expect(json_response).to be_blank
  93. # inbound - II - block caller
  94. post "/api/v1/cti/#{token}", params: {
  95. event: 'newCall',
  96. direction: 'in',
  97. from: '491715000000',
  98. to: '4930600000000',
  99. call_id: '4991155921769858278-2',
  100. user: ['user+1', 'user+2'],
  101. }
  102. expect(response).to have_http_status(:ok)
  103. expect(json_response).to be_a(Hash)
  104. expect(json_response['action']).to eq('reject')
  105. expect(json_response['reason']).to eq('busy')
  106. # outbound - I - set default_caller_id
  107. post "/api/v1/cti/#{token}", params: {
  108. event: 'newCall',
  109. direction: 'out',
  110. from: '4930600000000',
  111. to: '4912347114711',
  112. call_id: '8621106404543334274-3',
  113. user: 'user 1',
  114. }
  115. expect(response).to have_http_status(:ok)
  116. expect(json_response).to be_a(Hash)
  117. expect(json_response['action']).to eq('dial')
  118. expect(json_response['number']).to eq('4912347114711')
  119. expect(json_response['caller_id']).to eq('4930777000000')
  120. # outbound - II - set caller_id based on routing_table by explicite number
  121. post "/api/v1/cti/#{token}", params: {
  122. event: 'newCall',
  123. direction: 'out',
  124. from: '4930600000000',
  125. to: '491714000000',
  126. call_id: '8621106404543334274-4',
  127. user: 'user 1',
  128. }
  129. expect(response).to have_http_status(:ok)
  130. expect(json_response).to be_a(Hash)
  131. expect(json_response['action']).to eq('dial')
  132. expect(json_response['number']).to eq('491714000000')
  133. expect(json_response['caller_id']).to eq('41715880339000')
  134. # outbound - III - set caller_id based on routing_table by 41*
  135. post "/api/v1/cti/#{token}", params: {
  136. event: 'newCall',
  137. direction: 'out',
  138. from: '4930600000000',
  139. to: '4147110000000',
  140. call_id: '8621106404543334274-5',
  141. user: 'user 1',
  142. }
  143. expect(response).to have_http_status(:ok)
  144. expect(json_response).to be_a(Hash)
  145. expect(json_response['action']).to eq('dial')
  146. expect(json_response['number']).to eq('4147110000000')
  147. expect(json_response['caller_id']).to eq('41715880339000')
  148. # no config
  149. Setting.set('cti_config', {})
  150. post "/api/v1/cti/#{token}", params: {
  151. event: 'newCall',
  152. direction: 'in',
  153. from: '4912347114711',
  154. to: '4930600000000',
  155. call_id: '4991155921769858278-6',
  156. user: ['user+1', 'user+2'],
  157. }
  158. expect(response).to have_http_status(:unprocessable_entity)
  159. expect(json_response).to be_a(Hash)
  160. expect(json_response['error']).to eq('Feature not configured, please contact your admin!')
  161. end
  162. it 'does log call' do
  163. # outbound - I - new call
  164. post "/api/v1/cti/#{token}", params: {
  165. event: 'newCall',
  166. direction: 'out',
  167. from: '4930600000000',
  168. to: '4912347114711',
  169. call_id: '1234567890-1',
  170. user: 'user 1',
  171. }
  172. expect(response).to have_http_status(:ok)
  173. log = Cti::Log.find_by(call_id: '1234567890-1')
  174. expect(log).to be_truthy
  175. expect(log.from).to eq('4930777000000')
  176. expect(log.to).to eq('4912347114711')
  177. expect(log.direction).to eq('out')
  178. expect(log.from_comment).to eq('user 1')
  179. expect(log.to_comment).to eq('CallerId Customer1')
  180. expect(log.comment).to be_nil
  181. expect(log.queue).to eq('4930777000000')
  182. expect(log.state).to eq('newCall')
  183. expect(log.done).to be(true)
  184. expect(log.initialized_at).to be_truthy
  185. expect(log.start_at).to be_nil
  186. expect(log.end_at).to be_nil
  187. expect(log.duration_waiting_time).to be_nil
  188. expect(log.duration_talking_time).to be_nil
  189. travel 2.seconds
  190. # outbound - I - hangup by agent
  191. post "/api/v1/cti/#{token}", params: {
  192. event: 'hangup',
  193. direction: 'out',
  194. call_id: '1234567890-1',
  195. cause: 'cancel',
  196. }
  197. expect(response).to have_http_status(:ok)
  198. log = Cti::Log.find_by(call_id: '1234567890-1')
  199. expect(log).to be_truthy
  200. expect(log.from).to eq('4930777000000')
  201. expect(log.to).to eq('4912347114711')
  202. expect(log.direction).to eq('out')
  203. expect(log.from_comment).to eq('user 1')
  204. expect(log.to_comment).to eq('CallerId Customer1')
  205. expect(log.comment).to eq('cancel')
  206. expect(log.queue).to eq('4930777000000')
  207. expect(log.state).to eq('hangup')
  208. expect(log.done).to be(true)
  209. expect(log.initialized_at).to be_truthy
  210. expect(log.start_at).to be_nil
  211. expect(log.end_at).to be_truthy
  212. expect(log.duration_waiting_time).to be_between(2, 3)
  213. expect(log.duration_talking_time).to be_nil
  214. # outbound - II - new call
  215. post "/api/v1/cti/#{token}", params: {
  216. event: 'newCall',
  217. direction: 'out',
  218. from: '4930600000000',
  219. to: '4912347114711',
  220. call_id: '1234567890-2',
  221. user: ['user 1'],
  222. }
  223. expect(response).to have_http_status(:ok)
  224. log = Cti::Log.find_by(call_id: '1234567890-2')
  225. expect(log).to be_truthy
  226. expect(log.from).to eq('4930777000000')
  227. expect(log.to).to eq('4912347114711')
  228. expect(log.direction).to eq('out')
  229. expect(log.from_comment).to eq('user 1')
  230. expect(log.to_comment).to eq('CallerId Customer1')
  231. expect(log.comment).to be_nil
  232. expect(log.queue).to eq('4930777000000')
  233. expect(log.state).to eq('newCall')
  234. expect(log.done).to be(true)
  235. expect(log.initialized_at).to be_truthy
  236. expect(log.start_at).to be_nil
  237. expect(log.end_at).to be_nil
  238. expect(log.duration_waiting_time).to be_nil
  239. expect(log.duration_talking_time).to be_nil
  240. travel 2.seconds
  241. # outbound - II - answer by customer
  242. post "/api/v1/cti/#{token}", params: {
  243. event: 'answer',
  244. direction: 'out',
  245. call_id: '1234567890-2',
  246. from: '4930600000000',
  247. to: '4912347114711',
  248. }
  249. expect(response).to have_http_status(:ok)
  250. log = Cti::Log.find_by(call_id: '1234567890-2')
  251. expect(log).to be_truthy
  252. expect(log.from).to eq('4930777000000')
  253. expect(log.to).to eq('4912347114711')
  254. expect(log.direction).to eq('out')
  255. expect(log.from_comment).to eq('user 1')
  256. expect(log.to_comment).to eq('CallerId Customer1')
  257. expect(log.comment).to be_nil
  258. expect(log.queue).to eq('4930777000000')
  259. expect(log.state).to eq('answer')
  260. expect(log.done).to be(true)
  261. expect(log.initialized_at).to be_truthy
  262. expect(log.start_at).to be_truthy
  263. expect(log.end_at).to be_nil
  264. expect(log.duration_waiting_time).to be_between(2, 3)
  265. expect(log.duration_talking_time).to be_nil
  266. travel 2.seconds
  267. # outbound - II - hangup by customer
  268. post "/api/v1/cti/#{token}", params: {
  269. event: 'hangup',
  270. direction: 'out',
  271. call_id: '1234567890-2',
  272. cause: 'normalClearing',
  273. from: '4930600000000',
  274. to: '4912347114711',
  275. }
  276. expect(response).to have_http_status(:ok)
  277. log = Cti::Log.find_by(call_id: '1234567890-2')
  278. expect(log).to be_truthy
  279. expect(log.from).to eq('4930777000000')
  280. expect(log.to).to eq('4912347114711')
  281. expect(log.direction).to eq('out')
  282. expect(log.from_comment).to eq('user 1')
  283. expect(log.to_comment).to eq('CallerId Customer1')
  284. expect(log.comment).to eq('normalClearing')
  285. expect(log.queue).to eq('4930777000000')
  286. expect(log.state).to eq('hangup')
  287. expect(log.done).to be(true)
  288. expect(log.initialized_at).to be_truthy
  289. expect(log.start_at).to be_truthy
  290. expect(log.end_at).to be_truthy
  291. expect(log.duration_waiting_time).to be_between(2, 3)
  292. expect(log.duration_talking_time).to be_between(2, 3)
  293. travel 1.second
  294. # inbound - I - new call
  295. post "/api/v1/cti/#{token}", params: {
  296. event: 'newCall',
  297. direction: 'in',
  298. to: '4930600000000',
  299. from: '4912347114711',
  300. call_id: '1234567890-3',
  301. user: 'user 1',
  302. }
  303. expect(response).to have_http_status(:ok)
  304. log = Cti::Log.find_by(call_id: '1234567890-3')
  305. expect(log).to be_truthy
  306. expect(log.to).to eq('4930600000000')
  307. expect(log.from).to eq('4912347114711')
  308. expect(log.direction).to eq('in')
  309. expect(log.to_comment).to eq('user 1')
  310. expect(log.from_comment).to eq('CallerId Customer1')
  311. expect(log.comment).to be_nil
  312. expect(log.queue).to eq('4930600000000')
  313. expect(log.state).to eq('newCall')
  314. expect(log.done).to be(false)
  315. expect(log.initialized_at).to be_truthy
  316. expect(log.start_at).to be_nil
  317. expect(log.end_at).to be_nil
  318. expect(log.duration_waiting_time).to be_nil
  319. expect(log.duration_talking_time).to be_nil
  320. travel 1.second
  321. # inbound - I - answer by customer
  322. post "/api/v1/cti/#{token}", params: {
  323. event: 'answer',
  324. direction: 'in',
  325. call_id: '1234567890-3',
  326. to: '4930600000000',
  327. from: '4912347114711',
  328. }
  329. expect(response).to have_http_status(:ok)
  330. log = Cti::Log.find_by(call_id: '1234567890-3')
  331. expect(log).to be_truthy
  332. expect(log.to).to eq('4930600000000')
  333. expect(log.from).to eq('4912347114711')
  334. expect(log.direction).to eq('in')
  335. expect(log.to_comment).to eq('user 1')
  336. expect(log.from_comment).to eq('CallerId Customer1')
  337. expect(log.comment).to be_nil
  338. expect(log.queue).to eq('4930600000000')
  339. expect(log.state).to eq('answer')
  340. expect(log.done).to be(true)
  341. expect(log.initialized_at).to be_truthy
  342. expect(log.start_at).to be_truthy
  343. expect(log.end_at).to be_nil
  344. expect(log.duration_waiting_time).to be_truthy
  345. expect(log.duration_talking_time).to be_nil
  346. travel 1.second
  347. # inbound - I - hangup by customer
  348. post "/api/v1/cti/#{token}", params: {
  349. event: 'hangup',
  350. direction: 'in',
  351. call_id: '1234567890-3',
  352. cause: 'normalClearing',
  353. to: '4930600000000',
  354. from: '4912347114711',
  355. }
  356. expect(response).to have_http_status(:ok)
  357. log = Cti::Log.find_by(call_id: '1234567890-3')
  358. expect(log).to be_truthy
  359. expect(log.to).to eq('4930600000000')
  360. expect(log.from).to eq('4912347114711')
  361. expect(log.direction).to eq('in')
  362. expect(log.to_comment).to eq('user 1')
  363. expect(log.from_comment).to eq('CallerId Customer1')
  364. expect(log.comment).to eq('normalClearing')
  365. expect(log.queue).to eq('4930600000000')
  366. expect(log.state).to eq('hangup')
  367. expect(log.done).to be(true)
  368. expect(log.initialized_at).to be_truthy
  369. expect(log.start_at).to be_truthy
  370. expect(log.end_at).to be_truthy
  371. expect(log.duration_waiting_time).to be_truthy
  372. expect(log.duration_talking_time).to be_truthy
  373. travel 1.second
  374. # inbound - I - answer for hangup by customer
  375. post "/api/v1/cti/#{token}", params: {
  376. event: 'answer',
  377. direction: 'in',
  378. call_id: '1234567890-3',
  379. to: '4930600000000',
  380. from: '4912347114711',
  381. }, as: :json
  382. expect(response).to have_http_status(:ok)
  383. travel 1.second
  384. # inbound - II - new call
  385. post "/api/v1/cti/#{token}", params: {
  386. event: 'newCall',
  387. direction: 'in',
  388. to: '4930600000000',
  389. from: '4912347114711',
  390. call_id: '1234567890-4',
  391. user: ['user 1', 'user 2'],
  392. }
  393. expect(response).to have_http_status(:ok)
  394. log = Cti::Log.find_by(call_id: '1234567890-4')
  395. expect(log).to be_truthy
  396. expect(log.to).to eq('4930600000000')
  397. expect(log.from).to eq('4912347114711')
  398. expect(log.direction).to eq('in')
  399. expect(log.to_comment).to eq('user 1, user 2')
  400. expect(log.from_comment).to eq('CallerId Customer1')
  401. expect(log.comment).to be_nil
  402. expect(log.queue).to eq('4930600000000')
  403. expect(log.state).to eq('newCall')
  404. expect(log.done).to be(false)
  405. expect(log.initialized_at).to be_truthy
  406. expect(log.start_at).to be_nil
  407. expect(log.end_at).to be_nil
  408. expect(log.duration_waiting_time).to be_nil
  409. expect(log.duration_talking_time).to be_nil
  410. travel 1.second
  411. # inbound - II - answer by voicemail
  412. post "/api/v1/cti/#{token}", params: {
  413. event: 'answer',
  414. direction: 'in',
  415. call_id: '1234567890-4',
  416. to: '4930600000000',
  417. from: '4912347114711',
  418. user: 'voicemail',
  419. }
  420. expect(response).to have_http_status(:ok)
  421. log = Cti::Log.find_by(call_id: '1234567890-4')
  422. expect(log).to be_truthy
  423. expect(log.to).to eq('4930600000000')
  424. expect(log.from).to eq('4912347114711')
  425. expect(log.direction).to eq('in')
  426. expect(log.to_comment).to eq('voicemail')
  427. expect(log.from_comment).to eq('CallerId Customer1')
  428. expect(log.comment).to be_nil
  429. expect(log.queue).to eq('4930600000000')
  430. expect(log.state).to eq('answer')
  431. expect(log.done).to be(true)
  432. expect(log.initialized_at).to be_truthy
  433. expect(log.start_at).to be_truthy
  434. expect(log.end_at).to be_nil
  435. expect(log.duration_waiting_time).to be_truthy
  436. expect(log.duration_talking_time).to be_nil
  437. travel 1.second
  438. # inbound - II - hangup by customer
  439. post "/api/v1/cti/#{token}", params: {
  440. event: 'hangup',
  441. direction: 'in',
  442. call_id: '1234567890-4',
  443. cause: 'normalClearing',
  444. to: '4930600000000',
  445. from: '4912347114711',
  446. }
  447. expect(response).to have_http_status(:ok)
  448. log = Cti::Log.find_by(call_id: '1234567890-4')
  449. expect(log).to be_truthy
  450. expect(log.to).to eq('4930600000000')
  451. expect(log.from).to eq('4912347114711')
  452. expect(log.direction).to eq('in')
  453. expect(log.to_comment).to eq('voicemail')
  454. expect(log.from_comment).to eq('CallerId Customer1')
  455. expect(log.comment).to eq('normalClearing')
  456. expect(log.queue).to eq('4930600000000')
  457. expect(log.state).to eq('hangup')
  458. expect(log.done).to be(false)
  459. expect(log.initialized_at).to be_truthy
  460. expect(log.start_at).to be_truthy
  461. expect(log.end_at).to be_truthy
  462. expect(log.duration_waiting_time).to be_truthy
  463. expect(log.duration_talking_time).to be_truthy
  464. travel 1.second
  465. # inbound - III - new call
  466. post "/api/v1/cti/#{token}", params: {
  467. event: 'newCall',
  468. direction: 'in',
  469. to: '4930600000000',
  470. from: '4912347114711',
  471. call_id: '1234567890-5',
  472. user: 'user 1,user 2',
  473. }
  474. expect(response).to have_http_status(:ok)
  475. log = Cti::Log.find_by(call_id: '1234567890-5')
  476. expect(log).to be_truthy
  477. expect(log.to).to eq('4930600000000')
  478. expect(log.from).to eq('4912347114711')
  479. expect(log.direction).to eq('in')
  480. expect(log.to_comment).to eq('user 1,user 2')
  481. expect(log.from_comment).to eq('CallerId Customer1')
  482. expect(log.comment).to be_nil
  483. expect(log.queue).to eq('4930600000000')
  484. expect(log.state).to eq('newCall')
  485. expect(log.done).to be(false)
  486. expect(log.initialized_at).to be_truthy
  487. expect(log.start_at).to be_nil
  488. expect(log.end_at).to be_nil
  489. expect(log.duration_waiting_time).to be_nil
  490. expect(log.duration_talking_time).to be_nil
  491. travel 1.second
  492. # inbound - III - hangup by customer
  493. post "/api/v1/cti/#{token}", params: {
  494. event: 'hangup',
  495. direction: 'in',
  496. call_id: '1234567890-5',
  497. cause: 'normalClearing',
  498. to: '4930600000000',
  499. from: '4912347114711',
  500. }
  501. expect(response).to have_http_status(:ok)
  502. log = Cti::Log.find_by(call_id: '1234567890-5')
  503. expect(log).to be_truthy
  504. expect(log.to).to eq('4930600000000')
  505. expect(log.from).to eq('4912347114711')
  506. expect(log.direction).to eq('in')
  507. expect(log.to_comment).to eq('user 1,user 2')
  508. expect(log.from_comment).to eq('CallerId Customer1')
  509. expect(log.comment).to eq('normalClearing')
  510. expect(log.queue).to eq('4930600000000')
  511. expect(log.state).to eq('hangup')
  512. expect(log.done).to be(false)
  513. expect(log.initialized_at).to be_truthy
  514. expect(log.start_at).to be_nil
  515. expect(log.end_at).to be_truthy
  516. expect(log.duration_waiting_time).to be_truthy
  517. expect(log.duration_talking_time).to be_nil
  518. travel 1.second
  519. # inbound - IV - new call
  520. post "/api/v1/cti/#{token}", params: {
  521. event: 'newCall',
  522. direction: 'in',
  523. to: '4930600000000',
  524. from: '49999992222222',
  525. call_id: '1234567890-6',
  526. user: 'user 1,user 2',
  527. }
  528. expect(response).to have_http_status(:ok)
  529. log = Cti::Log.find_by(call_id: '1234567890-6')
  530. expect(log).to be_truthy
  531. expect(log.to).to eq('4930600000000')
  532. expect(log.from).to eq('49999992222222')
  533. expect(log.direction).to eq('in')
  534. expect(log.to_comment).to eq('user 1,user 2')
  535. expect(log.from_comment).to eq('CallerId Customer3,CallerId Customer2')
  536. expect(log.preferences['to']).to be_falsey
  537. expect(log.preferences['from']).to be_truthy
  538. expect(log.comment).to be_nil
  539. expect(log.queue).to eq('4930600000000')
  540. expect(log.state).to eq('newCall')
  541. expect(log.done).to be(false)
  542. expect(log.initialized_at).to be_truthy
  543. expect(log.start_at).to be_nil
  544. expect(log.end_at).to be_nil
  545. expect(log.duration_waiting_time).to be_nil
  546. expect(log.duration_talking_time).to be_nil
  547. travel 1.second
  548. # inbound - IV - new call
  549. post "/api/v1/cti/#{token}", params: {
  550. event: 'newCall',
  551. direction: 'in',
  552. to: '4930600000000',
  553. from: 'anonymous',
  554. call_id: '1234567890-7',
  555. user: 'user 1,user 2',
  556. queue: 'some_queue_name',
  557. }
  558. expect(response).to have_http_status(:ok)
  559. log = Cti::Log.find_by(call_id: '1234567890-7')
  560. expect(log).to be_truthy
  561. expect(log.to).to eq('4930600000000')
  562. expect(log.from).to eq('anonymous')
  563. expect(log.direction).to eq('in')
  564. expect(log.to_comment).to eq('user 1,user 2')
  565. expect(log.from_comment).to be_nil
  566. expect(log.preferences['to']).to be_falsey
  567. expect(log.preferences['from']).to be_falsey
  568. expect(log.comment).to be_nil
  569. expect(log.queue).to eq('some_queue_name')
  570. expect(log.state).to eq('newCall')
  571. expect(log.done).to be(false)
  572. expect(log.initialized_at).to be_truthy
  573. expect(log.start_at).to be_nil
  574. expect(log.end_at).to be_nil
  575. expect(log.duration_waiting_time).to be_nil
  576. expect(log.duration_talking_time).to be_nil
  577. get '/api/v1/cti/log'
  578. expect(response).to have_http_status(:forbidden)
  579. # get caller list
  580. authenticated_as(agent)
  581. get '/api/v1/cti/log', as: :json
  582. expect(response).to have_http_status(:ok)
  583. expect(json_response['list']).to be_a(Array)
  584. expect(json_response['list'].count).to eq(7)
  585. expect(json_response['assets']).to be_truthy
  586. expect(json_response['assets']['User']).to be_truthy
  587. expect(json_response['assets']['User'][customer2.id.to_s]).to be_truthy
  588. expect(json_response['assets']['User'][customer3.id.to_s]).to be_truthy
  589. expect(json_response['list'][0]['call_id']).to eq('1234567890-7')
  590. expect(json_response['list'][1]['call_id']).to eq('1234567890-6')
  591. expect(json_response['list'][2]['call_id']).to eq('1234567890-5')
  592. expect(json_response['list'][3]['call_id']).to eq('1234567890-4')
  593. expect(json_response['list'][4]['call_id']).to eq('1234567890-3')
  594. expect(json_response['list'][5]['call_id']).to eq('1234567890-2')
  595. expect(json_response['list'][5]['state']).to eq('hangup')
  596. expect(json_response['list'][5]['from']).to eq('4930777000000')
  597. expect(json_response['list'][5]['from_comment']).to eq('user 1')
  598. expect(json_response['list'][5]['to']).to eq('4912347114711')
  599. expect(json_response['list'][5]['to_comment']).to eq('CallerId Customer1')
  600. expect(json_response['list'][5]['comment']).to eq('normalClearing')
  601. expect(json_response['list'][5]['state']).to eq('hangup')
  602. expect(json_response['list'][6]['call_id']).to eq('1234567890-1')
  603. end
  604. it 'does log call with notify group with two a log entry' do
  605. # outbound - I - new call
  606. post "/api/v1/cti/#{token}", params: {
  607. event: 'newCall',
  608. direction: 'out',
  609. from: '4930600000000',
  610. to: '4912347114711',
  611. call_id: '1234567890-1',
  612. user: 'user 1',
  613. }
  614. expect(response).to have_http_status(:ok)
  615. # outbound - II - new call
  616. post "/api/v1/cti/#{token}", params: {
  617. event: 'newCall',
  618. direction: 'out',
  619. from: '4930600000000',
  620. to: '4912347114711',
  621. call_id: '1234567890-2',
  622. user: 'user 1',
  623. }
  624. # inbound - III - new call
  625. post "/api/v1/cti/#{token}", params: {
  626. event: 'newCall',
  627. direction: 'in',
  628. to: '4930600000000',
  629. from: '4912347114711',
  630. call_id: '1234567890-5',
  631. user: 'user 1,user 2',
  632. }
  633. expect(response).to have_http_status(:ok)
  634. # get caller list (with notify group with 2 log entries)
  635. cti_config = Setting.get('cti_config')
  636. cti_config[:notify_map] = [{ queue: '4930777000000', user_ids: [agent.id.to_s] }]
  637. Setting.set('cti_config', cti_config)
  638. authenticated_as(agent)
  639. get '/api/v1/cti/log', as: :json
  640. expect(response).to have_http_status(:ok)
  641. expect(json_response.dig('assets', 'User')).not_to be_nil
  642. expect(json_response['list'].pluck('call_id')).to match_array(%w[1234567890-1 1234567890-2])
  643. end
  644. it 'does log call with notify group without a log entry' do
  645. # outbound - I - new call
  646. post "/api/v1/cti/#{token}", params: {
  647. event: 'newCall',
  648. direction: 'out',
  649. from: '4930600000000',
  650. to: '4912347114711',
  651. call_id: '1234567890-1',
  652. user: 'user 1',
  653. }
  654. expect(response).to have_http_status(:ok)
  655. # outbound - II - new call
  656. post "/api/v1/cti/#{token}", params: {
  657. event: 'newCall',
  658. direction: 'out',
  659. from: '4930600000000',
  660. to: '4912347114711',
  661. call_id: '1234567890-2',
  662. user: 'user 1',
  663. }
  664. # inbound - III - new call
  665. post "/api/v1/cti/#{token}", params: {
  666. event: 'newCall',
  667. direction: 'in',
  668. to: '4930600000000',
  669. from: '4912347114711',
  670. call_id: '1234567890-5',
  671. user: 'user 1,user 2',
  672. }
  673. expect(response).to have_http_status(:ok)
  674. # get caller list (with notify group without a log entry)
  675. cti_config = Setting.get('cti_config')
  676. cti_config[:notify_map] = [{ queue: '4912347114711', user_ids: [agent.to_s] }]
  677. Setting.set('cti_config', cti_config)
  678. authenticated_as(agent)
  679. get '/api/v1/cti/log', as: :json
  680. expect(response).to have_http_status(:ok)
  681. expect(json_response['list']).to eq([])
  682. end
  683. it 'does queue param tests' do
  684. # inbound - queue & user
  685. post "/api/v1/cti/#{token}", params: {
  686. event: 'newCall',
  687. direction: 'in',
  688. to: '4930600000000',
  689. from: 'anonymous',
  690. call_id: '1234567890-1',
  691. user: 'user 1,user 2',
  692. queue: 'some_queue_name',
  693. }
  694. expect(response).to have_http_status(:ok)
  695. log = Cti::Log.find_by(call_id: '1234567890-1')
  696. expect(log).to be_truthy
  697. expect(log.to).to eq('4930600000000')
  698. expect(log.from).to eq('anonymous')
  699. expect(log.direction).to eq('in')
  700. expect(log.to_comment).to eq('user 1,user 2')
  701. expect(log.from_comment).to be_nil
  702. expect(log.preferences['to']).to be_falsey
  703. expect(log.preferences['from']).to be_falsey
  704. expect(log.comment).to be_nil
  705. expect(log.queue).to eq('some_queue_name')
  706. expect(log.state).to eq('newCall')
  707. expect(log.done).to be(false)
  708. expect(log.initialized_at).to be_truthy
  709. expect(log.start_at).to be_nil
  710. expect(log.end_at).to be_nil
  711. expect(log.duration_waiting_time).to be_nil
  712. expect(log.duration_talking_time).to be_nil
  713. # inbound - queue & no user
  714. post "/api/v1/cti/#{token}", params: {
  715. event: 'newCall',
  716. direction: 'in',
  717. to: '4930600000000',
  718. from: 'anonymous',
  719. call_id: '1234567890-2',
  720. user: '',
  721. queue: 'some_queue_name',
  722. }
  723. expect(response).to have_http_status(:ok)
  724. log = Cti::Log.find_by(call_id: '1234567890-2')
  725. expect(log).to be_truthy
  726. expect(log.to).to eq('4930600000000')
  727. expect(log.from).to eq('anonymous')
  728. expect(log.direction).to eq('in')
  729. expect(log.to_comment).to eq('some_queue_name')
  730. expect(log.from_comment).to be_nil
  731. expect(log.preferences['to']).to be_falsey
  732. expect(log.preferences['from']).to be_falsey
  733. expect(log.comment).to be_nil
  734. expect(log.queue).to eq('some_queue_name')
  735. expect(log.state).to eq('newCall')
  736. expect(log.done).to be(false)
  737. expect(log.initialized_at).to be_truthy
  738. expect(log.start_at).to be_nil
  739. expect(log.end_at).to be_nil
  740. expect(log.duration_waiting_time).to be_nil
  741. expect(log.duration_talking_time).to be_nil
  742. end
  743. it 'flags caller_log as done' do
  744. cti_log1 = create(:cti_log)
  745. log = Cti::Log.find(cti_log1.id)
  746. expect(log.done).to be(false)
  747. authenticated_as(agent)
  748. post "/api/v1/cti/done/#{cti_log1.id}", params: {
  749. done: true
  750. }
  751. expect(response).to have_http_status(:ok)
  752. log = Cti::Log.find(cti_log1.id)
  753. expect(log.done).to be(true)
  754. end
  755. it 'flags all caller_logs as done via done_bulk' do
  756. cti_log1 = create(:cti_log)
  757. cti_log2 = create(:cti_log)
  758. log = Cti::Log.find(cti_log1.id)
  759. expect(log.done).to be(false)
  760. authenticated_as(agent)
  761. post '/api/v1/cti/done/bulk', params: {
  762. ids: [cti_log1.id, cti_log2.id]
  763. }
  764. expect(response).to have_http_status(:ok)
  765. log1 = Cti::Log.find(cti_log1.id)
  766. expect(log1.done).to be(true)
  767. log2 = Cti::Log.find(cti_log2.id)
  768. expect(log2.done).to be(true)
  769. end
  770. end
  771. end