From 7d63868996492fb4db666cfa770b2374b334c454 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 13 Aug 2018 15:10:24 +0800 Subject: [PATCH] Force UTF-8 encoding for Exchange folders (fixes #2152) --- lib/import/exchange/folder.rb | 18 +++++--------- spec/lib/import/exchange/folder_spec.rb | 33 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 spec/lib/import/exchange/folder_spec.rb diff --git a/lib/import/exchange/folder.rb b/lib/import/exchange/folder.rb index ca0d44bf6..c442179dd 100644 --- a/lib/import/exchange/folder.rb +++ b/lib/import/exchange/folder.rb @@ -25,7 +25,8 @@ module Import end def find(id) - @lookup_map[id] ||= @connection.get_folder(id) + (@lookup_map[id] ||= @connection.get_folder(id)) || + id_folder_map[id] end def all @@ -42,21 +43,14 @@ module Import end def display_path(folder) - display_name = folder.display_name - return display_name if !folder.parent_folder_id - + display_name = folder.display_name.utf8_encode(fallback: :read_as_sanitized_binary) parent_folder = find(folder.parent_folder_id) - return display_name if !parent_folder - parent_folder = id_folder_map[folder.parent_folder_id] - return display_name if !parent_folder + return display_name if parent_folder.blank? - # recursive - parent_folder_path = display_path(parent_folder) - - "#{parent_folder_path} -> #{display_name}" + "#{display_path(parent_folder)} -> #{display_name}" rescue Viewpoint::EWS::EwsError - folder.display_name + display_name end private diff --git a/spec/lib/import/exchange/folder_spec.rb b/spec/lib/import/exchange/folder_spec.rb new file mode 100644 index 000000000..9515eb42b --- /dev/null +++ b/spec/lib/import/exchange/folder_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe Import::Exchange::Folder do + # see https://github.com/zammad/zammad/issues/2152 + # WARNING! This test is closely tied to the implementation. :( + describe '#display_path (#2152)' do + let(:subject) { described_class.new(connection) } + let(:connection) { instance_double('Viewpoint::EWSClient') } + let(:root_folder) { double('EWS Folder') } + let(:child_folder) { double('EWS Folder') } + let(:exception_case) { double('EWS Folder') } + + before do + allow(root_folder).to receive(:display_name).and_return('你好'.b) + allow(root_folder).to receive(:parent_folder_id).and_return(nil) + + allow(child_folder).to receive(:display_name).and_return('你好'.b) + allow(child_folder).to receive(:parent_folder_id).and_return(1) + + allow(exception_case).to receive(:display_name).and_return('你好'.b) + allow(exception_case).to receive(:parent_folder_id).and_raise(Viewpoint::EWS::EwsError) + + allow(subject).to receive(:find).with(any_args).and_return(root_folder) + allow(subject).to receive(:find).with(nil).and_return(nil) + end + + it 'returns a valid UTF-8 string' do + expect { subject.display_path(root_folder).to_json }.not_to raise_error + expect { subject.display_path(child_folder).to_json }.not_to raise_error + expect { subject.display_path(exception_case).to_json }.not_to raise_error + end + end +end