From 97f55bcd83cc17702642bad6873c639d4f920a10 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Tue, 10 Jul 2018 13:32:24 +0800 Subject: [PATCH] Add error check if Exchange config gives empty folder list (fixes #1802) --- .../integration/exchange_controller.rb | 21 +++++++------- spec/requests/integration/exchange_spec.rb | 28 +++++++++++++++++++ spec/support/request.rb | 5 ++-- 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 spec/requests/integration/exchange_spec.rb diff --git a/app/controllers/integration/exchange_controller.rb b/app/controllers/integration/exchange_controller.rb index 7f7f2c20d..fce76fbf7 100644 --- a/app/controllers/integration/exchange_controller.rb +++ b/app/controllers/integration/exchange_controller.rb @@ -25,9 +25,10 @@ class Integration::ExchangeController < ApplicationController def folders answer_with do Sequencer.process('Import::Exchange::AvailableFolders', - parameters: { - ews_config: ews_config - }) + parameters: { ews_config: ews_config }) + .tap do |res| + raise 'No folders found for given user credentials.' if res[:folders].blank? + end end end @@ -35,14 +36,12 @@ class Integration::ExchangeController < ApplicationController answer_with do raise 'Please select at least one folder.' if params[:folders].blank? - examples = Sequencer.process('Import::Exchange::AttributesExamples', - parameters: { - ews_folder_ids: params[:folders], - ews_config: ews_config - }) - examples.tap do |result| - raise 'No entries found in selected folder(s).' if result[:attributes].blank? - end + Sequencer.process('Import::Exchange::AttributesExamples', + parameters: { ews_folder_ids: params[:folders], + ews_config: ews_config }) + .tap do |res| + raise 'No entries found in selected folder(s).' if res[:attributes].blank? + end end end diff --git a/spec/requests/integration/exchange_spec.rb b/spec/requests/integration/exchange_spec.rb new file mode 100644 index 000000000..e21b075a5 --- /dev/null +++ b/spec/requests/integration/exchange_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +RSpec.describe 'Exchange integration endpoint', type: :request do + before { authenticated_as(admin_with_admin_user_permissions) } + + let(:admin_with_admin_user_permissions) do + create(:user, roles: [role_with_admin_user_permissions]) + end + + let(:role_with_admin_user_permissions) do + create(:role).tap { |role| role.permission_grant('admin.integration') } + end + + describe 'EWS folder retrieval' do + context 'when no folders found' do + let(:empty_folder_list) { { folders: {} } } + + it 'responds with an error message' do + allow(Sequencer).to receive(:process).with(any_args).and_return(empty_folder_list) + + post api_v1_integration_exchange_folders_path, + params: {}, as: :json + + expect(json_response).to include('result' => 'failed').and include('message') + end + end + end +end diff --git a/spec/support/request.rb b/spec/support/request.rb index 981a30434..762859a04 100644 --- a/spec/support/request.rb +++ b/spec/support/request.rb @@ -8,8 +8,9 @@ module ZammadSpecSupportRequest %i[get post patch put delete head].each do |method_id| define_method(method_id) do |path, **args| - headers = Hash(headers).merge(Hash(@headers)) - super(path, headers: headers, **args) + args = args.with_indifferent_access + args[:headers] = Hash(args[:headers]).merge!(Hash(@headers)) + super(path, **args.symbolize_keys) end end