log_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. require 'rails_helper'
  2. RSpec.describe Cti::Log do
  3. subject(:user) { create(:user, roles: Role.where(name: 'Agent'), phone: phone) }
  4. let(:phone) { '' }
  5. let(:log) { create(:'cti/log') }
  6. describe '.log' do
  7. it 'returns a hash with :list and :assets keys' do
  8. expect(described_class.log(user)).to match(hash_including(:list, :assets))
  9. end
  10. context 'when pretty is not generated' do
  11. let(:log) { create(:'cti/log') }
  12. before do
  13. log.update_column(:preferences, nil)
  14. end
  15. it 'does fallback generate the pretty value' do
  16. expect(log.reload.attributes['from_pretty']).to eq('+49 30 609854180')
  17. end
  18. end
  19. context 'when over 60 Log records exist' do
  20. subject!(:cti_logs) do
  21. 61.times.map do |_i| # rubocop:disable Performance/TimesMap
  22. travel 1.second
  23. create(:'cti/log')
  24. end
  25. end
  26. it 'returns the 60 latest ones in the :list key' do
  27. expect(described_class.log(user)[:list]).to match_array(cti_logs.last(60))
  28. end
  29. end
  30. context 'when Log records have arrays of CallerId attributes in #preferences[:to] / #preferences[:from]' do
  31. subject!(:cti_log) { create(:'cti/log', preferences: { from: [caller_id] }) }
  32. let(:caller_id) { create(:caller_id) }
  33. let(:caller_user) { User.find_by(id: caller_id.user_id) }
  34. it 'returns a hash of the CallerId Users and their assets in the :assets key' do
  35. expect(described_class.log(user)[:assets]).to eq(caller_user.assets({}))
  36. end
  37. end
  38. context 'when a notify map is defined' do
  39. subject!(:cti_logs) do
  40. [create(:'cti/log', queue: 'queue0'),
  41. create(:'cti/log', queue: 'queue2'),
  42. create(:'cti/log', queue: 'queue3'),
  43. create(:'cti/log', queue: 'queue4')]
  44. end
  45. before do
  46. cti_config = Setting.get('cti_config')
  47. cti_config[:notify_map] = [ { queue: 'queue4', user_ids: [user.id.to_s] } ]
  48. Setting.set('cti_config', cti_config)
  49. end
  50. it 'returns one matching log record' do
  51. expect(described_class.log(user)[:list]).to match_array([cti_logs[3]])
  52. end
  53. end
  54. end
  55. describe '.push_caller_list_update?' do
  56. let!(:existing_logs) { create_list(:'cti/log', 60) }
  57. let(:log) { create(:'cti/log') }
  58. context 'when given log is older than existing logs' do
  59. before { travel(-10.seconds) }
  60. it 'return false' do
  61. expect(described_class.push_caller_list_update?(log)).to eq false
  62. end
  63. end
  64. context 'when given log is newer than existing logs' do
  65. before { travel(10.seconds) }
  66. it 'return true' do
  67. expect(described_class.push_caller_list_update?(log)).to eq true
  68. end
  69. end
  70. end
  71. describe '.process' do
  72. let(:attributes) do
  73. {
  74. 'cause' => cause,
  75. 'event' => event,
  76. 'user' => 'user 1',
  77. 'from' => '49123456',
  78. 'to' => '49123457',
  79. 'call_id' => '1',
  80. 'direction' => 'in',
  81. }
  82. end
  83. let(:cause) { '' }
  84. context 'for event "newCall"' do
  85. let(:event) { 'newCall' }
  86. context 'with unrecognized "call_id"' do
  87. it 'creates a new Log record' do
  88. expect { described_class.process(attributes) }
  89. .to change(described_class, :count).by(1)
  90. expect(described_class.last.attributes)
  91. .to include(
  92. 'call_id' => '1',
  93. 'state' => 'newCall',
  94. 'done' => false,
  95. 'queue' => '49123457',
  96. 'from' => '49123456',
  97. 'from_comment' => nil,
  98. 'from_pretty' => '49123456',
  99. 'start_at' => nil,
  100. 'end_at' => nil,
  101. 'to' => '49123457',
  102. 'to_comment' => 'user 1',
  103. 'to_pretty' => '49123457'
  104. )
  105. end
  106. context 'for direction "in", with a CallerId record matching the "from" number' do
  107. let!(:caller_id) { create(:caller_id, caller_id: '49123456') }
  108. before { attributes.merge!('direction' => 'in') }
  109. it 'saves that CallerId’s attributes in the new Log’s #preferences[:from] attribute' do
  110. described_class.process(attributes)
  111. expect(described_class.last.preferences[:from].first)
  112. .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone
  113. end
  114. end
  115. context 'for direction "out", with a CallerId record matching the "to" number' do
  116. let!(:caller_id) { create(:caller_id, caller_id: '49123457') }
  117. before { attributes.merge!('direction' => 'out') }
  118. it 'saves that CallerId’s attributes in the new Log’s #preferences[:to] attribute' do
  119. described_class.process(attributes)
  120. expect(described_class.last.preferences[:to].first)
  121. .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone
  122. end
  123. end
  124. end
  125. context 'with recognized "call_id"' do
  126. before { create(:'cti/log', call_id: '1') }
  127. it 'raises an error' do
  128. expect { described_class.process(attributes) }.to raise_error(%r{call_id \S+ already exists!})
  129. end
  130. end
  131. end
  132. context 'for event "answer"' do
  133. let(:event) { 'answer' }
  134. context 'with unrecognized "call_id"' do
  135. it 'raises an error' do
  136. expect { described_class.process(attributes) }.to raise_error(%r{No such call_id})
  137. end
  138. end
  139. context 'with recognized "call_id"' do
  140. context 'for Log with #state "newCall"' do
  141. let(:log) { create(:'cti/log', call_id: 1, state: 'newCall', done: false) }
  142. it 'returns early with no changes' do
  143. expect { described_class.process(attributes) }
  144. .to change { log.reload.state }.to('answer')
  145. .and change { log.reload.done }.to(true)
  146. end
  147. end
  148. context 'for Log with #state "hangup"' do
  149. let(:log) { create(:'cti/log', call_id: 1, state: 'hangup', done: false) }
  150. it 'returns early with no changes' do
  151. expect { described_class.process(attributes) }
  152. .not_to change(log, :reload)
  153. end
  154. end
  155. end
  156. end
  157. context 'for event "hangup"' do
  158. let(:event) { 'hangup' }
  159. context 'with unrecognized "call_id"' do
  160. it 'raises an error' do
  161. expect { described_class.process(attributes) }.to raise_error(%r{No such call_id})
  162. end
  163. end
  164. context 'with recognized "call_id"' do
  165. context 'for Log with #state "newCall"' do
  166. let(:log) { create(:'cti/log', call_id: 1, state: 'newCall', done: false) }
  167. it 'sets attributes #state: "hangup", #done: false' do
  168. expect { described_class.process(attributes) }
  169. .to change { log.reload.state }.to('hangup')
  170. .and not_change { log.reload.done }
  171. end
  172. context 'when call is forwarded' do
  173. let(:cause) { 'forwarded' }
  174. it 'sets attributes #state: "hangup", #done: true' do
  175. expect { described_class.process(attributes) }
  176. .to change { log.reload.state }.to('hangup')
  177. .and change { log.reload.done }.to(true)
  178. end
  179. end
  180. end
  181. context 'for Log with #state "answer"' do
  182. let(:log) { create(:'cti/log', call_id: 1, state: 'answer', done: true) }
  183. it 'sets attributes #state: "hangup"' do
  184. expect { described_class.process(attributes) }
  185. .to change { log.reload.state }.to('hangup')
  186. .and not_change { log.reload.done }
  187. end
  188. context 'when call is sent to voicemail' do
  189. before { log.update(to_comment: 'voicemail') }
  190. it 'sets attributes #state: "hangup", #done: false' do
  191. expect { described_class.process(attributes) }
  192. .to change { log.reload.state }.to('hangup')
  193. .and change { log.reload.done }.to(false)
  194. end
  195. end
  196. end
  197. end
  198. end
  199. context 'for preferences.from verification' do
  200. subject(:log) do
  201. described_class.process(attributes)
  202. end
  203. let(:customer_of_ticket) { create(:customer) }
  204. let(:ticket_sample) do
  205. create(:ticket_article, created_by_id: customer_of_ticket.id, body: 'some text 0123457')
  206. TransactionDispatcher.commit
  207. Scheduler.worker(true)
  208. end
  209. let(:caller_id) { '0123456' }
  210. let(:attributes) do
  211. {
  212. 'cause' => '',
  213. 'event' => 'newCall',
  214. 'user' => 'user 1',
  215. 'from' => caller_id,
  216. 'to' => '49123450',
  217. 'call_id' => '1',
  218. 'direction' => 'in',
  219. }
  220. end
  221. context 'with now related customer' do
  222. it 'gives no caller information' do
  223. expect(log.preferences[:from]).to eq(nil)
  224. end
  225. end
  226. context 'with related known customer' do
  227. let!(:customer) { create(:customer, phone: '0123456') }
  228. it 'gives caller information' do
  229. expect(log.preferences[:from].count).to eq(1)
  230. expect(log.preferences[:from].first)
  231. .to include(
  232. 'level' => 'known',
  233. 'user_id' => customer.id,
  234. )
  235. end
  236. end
  237. context 'with related known customers' do
  238. let!(:customer1) { create(:customer, phone: '0123456') }
  239. let!(:customer2) { create(:customer, phone: '0123456') }
  240. it 'gives caller information' do
  241. expect(log.preferences[:from].count).to eq(2)
  242. expect(log.preferences[:from].first)
  243. .to include(
  244. 'level' => 'known',
  245. 'user_id' => customer2.id,
  246. )
  247. end
  248. end
  249. context 'with related maybe customer' do
  250. let(:caller_id) { '0123457' }
  251. let!(:ticket) { ticket_sample }
  252. it 'gives caller information' do
  253. expect(log.preferences[:from].count).to eq(1)
  254. expect(log.preferences[:from].first)
  255. .to include(
  256. 'level' => 'maybe',
  257. 'user_id' => customer_of_ticket.id,
  258. )
  259. end
  260. end
  261. context 'with related maybe and known customer' do
  262. let(:caller_id) { '0123457' }
  263. let!(:customer) { create(:customer, phone: '0123457') }
  264. let!(:ticket) { ticket_sample }
  265. it 'gives caller information' do
  266. expect(log.preferences[:from].count).to eq(1)
  267. expect(log.preferences[:from].first)
  268. .to include(
  269. 'level' => 'known',
  270. 'user_id' => customer.id,
  271. )
  272. end
  273. end
  274. end
  275. end
  276. describe 'Callbacks -' do
  277. describe 'Updating agent sessions:' do
  278. before { allow(Sessions).to receive(:send_to).with(any_args) }
  279. context 'on creation' do
  280. it 'pushes "cti_list_push" event' do
  281. User.with_permissions('cti.agent').each do |u|
  282. expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' })
  283. end
  284. create(:cti_log)
  285. end
  286. context 'with over 60 existing Log records' do
  287. before { create_list(:cti_log, 60) }
  288. it '(always) pushes "cti_list_push" event' do
  289. User.with_permissions('cti.agent').each do |u|
  290. expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' })
  291. end
  292. create(:cti_log)
  293. end
  294. end
  295. end
  296. context 'on update' do
  297. subject!(:log) { create(:cti_log) }
  298. it 'pushes "cti_list_push" event' do
  299. User.with_permissions('cti.agent').each do |u|
  300. expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' })
  301. end
  302. log.touch
  303. end
  304. context 'when among the latest 60 Log records' do
  305. before { create_list(:cti_log, 59) }
  306. it 'pushes "cti_list_push" event' do
  307. User.with_permissions('cti.agent').each do |u|
  308. expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' })
  309. end
  310. log.touch
  311. end
  312. end
  313. context 'when not among the latest 60 Log records' do
  314. before { create_list(:cti_log, 60) }
  315. it 'does NOT push "cti_list_push" event' do
  316. User.with_permissions('cti.agent').each do |u|
  317. expect(Sessions).not_to receive(:send_to).with(u.id, { event: 'cti_list_push' })
  318. end
  319. log.touch
  320. end
  321. end
  322. end
  323. end
  324. end
  325. describe '#from_pretty' do
  326. context 'with complete, E164 international numbers' do
  327. subject(:log) { create(:cti_log, from: '4930609854180') }
  328. it 'gives the number in prettified format' do
  329. expect(log.from_pretty).to eq('+49 30 609854180')
  330. end
  331. end
  332. context 'with private network numbers' do
  333. subject(:log) { create(:cti_log, from: '007') }
  334. it 'gives the number unaltered' do
  335. expect(log.from_pretty).to eq('007')
  336. end
  337. end
  338. end
  339. describe '#to_pretty' do
  340. context 'with complete, E164 international numbers' do
  341. subject(:log) { create(:cti_log, to: '4930609811111') }
  342. it 'gives the number in prettified format' do
  343. expect(log.to_pretty).to eq('+49 30 609811111')
  344. end
  345. end
  346. context 'with private network numbers' do
  347. subject(:log) { create(:cti_log, to: '008') }
  348. it 'gives the number unaltered' do
  349. expect(log.to_pretty).to eq('008')
  350. end
  351. end
  352. end
  353. describe '.queues_of_user' do
  354. context 'without notify_map and no own phone number' do
  355. it 'gives an empty array' do
  356. expect(described_class.queues_of_user(user, Setting.get('cti_config'))).to eq([])
  357. end
  358. end
  359. context 'with notify_map and no own phone number' do
  360. before do
  361. cti_config = Setting.get('cti_config')
  362. cti_config[:notify_map] = [ { queue: 'queue4', user_ids: [user.id.to_s] } ]
  363. Setting.set('cti_config', cti_config)
  364. end
  365. it 'gives an array with queue' do
  366. expect(described_class.queues_of_user(user, Setting.get('cti_config'))).to eq(['queue4'])
  367. end
  368. end
  369. context 'with notify_map and with own phone number' do
  370. let(:phone) { '012345678' }
  371. before do
  372. cti_config = Setting.get('cti_config')
  373. cti_config[:notify_map] = [ { queue: 'queue4', user_ids: [user.id.to_s] } ]
  374. Setting.set('cti_config', cti_config)
  375. end
  376. it 'gives an array with queue and phone number' do
  377. expect(described_class.queues_of_user(user, Setting.get('cti_config'))).to eq(%w[queue4 4912345678])
  378. end
  379. end
  380. end
  381. describe '#best_customer_id_of_log_entry' do
  382. subject(:log1) do
  383. described_class.process(
  384. 'event' => 'newCall',
  385. 'user' => 'user 1',
  386. 'from' => '01234599',
  387. 'to' => '49123450',
  388. 'call_id' => '1',
  389. 'direction' => 'in',
  390. )
  391. end
  392. let!(:agent1) { create(:agent, phone: '01234599') }
  393. let!(:customer2) { create(:customer, phone: '') }
  394. let!(:ticket_article1) { create(:ticket_article, created_by_id: customer2.id, body: 'some text 01234599') }
  395. context 'with agent1 (known), customer1 (known) and customer2 (maybe)' do
  396. let!(:customer1) { create(:customer, phone: '01234599') }
  397. it 'gives customer1' do
  398. expect(log1.best_customer_id_of_log_entry).to eq(customer1.id)
  399. end
  400. end
  401. context 'with agent1 (known) and customer2 (maybe)' do
  402. it 'gives customer2' do
  403. expect(log1.best_customer_id_of_log_entry).to eq(agent1.id)
  404. end
  405. end
  406. end
  407. describe '#to_json' do
  408. it 'includes virtual attributes' do
  409. expect(log.as_json).to include('from_pretty', 'to_pretty')
  410. end
  411. end
  412. end