cti_caller_id_test.rb 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. require 'test_helper'
  2. class CtiCallerIdTest < ActiveSupport::TestCase
  3. setup do
  4. Ticket.destroy_all
  5. Cti::CallerId.destroy_all
  6. @agent1 = User.create_or_update(
  7. login: 'ticket-caller_id-agent1@example.com',
  8. firstname: 'CallerId',
  9. lastname: 'Agent1',
  10. email: 'ticket-caller_id-agent1@example.com',
  11. active: true,
  12. phone: '+49 1111 222222',
  13. fax: '+49 1111 222223',
  14. mobile: '+49 1111 222223',
  15. note: 'Phone at home: +49 1111 222224',
  16. updated_by_id: 1,
  17. created_by_id: 1,
  18. )
  19. @agent2 = User.create_or_update(
  20. login: 'ticket-caller_id-agent2@example.com',
  21. firstname: 'CallerId',
  22. lastname: 'Agent2',
  23. email: 'ticket-caller_id-agent2@example.com',
  24. phone: '+49 2222 222222',
  25. note: 'Phone at home: <b>+49 2222 222224</b>',
  26. active: true,
  27. updated_by_id: 1,
  28. created_by_id: 1,
  29. )
  30. @agent3 = User.create_or_update(
  31. login: 'ticket-caller_id-agent3@example.com',
  32. firstname: 'CallerId',
  33. lastname: 'Agent3',
  34. email: 'ticket-caller_id-agent3@example.com',
  35. phone: '+49 2222 222222',
  36. active: true,
  37. updated_by_id: 1,
  38. created_by_id: 1,
  39. )
  40. @customer1 = User.create_or_update(
  41. login: 'ticket-caller_id-customer1@example.com',
  42. firstname: 'CallerId',
  43. lastname: 'Customer1',
  44. email: 'ticket-caller_id-customer1@example.com',
  45. active: true,
  46. updated_by_id: 1,
  47. created_by_id: 1,
  48. )
  49. Observer::Transaction.commit
  50. Scheduler.worker(true)
  51. end
  52. test '1 lookups' do
  53. Cti::CallerId.rebuild
  54. caller_ids = Cti::CallerId.lookup('491111222277')
  55. assert_equal(0, caller_ids.length)
  56. caller_ids = Cti::CallerId.lookup('491111222223')
  57. assert_equal(1, caller_ids.length)
  58. assert_equal(@agent1.id, caller_ids[0].user_id)
  59. assert_equal('known', caller_ids[0].level)
  60. caller_ids = Cti::CallerId.lookup('492222222222')
  61. assert_equal(2, caller_ids.length)
  62. assert_equal(@agent3.id, caller_ids[0].user_id)
  63. assert_equal('known', caller_ids[0].level)
  64. assert_equal(@agent2.id, caller_ids[1].user_id)
  65. assert_equal('known', caller_ids[1].level)
  66. # create ticket in group
  67. ticket1 = Ticket.create!(
  68. title: 'some caller id test 1',
  69. group: Group.lookup(name: 'Users'),
  70. customer: @customer1,
  71. state: Ticket::State.lookup(name: 'new'),
  72. priority: Ticket::Priority.lookup(name: '2 normal'),
  73. updated_by_id: @agent1.id,
  74. created_by_id: @agent1.id,
  75. )
  76. article1 = Ticket::Article.create!(
  77. ticket_id: ticket1.id,
  78. from: 'some_sender@example.com',
  79. to: 'some_recipient@example.com',
  80. subject: 'some subject',
  81. message_id: 'some@id',
  82. body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr
  83. Fon (LIN): +49 222 6112222 Mo-Di
  84. Mob: +49 333 8362222",
  85. internal: false,
  86. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  87. type: Ticket::Article::Type.where(name: 'email').first,
  88. updated_by_id: @customer1.id,
  89. created_by_id: @customer1.id,
  90. )
  91. assert(ticket1)
  92. # create ticket in group
  93. ticket2 = Ticket.create!(
  94. title: 'some caller id test 2',
  95. group: Group.lookup(name: 'Users'),
  96. customer: @customer1,
  97. state: Ticket::State.lookup(name: 'new'),
  98. priority: Ticket::Priority.lookup(name: '2 normal'),
  99. updated_by_id: @agent1.id,
  100. created_by_id: @agent1.id,
  101. )
  102. article2 = Ticket::Article.create!(
  103. ticket_id: ticket2.id,
  104. from: 'some_sender@example.com',
  105. to: 'some_recipient@example.com',
  106. subject: 'some subject',
  107. message_id: 'some@id',
  108. body: "some message\nFon (GEL): +49 111 111-1111 Mi-Fr
  109. Fon (LIN): +49 222 1112222 Mo-Di
  110. Mob: +49 333 1112222",
  111. internal: false,
  112. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  113. type: Ticket::Article::Type.where(name: 'email').first,
  114. updated_by_id: @agent1.id,
  115. created_by_id: @agent1.id,
  116. )
  117. assert(ticket2)
  118. Observer::Transaction.commit
  119. Scheduler.worker(true)
  120. caller_ids = Cti::CallerId.lookup('491111222277')
  121. assert_equal(0, caller_ids.length)
  122. caller_ids = Cti::CallerId.lookup('491111222223')
  123. assert_equal(1, caller_ids.length)
  124. assert_equal(@agent1.id, caller_ids[0].user_id)
  125. assert_equal('known', caller_ids[0].level)
  126. caller_ids = Cti::CallerId.lookup('492222222222')
  127. assert_equal(2, caller_ids.length)
  128. assert_equal(@agent3.id, caller_ids[0].user_id)
  129. assert_equal('known', caller_ids[0].level)
  130. assert_equal(@agent2.id, caller_ids[1].user_id)
  131. assert_equal('known', caller_ids[1].level)
  132. caller_ids = Cti::CallerId.lookup('492226112222')
  133. assert_equal(1, caller_ids.length)
  134. assert_equal(@customer1.id, caller_ids[0].user_id)
  135. assert_equal('maybe', caller_ids[0].level)
  136. caller_ids = Cti::CallerId.lookup('492221112222')
  137. assert_equal(0, caller_ids.length)
  138. end
  139. test '2 lookups' do
  140. Cti::CallerId.destroy_all
  141. Cti::CallerId.maybe_add(
  142. caller_id: '4999999999',
  143. level: 'maybe',
  144. user_id: 2,
  145. object: 'Ticket',
  146. o_id: 2,
  147. )
  148. Cti::CallerId.maybe_add(
  149. caller_id: '4912345678901',
  150. comment: 'Hairdresser Bob Smith, San Francisco',
  151. level: 'public',
  152. user_id: 2,
  153. object: 'GoYello',
  154. o_id: 1,
  155. )
  156. caller_ids = Cti::CallerId.lookup('4912345678901')
  157. assert_equal(1, caller_ids.length)
  158. assert_equal('public', caller_ids[0].level)
  159. assert_equal(2, caller_ids[0].user_id)
  160. assert_equal('Hairdresser Bob Smith, San Francisco', caller_ids[0].comment)
  161. Cti::CallerId.maybe_add(
  162. caller_id: '4912345678901',
  163. level: 'maybe',
  164. user_id: 2,
  165. object: 'Ticket',
  166. o_id: 2,
  167. )
  168. caller_ids = Cti::CallerId.lookup('4912345678901')
  169. assert_equal(1, caller_ids.length)
  170. assert_equal('maybe', caller_ids[0].level)
  171. assert_equal(2, caller_ids[0].user_id)
  172. assert_nil(caller_ids[0].comment)
  173. Cti::CallerId.maybe_add(
  174. caller_id: '4912345678901',
  175. level: 'maybe',
  176. user_id: 2,
  177. object: 'Ticket',
  178. o_id: 2,
  179. )
  180. caller_ids = Cti::CallerId.lookup('4912345678901')
  181. assert_equal(1, caller_ids.length)
  182. assert_equal('maybe', caller_ids[0].level)
  183. assert_equal(2, caller_ids[0].user_id)
  184. assert_nil(caller_ids[0].comment)
  185. user_id = User.find_by(login: 'ticket-caller_id-customer1@example.com').id
  186. Cti::CallerId.maybe_add(
  187. caller_id: '4912345678901',
  188. level: 'maybe',
  189. user_id: user_id,
  190. object: 'Ticket',
  191. o_id: 2,
  192. )
  193. caller_ids = Cti::CallerId.lookup('4912345678901')
  194. assert_equal(2, caller_ids.length)
  195. assert_equal('maybe', caller_ids[0].level)
  196. assert_equal(user_id, caller_ids[0].user_id)
  197. assert_nil(caller_ids[0].comment)
  198. assert_equal('maybe', caller_ids[1].level)
  199. assert_equal(2, caller_ids[1].user_id)
  200. assert_nil(caller_ids[1].comment)
  201. Cti::CallerId.maybe_add(
  202. caller_id: '4912345678901',
  203. level: 'known',
  204. user_id: user_id,
  205. object: 'User',
  206. o_id: 2,
  207. )
  208. caller_ids = Cti::CallerId.lookup('4912345678901')
  209. assert_equal(1, caller_ids.length)
  210. assert_equal('known', caller_ids[0].level)
  211. assert_equal(user_id, caller_ids[0].user_id)
  212. assert_nil(caller_ids[0].comment)
  213. end
  214. test '3 process - log' do
  215. ticket1 = Ticket.create!(
  216. title: 'some caller id test 1',
  217. group: Group.lookup(name: 'Users'),
  218. customer: @customer1,
  219. state: Ticket::State.lookup(name: 'new'),
  220. priority: Ticket::Priority.lookup(name: '2 normal'),
  221. updated_by_id: @agent1.id,
  222. created_by_id: @agent1.id,
  223. )
  224. article1 = Ticket::Article.create!(
  225. ticket_id: ticket1.id,
  226. from: 'some_sender@example.com',
  227. to: 'some_recipient@example.com',
  228. subject: 'some subject',
  229. message_id: 'some@id',
  230. body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr
  231. Fon (LIN): +49 222 6112222 Mo-Di
  232. Mob: +49 333 8362222",
  233. internal: false,
  234. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  235. type: Ticket::Article::Type.where(name: 'email').first,
  236. updated_by_id: @customer1.id,
  237. created_by_id: @customer1.id,
  238. )
  239. assert(ticket1)
  240. ticket2 = Ticket.create!(
  241. title: 'some caller id test 2',
  242. group: Group.lookup(name: 'Users'),
  243. customer: @customer1,
  244. state: Ticket::State.lookup(name: 'new'),
  245. priority: Ticket::Priority.lookup(name: '2 normal'),
  246. updated_by_id: @agent1.id,
  247. created_by_id: @agent1.id,
  248. )
  249. article2 = Ticket::Article.create!(
  250. ticket_id: ticket2.id,
  251. from: 'some_sender@example.com',
  252. to: 'some_recipient@example.com',
  253. subject: 'some subject',
  254. message_id: 'some@id',
  255. body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr
  256. Fon (LIN): +49 222 6112222 Mo-Di
  257. Mob: +49 333 8362222",
  258. internal: false,
  259. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  260. type: Ticket::Article::Type.where(name: 'email').first,
  261. updated_by_id: @customer1.id,
  262. created_by_id: @customer1.id,
  263. )
  264. assert(ticket2)
  265. Cti::CallerId.rebuild
  266. Cti::Log.process(
  267. 'cause' => '',
  268. 'event' => 'newCall',
  269. 'user' => 'user 1',
  270. 'from' => '491113661111',
  271. 'to' => '4930600000000',
  272. 'callId' => '4991155921769858278-1',
  273. 'direction' => 'in',
  274. )
  275. log = Cti::Log.log
  276. assert(log[:list])
  277. assert(log[:assets])
  278. assert(log[:list][0])
  279. assert_not(log[:list][1])
  280. assert(log[:list][0].preferences)
  281. assert(log[:list][0].preferences[:from])
  282. assert_equal(1, log[:list][0].preferences[:from].count)
  283. assert_equal(@customer1.id, log[:list][0].preferences[:from][0][:user_id])
  284. assert_equal('maybe', log[:list][0].preferences[:from][0][:level])
  285. end
  286. test '4 touch caller log / don\'t touch caller log' do
  287. 5.times do |count|
  288. travel 2.seconds
  289. Cti::Log.process(
  290. 'cause' => '',
  291. 'event' => 'newCall',
  292. 'user' => 'user 1',
  293. 'from' => '491111222222',
  294. 'to' => '4930600000000',
  295. 'callId' => "touch-loop-#{count}",
  296. 'direction' => 'in',
  297. )
  298. end
  299. # do not update Cti::Log on user touch
  300. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  301. travel 10.minutes
  302. @agent1.reload
  303. @agent1.touch
  304. Observer::Transaction.commit
  305. Scheduler.worker(true)
  306. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  307. # do update old Cti::Log on phone update of user
  308. @agent1.reload
  309. @agent1.phone = '+49 1111 222222 999'
  310. @agent1.save!
  311. Observer::Transaction.commit
  312. Scheduler.worker(true)
  313. assert_not_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  314. # new call with not known number
  315. travel 10.minutes
  316. Cti::Log.process(
  317. 'cause' => '',
  318. 'event' => 'newCall',
  319. 'user' => 'user 1',
  320. 'from' => '49111122222277',
  321. 'to' => '4930600000000',
  322. 'callId' => 'touch-loop-20',
  323. 'direction' => 'in',
  324. )
  325. # set not known number for agent1
  326. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  327. travel 10.minutes
  328. @agent1.reload
  329. @agent1.phone = '+49 1111 222222 77'
  330. @agent1.save!
  331. Observer::Transaction.commit
  332. Scheduler.worker(true)
  333. assert_not_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  334. # verify last updated entry
  335. last = Cti::Log.order(updated_at: :desc).first
  336. assert_equal('49111122222277', last.preferences[:from][0][:caller_id])
  337. assert_nil(last.preferences[:from][0][:comment])
  338. assert_equal('known', last.preferences[:from][0][:level])
  339. assert_equal('User', last.preferences[:from][0][:object])
  340. assert_equal(@agent1.id, last.preferences[:from][0][:o_id])
  341. # create new user with no phone number
  342. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  343. travel 30.minutes
  344. agent4 = User.create!(
  345. login: 'ticket-caller_id-agent4@example.com',
  346. firstname: 'CallerId',
  347. lastname: 'Agent4',
  348. email: 'ticket-caller_id-agent4@example.com',
  349. active: true,
  350. updated_by_id: 1,
  351. created_by_id: 1,
  352. )
  353. Observer::Transaction.commit
  354. Scheduler.worker(true)
  355. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  356. # verify if caller log is updated with '' value for phone
  357. agent4.reload
  358. agent4.phone = ''
  359. agent4.save!
  360. Observer::Transaction.commit
  361. Scheduler.worker(true)
  362. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  363. # verify if caller log is updated with nil value for phone
  364. agent4.reload
  365. agent4.phone = nil
  366. agent4.save!
  367. Observer::Transaction.commit
  368. Scheduler.worker(true)
  369. # verify if caller log is updated with existing caller log value for phone
  370. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  371. agent4.reload
  372. agent4.phone = '+49 1111 222222'
  373. agent4.save!
  374. Observer::Transaction.commit
  375. Scheduler.worker(true)
  376. assert_not_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  377. # verify if caller log is no value change for phone
  378. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  379. travel 30.minutes
  380. agent4.save!
  381. Observer::Transaction.commit
  382. Scheduler.worker(true)
  383. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  384. # verify if caller log is updated with '' value for phone
  385. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  386. travel 30.minutes
  387. agent4.reload
  388. agent4.phone = ''
  389. agent4.save!
  390. Observer::Transaction.commit
  391. Scheduler.worker(true)
  392. assert_not_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  393. # verify if caller log is updated if new ticket with existing caller id is created
  394. last_updated_at = Cti::Log.order(updated_at: :desc).first.updated_at
  395. travel 30.minutes
  396. last_caller_id_count = Cti::CallerId.count
  397. ticket1 = Ticket.create!(
  398. title: 'some caller id test 1',
  399. group: Group.lookup(name: 'Users'),
  400. customer: @customer1,
  401. state: Ticket::State.lookup(name: 'new'),
  402. priority: Ticket::Priority.lookup(name: '2 normal'),
  403. updated_by_id: @customer1.id,
  404. created_by_id: @customer1.id,
  405. )
  406. article1 = Ticket::Article.create!(
  407. ticket_id: ticket1.id,
  408. from: 'some_sender@example.com',
  409. to: 'some_recipient@example.com',
  410. subject: 'some subject',
  411. message_id: 'some@id',
  412. body: "some message\n+49 1111 222222",
  413. internal: false,
  414. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  415. type: Ticket::Article::Type.where(name: 'email').first,
  416. updated_by_id: @customer1.id,
  417. created_by_id: @customer1.id,
  418. )
  419. Observer::Transaction.commit
  420. Scheduler.worker(true)
  421. assert_equal(last_caller_id_count + 2, Cti::CallerId.count)
  422. assert_equal(last_updated_at, Cti::Log.order(updated_at: :desc).first.updated_at)
  423. end
  424. test '5 probe if caller log need to be pushed' do
  425. Cti::Log.process(
  426. 'cause' => '',
  427. 'event' => 'newCall',
  428. 'user' => 'user 1',
  429. 'from' => '491111222222',
  430. 'to' => '4930600000000',
  431. 'callId' => 'touch-loop-0',
  432. 'direction' => 'in',
  433. )
  434. assert(Cti::Log.push_caller_list_update?(Cti::Log.last))
  435. 65.times do |count|
  436. travel 1.hour
  437. Cti::Log.process(
  438. 'cause' => '',
  439. 'event' => 'newCall',
  440. 'user' => 'user 1',
  441. 'from' => '491111222222',
  442. 'to' => '4930600000000',
  443. 'callId' => "touch-loop-1-#{count}",
  444. 'direction' => 'in',
  445. )
  446. end
  447. assert(Cti::Log.push_caller_list_update?(Cti::Log.last))
  448. assert_not(Cti::Log.push_caller_list_update?(Cti::Log.first))
  449. 65.times do |count|
  450. travel 1.minute
  451. Cti::Log.process(
  452. 'cause' => '',
  453. 'event' => 'newCall',
  454. 'user' => 'user 1',
  455. 'from' => '491111222222',
  456. 'to' => '4930600000000',
  457. 'callId' => "touch-loop-2-#{count}",
  458. 'direction' => 'in',
  459. )
  460. end
  461. assert(Cti::Log.push_caller_list_update?(Cti::Log.last))
  462. assert_not(Cti::Log.push_caller_list_update?(Cti::Log.first))
  463. travel 2.seconds
  464. Cti::Log.process(
  465. 'cause' => '',
  466. 'event' => 'newCall',
  467. 'user' => 'user 1',
  468. 'from' => '491111222222',
  469. 'to' => '4930600000000',
  470. 'callId' => 'touch-loop-3-1',
  471. 'direction' => 'in',
  472. )
  473. assert(Cti::Log.push_caller_list_update?(Cti::Log.last))
  474. end
  475. test 'user delete with caller log rebuild' do
  476. assert_equal(2, Cti::CallerId.where(user_id: @agent2.id).count)
  477. @agent2.destroy!
  478. assert_equal(0, Cti::CallerId.where(user_id: @agent2.id).count)
  479. Observer::Transaction.commit
  480. Scheduler.worker(true)
  481. assert_equal(0, Cti::CallerId.where(user_id: @agent2.id).count)
  482. end
  483. test 'order of events' do
  484. Cti::Log.process(
  485. 'cause' => '',
  486. 'event' => 'newCall',
  487. 'user' => 'user 1',
  488. 'from' => '491111222222',
  489. 'to' => '4930600000000',
  490. 'callId' => 'touch-loop-1',
  491. 'direction' => 'in',
  492. )
  493. last = Cti::Log.last
  494. assert_equal(last.state, 'newCall')
  495. assert_equal(last.done, false)
  496. travel 2.seconds
  497. Cti::Log.process(
  498. 'cause' => '',
  499. 'event' => 'hangup',
  500. 'user' => 'user 1',
  501. 'from' => '491111222222',
  502. 'to' => '4930600000000',
  503. 'callId' => 'touch-loop-1',
  504. 'direction' => 'in',
  505. )
  506. last.reload
  507. assert_equal(last.state, 'hangup')
  508. assert_equal(last.done, false)
  509. travel 2.seconds
  510. Cti::Log.process(
  511. 'cause' => '',
  512. 'event' => 'answer',
  513. 'user' => 'user 1',
  514. 'from' => '491111222222',
  515. 'to' => '4930600000000',
  516. 'callId' => 'touch-loop-1',
  517. 'direction' => 'in',
  518. )
  519. last.reload
  520. assert_equal(last.state, 'hangup')
  521. assert_equal(last.done, false)
  522. end
  523. test 'not answered should be not marked as done' do
  524. Cti::Log.process(
  525. 'cause' => '',
  526. 'event' => 'newCall',
  527. 'user' => 'user 1',
  528. 'from' => '491111222222',
  529. 'to' => '4930600000000',
  530. 'callId' => 'touch-loop-1',
  531. 'direction' => 'in',
  532. )
  533. last = Cti::Log.last
  534. assert_equal(last.state, 'newCall')
  535. assert_equal(last.done, false)
  536. travel 2.seconds
  537. Cti::Log.process(
  538. 'cause' => '',
  539. 'event' => 'hangup',
  540. 'user' => 'user 1',
  541. 'from' => '491111222222',
  542. 'to' => '4930600000000',
  543. 'callId' => 'touch-loop-1',
  544. 'direction' => 'in',
  545. )
  546. last.reload
  547. assert_equal(last.state, 'hangup')
  548. assert_equal(last.done, false)
  549. end
  550. test 'answered should be marked as done' do
  551. Cti::Log.process(
  552. 'cause' => '',
  553. 'event' => 'newCall',
  554. 'user' => 'user 1',
  555. 'from' => '491111222222',
  556. 'to' => '4930600000000',
  557. 'callId' => 'touch-loop-1',
  558. 'direction' => 'in',
  559. )
  560. last = Cti::Log.last
  561. assert_equal(last.state, 'newCall')
  562. assert_equal(last.done, false)
  563. travel 2.seconds
  564. Cti::Log.process(
  565. 'cause' => '',
  566. 'event' => 'answer',
  567. 'user' => 'user 1',
  568. 'from' => '491111222222',
  569. 'to' => '4930600000000',
  570. 'callId' => 'touch-loop-1',
  571. 'direction' => 'in',
  572. )
  573. last = Cti::Log.last
  574. assert_equal(last.state, 'answer')
  575. assert_equal(last.done, true)
  576. travel 2.seconds
  577. Cti::Log.process(
  578. 'cause' => '',
  579. 'event' => 'hangup',
  580. 'user' => 'user 1',
  581. 'from' => '491111222222',
  582. 'to' => '4930600000000',
  583. 'callId' => 'touch-loop-1',
  584. 'direction' => 'in',
  585. )
  586. last.reload
  587. assert_equal(last.state, 'hangup')
  588. assert_equal(last.done, true)
  589. end
  590. test 'voicemail should not be marked as done' do
  591. Cti::Log.process(
  592. 'cause' => '',
  593. 'event' => 'newCall',
  594. 'user' => 'user 1',
  595. 'from' => '491111222222',
  596. 'to' => '4930600000000',
  597. 'callId' => 'touch-loop-1',
  598. 'direction' => 'in',
  599. )
  600. last = Cti::Log.last
  601. assert_equal(last.state, 'newCall')
  602. assert_equal(last.done, false)
  603. Cti::Log.process(
  604. 'cause' => '',
  605. 'event' => 'answer',
  606. 'user' => 'voicemail',
  607. 'from' => '491111222222',
  608. 'to' => '4930600000000',
  609. 'callId' => 'touch-loop-1',
  610. 'direction' => 'in',
  611. )
  612. last = Cti::Log.last
  613. assert_equal(last.state, 'answer')
  614. assert_equal(last.done, true)
  615. travel 2.seconds
  616. Cti::Log.process(
  617. 'cause' => '',
  618. 'event' => 'hangup',
  619. 'user' => 'user 1',
  620. 'from' => '491111222222',
  621. 'to' => '4930600000000',
  622. 'callId' => 'touch-loop-1',
  623. 'direction' => 'in',
  624. )
  625. last.reload
  626. assert_equal(last.state, 'hangup')
  627. assert_equal(last.done, false)
  628. end
  629. test 'forwarded should be marked as done' do
  630. Cti::Log.process(
  631. 'cause' => '',
  632. 'event' => 'newCall',
  633. 'user' => 'user 1',
  634. 'from' => '491111222222',
  635. 'to' => '4930600000000',
  636. 'callId' => 'touch-loop-1',
  637. 'direction' => 'in',
  638. )
  639. last = Cti::Log.last
  640. assert_equal(last.state, 'newCall')
  641. assert_equal(last.done, false)
  642. travel 2.seconds
  643. Cti::Log.process(
  644. 'cause' => 'forwarded',
  645. 'event' => 'hangup',
  646. 'user' => 'user 1',
  647. 'from' => '491111222222',
  648. 'to' => '4930600000000',
  649. 'callId' => 'touch-loop-1',
  650. 'direction' => 'in',
  651. )
  652. last.reload
  653. assert_equal(last.state, 'hangup')
  654. assert_equal(last.done, true)
  655. end
  656. end