placetel_spec.rb 25 KB

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