cti_spec.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. end
  92. end
  93. end
  94. context 'without known customer and without active tickets' do
  95. let(:first_params) { params.merge(event: 'newCall', direction: 'out', from: '001', to: '002') }
  96. let(:second_params) { params.merge(event: 'answer', answeringNumber: agent_phone) }
  97. before do
  98. visit_cti
  99. place_call
  100. end
  101. it 'opens a new ticket after phone call inbound' do
  102. within(:active_content) do
  103. expect(page).to have_text('New Ticket')
  104. expect(page).to have_css("input[name='title'][value='Call from 0190333']", visible: :all)
  105. expect(page).to have_no_css('.tabsSidebar-tab[data-tab="customer"]')
  106. end
  107. end
  108. end
  109. context 'with known customer and with active tickets' do
  110. before do
  111. create(:ticket, customer: customer)
  112. visit_cti
  113. place_call
  114. end
  115. it 'opens the customer profile screen after phone call inbound with tickets in the last month' do
  116. within(:active_content) do
  117. expect(page).to have_text(customer.fullname)
  118. end
  119. end
  120. end
  121. end
  122. context 'with incoming call' do
  123. before do
  124. visit_cti
  125. place_call
  126. end
  127. it 'increments the call counter notification badge' do
  128. within '[href="#cti"].js-phoneMenuItem' do
  129. counter = find('.counter')
  130. expect(counter).to have_content 1
  131. end
  132. end
  133. end
  134. context 'when incoming call is checked' do
  135. before do
  136. visit_cti
  137. place_call
  138. end
  139. it 'clears the call counter notification badge' do
  140. within :active_content do
  141. find('.table-checkbox input.js-check', visible: :all).check allow_label_click: true
  142. end
  143. within '[href="#cti"].js-phoneMenuItem' do
  144. expect(page).to have_no_selector('.counter')
  145. end
  146. end
  147. end
  148. # Regression test for #2018
  149. context 'phone numbers format' do
  150. before do
  151. visit_cti
  152. place_call
  153. end
  154. context 'with private number' do
  155. let(:customer_phone) { '007' }
  156. let(:agent_phone) { '008' }
  157. it 'appears verbatim' do
  158. within :active_content do
  159. expect(page).to have_css('.js-callerLog', text: customer_phone)
  160. .and have_css('.js-callerLog', text: agent_phone)
  161. end
  162. end
  163. end
  164. context 'with e164 number' do
  165. let(:customer_phone) { '4930609854180' }
  166. let(:agent_phone) { '4930609811111' }
  167. let(:prettified_customer_phone) { '+49 30 609854180' }
  168. let(:prettified_current_user_phone) { '+49 30 609811111' }
  169. it 'appears prettified' do
  170. within :active_content do
  171. expect(page).to have_css('.js-callerLog', text: prettified_customer_phone)
  172. .and have_css('.js-callerLog', text: prettified_current_user_phone)
  173. end
  174. end
  175. it 'done not appear verbatim' do
  176. within :active_content do
  177. expect(page).to have_no_selector('.js-callerLog', text: customer_phone)
  178. end
  179. end
  180. end
  181. end
  182. # Regression test for #2096
  183. context 'with inactive user' do
  184. before do
  185. visit_cti
  186. place_call
  187. end
  188. let(:customer) do
  189. create(:customer,
  190. phone: customer_phone,
  191. active: false,
  192. firstname: 'John',
  193. lastname: 'Doe')
  194. end
  195. it 'appears inactive' do
  196. within :active_content do
  197. expect(page).to have_css('span.avatar--inactive', text: 'JD')
  198. end
  199. end
  200. end
  201. # Regression test for #2075
  202. context 'when user is with organization name' do
  203. before do
  204. visit_cti
  205. place_call
  206. end
  207. let(:firstname) { 'John' }
  208. let(:lastname) { 'Doe' }
  209. let(:organization_name) { 'Test Organization' }
  210. let(:organization) { create(:organization, name: organization_name) }
  211. let(:full_name) { "#{firstname} #{lastname}" }
  212. let(:customer) do
  213. create(:customer,
  214. phone: customer_phone,
  215. firstname: firstname,
  216. lastname: lastname,
  217. organization: organization)
  218. end
  219. shared_examples 'showing user with thier organization name' do
  220. it 'shows user with thier organization name' do
  221. within :active_content do
  222. expect(page).to have_css(
  223. '.js-callerLog tr div.user-popover',
  224. text: "#{full_name} (#{organization_name})"
  225. )
  226. end
  227. end
  228. end
  229. context 'with call direction out' do
  230. let(:first_params) { params.merge(event: 'newCall', direction: 'out', from: agent_phone, to: customer.phone) }
  231. let(:second_params) { params.merge(event: 'hangup', direction: 'out', from: agent_phone, to: customer.phone) }
  232. it_behaves_like 'showing user with thier organization name'
  233. end
  234. context 'with call direction in' do
  235. let(:first_params) { params.merge(event: 'newCall', direction: 'in') }
  236. let(:second_params) { params.merge(event: 'hangup', direction: 'in') }
  237. it_behaves_like 'showing user with thier organization name'
  238. end
  239. end
  240. end