Specify updated_by_id in LDAP deactivation sequencer unit (fixes #2111)

This commit is contained in:
Ryan Lue 2018-07-19 18:25:20 +08:00
parent 903777588a
commit c61dd3eab4
2 changed files with 34 additions and 10 deletions

View file

@ -10,16 +10,11 @@ class Sequencer
def process
return if dry_run
# we need to update in slices since some DBs
# have a limit for IN length
lost_ids.each_slice(5000) do |slice|
# we need to instanciate every entry and set
# the active state this way to send notifications
# to the client
::User.where(id: slice).each do |user|
user.update!(active: false)
end
# Why not use `#update_all`?
# It bypasses validations/callbacks
# (which are used to send notifications to the client)
::User.where(id: lost_ids).find_each do |user|
user.update!(active: false, updated_by_id: 1)
end
end
end

View file

@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe Sequencer::Unit::Import::Ldap::Users::Lost::Deactivate, sequencer: :unit do
let!(:lost_users) { create_list(:user, sample_length, attributes) }
let(:sample_length) { 2 }
context 'when provided ids of active users' do
let(:attributes) { { active: true } }
it 'deactivates them' do
expect { process(lost_ids: lost_users.pluck(:id), dry_run: false) }
.to change { lost_users.each(&:reload).pluck(:active) }.to(Array.new(sample_length, false))
end
end
context 'when provided ids of users with any `updated_by_id`' do
# ordinarily, a History log's created_by_id is based on this value (or UserInfo.current_user_id),
# but this Sequencer unit is expected to override it
let(:attributes) { { updated_by_id: 2 } }
it 'enforces created_by_id => 1 in newly created History logs' do
expect { process(lost_ids: lost_users.pluck(:id), dry_run: false) }
.to change { History.count }.by(sample_length)
expect(History.last(sample_length).pluck(:created_by_id))
.to eq(Array.new(sample_length, 1))
end
end
end