cti_spec.rb 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Caller log', authenticated_as: :authenticate, type: :system do
  4. let(:agent_phone) { '0190111' }
  5. let(:customer_phone) { '0190333' }
  6. let(:cti_token) { 'token1234' }
  7. let(:agent) { create(:agent, phone: agent_phone) }
  8. let(:customer) { create(:customer, phone: customer_phone) }
  9. let(:cti_on) { true }
  10. let(:params) do
  11. {
  12. direction: 'in',
  13. from: customer.phone,
  14. to: agent_phone,
  15. callId: '111',
  16. cause: 'busy',
  17. }
  18. end
  19. let(:first_params) { params.merge(event: 'newCall') }
  20. let(:second_params) { params.merge(event: 'hangup') }
  21. let(:visit_cti) do
  22. visit 'cti'
  23. ensure_websocket
  24. end
  25. let(:place_call) do
  26. post "#{Capybara.app_host}/api/v1/cti/#{cti_token}", params: first_params
  27. post "#{Capybara.app_host}/api/v1/cti/#{cti_token}", params: second_params
  28. end
  29. # Do the preperation before the authentication.
  30. def authenticate
  31. Setting.set('cti_integration', cti_on)
  32. Setting.set('cti_token', cti_token)
  33. agent
  34. end
  35. context 'when cti integration is on' do
  36. it 'shows the phone menu in nav bar' do
  37. visit '/'
  38. ensure_websocket
  39. within '#navigation .menu' do
  40. place_call
  41. expect(page).to have_link('Phone', href: '#cti')
  42. end
  43. end
  44. context 'when agent is on the phone' do
  45. let(:agent) do
  46. create(:agent, phone: agent_phone).tap do |user|
  47. user.preferences[:cti] = true
  48. user.save!
  49. end
  50. end
  51. let(:cti_log) do
  52. create(:cti_log,
  53. direction: 'in',
  54. from: customer.phone,
  55. preferences: { from: [{ user_id: customer.id }] })
  56. end
  57. before { cti_log }
  58. it 'shows call and opens user profile on click' do
  59. visit '/'
  60. within '.call-widgets .user-card' do
  61. click_on customer.fullname
  62. end
  63. expect(current_url).to end_with("user/profile/#{customer.id}")
  64. end
  65. end
  66. end
  67. context 'when cti integration is not on' do
  68. let(:cti_on) { false }
  69. it 'does not show the phone menu in nav bar' do
  70. visit '/'
  71. within '#navigation .menu' do
  72. expect(page).to have_no_link('Phone', href: '#cti')
  73. end
  74. end
  75. end
  76. context 'when a customer call is answered' do
  77. let(:second_params) { params.merge(event: 'answer', answeringNumber: agent_phone) }
  78. context 'with known customer and without active tickets' do
  79. before do
  80. travel(-2.months)
  81. create(:ticket, customer: customer)
  82. travel_back
  83. visit_cti
  84. place_call
  85. end
  86. it 'opens a new ticket after phone call inbound' do
  87. within(:active_content) do
  88. expect(page).to have_text('New Ticket')
  89. expect(page).to have_css('input[name="title"][value="Call from 0190333"]', visible: :all)
  90. expect(page).to have_css('.tabsSidebar-tab[data-tab="customer"]', visible: :all)
  91. expect(page).to have_css("input[name=customer_id][value='#{customer.id}']", visible: :hide)
  92. expect(find('[name=customer_id_completion]').value).to eq "#{customer.fullname} <#{customer.email}>"
  93. end
  94. end
  95. end
  96. context 'without known customer and without active tickets' do
  97. let(:first_params) { params.merge(event: 'newCall', direction: 'out', from: '001', to: '002') }
  98. let(:second_params) { params.merge(event: 'answer', answeringNumber: agent_phone) }
  99. before do
  100. visit_cti
  101. place_call
  102. end
  103. it 'opens a new ticket after phone call inbound' do
  104. within(:active_content) do
  105. expect(page).to have_text('New Ticket')
  106. expect(page).to have_css("input[name='title'][value='Call from 0190333']", visible: :all)
  107. expect(page).to have_no_css('.tabsSidebar-tab[data-tab="customer"]')
  108. expect(find('[name=customer_id_completion]').value).to eq ''
  109. end
  110. end
  111. end
  112. context 'with known customer and with active tickets' do
  113. before do
  114. create(:ticket, customer: customer)
  115. visit_cti
  116. place_call
  117. end
  118. it 'opens the customer profile screen after phone call inbound with tickets in the last month' do
  119. within(:active_content) do
  120. expect(page).to have_text(customer.fullname)
  121. end
  122. end
  123. end
  124. context 'with phone number only known customer and without active tickets' do
  125. let(:customer_phone) { '0190444' }
  126. let(:customer) { create(:customer, phone: customer_phone, email: nil, firstname: nil, lastname: nil) }
  127. before do
  128. travel(-2.months)
  129. create(:ticket, customer: customer)
  130. travel_back
  131. visit_cti
  132. place_call
  133. end
  134. it 'opens a new ticket after phone call inbound' do
  135. within(:active_content) do
  136. expect(page).to have_text('New Ticket')
  137. expect(page).to have_css('input[name="title"][value="Call from 0190444"]', visible: :all)
  138. expect(page).to have_css('.tabsSidebar-tab[data-tab="customer"]', visible: :all)
  139. expect(page).to have_css("input[name=customer_id][value='#{customer.id}']", visible: :hide)
  140. expect(find('[name=customer_id_completion]').value).to eq '0190444'
  141. end
  142. end
  143. end
  144. end
  145. context 'with incoming call' do
  146. before do
  147. visit_cti
  148. place_call
  149. end
  150. it 'increments the call counter notification badge' do
  151. within '[href="#cti"].js-phoneMenuItem' do
  152. counter = find('.counter')
  153. expect(counter).to have_content 1
  154. end
  155. end
  156. end
  157. context 'when incoming call is checked' do
  158. before do
  159. visit_cti
  160. place_call
  161. end
  162. it 'clears the call counter notification badge' do
  163. within :active_content do
  164. find('.table-checkbox input.js-check', visible: :all).check allow_label_click: true
  165. end
  166. within '[href="#cti"].js-phoneMenuItem' do
  167. expect(page).to have_no_selector('.counter')
  168. end
  169. end
  170. end
  171. # Regression test for #2018
  172. context 'phone numbers format' do
  173. before do
  174. visit_cti
  175. place_call
  176. end
  177. context 'with private number' do
  178. let(:customer_phone) { '007' }
  179. let(:agent_phone) { '008' }
  180. it 'appears verbatim' do
  181. within :active_content do
  182. expect(page).to have_css('.js-callerLog', text: customer_phone)
  183. .and have_css('.js-callerLog', text: agent_phone)
  184. end
  185. end
  186. end
  187. context 'with e164 number' do
  188. let(:customer_phone) { '4930609854180' }
  189. let(:agent_phone) { '4930609811111' }
  190. let(:prettified_customer_phone) { '+49 30 609854180' }
  191. let(:prettified_current_user_phone) { '+49 30 609811111' }
  192. it 'appears prettified' do
  193. within :active_content do
  194. expect(page).to have_css('.js-callerLog', text: prettified_customer_phone)
  195. .and have_css('.js-callerLog', text: prettified_current_user_phone)
  196. end
  197. end
  198. it 'done not appear verbatim' do
  199. within :active_content do
  200. expect(page).to have_no_selector('.js-callerLog', text: customer_phone)
  201. end
  202. end
  203. end
  204. end
  205. # Regression test for #2096
  206. context 'with inactive user' do
  207. before do
  208. visit_cti
  209. place_call
  210. end
  211. let(:customer) do
  212. create(:customer,
  213. phone: customer_phone,
  214. active: false,
  215. firstname: 'John',
  216. lastname: 'Doe')
  217. end
  218. it 'appears inactive' do
  219. within :active_content do
  220. expect(page).to have_css('span.avatar--inactive', text: 'JD')
  221. end
  222. end
  223. end
  224. # Regression test for #2075
  225. context 'when user is with organization name' do
  226. before do
  227. visit_cti
  228. place_call
  229. end
  230. let(:firstname) { 'John' }
  231. let(:lastname) { 'Doe' }
  232. let(:organization_name) { 'Test Organization' }
  233. let(:organization) { create(:organization, name: organization_name) }
  234. let(:full_name) { "#{firstname} #{lastname}" }
  235. let(:customer) do
  236. create(:customer,
  237. phone: customer_phone,
  238. firstname: firstname,
  239. lastname: lastname,
  240. organization: organization)
  241. end
  242. shared_examples 'showing user with thier organization name' do
  243. it 'shows user with thier organization name' do
  244. within :active_content do
  245. expect(page).to have_css(
  246. '.js-callerLog tr div.user-popover',
  247. text: "#{full_name} (#{organization_name})"
  248. )
  249. end
  250. end
  251. end
  252. context 'with call direction out' do
  253. let(:first_params) { params.merge(event: 'newCall', direction: 'out', from: agent_phone, to: customer.phone) }
  254. let(:second_params) { params.merge(event: 'hangup', direction: 'out', from: agent_phone, to: customer.phone) }
  255. it_behaves_like 'showing user with thier organization name'
  256. end
  257. context 'with call direction in' do
  258. let(:first_params) { params.merge(event: 'newCall', direction: 'in') }
  259. let(:second_params) { params.merge(event: 'hangup', direction: 'in') }
  260. it_behaves_like 'showing user with thier organization name'
  261. end
  262. end
  263. end