cti_spec.rb 9.1 KB

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