sipgate_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. require 'rails_helper'
  2. RSpec.describe 'Integration Sipgate', type: :request do
  3. let(:agent) do
  4. create(:agent)
  5. end
  6. let!(:customer1) do
  7. create(
  8. :customer,
  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: '+4912347114711',
  15. note: 'Phone at home: +49 99999 222224',
  16. )
  17. end
  18. let!(:customer2) do
  19. create(
  20. :customer,
  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!(:customer3) do
  28. create(
  29. :customer,
  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('sipgate_integration', true)
  39. Setting.set('sipgate_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 basic call' do
  70. # inbound - I
  71. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  72. post '/api/v1/sipgate/in', params: params
  73. expect(@response).to have_http_status(:ok)
  74. on_hangup = nil
  75. on_answer = nil
  76. content = @response.body
  77. response = REXML::Document.new(content)
  78. response.elements.each('Response') do |element|
  79. on_hangup = element.attributes['onHangup']
  80. on_answer = element.attributes['onAnswer']
  81. end
  82. expect(on_hangup).to eq('http://zammad.example.com/api/v1/sipgate/in')
  83. expect(on_answer).to eq('http://zammad.example.com/api/v1/sipgate/in')
  84. # inbound - II - block caller
  85. params = 'event=newCall&direction=in&from=491715000000&to=4930600000000&callId=4991155921769858278-2&user%5B%5D=user+1&user%5B%5D=user+2'
  86. post '/api/v1/sipgate/in', params: params
  87. expect(@response).to have_http_status(:ok)
  88. on_hangup = nil
  89. on_answer = nil
  90. content = @response.body
  91. response = REXML::Document.new(content)
  92. response.elements.each('Response') do |element|
  93. on_hangup = element.attributes['onHangup']
  94. on_answer = element.attributes['onAnswer']
  95. end
  96. expect(on_hangup).to eq('http://zammad.example.com/api/v1/sipgate/in')
  97. expect(on_answer).to eq('http://zammad.example.com/api/v1/sipgate/in')
  98. reason = nil
  99. response.elements.each('Response/Reject') do |element|
  100. reason = element.attributes['reason']
  101. end
  102. expect(reason).to eq('busy')
  103. # outbound - I - set default_caller_id
  104. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=8621106404543334274-3&user%5B%5D=user+1'
  105. post '/api/v1/sipgate/out', params: params
  106. expect(@response).to have_http_status(:ok)
  107. on_hangup = nil
  108. on_answer = nil
  109. caller_id = nil
  110. number_to_dail = nil
  111. content = @response.body
  112. response = REXML::Document.new(content)
  113. response.elements.each('Response') do |element|
  114. on_hangup = element.attributes['onHangup']
  115. on_answer = element.attributes['onAnswer']
  116. end
  117. response.elements.each('Response/Dial') do |element|
  118. caller_id = element.attributes['callerId']
  119. end
  120. response.elements.each('Response/Dial/Number') do |element|
  121. number_to_dail = element.text
  122. end
  123. expect(caller_id).to eq('4930777000000')
  124. expect(number_to_dail).to eq('4912347114711')
  125. expect(on_hangup).to eq('http://zammad.example.com/api/v1/sipgate/out')
  126. expect(on_answer).to eq('http://zammad.example.com/api/v1/sipgate/out')
  127. # outbound - II - set caller_id based on routing_table by explicite number
  128. params = 'event=newCall&direction=out&from=4930600000000&to=491714000000&callId=8621106404543334274-4&user%5B%5D=user+1'
  129. post '/api/v1/sipgate/out', params: params
  130. expect(@response).to have_http_status(:ok)
  131. on_hangup = nil
  132. on_answer = nil
  133. caller_id = nil
  134. number_to_dail = nil
  135. content = @response.body
  136. response = REXML::Document.new(content)
  137. response.elements.each('Response') do |element|
  138. on_hangup = element.attributes['onHangup']
  139. on_answer = element.attributes['onAnswer']
  140. end
  141. response.elements.each('Response/Dial') do |element|
  142. caller_id = element.attributes['callerId']
  143. end
  144. response.elements.each('Response/Dial/Number') do |element|
  145. number_to_dail = element.text
  146. end
  147. expect(caller_id).to eq('41715880339000')
  148. expect(number_to_dail).to eq('491714000000')
  149. expect(on_hangup).to eq('http://zammad.example.com/api/v1/sipgate/out')
  150. expect(on_answer).to eq('http://zammad.example.com/api/v1/sipgate/out')
  151. # outbound - III - set caller_id based on routing_table by 41*
  152. params = 'event=newCall&direction=out&from=4930600000000&to=4147110000000&callId=8621106404543334274-5&user%5B%5D=user+1'
  153. post '/api/v1/sipgate/out', params: params
  154. expect(@response).to have_http_status(:ok)
  155. on_hangup = nil
  156. on_answer = nil
  157. caller_id = nil
  158. number_to_dail = nil
  159. content = @response.body
  160. response = REXML::Document.new(content)
  161. response.elements.each('Response') do |element|
  162. on_hangup = element.attributes['onHangup']
  163. on_answer = element.attributes['onAnswer']
  164. end
  165. response.elements.each('Response/Dial') do |element|
  166. caller_id = element.attributes['callerId']
  167. end
  168. response.elements.each('Response/Dial/Number') do |element|
  169. number_to_dail = element.text
  170. end
  171. expect(caller_id).to eq('41715880339000')
  172. expect(number_to_dail).to eq('4147110000000')
  173. expect(on_hangup).to eq('http://zammad.example.com/api/v1/sipgate/out')
  174. expect(on_answer).to eq('http://zammad.example.com/api/v1/sipgate/out')
  175. # no config
  176. Setting.set('sipgate_config', {})
  177. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-6&user%5B%5D=user+1&user%5B%5D=user+2'
  178. post '/api/v1/sipgate/in', params: params
  179. expect(@response).to have_http_status(:unprocessable_entity)
  180. error = nil
  181. content = @response.body
  182. response = REXML::Document.new(content)
  183. response.elements.each('Response/Error') do |element|
  184. error = element.text
  185. end
  186. expect(error).to eq('Feature not configured, please contact your admin!')
  187. end
  188. it 'does log call' do
  189. # outbound - I - new call
  190. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-1&user%5B%5D=user+1'
  191. post '/api/v1/sipgate/out', params: params
  192. expect(@response).to have_http_status(:ok)
  193. log = Cti::Log.find_by(call_id: '1234567890-1')
  194. expect(log).to be_truthy
  195. expect(log.from).to eq('4930777000000')
  196. expect(log.to).to eq('4912347114711')
  197. expect(log.direction).to eq('out')
  198. expect(log.from_comment).to eq('user 1')
  199. expect(log.to_comment).to eq('CallerId Customer1')
  200. expect(log.comment).to be_nil
  201. expect(log.state).to eq('newCall')
  202. expect(log.done).to eq(true)
  203. travel 1.second
  204. # outbound - I - hangup by agent
  205. params = 'event=hangup&direction=out&callId=1234567890-1&cause=cancel'
  206. post '/api/v1/sipgate/out', params: params
  207. expect(@response).to have_http_status(:ok)
  208. log = Cti::Log.find_by(call_id: '1234567890-1')
  209. expect(log).to be_truthy
  210. expect(log.from).to eq('4930777000000')
  211. expect(log.to).to eq('4912347114711')
  212. expect(log.direction).to eq('out')
  213. expect(log.from_comment).to eq('user 1')
  214. expect(log.to_comment).to eq('CallerId Customer1')
  215. expect(log.comment).to eq('cancel')
  216. expect(log.state).to eq('hangup')
  217. expect(log.done).to eq(true)
  218. travel 1.second
  219. # outbound - II - new call
  220. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-2&user%5B%5D=user+1'
  221. post '/api/v1/sipgate/out', 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('4912347114711')
  227. expect(log.direction).to eq('out')
  228. expect(log.from_comment).to eq('user 1')
  229. expect(log.to_comment).to eq('CallerId Customer1')
  230. expect(log.comment).to be_nil
  231. expect(log.state).to eq('newCall')
  232. expect(log.done).to eq(true)
  233. travel 1.second
  234. # outbound - II - answer by customer
  235. params = 'event=answer&direction=out&callId=1234567890-2&from=4930600000000&to=4912347114711'
  236. post '/api/v1/sipgate/out', params: params
  237. expect(@response).to have_http_status(:ok)
  238. log = Cti::Log.find_by(call_id: '1234567890-2')
  239. expect(log).to be_truthy
  240. expect(log.from).to eq('4930777000000')
  241. expect(log.to).to eq('4912347114711')
  242. expect(log.direction).to eq('out')
  243. expect(log.from_comment).to eq('user 1')
  244. expect(log.to_comment).to eq('CallerId Customer1')
  245. expect(log.comment).to be_nil
  246. expect(log.state).to eq('answer')
  247. expect(log.done).to eq(true)
  248. travel 1.second
  249. # outbound - II - hangup by customer
  250. params = 'event=hangup&direction=out&callId=1234567890-2&cause=normalClearing&from=4930600000000&to=4912347114711'
  251. post '/api/v1/sipgate/out', params: params
  252. expect(@response).to have_http_status(:ok)
  253. log = Cti::Log.find_by(call_id: '1234567890-2')
  254. expect(log).to be_truthy
  255. expect(log.from).to eq('4930777000000')
  256. expect(log.to).to eq('4912347114711')
  257. expect(log.direction).to eq('out')
  258. expect(log.from_comment).to eq('user 1')
  259. expect(log.to_comment).to eq('CallerId Customer1')
  260. expect(log.comment).to eq('normalClearing')
  261. expect(log.state).to eq('hangup')
  262. expect(log.done).to eq(true)
  263. travel 1.second
  264. # inbound - I - new call
  265. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-3&user%5B%5D=user+1'
  266. post '/api/v1/sipgate/in', params: params
  267. expect(@response).to have_http_status(:ok)
  268. log = Cti::Log.find_by(call_id: '1234567890-3')
  269. expect(log).to be_truthy
  270. expect(log.to).to eq('4930600000000')
  271. expect(log.from).to eq('4912347114711')
  272. expect(log.direction).to eq('in')
  273. expect(log.to_comment).to eq('user 1')
  274. expect(log.from_comment).to eq('CallerId Customer1')
  275. expect(log.comment).to be_nil
  276. expect(log.state).to eq('newCall')
  277. expect(log.done).to eq(false)
  278. travel 1.second
  279. # inbound - I - answer by customer
  280. params = 'event=answer&direction=in&callId=1234567890-3&to=4930600000000&from=4912347114711'
  281. post '/api/v1/sipgate/in', 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('4930600000000')
  286. expect(log.from).to eq('4912347114711')
  287. expect(log.direction).to eq('in')
  288. expect(log.to_comment).to eq('user 1')
  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 eq(true)
  293. travel 1.second
  294. # inbound - I - hangup by customer
  295. params = 'event=hangup&direction=in&callId=1234567890-3&cause=normalClearing&to=4930600000000&from=4912347114711'
  296. post '/api/v1/sipgate/in', params: params
  297. expect(@response).to have_http_status(:ok)
  298. log = Cti::Log.find_by(call_id: '1234567890-3')
  299. expect(log).to be_truthy
  300. expect(log.to).to eq('4930600000000')
  301. expect(log.from).to eq('4912347114711')
  302. expect(log.direction).to eq('in')
  303. expect(log.to_comment).to eq('user 1')
  304. expect(log.from_comment).to eq('CallerId Customer1')
  305. expect(log.comment).to eq('normalClearing')
  306. expect(log.state).to eq('hangup')
  307. expect(log.done).to eq(true)
  308. travel 1.second
  309. # inbound - II - new call
  310. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-4&user%5B%5D=user+1,user+2'
  311. post '/api/v1/sipgate/in', params: params
  312. expect(@response).to have_http_status(:ok)
  313. log = Cti::Log.find_by(call_id: '1234567890-4')
  314. expect(log).to be_truthy
  315. expect(log.to).to eq('4930600000000')
  316. expect(log.from).to eq('4912347114711')
  317. expect(log.direction).to eq('in')
  318. expect(log.to_comment).to eq('user 1,user 2')
  319. expect(log.from_comment).to eq('CallerId Customer1')
  320. expect(log.comment).to be_nil
  321. expect(log.state).to eq('newCall')
  322. expect(log.done).to eq(false)
  323. travel 1.second
  324. # inbound - II - answer by voicemail
  325. params = 'event=answer&direction=in&callId=1234567890-4&to=4930600000000&from=4912347114711&user=voicemail'
  326. post '/api/v1/sipgate/in', params: params
  327. expect(@response).to have_http_status(:ok)
  328. log = Cti::Log.find_by(call_id: '1234567890-4')
  329. expect(log).to be_truthy
  330. expect(log.to).to eq('4930600000000')
  331. expect(log.from).to eq('4912347114711')
  332. expect(log.direction).to eq('in')
  333. expect(log.to_comment).to eq('voicemail')
  334. expect(log.from_comment).to eq('CallerId Customer1')
  335. expect(log.comment).to be_nil
  336. expect(log.state).to eq('answer')
  337. expect(log.done).to eq(true)
  338. travel 1.second
  339. # inbound - II - hangup by customer
  340. params = 'event=hangup&direction=in&callId=1234567890-4&cause=normalClearing&to=4930600000000&from=4912347114711'
  341. post '/api/v1/sipgate/in', 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('4930600000000')
  346. expect(log.from).to eq('4912347114711')
  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 eq('normalClearing')
  351. expect(log.state).to eq('hangup')
  352. expect(log.done).to eq(false)
  353. travel 1.second
  354. # inbound - III - new call
  355. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-5&user%5B%5D=user+1,user+2'
  356. post '/api/v1/sipgate/in', params: params
  357. expect(@response).to have_http_status(:ok)
  358. log = Cti::Log.find_by(call_id: '1234567890-5')
  359. expect(log).to be_truthy
  360. expect(log.to).to eq('4930600000000')
  361. expect(log.from).to eq('4912347114711')
  362. expect(log.direction).to eq('in')
  363. expect(log.to_comment).to eq('user 1,user 2')
  364. expect(log.from_comment).to eq('CallerId Customer1')
  365. expect(log.comment).to be_nil
  366. expect(log.state).to eq('newCall')
  367. expect(log.done).to eq(false)
  368. travel 1.second
  369. # inbound - III - hangup by customer
  370. params = 'event=hangup&direction=in&callId=1234567890-5&cause=normalClearing&to=4930600000000&from=4912347114711'
  371. post '/api/v1/sipgate/in', params: params
  372. expect(@response).to have_http_status(:ok)
  373. log = Cti::Log.find_by(call_id: '1234567890-5')
  374. expect(log).to be_truthy
  375. expect(log.to).to eq('4930600000000')
  376. expect(log.from).to eq('4912347114711')
  377. expect(log.direction).to eq('in')
  378. expect(log.to_comment).to eq('user 1,user 2')
  379. expect(log.from_comment).to eq('CallerId Customer1')
  380. expect(log.comment).to eq('normalClearing')
  381. expect(log.state).to eq('hangup')
  382. expect(log.done).to eq(false)
  383. travel 1.second
  384. # inbound - IV - new call
  385. params = 'event=newCall&direction=in&to=4930600000000&from=49999992222222&callId=1234567890-6&user%5B%5D=user+1,user+2'
  386. post '/api/v1/sipgate/in', params: params
  387. expect(@response).to have_http_status(:ok)
  388. log = Cti::Log.find_by(call_id: '1234567890-6')
  389. expect(log).to be_truthy
  390. expect(log.to).to eq('4930600000000')
  391. expect(log.from).to eq('49999992222222')
  392. expect(log.direction).to eq('in')
  393. expect(log.to_comment).to eq('user 1,user 2')
  394. expect(log.from_comment).to eq('CallerId Customer3,CallerId Customer2')
  395. expect(log.preferences['to']).to be_falsey
  396. expect(log.preferences['from']).to be_truthy
  397. expect(log.comment).to be_nil
  398. expect(log.state).to eq('newCall')
  399. expect(log.done).to eq(false)
  400. # get caller list
  401. get '/api/v1/cti/log'
  402. expect(@response).to have_http_status(:forbidden)
  403. authenticated_as(agent)
  404. get '/api/v1/cti/log', as: :json
  405. expect(@response).to have_http_status(:ok)
  406. expect(json_response['list']).to be_a_kind_of(Array)
  407. expect(json_response['list'].count).to eq(6)
  408. expect(json_response['assets']).to be_truthy
  409. expect(json_response['assets']['User']).to be_truthy
  410. expect(json_response['assets']['User'][customer2.id.to_s]).to be_truthy
  411. expect(json_response['assets']['User'][customer3.id.to_s]).to be_truthy
  412. expect(json_response['list'][0]['call_id']).to eq('1234567890-6')
  413. expect(json_response['list'][1]['call_id']).to eq('1234567890-5')
  414. expect(json_response['list'][2]['call_id']).to eq('1234567890-4')
  415. expect(json_response['list'][3]['call_id']).to eq('1234567890-3')
  416. expect(json_response['list'][4]['call_id']).to eq('1234567890-2')
  417. expect(json_response['list'][4]['state']).to eq('hangup')
  418. expect(json_response['list'][4]['from']).to eq('4930777000000')
  419. expect(json_response['list'][4]['from_comment']).to eq('user 1')
  420. expect(json_response['list'][4]['to']).to eq('4912347114711')
  421. expect(json_response['list'][4]['to_comment']).to eq('CallerId Customer1')
  422. expect(json_response['list'][4]['comment']).to eq('normalClearing')
  423. expect(json_response['list'][4]['state']).to eq('hangup')
  424. expect(json_response['list'][5]['call_id']).to eq('1234567890-1')
  425. end
  426. it 'alternative fqdn' do
  427. Setting.set('sipgate_alternative_fqdn', 'external.host.example.com')
  428. # inbound - I
  429. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  430. post '/api/v1/sipgate/in', params: params
  431. expect(@response).to have_http_status(:ok)
  432. on_hangup = nil
  433. on_answer = nil
  434. content = @response.body
  435. response = REXML::Document.new(content)
  436. response.elements.each('Response') do |element|
  437. on_hangup = element.attributes['onHangup']
  438. on_answer = element.attributes['onAnswer']
  439. end
  440. expect(on_hangup).to eq('http://external.host.example.com/api/v1/sipgate/in')
  441. expect(on_answer).to eq('http://external.host.example.com/api/v1/sipgate/in')
  442. end
  443. end
  444. end