sipgate_controller_test.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. # encoding: utf-8
  2. require 'test_helper'
  3. require 'rexml/document'
  4. class SipgateControllerTest < ActionDispatch::IntegrationTest
  5. setup do
  6. Cti::Log.destroy_all
  7. Setting.create_or_update(
  8. title: 'sipgate.io integration',
  9. name: 'sipgate_integration',
  10. area: 'Integration::Switch',
  11. description: 'Define if sipgate.io (http://www.sipgate.io) is enabled or not.',
  12. options: {
  13. form: [
  14. {
  15. display: '',
  16. null: true,
  17. name: 'sipgate_integration',
  18. tag: 'boolean',
  19. options: {
  20. true => 'yes',
  21. false => 'no',
  22. },
  23. },
  24. ],
  25. },
  26. state: true,
  27. preferences: { prio: 1 },
  28. frontend: false
  29. )
  30. Setting.create_or_update(
  31. title: 'sipgate.io config',
  32. name: 'sipgate_config',
  33. area: 'Integration::Sipgate',
  34. description: 'Define the sipgate.io config.',
  35. options: {},
  36. state: {
  37. outbound: {
  38. routing_table: [
  39. {
  40. dest: '41*',
  41. caller_id: '41715880339000',
  42. },
  43. {
  44. dest: '491714000000',
  45. caller_id: '41715880339000',
  46. },
  47. ],
  48. default_caller_id: '4930777000000',
  49. },
  50. inbound: {
  51. block_caller_ids: [
  52. {
  53. caller_id: '491715000000',
  54. note: 'some note',
  55. }
  56. ],
  57. notify_user_ids: {
  58. 2 => true,
  59. 4 => false,
  60. },
  61. }
  62. },
  63. frontend: false,
  64. preferences: { prio: 2 },
  65. )
  66. groups = Group.where(name: 'Users')
  67. roles = Role.where(name: %w(Agent))
  68. agent = User.create_or_update(
  69. login: 'cti-agent@example.com',
  70. firstname: 'E',
  71. lastname: 'S',
  72. email: 'cti-agent@example.com',
  73. password: 'agentpw',
  74. active: true,
  75. roles: roles,
  76. groups: groups,
  77. updated_by_id: 1,
  78. created_by_id: 1,
  79. )
  80. customer1 = User.create_or_update(
  81. login: 'ticket-caller_id_sipgate-customer1@example.com',
  82. firstname: 'CallerId',
  83. lastname: 'Customer1',
  84. email: 'ticket-caller_id_sipgate-customer1@example.com',
  85. password: 'customerpw',
  86. active: true,
  87. phone: '+49 99999 222222',
  88. fax: '+49 99999 222223',
  89. mobile: '+4912347114711',
  90. note: 'Phone at home: +49 99999 222224',
  91. updated_by_id: 1,
  92. created_by_id: 1,
  93. )
  94. customer2 = User.create_or_update(
  95. login: 'ticket-caller_id_sipgate-customer2@example.com',
  96. firstname: 'CallerId',
  97. lastname: 'Customer2',
  98. email: 'ticket-caller_id_sipgate-customer2@example.com',
  99. password: 'customerpw',
  100. active: true,
  101. phone: '+49 99999 222222 2',
  102. updated_by_id: 1,
  103. created_by_id: 1,
  104. )
  105. customer3 = User.create_or_update(
  106. login: 'ticket-caller_id_sipgate-customer3@example.com',
  107. firstname: 'CallerId',
  108. lastname: 'Customer3',
  109. email: 'ticket-caller_id_sipgate-customer3@example.com',
  110. password: 'customerpw',
  111. active: true,
  112. phone: '+49 99999 222222 2',
  113. updated_by_id: 1,
  114. created_by_id: 1,
  115. )
  116. Cti::CallerId.rebuild
  117. end
  118. test 'basic call' do
  119. # inbound - I
  120. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-1&user%5B%5D=user+1&user%5B%5D=user+2'
  121. post '/api/v1/sipgate/in', params
  122. assert_response(200)
  123. on_hangup = nil
  124. on_answer = nil
  125. content = @response.body
  126. response = REXML::Document.new(content)
  127. response.elements.each('Response') do |element|
  128. on_hangup = element.attributes['onHangup']
  129. on_answer = element.attributes['onAnswer']
  130. end
  131. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup)
  132. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer)
  133. # inbound - II - block caller
  134. params = 'event=newCall&direction=in&from=491715000000&to=4930600000000&callId=4991155921769858278-2&user%5B%5D=user+1&user%5B%5D=user+2'
  135. post '/api/v1/sipgate/in', params
  136. assert_response(200)
  137. on_hangup = nil
  138. on_answer = nil
  139. content = @response.body
  140. response = REXML::Document.new(content)
  141. response.elements.each('Response') do |element|
  142. on_hangup = element.attributes['onHangup']
  143. on_answer = element.attributes['onAnswer']
  144. end
  145. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup)
  146. assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer)
  147. reason = nil
  148. response.elements.each('Response/Reject') do |element|
  149. reason = element.attributes['reason']
  150. end
  151. assert_equal('busy', reason)
  152. # outbound - I - set default_caller_id
  153. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=8621106404543334274-3&user%5B%5D=user+1'
  154. post '/api/v1/sipgate/out', params
  155. assert_response(200)
  156. on_hangup = nil
  157. on_answer = nil
  158. caller_id = nil
  159. number_to_dail = nil
  160. content = @response.body
  161. response = REXML::Document.new(content)
  162. response.elements.each('Response') do |element|
  163. on_hangup = element.attributes['onHangup']
  164. on_answer = element.attributes['onAnswer']
  165. end
  166. response.elements.each('Response/Dial') do |element|
  167. caller_id = element.attributes['callerId']
  168. end
  169. response.elements.each('Response/Dial/Number') do |element|
  170. number_to_dail = element.text
  171. end
  172. assert_equal('4930777000000', caller_id)
  173. assert_equal('4912347114711', number_to_dail)
  174. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  175. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  176. # outbound - II - set caller_id based on routing_table by explicite number
  177. params = 'event=newCall&direction=out&from=4930600000000&to=491714000000&callId=8621106404543334274-4&user%5B%5D=user+1'
  178. post '/api/v1/sipgate/out', params
  179. assert_response(200)
  180. on_hangup = nil
  181. on_answer = nil
  182. caller_id = nil
  183. number_to_dail = nil
  184. content = @response.body
  185. response = REXML::Document.new(content)
  186. response.elements.each('Response') do |element|
  187. on_hangup = element.attributes['onHangup']
  188. on_answer = element.attributes['onAnswer']
  189. end
  190. response.elements.each('Response/Dial') do |element|
  191. caller_id = element.attributes['callerId']
  192. end
  193. response.elements.each('Response/Dial/Number') do |element|
  194. number_to_dail = element.text
  195. end
  196. assert_equal('41715880339000', caller_id)
  197. assert_equal('491714000000', number_to_dail)
  198. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  199. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  200. # outbound - III - set caller_id based on routing_table by 41*
  201. params = 'event=newCall&direction=out&from=4930600000000&to=4147110000000&callId=8621106404543334274-5&user%5B%5D=user+1'
  202. post '/api/v1/sipgate/out', params
  203. assert_response(200)
  204. on_hangup = nil
  205. on_answer = nil
  206. caller_id = nil
  207. number_to_dail = nil
  208. content = @response.body
  209. response = REXML::Document.new(content)
  210. response.elements.each('Response') do |element|
  211. on_hangup = element.attributes['onHangup']
  212. on_answer = element.attributes['onAnswer']
  213. end
  214. response.elements.each('Response/Dial') do |element|
  215. caller_id = element.attributes['callerId']
  216. end
  217. response.elements.each('Response/Dial/Number') do |element|
  218. number_to_dail = element.text
  219. end
  220. assert_equal('41715880339000', caller_id)
  221. assert_equal('4147110000000', number_to_dail)
  222. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_hangup)
  223. assert_equal('http://zammad.example.com/api/v1/sipgate/out', on_answer)
  224. # no config
  225. Setting.set('sipgate_config', {})
  226. params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278-6&user%5B%5D=user+1&user%5B%5D=user+2'
  227. post '/api/v1/sipgate/in', params
  228. assert_response(422)
  229. error = nil
  230. content = @response.body
  231. response = REXML::Document.new(content)
  232. response.elements.each('Response/Error') do |element|
  233. error = element.text
  234. end
  235. assert_equal('Feature not configured, please contact your admin!', error)
  236. end
  237. test 'log call' do
  238. # outbound - I - new call
  239. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-1&user%5B%5D=user+1'
  240. post '/api/v1/sipgate/out', params
  241. assert_response(200)
  242. log = Cti::Log.find_by(call_id: '1234567890-1')
  243. assert(log)
  244. assert_equal('4930777000000', log.from)
  245. assert_equal('4912347114711', log.to)
  246. assert_equal('out', log.direction)
  247. assert_equal('user 1', log.from_comment)
  248. assert_equal('CallerId Customer1', log.to_comment)
  249. assert_equal(nil, log.comment)
  250. assert_equal('newCall', log.state)
  251. assert_equal(true, log.done)
  252. # outbound - I - hangup by agent
  253. params = 'event=hangup&direction=out&callId=1234567890-1&cause=cancel'
  254. post '/api/v1/sipgate/out', params
  255. assert_response(200)
  256. log = Cti::Log.find_by(call_id: '1234567890-1')
  257. assert(log)
  258. assert_equal('4930777000000', log.from)
  259. assert_equal('4912347114711', log.to)
  260. assert_equal('out', log.direction)
  261. assert_equal('user 1', log.from_comment)
  262. assert_equal('CallerId Customer1', log.to_comment)
  263. assert_equal('cancel', log.comment)
  264. assert_equal('hangup', log.state)
  265. assert_equal(true, log.done)
  266. # outbound - II - new call
  267. params = 'event=newCall&direction=out&from=4930600000000&to=4912347114711&callId=1234567890-2&user%5B%5D=user+1'
  268. post '/api/v1/sipgate/out', params
  269. assert_response(200)
  270. log = Cti::Log.find_by(call_id: '1234567890-2')
  271. assert(log)
  272. assert_equal('4930777000000', log.from)
  273. assert_equal('4912347114711', log.to)
  274. assert_equal('out', log.direction)
  275. assert_equal('user 1', log.from_comment)
  276. assert_equal('CallerId Customer1', log.to_comment)
  277. assert_equal(nil, log.comment)
  278. assert_equal('newCall', log.state)
  279. assert_equal(true, log.done)
  280. # outbound - II - answer by customer
  281. params = 'event=answer&direction=out&callId=1234567890-2&from=4930600000000&to=4912347114711'
  282. post '/api/v1/sipgate/out', params
  283. assert_response(200)
  284. log = Cti::Log.find_by(call_id: '1234567890-2')
  285. assert(log)
  286. assert_equal('4930777000000', log.from)
  287. assert_equal('4912347114711', log.to)
  288. assert_equal('out', log.direction)
  289. assert_equal('user 1', log.from_comment)
  290. assert_equal('CallerId Customer1', log.to_comment)
  291. assert_equal(nil, log.comment)
  292. assert_equal('answer', log.state)
  293. assert_equal(true, log.done)
  294. # outbound - II - hangup by customer
  295. params = 'event=hangup&direction=out&callId=1234567890-2&cause=normalClearing&from=4930600000000&to=4912347114711'
  296. post '/api/v1/sipgate/out', params
  297. assert_response(200)
  298. log = Cti::Log.find_by(call_id: '1234567890-2')
  299. assert(log)
  300. assert_equal('4930777000000', log.from)
  301. assert_equal('4912347114711', log.to)
  302. assert_equal('out', log.direction)
  303. assert_equal('user 1', log.from_comment)
  304. assert_equal('CallerId Customer1', log.to_comment)
  305. assert_equal('normalClearing', log.comment)
  306. assert_equal('hangup', log.state)
  307. assert_equal(true, log.done)
  308. # inbound - I - new call
  309. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-3&user%5B%5D=user+1'
  310. post '/api/v1/sipgate/in', params
  311. assert_response(200)
  312. log = Cti::Log.find_by(call_id: '1234567890-3')
  313. assert(log)
  314. assert_equal('4930600000000', log.to)
  315. assert_equal('4912347114711', log.from)
  316. assert_equal('in', log.direction)
  317. assert_equal('user 1', log.to_comment)
  318. assert_equal('CallerId Customer1', log.from_comment)
  319. assert_equal(nil, log.comment)
  320. assert_equal('newCall', log.state)
  321. assert_equal(true, log.done)
  322. # inbound - I - answer by customer
  323. params = 'event=answer&direction=in&callId=1234567890-3&to=4930600000000&from=4912347114711'
  324. post '/api/v1/sipgate/in', params
  325. assert_response(200)
  326. log = Cti::Log.find_by(call_id: '1234567890-3')
  327. assert(log)
  328. assert_equal('4930600000000', log.to)
  329. assert_equal('4912347114711', log.from)
  330. assert_equal('in', log.direction)
  331. assert_equal('user 1', log.to_comment)
  332. assert_equal('CallerId Customer1', log.from_comment)
  333. assert_equal(nil, log.comment)
  334. assert_equal('answer', log.state)
  335. assert_equal(true, log.done)
  336. # inbound - I - hangup by customer
  337. params = 'event=hangup&direction=in&callId=1234567890-3&cause=normalClearing&to=4930600000000&from=4912347114711'
  338. post '/api/v1/sipgate/in', params
  339. assert_response(200)
  340. log = Cti::Log.find_by(call_id: '1234567890-3')
  341. assert(log)
  342. assert_equal('4930600000000', log.to)
  343. assert_equal('4912347114711', log.from)
  344. assert_equal('in', log.direction)
  345. assert_equal('user 1', log.to_comment)
  346. assert_equal('CallerId Customer1', log.from_comment)
  347. assert_equal('normalClearing', log.comment)
  348. assert_equal('hangup', log.state)
  349. assert_equal(true, log.done)
  350. # inbound - II - new call
  351. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-4&user%5B%5D=user+1,user+2'
  352. post '/api/v1/sipgate/in', params
  353. assert_response(200)
  354. log = Cti::Log.find_by(call_id: '1234567890-4')
  355. assert(log)
  356. assert_equal('4930600000000', log.to)
  357. assert_equal('4912347114711', log.from)
  358. assert_equal('in', log.direction)
  359. assert_equal('user 1,user 2', log.to_comment)
  360. assert_equal('CallerId Customer1', log.from_comment)
  361. assert_equal(nil, log.comment)
  362. assert_equal('newCall', log.state)
  363. assert_equal(true, log.done)
  364. # inbound - II - answer by voicemail
  365. params = 'event=answer&direction=in&callId=1234567890-4&to=4930600000000&from=4912347114711&user=voicemail'
  366. post '/api/v1/sipgate/in', params
  367. assert_response(200)
  368. log = Cti::Log.find_by(call_id: '1234567890-4')
  369. assert(log)
  370. assert_equal('4930600000000', log.to)
  371. assert_equal('4912347114711', log.from)
  372. assert_equal('in', log.direction)
  373. assert_equal('voicemail', log.to_comment)
  374. assert_equal('CallerId Customer1', log.from_comment)
  375. assert_equal(nil, log.comment)
  376. assert_equal('answer', log.state)
  377. assert_equal(true, log.done)
  378. # inbound - II - hangup by customer
  379. params = 'event=hangup&direction=in&callId=1234567890-4&cause=normalClearing&to=4930600000000&from=4912347114711'
  380. post '/api/v1/sipgate/in', params
  381. assert_response(200)
  382. log = Cti::Log.find_by(call_id: '1234567890-4')
  383. assert(log)
  384. assert_equal('4930600000000', log.to)
  385. assert_equal('4912347114711', log.from)
  386. assert_equal('in', log.direction)
  387. assert_equal('voicemail', log.to_comment)
  388. assert_equal('CallerId Customer1', log.from_comment)
  389. assert_equal('normalClearing', log.comment)
  390. assert_equal('hangup', log.state)
  391. assert_equal(false, log.done)
  392. # inbound - III - new call
  393. params = 'event=newCall&direction=in&to=4930600000000&from=4912347114711&callId=1234567890-5&user%5B%5D=user+1,user+2'
  394. post '/api/v1/sipgate/in', params
  395. assert_response(200)
  396. log = Cti::Log.find_by(call_id: '1234567890-5')
  397. assert(log)
  398. assert_equal('4930600000000', log.to)
  399. assert_equal('4912347114711', log.from)
  400. assert_equal('in', log.direction)
  401. assert_equal('user 1,user 2', log.to_comment)
  402. assert_equal('CallerId Customer1', log.from_comment)
  403. assert_equal(nil, log.comment)
  404. assert_equal('newCall', log.state)
  405. assert_equal(true, log.done)
  406. # inbound - III - hangup by customer
  407. params = 'event=hangup&direction=in&callId=1234567890-5&cause=normalClearing&to=4930600000000&from=4912347114711'
  408. post '/api/v1/sipgate/in', params
  409. assert_response(200)
  410. log = Cti::Log.find_by(call_id: '1234567890-5')
  411. assert(log)
  412. assert_equal('4930600000000', log.to)
  413. assert_equal('4912347114711', log.from)
  414. assert_equal('in', log.direction)
  415. assert_equal('user 1,user 2', log.to_comment)
  416. assert_equal('CallerId Customer1', log.from_comment)
  417. assert_equal('normalClearing', log.comment)
  418. assert_equal('hangup', log.state)
  419. assert_equal(false, log.done)
  420. # inbound - IV - new call
  421. params = 'event=newCall&direction=in&to=4930600000000&from=49999992222222&callId=1234567890-6&user%5B%5D=user+1,user+2'
  422. post '/api/v1/sipgate/in', params
  423. assert_response(200)
  424. log = Cti::Log.find_by(call_id: '1234567890-6')
  425. assert(log)
  426. assert_equal('4930600000000', log.to)
  427. assert_equal('49999992222222', log.from)
  428. assert_equal('in', log.direction)
  429. assert_equal('user 1,user 2', log.to_comment)
  430. assert_equal('CallerId Customer3,CallerId Customer2', log.from_comment)
  431. assert_not(log.preferences['to'])
  432. assert(log.preferences['from'])
  433. assert_equal(nil, log.comment)
  434. assert_equal('newCall', log.state)
  435. assert_equal(true, log.done)
  436. # get caller list
  437. get '/api/v1/cti/log'
  438. assert_response(401)
  439. customer2 = User.lookup(login: 'ticket-caller_id_sipgate-customer2@example.com')
  440. customer3 = User.lookup(login: 'ticket-caller_id_sipgate-customer3@example.com')
  441. headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  442. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('cti-agent@example.com', 'agentpw')
  443. get '/api/v1/cti/log', {}, headers.merge('Authorization' => credentials)
  444. assert_response(200)
  445. result = JSON.parse(@response.body)
  446. assert_equal(result['list'].class, Array)
  447. assert_equal(6, result['list'].count)
  448. assert(result['assets'])
  449. assert(result['assets']['User'])
  450. assert(result['assets']['User'][customer2.id.to_s])
  451. assert(result['assets']['User'][customer3.id.to_s])
  452. assert_equal('1234567890-6', result['list'][0]['call_id'])
  453. assert_equal('1234567890-5', result['list'][1]['call_id'])
  454. assert_equal('1234567890-4', result['list'][2]['call_id'])
  455. assert_equal('1234567890-3', result['list'][3]['call_id'])
  456. assert_equal('1234567890-2', result['list'][4]['call_id'])
  457. assert_equal('hangup', result['list'][4]['state'])
  458. assert_equal('4930777000000', result['list'][4]['from'])
  459. assert_equal('user 1', result['list'][4]['from_comment'])
  460. assert_equal('4912347114711', result['list'][4]['to'])
  461. assert_equal('CallerId Customer1', result['list'][4]['to_comment'])
  462. assert_equal('normalClearing', result['list'][4]['comment'])
  463. assert_equal('hangup', result['list'][4]['state'])
  464. assert_equal('1234567890-1', result['list'][5]['call_id'])
  465. end
  466. end