From 30b05bd991fb449fadd538804b4b3e45e51e83fc Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 21 Apr 2016 15:43:13 +0200 Subject: [PATCH] Improved error handling. --- .../integration/sipgate_controller.rb | 33 ++++++++++++------- test/integration/sipgate_controller_test.rb | 13 ++++++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/app/controllers/integration/sipgate_controller.rb b/app/controllers/integration/sipgate_controller.rb index 721240765..8006b8635 100644 --- a/app/controllers/integration/sipgate_controller.rb +++ b/app/controllers/integration/sipgate_controller.rb @@ -5,11 +5,11 @@ class Integration::SipgateController < ApplicationController # notify about inbound call / block inbound call def in - return if feature_disabled + return if !configured? config = Setting.get('sipgate_config') - config_inbound = config[:inbound] - block_caller_ids = config_inbound[:block_caller_ids] + config_inbound = config[:inbound] || {} + block_caller_ids = config_inbound[:block_caller_ids] || [] if params['event'] == 'newCall' @@ -49,7 +49,7 @@ class Integration::SipgateController < ApplicationController # set caller id of outbound call def out - return if feature_disabled + return if !configured? config = Setting.get('sipgate_config') config_outbound = config[:outbound][:routing_table] @@ -91,15 +91,26 @@ class Integration::SipgateController < ApplicationController private - def feature_disabled + def configured? if !Setting.get('sipgate_integration') - render( - json: {}, - status: :unauthorized - ) - return true + xml_error('Feature is disable, please contact your admin to enable it!') + return false end - false + config = Setting.get('sipgate_config') + if !config || !config[:inbound] || !config[:outbound] + xml_error('Feature not configured, please contact your admin!') + return false + end + true + end + + def xml_error(error) + xml = Builder::XmlMarkup.new(indent: 2) + xml.instruct! + content = xml.Response() do + xml.Error(error) + end + send_data content, type: 'application/xml; charset=UTF-8;', status: '500' end def base_url diff --git a/test/integration/sipgate_controller_test.rb b/test/integration/sipgate_controller_test.rb index cb102e066..fa9432433 100644 --- a/test/integration/sipgate_controller_test.rb +++ b/test/integration/sipgate_controller_test.rb @@ -179,6 +179,19 @@ class SipgateControllerTest < ActionDispatch::IntegrationTest assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup) assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer) + # no config + Setting.set('sipgate_config', {}) + params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278&user%5B%5D=user+1&user%5B%5D=user+2' + post '/api/v1/sipgate/in', params + assert_response(500) + error = nil + content = @response.body + response = REXML::Document.new(content) + response.elements.each('Response/Error') do |element| + error = element.text + end + assert_equal('Feature not configured, please contact your admin!', error) + end end