placetel_spec.rb 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Integration Placetel', type: :request do
  4. let(:agent) do
  5. create(:agent)
  6. end
  7. let!(:customer1) do
  8. create(
  9. :customer,
  10. login: 'ticket-caller_id_cti-customer1@example.com',
  11. firstname: 'CallerId',
  12. lastname: 'Customer1',
  13. phone: '+49 99999 222222',
  14. fax: '+49 99999 222223',
  15. mobile: '+01114100300',
  16. note: 'Phone at home: +49 99999 222224',
  17. )
  18. end
  19. let!(:customer2) do
  20. create(
  21. :customer,
  22. login: 'ticket-caller_id_cti-customer2@example.com',
  23. firstname: 'CallerId',
  24. lastname: 'Customer2',
  25. phone: '+49 99999 222222 2',
  26. )
  27. end
  28. let!(:customer3) do
  29. create(
  30. :customer,
  31. login: 'ticket-caller_id_cti-customer3@example.com',
  32. firstname: 'CallerId',
  33. lastname: 'Customer3',
  34. phone: '+49 99999 222222 2',
  35. )
  36. end
  37. before do
  38. Cti::Log.destroy_all
  39. Setting.set('placetel_integration', true)
  40. Setting.set('placetel_config', {
  41. outbound: {
  42. routing_table: [
  43. {
  44. dest: '41*',
  45. caller_id: '41715880339000',
  46. },
  47. {
  48. dest: '491714000000',
  49. caller_id: '41715880339000',
  50. },
  51. ],
  52. default_caller_id: '4930777000000',
  53. },
  54. inbound: {
  55. block_caller_ids: [
  56. {
  57. caller_id: '491715000000',
  58. note: 'some note',
  59. }
  60. ],
  61. notify_user_ids: {
  62. 2 => true,
  63. 4 => false,
  64. },
  65. }
  66. })
  67. Cti::CallerId.rebuild
  68. end
  69. describe 'request handling' do
  70. it 'does token check' do
  71. params = 'event=IncomingCall&from=01114100300&to=030600000000&call_id=4991155921769858278-1'
  72. post '/api/v1/placetel/not_existing_token', params: params
  73. expect(response).to have_http_status(:unauthorized)
  74. error = nil
  75. local_response = REXML::Document.new(response.body)
  76. local_response.elements.each('Response/Error') do |element|
  77. error = element.text
  78. end
  79. expect(error).to eq('Invalid token, please contact your admin!')
  80. end
  81. it 'does basic call' do
  82. token = Setting.get('placetel_token')
  83. # inbound - I
  84. params = 'event=IncomingCall&from=01114100300&to=030600000000&call_id=4991155921769858278-1'
  85. post "/api/v1/placetel/#{token}", params: params
  86. expect(response).to have_http_status(:ok)
  87. local_response = REXML::Document.new(response.body)
  88. expect(local_response.elements.count).to eq(1)
  89. expect(local_response.elements.first.to_s).to eq('<Response/>')
  90. # inbound - II - block caller
  91. params = 'event=IncomingCall&from=491715000000&to=030600000000&call_id=4991155921769858278-2'
  92. post "/api/v1/placetel/#{token}", params: params
  93. expect(response).to have_http_status(:ok)
  94. local_response = REXML::Document.new(response.body)
  95. reason = nil
  96. local_response.elements.each('Response/Reject') do |element|
  97. reason = element.attributes['reason']
  98. end
  99. expect(reason).to eq('busy')
  100. # outbound - I - set default_caller_id
  101. params = 'event=OutgoingCall&direction=out&from=030600000000&to=01114100300&call_id=8621106404543334274-3'
  102. post "/api/v1/placetel/#{token}", params: params
  103. expect(response).to have_http_status(:ok)
  104. caller_id = nil
  105. number_to_dail = nil
  106. lcoal_response = REXML::Document.new(response.body)
  107. lcoal_response.elements.each('Response/Dial') do |element|
  108. caller_id = element.attributes['callerId']
  109. end
  110. lcoal_response.elements.each('Response/Dial/Number') do |element|
  111. number_to_dail = element.text
  112. end
  113. expect(caller_id).to eq('4930777000000')
  114. expect(number_to_dail).to eq('01114100300')
  115. # outbound - II - set caller_id based on routing_table by explicite number
  116. params = 'event=OutgoingCall&direction=out&from=030600000000&to=491714000000&call_id=8621106404543334274-4'
  117. post "/api/v1/placetel/#{token}", params: params
  118. expect(response).to have_http_status(:ok)
  119. caller_id = nil
  120. number_to_dail = nil
  121. lcoal_response = REXML::Document.new(response.body)
  122. lcoal_response.elements.each('Response/Dial') do |element|
  123. caller_id = element.attributes['callerId']
  124. end
  125. lcoal_response.elements.each('Response/Dial/Number') do |element|
  126. number_to_dail = element.text
  127. end
  128. expect(caller_id).to eq('41715880339000')
  129. expect(number_to_dail).to eq('491714000000')
  130. # outbound - III - set caller_id based on routing_table by 41*
  131. params = 'event=OutgoingCall&direction=out&from=030600000000&to=4147110000000&call_id=8621106404543334274-5'
  132. post "/api/v1/placetel/#{token}", params: params
  133. expect(response).to have_http_status(:ok)
  134. caller_id = nil
  135. number_to_dail = nil
  136. lcoal_response = REXML::Document.new(response.body)
  137. lcoal_response.elements.each('Response/Dial') do |element|
  138. caller_id = element.attributes['callerId']
  139. end
  140. lcoal_response.elements.each('Response/Dial/Number') do |element|
  141. number_to_dail = element.text
  142. end
  143. expect(caller_id).to eq('41715880339000')
  144. expect(number_to_dail).to eq('4147110000000')
  145. # no config
  146. Setting.set('placetel_config', {})
  147. params = 'event=IncomingCall&from=01114100300&to=030600000000&call_id=4991155921769858278-6'
  148. post "/api/v1/placetel/#{token}", params: params
  149. expect(response).to have_http_status(:unprocessable_entity)
  150. error = nil
  151. local_response = REXML::Document.new(response.body)
  152. local_response.elements.each('Response/Error') do |element|
  153. error = element.text
  154. end
  155. expect(error).to eq('Feature not configured, please contact your admin!')
  156. end
  157. it 'does log call' do
  158. token = Setting.get('placetel_token')
  159. # outbound - I - new call
  160. params = 'event=OutgoingCall&direction=out&from=030600000000&to=01114100300&call_id=1234567890-1'
  161. post "/api/v1/placetel/#{token}", params: params
  162. expect(response).to have_http_status(:ok)
  163. log = Cti::Log.find_by(call_id: '1234567890-1')
  164. expect(log).to be_truthy
  165. expect(log.from).to eq('4930777000000')
  166. expect(log.to).to eq('01114100300')
  167. expect(log.direction).to eq('out')
  168. expect(log.from_comment).to be_nil
  169. expect(log.to_comment).to eq('CallerId Customer1')
  170. expect(log.comment).to be_nil
  171. expect(log.state).to eq('newCall')
  172. expect(log.done).to be(true)
  173. expect(log.initialized_at).to be_truthy
  174. expect(log.start_at).to be_nil
  175. expect(log.end_at).to be_nil
  176. expect(log.duration_waiting_time).to be_nil
  177. expect(log.duration_talking_time).to be_nil
  178. travel 1.second
  179. # outbound - I - hangup by agent
  180. params = 'event=HungUp&call_id=1234567890-1&type=missed'
  181. post "/api/v1/placetel/#{token}", params: params
  182. expect(response).to have_http_status(:ok)
  183. log = Cti::Log.find_by(call_id: '1234567890-1')
  184. expect(log).to be_truthy
  185. expect(log.from).to eq('4930777000000')
  186. expect(log.to).to eq('01114100300')
  187. expect(log.direction).to eq('out')
  188. expect(log.from_comment).to be_nil
  189. expect(log.to_comment).to eq('CallerId Customer1')
  190. expect(log.comment).to eq('cancel')
  191. expect(log.state).to eq('hangup')
  192. expect(log.done).to be(true)
  193. expect(log.initialized_at).to be_truthy
  194. expect(log.start_at).to be_nil
  195. expect(log.end_at).to be_truthy
  196. expect(log.duration_waiting_time).to be_truthy
  197. expect(log.duration_talking_time).to be_nil
  198. travel 1.second
  199. # outbound - II - new call
  200. params = 'event=OutgoingCall&direction=out&from=030600000000&to=01114100300&call_id=1234567890-2'
  201. post "/api/v1/placetel/#{token}", params: params
  202. expect(response).to have_http_status(:ok)
  203. log = Cti::Log.find_by(call_id: '1234567890-2')
  204. expect(log).to be_truthy
  205. expect(log.from).to eq('4930777000000')
  206. expect(log.to).to eq('01114100300')
  207. expect(log.direction).to eq('out')
  208. expect(log.from_comment).to be_nil
  209. expect(log.to_comment).to eq('CallerId Customer1')
  210. expect(log.comment).to be_nil
  211. expect(log.state).to eq('newCall')
  212. expect(log.done).to be(true)
  213. expect(log.initialized_at).to be_truthy
  214. expect(log.start_at).to be_nil
  215. expect(log.end_at).to be_nil
  216. expect(log.duration_waiting_time).to be_nil
  217. expect(log.duration_talking_time).to be_nil
  218. travel 1.second
  219. # outbound - II - answer by customer
  220. params = 'event=CallAccepted&call_id=1234567890-2&from=030600000000&to=01114100300'
  221. post "/api/v1/placetel/#{token}", params: params
  222. expect(response).to have_http_status(:ok)
  223. log = Cti::Log.find_by(call_id: '1234567890-2')
  224. expect(log).to be_truthy
  225. expect(log.from).to eq('4930777000000')
  226. expect(log.to).to eq('01114100300')
  227. expect(log.direction).to eq('out')
  228. expect(log.from_comment).to be_nil
  229. expect(log.to_comment).to eq('CallerId Customer1')
  230. expect(log.comment).to be_nil
  231. expect(log.state).to eq('answer')
  232. expect(log.done).to be(true)
  233. expect(log.initialized_at).to be_truthy
  234. expect(log.start_at).to be_truthy
  235. expect(log.end_at).to be_nil
  236. expect(log.duration_waiting_time).to be_truthy
  237. expect(log.duration_talking_time).to be_nil
  238. travel 1.second
  239. # outbound - II - hangup by customer
  240. params = 'event=HungUp&call_id=1234567890-2&type=accepted&from=030600000000&to=01114100300'
  241. post "/api/v1/placetel/#{token}", params: params
  242. expect(response).to have_http_status(:ok)
  243. log = Cti::Log.find_by(call_id: '1234567890-2')
  244. expect(log).to be_truthy
  245. expect(log.from).to eq('4930777000000')
  246. expect(log.to).to eq('01114100300')
  247. expect(log.direction).to eq('out')
  248. expect(log.from_comment).to be_nil
  249. expect(log.to_comment).to eq('CallerId Customer1')
  250. expect(log.comment).to eq('normalClearing')
  251. expect(log.state).to eq('hangup')
  252. expect(log.done).to be(true)
  253. expect(log.initialized_at).to be_truthy
  254. expect(log.start_at).to be_truthy
  255. expect(log.end_at).to be_truthy
  256. expect(log.duration_waiting_time).to be_truthy
  257. expect(log.duration_talking_time).to be_truthy
  258. travel 1.second
  259. # inbound - I - new call
  260. params = 'event=IncomingCall&to=030600000000&from=01114100300&call_id=1234567890-3'
  261. post "/api/v1/placetel/#{token}", params: params
  262. expect(response).to have_http_status(:ok)
  263. log = Cti::Log.find_by(call_id: '1234567890-3')
  264. expect(log).to be_truthy
  265. expect(log.to).to eq('030600000000')
  266. expect(log.from).to eq('01114100300')
  267. expect(log.direction).to eq('in')
  268. expect(log.to_comment).to be_nil
  269. expect(log.from_comment).to eq('CallerId Customer1')
  270. expect(log.comment).to be_nil
  271. expect(log.state).to eq('newCall')
  272. expect(log.done).to be(false)
  273. expect(log.initialized_at).to be_truthy
  274. expect(log.start_at).to be_nil
  275. expect(log.end_at).to be_nil
  276. expect(log.duration_waiting_time).to be_nil
  277. expect(log.duration_talking_time).to be_nil
  278. travel 1.second
  279. # inbound - I - answer by customer
  280. params = 'event=CallAccepted&call_id=1234567890-3&to=030600000000&from=01114100300'
  281. post "/api/v1/placetel/#{token}", params: params
  282. expect(response).to have_http_status(:ok)
  283. log = Cti::Log.find_by(call_id: '1234567890-3')
  284. expect(log).to be_truthy
  285. expect(log.to).to eq('030600000000')
  286. expect(log.from).to eq('01114100300')
  287. expect(log.direction).to eq('in')
  288. expect(log.to_comment).to be_nil
  289. expect(log.from_comment).to eq('CallerId Customer1')
  290. expect(log.comment).to be_nil
  291. expect(log.state).to eq('answer')
  292. expect(log.done).to be(true)
  293. expect(log.initialized_at).to be_truthy
  294. expect(log.start_at).to be_truthy
  295. expect(log.end_at).to be_nil
  296. expect(log.duration_waiting_time).to be_truthy
  297. expect(log.duration_talking_time).to be_nil
  298. travel 1.second
  299. # inbound - I - hangup by customer
  300. params = 'event=HungUp&call_id=1234567890-3&type=accepted&to=030600000000&from=01114100300'
  301. post "/api/v1/placetel/#{token}", params: params
  302. expect(response).to have_http_status(:ok)
  303. log = Cti::Log.find_by(call_id: '1234567890-3')
  304. expect(log).to be_truthy
  305. expect(log.to).to eq('030600000000')
  306. expect(log.from).to eq('01114100300')
  307. expect(log.direction).to eq('in')
  308. expect(log.to_comment).to be_nil
  309. expect(log.from_comment).to eq('CallerId Customer1')
  310. expect(log.comment).to eq('normalClearing')
  311. expect(log.state).to eq('hangup')
  312. expect(log.done).to be(true)
  313. expect(log.initialized_at).to be_truthy
  314. expect(log.start_at).to be_truthy
  315. expect(log.end_at).to be_truthy
  316. expect(log.duration_waiting_time).to be_truthy
  317. expect(log.duration_talking_time).to be_truthy
  318. travel 1.second
  319. # inbound - II - new call
  320. params = 'event=IncomingCall&to=030600000000&from=01114100300&call_id=1234567890-4'
  321. post "/api/v1/placetel/#{token}", params: params
  322. expect(response).to have_http_status(:ok)
  323. log = Cti::Log.find_by(call_id: '1234567890-4')
  324. expect(log).to be_truthy
  325. expect(log.to).to eq('030600000000')
  326. expect(log.from).to eq('01114100300')
  327. expect(log.direction).to eq('in')
  328. expect(log.to_comment).to be_nil
  329. expect(log.from_comment).to eq('CallerId Customer1')
  330. expect(log.comment).to be_nil
  331. expect(log.state).to eq('newCall')
  332. expect(log.done).to be(false)
  333. expect(log.initialized_at).to be_truthy
  334. expect(log.start_at).to be_nil
  335. expect(log.end_at).to be_nil
  336. expect(log.duration_waiting_time).to be_nil
  337. expect(log.duration_talking_time).to be_nil
  338. travel 1.second
  339. # inbound - II - answer by voicemail
  340. params = 'event=CallAccepted&call_id=1234567890-4&to=030600000000&from=01114100300&user=voicemail'
  341. post "/api/v1/placetel/#{token}", params: params
  342. expect(response).to have_http_status(:ok)
  343. log = Cti::Log.find_by(call_id: '1234567890-4')
  344. expect(log).to be_truthy
  345. expect(log.to).to eq('030600000000')
  346. expect(log.from).to eq('01114100300')
  347. expect(log.direction).to eq('in')
  348. expect(log.to_comment).to eq('voicemail')
  349. expect(log.from_comment).to eq('CallerId Customer1')
  350. expect(log.comment).to be_nil
  351. expect(log.state).to eq('answer')
  352. expect(log.done).to be(true)
  353. expect(log.initialized_at).to be_truthy
  354. expect(log.start_at).to be_truthy
  355. expect(log.end_at).to be_nil
  356. expect(log.duration_waiting_time).to be_truthy
  357. expect(log.duration_talking_time).to be_nil
  358. travel 1.second
  359. # inbound - II - hangup by customer
  360. params = 'event=HungUp&call_id=1234567890-4&type=accepted&to=030600000000&from=01114100300'
  361. post "/api/v1/placetel/#{token}", params: params
  362. expect(response).to have_http_status(:ok)
  363. log = Cti::Log.find_by(call_id: '1234567890-4')
  364. expect(log).to be_truthy
  365. expect(log.to).to eq('030600000000')
  366. expect(log.from).to eq('01114100300')
  367. expect(log.direction).to eq('in')
  368. expect(log.to_comment).to eq('voicemail')
  369. expect(log.from_comment).to eq('CallerId Customer1')
  370. expect(log.comment).to eq('normalClearing')
  371. expect(log.state).to eq('hangup')
  372. expect(log.done).to be(false)
  373. expect(log.initialized_at).to be_truthy
  374. expect(log.start_at).to be_truthy
  375. expect(log.end_at).to be_truthy
  376. expect(log.duration_waiting_time).to be_truthy
  377. expect(log.duration_talking_time).to be_truthy
  378. travel 1.second
  379. # inbound - III - new call
  380. params = 'event=IncomingCall&to=030600000000&from=01114100300&call_id=1234567890-5'
  381. post "/api/v1/placetel/#{token}", params: params
  382. expect(response).to have_http_status(:ok)
  383. log = Cti::Log.find_by(call_id: '1234567890-5')
  384. expect(log).to be_truthy
  385. expect(log.to).to eq('030600000000')
  386. expect(log.from).to eq('01114100300')
  387. expect(log.direction).to eq('in')
  388. expect(log.to_comment).to be_nil
  389. expect(log.from_comment).to eq('CallerId Customer1')
  390. expect(log.comment).to be_nil
  391. expect(log.state).to eq('newCall')
  392. expect(log.done).to be(false)
  393. expect(log.initialized_at).to be_truthy
  394. expect(log.start_at).to be_nil
  395. expect(log.end_at).to be_nil
  396. expect(log.duration_waiting_time).to be_nil
  397. expect(log.duration_talking_time).to be_nil
  398. travel 1.second
  399. # inbound - III - hangup by customer
  400. params = 'event=HungUp&call_id=1234567890-5&type=accepted&to=030600000000&from=01114100300'
  401. post "/api/v1/placetel/#{token}", params: params
  402. expect(response).to have_http_status(:ok)
  403. log = Cti::Log.find_by(call_id: '1234567890-5')
  404. expect(log).to be_truthy
  405. expect(log.to).to eq('030600000000')
  406. expect(log.from).to eq('01114100300')
  407. expect(log.direction).to eq('in')
  408. expect(log.to_comment).to be_nil
  409. expect(log.from_comment).to eq('CallerId Customer1')
  410. expect(log.comment).to eq('normalClearing')
  411. expect(log.state).to eq('hangup')
  412. expect(log.done).to be(false)
  413. expect(log.initialized_at).to be_truthy
  414. expect(log.start_at).to be_nil
  415. expect(log.end_at).to be_truthy
  416. expect(log.duration_waiting_time).to be_truthy
  417. expect(log.duration_talking_time).to be_nil
  418. travel 1.second
  419. # inbound - IV - new call
  420. params = 'event=IncomingCall&to=030600000000&from=49999992222222&call_id=1234567890-6'
  421. post "/api/v1/placetel/#{token}", params: params
  422. expect(response).to have_http_status(:ok)
  423. log = Cti::Log.find_by(call_id: '1234567890-6')
  424. expect(log).to be_truthy
  425. expect(log.to).to eq('030600000000')
  426. expect(log.from).to eq('49999992222222')
  427. expect(log.direction).to eq('in')
  428. expect(log.to_comment).to be_nil
  429. expect(log.from_comment).to eq('CallerId Customer3,CallerId Customer2')
  430. expect(log.preferences['to']).to be_falsey
  431. expect(log.preferences['from']).to be_truthy
  432. expect(log.comment).to be_nil
  433. expect(log.state).to eq('newCall')
  434. expect(log.done).to be(false)
  435. expect(log.initialized_at).to be_truthy
  436. expect(log.start_at).to be_nil
  437. expect(log.end_at).to be_nil
  438. expect(log.duration_waiting_time).to be_nil
  439. expect(log.duration_talking_time).to be_nil
  440. travel 1.second
  441. # inbound - IV - new call
  442. params = 'event=IncomingCall&to=030600000000&from=anonymous&call_id=1234567890-7'
  443. post "/api/v1/placetel/#{token}", params: params
  444. expect(response).to have_http_status(:ok)
  445. log = Cti::Log.find_by(call_id: '1234567890-7')
  446. expect(log).to be_truthy
  447. expect(log.to).to eq('030600000000')
  448. expect(log.from).to eq('anonymous')
  449. expect(log.direction).to eq('in')
  450. expect(log.to_comment).to be_nil
  451. expect(log.from_comment).to be_nil
  452. expect(log.preferences['to']).to be_falsey
  453. expect(log.preferences['from']).to be_falsey
  454. expect(log.comment).to be_nil
  455. expect(log.state).to eq('newCall')
  456. expect(log.done).to be(false)
  457. expect(log.initialized_at).to be_truthy
  458. expect(log.start_at).to be_nil
  459. expect(log.end_at).to be_nil
  460. expect(log.duration_waiting_time).to be_nil
  461. expect(log.duration_talking_time).to be_nil
  462. travel 1.second
  463. # inbound - IIV - new call
  464. params = 'event=IncomingCall&to=030600000000&from=anonymous&call_id=1234567890-8'
  465. post "/api/v1/placetel/#{token}", params: params
  466. expect(response).to have_http_status(:ok)
  467. log = Cti::Log.find_by(call_id: '1234567890-8')
  468. expect(log).to be_truthy
  469. expect(log.to).to eq('030600000000')
  470. expect(log.from).to eq('anonymous')
  471. expect(log.direction).to eq('in')
  472. expect(log.to_comment).to be_nil
  473. expect(log.from_comment).to be_nil
  474. expect(log.preferences['to']).to be_falsey
  475. expect(log.preferences['from']).to be_falsey
  476. expect(log.comment).to be_nil
  477. expect(log.state).to eq('newCall')
  478. expect(log.done).to be(false)
  479. expect(log.initialized_at).to be_truthy
  480. expect(log.start_at).to be_nil
  481. expect(log.end_at).to be_nil
  482. expect(log.duration_waiting_time).to be_nil
  483. expect(log.duration_talking_time).to be_nil
  484. travel 1.second
  485. # inbound - IIV - hangup by anonymous
  486. params = 'event=HungUp&call_id=1234567890-8&type=accepted&to=030600000000&from=anonymous'
  487. post "/api/v1/placetel/#{token}", params: params
  488. expect(response).to have_http_status(:ok)
  489. log = Cti::Log.find_by(call_id: '1234567890-8')
  490. expect(log).to be_truthy
  491. expect(log.to).to eq('030600000000')
  492. expect(log.from).to eq('anonymous')
  493. expect(log.direction).to eq('in')
  494. expect(log.to_comment).to be_nil
  495. expect(log.from_comment).to be_nil
  496. expect(log.comment).to eq('normalClearing')
  497. expect(log.state).to eq('hangup')
  498. expect(log.done).to be(false)
  499. expect(log.initialized_at).to be_truthy
  500. expect(log.start_at).to be_nil
  501. expect(log.end_at).to be_truthy
  502. expect(log.duration_waiting_time).to be_truthy
  503. expect(log.duration_talking_time).to be_nil
  504. # get caller list
  505. get '/api/v1/cti/log'
  506. expect(response).to have_http_status(:forbidden)
  507. authenticated_as(agent)
  508. get '/api/v1/cti/log', as: :json
  509. expect(response).to have_http_status(:ok)
  510. expect(json_response['list']).to be_a(Array)
  511. expect(json_response['list'].count).to eq(8)
  512. expect(json_response['assets']).to be_truthy
  513. expect(json_response['assets']['User']).to be_truthy
  514. expect(json_response['assets']['User'][customer2.id.to_s]).to be_truthy
  515. expect(json_response['assets']['User'][customer3.id.to_s]).to be_truthy
  516. expect(json_response['list'][0]['call_id']).to eq('1234567890-8')
  517. expect(json_response['list'][1]['call_id']).to eq('1234567890-7')
  518. expect(json_response['list'][2]['call_id']).to eq('1234567890-6')
  519. expect(json_response['list'][3]['call_id']).to eq('1234567890-5')
  520. expect(json_response['list'][4]['call_id']).to eq('1234567890-4')
  521. expect(json_response['list'][5]['call_id']).to eq('1234567890-3')
  522. expect(json_response['list'][6]['call_id']).to eq('1234567890-2')
  523. expect(json_response['list'][6]['state']).to eq('hangup')
  524. expect(json_response['list'][6]['from']).to eq('4930777000000')
  525. expect(json_response['list'][6]['from_comment']).to be_nil
  526. expect(json_response['list'][6]['to']).to eq('01114100300')
  527. expect(json_response['list'][6]['to_comment']).to eq('CallerId Customer1')
  528. expect(json_response['list'][6]['comment']).to eq('normalClearing')
  529. expect(json_response['list'][6]['state']).to eq('hangup')
  530. expect(json_response['list'][7]['call_id']).to eq('1234567890-1')
  531. end
  532. it 'does log call with peer' do
  533. token = Setting.get('placetel_token')
  534. # outbound - I - new call
  535. params = 'event=OutgoingCall&direction=out&from=030600000000&to=01114100300&call_id=1234567890-1&peer=something@example.com'
  536. post "/api/v1/placetel/#{token}", params: params
  537. expect(response).to have_http_status(:ok)
  538. log = Cti::Log.find_by(call_id: '1234567890-1')
  539. expect(log).to be_truthy
  540. expect(log.from).to eq('4930777000000')
  541. expect(log.to).to eq('01114100300')
  542. expect(log.direction).to eq('out')
  543. expect(log.from_comment).to be_nil
  544. expect(log.to_comment).to eq('CallerId Customer1')
  545. expect(log.comment).to be_nil
  546. expect(log.state).to eq('newCall')
  547. expect(log.done).to be(true)
  548. expect(log.initialized_at).to be_truthy
  549. expect(log.start_at).to be_nil
  550. expect(log.end_at).to be_nil
  551. expect(log.duration_waiting_time).to be_nil
  552. expect(log.duration_talking_time).to be_nil
  553. config = Setting.get('placetel_config')
  554. config[:api_token] = '123'
  555. config[:outbound][:default_caller_id] = ''
  556. Setting.set('placetel_config', config)
  557. stub_request(:get, Cti::Driver::Placetel::PLACETEL_SIP_USERS_URL)
  558. .to_return(status: 200, body: [{ 'callerid' => '03055571600', 'did' => 10, 'name' => 'Bob Smith', 'type' => 'standard', 'sipuid' => '777008478072@example.com' }, { 'callerid' => '03055571600', 'did' => 12, 'name' => 'Josef Müller', 'type' => 'standard', 'sipuid' => '777042617425@example.com' }].to_json)
  559. params = 'event=OutgoingCall&direction=out&to=099999222222&call_id=1234567890-2&from=777008478072@example.com'
  560. post "/api/v1/placetel/#{token}", params: params
  561. expect(response).to have_http_status(:ok)
  562. log = Cti::Log.find_by(call_id: '1234567890-2')
  563. expect(log).to be_truthy
  564. expect(log.from).to eq('777008478072@example.com')
  565. expect(log.to).to eq('099999222222')
  566. expect(log.direction).to eq('out')
  567. expect(log.from_comment).to eq('Bob Smith')
  568. expect(log.to_comment).to eq('CallerId Customer1')
  569. expect(log.comment).to be_nil
  570. expect(log.state).to eq('newCall')
  571. expect(log.done).to be(true)
  572. expect(log.initialized_at).to be_truthy
  573. expect(log.start_at).to be_nil
  574. expect(log.end_at).to be_nil
  575. expect(log.duration_waiting_time).to be_nil
  576. expect(log.duration_talking_time).to be_nil
  577. # check if cache is filled
  578. expect(Rails.cache.read('placetelGetVoipUsers')['777008478072@example.com']).to eq('Bob Smith')
  579. params = 'event=IncomingCall&direction=in&to=030600000000&from=012345&call_id=1234567890-3&peer=777008478072@example.com'
  580. post "/api/v1/placetel/#{token}", params: params
  581. expect(response).to have_http_status(:ok)
  582. log = Cti::Log.find_by(call_id: '1234567890-3')
  583. expect(log).to be_truthy
  584. expect(log.from).to eq('012345')
  585. expect(log.to).to eq('030600000000')
  586. expect(log.direction).to eq('in')
  587. expect(log.from_comment).to be_nil
  588. expect(log.to_comment).to eq('Bob Smith')
  589. expect(log.comment).to be_nil
  590. expect(log.state).to eq('newCall')
  591. expect(log.done).to be(false)
  592. expect(log.initialized_at).to be_truthy
  593. expect(log.start_at).to be_nil
  594. expect(log.end_at).to be_nil
  595. expect(log.duration_waiting_time).to be_nil
  596. expect(log.duration_talking_time).to be_nil
  597. # check if cache is filled
  598. expect(Rails.cache.read('placetelGetVoipUsers')['777008478072@example.com']).to eq('Bob Smith')
  599. end
  600. it 'does catch failed sip user response with http error status' do
  601. stub_request(:get, Cti::Driver::Placetel::PLACETEL_SIP_USERS_URL).to_return(status: 400, body: '')
  602. expect(Cti::Driver::Placetel.new({ params: { event: 'test_event' } }).load_voip_users).to eq({})
  603. end
  604. it 'does catch failed sip user response with empty body' do
  605. stub_request(:get, Cti::Driver::Placetel::PLACETEL_SIP_USERS_URL).to_return(status: 200, body: {}.to_json)
  606. expect(Cti::Driver::Placetel.new({ params: { event: 'test_event' } }).load_voip_users).to eq({})
  607. end
  608. it 'does catch failed sip user response with failed result state in body' do
  609. stub_request(:get, Cti::Driver::Placetel::PLACETEL_SIP_USERS_URL).to_return(status: 200, body: { result: '-1' }.to_json)
  610. expect(Cti::Driver::Placetel.new({ params: { event: 'test_event' } }).load_voip_users).to eq({})
  611. end
  612. it 'does catch failed sip user response with invalid response format' do
  613. stub_request(:get, Cti::Driver::Placetel::PLACETEL_SIP_USERS_URL).to_return(status: 200, body: { foo: 123 }.to_json)
  614. expect(Cti::Driver::Placetel.new({ params: { event: 'test_event' } }).load_voip_users).to eq({})
  615. end
  616. end
  617. end