From 1f3da97cc9a3bbd3bded710b17ec875d85f2ccad Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 15 Aug 2018 18:05:54 +0800 Subject: [PATCH] Avoid saving users during LDAP import if unchanged (fixes #2187) --- .../unit/import/common/model/save.rb | 5 +- .../unit/import/common/model/save_spec.rb | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 spec/lib/sequencer/unit/import/common/model/save_spec.rb diff --git a/lib/sequencer/unit/import/common/model/save.rb b/lib/sequencer/unit/import/common/model/save.rb index de0595bf1..ea8d12646 100644 --- a/lib/sequencer/unit/import/common/model/save.rb +++ b/lib/sequencer/unit/import/common/model/save.rb @@ -6,11 +6,14 @@ class Sequencer module Common module Model class Save < Sequencer::Unit::Base + prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure - uses :instance, :dry_run + uses :instance, :action, :dry_run provides :instance + skip_action :skipped, :failed, :unchanged + def process return if dry_run return if instance.blank? diff --git a/spec/lib/sequencer/unit/import/common/model/save_spec.rb b/spec/lib/sequencer/unit/import/common/model/save_spec.rb new file mode 100644 index 000000000..d9894c8fe --- /dev/null +++ b/spec/lib/sequencer/unit/import/common/model/save_spec.rb @@ -0,0 +1,47 @@ +require 'rails_helper' + +RSpec.describe Sequencer::Unit::Import::Common::Model::Save, sequencer: :unit do + let(:user) { instance_double('User') } + + before { allow(user).to receive(:save!) } + + context 'for action: :created' do + it 'calls #save!' do + process(action: :created, instance: user, dry_run: false) + + expect(user).to have_received(:save!) + end + end + + context 'for action: :updated' do + it 'calls #save!' do + process(action: :updated, instance: user, dry_run: false) + + expect(user).to have_received(:save!) + end + end + + context 'for action: :unchanged' do + it 'avoids calling #save!' do + process(action: :unchanged, instance: user, dry_run: false) + + expect(user).not_to have_received(:save!) + end + end + + context 'for action: :skipped' do + it 'avoids calling #save!' do + process(action: :skipped, instance: user, dry_run: false) + + expect(user).not_to have_received(:save!) + end + end + + context 'for action: :failed' do + it 'avoids calling #save!' do + process(action: :failed, instance: user, dry_run: false) + + expect(user).not_to have_received(:save!) + end + end +end