cti_spec.rb 29 KB

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