Browse Source

Add autodiscovery exception handling to ExchangeController

Ryan Lue 6 years ago
parent
commit
2e4b7128f1

+ 5 - 3
app/controllers/integration/exchange_controller.rb

@@ -16,9 +16,11 @@ class Integration::ExchangeController < ApplicationController
         client.http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
       end
 
-      {
-        endpoint: client.try(:autodiscover).try(:ews_url),
-      }
+      begin
+        { endpoint: client.autodiscover&.ews_url }
+      rescue Errno::EADDRNOTAVAIL
+        {}
+      end
     end
   end
 

+ 19 - 1
spec/requests/integration/exchange_spec.rb

@@ -12,7 +12,8 @@ RSpec.describe 'Exchange integration endpoint', type: :request do
   end
 
   describe 'EWS folder retrieval' do
-    context 'when no folders found' do
+    # see https://github.com/zammad/zammad/issues/1802
+    context 'when no folders found (#1802)' do
       let(:empty_folder_list) { { folders: {} } }
 
       it 'responds with an error message' do
@@ -25,4 +26,21 @@ RSpec.describe 'Exchange integration endpoint', type: :request do
       end
     end
   end
+
+  describe 'autodiscovery' do
+    # see https://github.com/zammad/zammad/issues/2065
+    context 'when Autodiscover gem raises Errno::EADDRNOTAVAIL (#2065)' do
+      let(:client) { instance_double('Autodiscover::Client') }
+
+      it 'rescues and responds with an empty hash (to proceed to manual configuration)' do
+        allow(Autodiscover::Client).to receive(:new).with(any_args).and_return(client)
+        allow(client).to receive(:autodiscover).and_raise(Errno::EADDRNOTAVAIL)
+
+        post api_v1_integration_exchange_autodiscover_path,
+             params: {}, as: :json
+
+        expect(json_response).to eq('result' => 'ok')
+      end
+    end
+  end
 end