diff --git a/lib/sequencer/unit/import/freshdesk/conversation/inline_images.rb b/lib/sequencer/unit/import/freshdesk/conversation/inline_images.rb index 6baa5c810..d9a77ebd3 100644 --- a/lib/sequencer/unit/import/freshdesk/conversation/inline_images.rb +++ b/lib/sequencer/unit/import/freshdesk/conversation/inline_images.rb @@ -21,14 +21,17 @@ class Sequencer end def self.inline_data(freshdesk_url) - @cache ||= {} - return @cache[freshdesk_url] if @cache[freshdesk_url] + clean_freshdesk_url = freshdesk_url.gsub(%r{^cid:}, '') + return if !%r{^(http|https)://.+?$}.match?(clean_freshdesk_url) - image_data = download(freshdesk_url) + @cache ||= {} + return @cache[clean_freshdesk_url] if @cache[clean_freshdesk_url] + + image_data = download(clean_freshdesk_url) return if image_data.blank? - @cache[freshdesk_url] = "data:image/png;base64,#{Base64.strict_encode64(image_data)}" - @cache[freshdesk_url] + @cache[clean_freshdesk_url] = "data:image/png;base64,#{Base64.strict_encode64(image_data)}" + @cache[clean_freshdesk_url] end def self.download(freshdesk_url) diff --git a/spec/lib/sequencer/sequence/import/freshdesk/conversation_spec.rb b/spec/lib/sequencer/sequence/import/freshdesk/conversation_spec.rb index 17ad6da5f..fbd8b6bef 100644 --- a/spec/lib/sequencer/sequence/import/freshdesk/conversation_spec.rb +++ b/spec/lib/sequencer/sequence/import/freshdesk/conversation_spec.rb @@ -5,10 +5,10 @@ require 'rails_helper' RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::Conversation, sequencer: :sequence do context 'when importing conversations from Freshdesk' do - + let(:inline_image_url) { 'https://eucattachment.freshdesk.com/inline/attachment?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ODAwMTIyMjY4NTMsImRvbWFpbiI6InphbW1hZC5mcmVzaGRlc2suY29tIiwiYWNjb3VudF9pZCI6MTg5MDU2MH0.705lNehzm--aO36CGFg0SW73j0NG3UWcRcN1_DXgtwc' } let(:resource) do { - 'body' => "
\n
Let's see if inline images work in a subsequent article:
\n
\n
", 'body_text' => "Let's see if inline images work in a subsequent article:", + 'body' => "
\n
Let's see if inline images work in a subsequent article:
\n
\n
", 'body_text' => "Let's see if inline images work in a subsequent article:", 'id' => 80_027_218_656, 'incoming' => false, 'private' => true, @@ -76,17 +76,21 @@ RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::Conversation, sequencer end end - it 'adds article with inline image' do - expect { process(process_payload) }.to change(Ticket::Article, :count).by(1) + shared_examples 'import article' do + it 'adds article with inline image' do + expect { process(process_payload) }.to change(Ticket::Article, :count).by(1) + end + + it 'correct attributes for added article' do + process(process_payload) + expect(Ticket::Article.last).to have_attributes( + to: 'info@zammad.org', + body: "\n
\n
Let's see if inline images work in a subsequent article:
\n
\n
\n", + ) + end end - it 'correct attributes for added article' do - process(process_payload) - expect(Ticket::Article.last).to have_attributes( - to: 'info@zammad.org', - body: "\n
\n
Let's see if inline images work in a subsequent article:
\n
\n
\n", - ) - end + include_examples 'import article' it 'updates already existing article' do expect do @@ -111,5 +115,29 @@ RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::Conversation, sequencer } ) end + + context 'when handling special inline images' do + context 'when inline image source contains special urls (e.g. "cid:https://...")' do + let(:inline_image_url) { 'cid:https://eucattachment.freshdesk.com/inline/attachment?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ODAwMTIyMjY4NTMsImRvbWFpbiI6InphbW1hZC5mcmVzaGRlc2suY29tIiwiYWNjb3VudF9pZCI6MTg5MDU2MH0.705lNehzm--aO36CGFg0SW73j0NG3UWcRcN1_DXgtwc' } + + include_examples 'import article' + end + + context 'when inline image source contains broken urls' do + let(:inline_image_url) { 'broken_image_url' } + + it 'skips image download with broken inline image url' do + expect { process(process_payload) }.to change(Ticket::Article, :count).by(1) + end + + it 'correct attributes for added article' do + process(process_payload) + expect(Ticket::Article.last).to have_attributes( + to: 'info@zammad.org', + body: "
\n
Let's see if inline images work in a subsequent article:
\n
\n
", + ) + end + end + end end end