Force UTF-8 encoding for Exchange folders (fixes #2152)

This commit is contained in:
Ryan Lue 2018-08-13 15:10:24 +08:00 committed by Ryan Lue
parent 2656ee965c
commit 7d63868996
2 changed files with 39 additions and 12 deletions

View file

@ -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

View file

@ -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