From 492ca261cef2e6398d9331992a8af1d126a92be2 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 21 Nov 2017 18:46:26 +0100 Subject: [PATCH] Fixed bug #1663 - Exchange integration tries to import user twice and fails. --- .../common/model/attributes/remote_id.rb | 2 +- .../common/model/attributes/remote_id_spec.rb | 70 +++++++++++++++++++ .../common/model/external_sync/lookup_spec.rb | 25 +++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 spec/lib/sequencer/unit/import/common/model/attributes/remote_id_spec.rb create mode 100644 spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb diff --git a/lib/sequencer/unit/import/common/model/attributes/remote_id.rb b/lib/sequencer/unit/import/common/model/attributes/remote_id.rb index 412fbb773..ca2dae12c 100644 --- a/lib/sequencer/unit/import/common/model/attributes/remote_id.rb +++ b/lib/sequencer/unit/import/common/model/attributes/remote_id.rb @@ -12,7 +12,7 @@ class Sequencer def process state.provide(:remote_id) do - resource.fetch(attribute) + resource.fetch(attribute).dup.to_s.downcase end rescue KeyError => e handle_failure(e) diff --git a/spec/lib/sequencer/unit/import/common/model/attributes/remote_id_spec.rb b/spec/lib/sequencer/unit/import/common/model/attributes/remote_id_spec.rb new file mode 100644 index 000000000..b82648474 --- /dev/null +++ b/spec/lib/sequencer/unit/import/common/model/attributes/remote_id_spec.rb @@ -0,0 +1,70 @@ +require 'rails_helper' + +RSpec.describe Sequencer::Unit::Import::Common::Model::Attributes::RemoteId, sequencer: :unit do + + it 'takes remote_id from id' do + parameters = { + resource: { + id: '123abc', + } + } + + provided = process(parameters) + + expect(provided).to include(remote_id: '123abc') + end + + it 'takes remote_id from attribute method result' do + parameters = { + resource: { + other_attribute: '123abc', + } + } + + provided = process(parameters) do |instance| + expect(instance).to receive(:attribute).and_return(:other_attribute) + end + + expect(provided).to include(remote_id: '123abc') + end + + it 'converts value to a String' do + parameters = { + resource: { + id: 1337, + } + } + + provided = process(parameters) + + expect(provided).to include(remote_id: '1337') + end + + it 'downcases the value to prevent case sensivity issues with the ORM' do + parameters = { + resource: { + id: 'AbCdEfG', + } + } + + provided = process(parameters) + + expect(provided[:remote_id]).to eq(parameters[:resource][:id].downcase) + end + + it 'duplicates the value to prevent attribute changes' do + parameters = { + resource: { + id: 'this is', + } + } + + provided = process(parameters) + + expect(provided[:remote_id]).to eq(parameters[:resource][:id]) + + parameters[:resource][:id] += ' a test' + + expect(provided[:remote_id]).not_to eq(parameters[:resource][:id]) + end +end diff --git a/spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb b/spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb new file mode 100644 index 000000000..58c33b2bd --- /dev/null +++ b/spec/lib/sequencer/unit/import/common/model/external_sync/lookup_spec.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +RSpec.describe Sequencer::Unit::Import::Common::Model::ExternalSync::Lookup, sequencer: :unit do + + it 'finds model_class instances by remote_id' do + user = create(:user) + external_sync_source = 'test' + remote_id = '1337' + + ExternalSync.create( + source: external_sync_source, + source_id: remote_id, + o_id: user.id, + object: user.class, + ) + + provided = process( + remote_id: remote_id, + model_class: user.class, + external_sync_source: external_sync_source, + ) + + expect(provided[:instance]).to eq(user) + end +end