Browse Source

Fixes #5375 - Kayako import fails when post includes json-files as attachment.

Dominik Klein 4 months ago
parent
commit
5c460457ad

+ 3 - 2
lib/sequencer/unit/import/kayako/post/attachments.rb

@@ -52,8 +52,9 @@ class Sequencer::Unit::Import::Kayako::Post::Attachments < Sequencer::Unit::Base
   def sync(attachment)
     logger.debug { "Downloading attachment #{attachment}" }
 
-    response =  request(
-      api_path: attachment['url_download'].gsub("#{Setting.get('import_kayako_endpoint')}/", ''),
+    response = request(
+      api_path:   attachment['url_download'].gsub("#{Setting.get('import_kayako_endpoint')}/", ''),
+      attachment: true,
     )
 
     return if dry_run

+ 2 - 2
lib/sequencer/unit/import/kayako/requester.rb

@@ -3,7 +3,7 @@
 module Sequencer::Unit::Import::Kayako::Requester
   mattr_accessor :session_id
 
-  def request(api_path:, params: nil)
+  def request(api_path:, params: nil, attachment: false)
     10.times do |iteration|
       response = perform_request(
         api_path: api_path,
@@ -11,7 +11,7 @@ module Sequencer::Unit::Import::Kayako::Requester
       )
 
       if response.is_a? Net::HTTPOK
-        refresh_session_id(response)
+        refresh_session_id(response) if !attachment
         return response
       end
 

+ 34 - 4
spec/lib/sequencer/sequence/import/kayako/post_spec.rb

@@ -38,7 +38,22 @@ RSpec.describe Sequencer::Sequence::Import::Kayako::Post, sequencer: :sequence d
         },
         'attachments'    => [
           {
-            'id'            => 1,
+            'id'            => 3,
+            'name'          => 'example.json',
+            'size'          => 100,
+            'width'         => 0,
+            'height'        => 0,
+            'type'          => 'application/json',
+            'content_id'    => nil,
+            'alt'           => nil,
+            'url'           => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/url',
+            'url_download'  => 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/download',
+            'thumbnails'    => [],
+            'created_at'    => '2021-08-16T08:43:46+00:00',
+            'resource_type' => 'attachment',
+          },
+          {
+            'id'            => 2,
             'name'          => 'example.log',
             'size'          => 1909,
             'width'         => 0,
@@ -51,7 +66,7 @@ RSpec.describe Sequencer::Sequence::Import::Kayako::Post, sequencer: :sequence d
             'thumbnails'    => [],
             'created_at'    => '2021-08-16T08:43:46+00:00',
             'resource_type' => 'attachment',
-          }
+          },
         ],
         'original'       => {
           'id'            => 4,
@@ -131,11 +146,24 @@ RSpec.describe Sequencer::Sequence::Import::Kayako::Post, sequencer: :sequence d
       }
     end
 
+    let(:imported_ticket_article_attachment_json) do
+      {
+        filename:    'example.json',
+        size:        '12',
+        preferences: {
+          'Content-Type': 'application/json'
+        }
+      }
+    end
+
     before do
       # Mock the attachment and inline image download requests.
       used_urls.each do |used_url|
         stub_request(:get, used_url).to_return(status: 200, body: '123', headers: {})
       end
+
+      # Special handling for json situation
+      stub_request(:get, 'https://yours.kayako.com/api/v1/cases/9999/notes/99999/attachments/3/download').to_return(status: 200, body: '{test: 123,}', headers: { 'content-type': 'application/json' })
     end
 
     it 'adds article with inline image' do
@@ -165,12 +193,14 @@ RSpec.describe Sequencer::Sequence::Import::Kayako::Post, sequencer: :sequence d
 
     it 'adds correct number of attachments' do
       process(process_payload)
-      expect(Ticket::Article.last.attachments.size).to eq 2
+      expect(Ticket::Article.last.attachments.size).to eq 3
     end
 
     it 'adds attachment content' do
       process(process_payload)
-      expect(Ticket::Article.last.attachments.last).to have_attributes(imported_ticket_article_attachment)
+
+      expect(Ticket::Article.last.attachments).to include(have_attributes(imported_ticket_article_attachment))
+        .and include(have_attributes(imported_ticket_article_attachment_json))
     end
   end
 end