Follow up: Revoked commit 50215b0b8e
which fixes issue 1663 - remote_id-s case sensitivity and storage format should be decided per remote_id / remote.
This commit is contained in:
parent
50215b0b8e
commit
21421f0695
10 changed files with 83 additions and 140 deletions
|
@ -5,10 +5,6 @@ class ExternalSync < ApplicationModel
|
|||
|
||||
class << self
|
||||
|
||||
def sanitized_source_id(source_id)
|
||||
Digest::SHA2.hexdigest(source_id)
|
||||
end
|
||||
|
||||
def changed?(object:, previous_changes: {}, current_changes:)
|
||||
changed = false
|
||||
previous_changes ||= {}
|
||||
|
|
|
@ -7,6 +7,7 @@ class Sequencer
|
|||
def self.sequence
|
||||
[
|
||||
'Import::Exchange::FolderContact::RemoteId',
|
||||
'Import::Common::RemoteId::CaseSensitive',
|
||||
'Import::Exchange::FolderContact::Mapping',
|
||||
'Import::Common::Model::Skip::Blank::Mapped',
|
||||
'Common::ModelClass::User',
|
||||
|
@ -15,6 +16,7 @@ class Sequencer
|
|||
'Import::Common::Model::Associations::Extract',
|
||||
'Import::Common::User::Attributes::Downcase',
|
||||
'Import::Common::User::Email::CheckValidity',
|
||||
'Import::Ldap::User::Lookup::Attributes',
|
||||
'Import::Common::Model::Attributes::AddByIds',
|
||||
'Import::Common::Model::Update',
|
||||
'Import::Common::Model::Create',
|
||||
|
|
|
@ -19,8 +19,8 @@ class Sequencer
|
|||
|
||||
def up_to_date?
|
||||
return false if entry.blank?
|
||||
return true if entry.source_id == sanitized_remote_id
|
||||
entry.update!(source_id: sanitized_remote_id)
|
||||
return true if entry.source_id == remote_id
|
||||
entry.update!(source_id: remote_id)
|
||||
true
|
||||
end
|
||||
|
||||
|
@ -37,15 +37,11 @@ class Sequencer
|
|||
def create
|
||||
::ExternalSync.create(
|
||||
source: external_sync_source,
|
||||
source_id: sanitized_remote_id,
|
||||
source_id: remote_id,
|
||||
object: model_class.name,
|
||||
o_id: instance.id
|
||||
)
|
||||
end
|
||||
|
||||
def sanitized_remote_id
|
||||
@sanitized_remote_id ||= ::ExternalSync.sanitized_source_id(remote_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,10 +14,10 @@ class Sequencer
|
|||
provides :instance
|
||||
|
||||
def process
|
||||
return if !synced_instance
|
||||
return if entry.blank?
|
||||
|
||||
state.provide(:instance) do
|
||||
model_class.find(synced_instance.o_id)
|
||||
model_class.find(entry.o_id)
|
||||
end
|
||||
rescue => e
|
||||
handle_failure(e)
|
||||
|
@ -25,47 +25,8 @@ class Sequencer
|
|||
|
||||
private
|
||||
|
||||
def synced_instance
|
||||
@synced_instance ||= correct_entry || corrected_entry
|
||||
end
|
||||
|
||||
def correct_entry
|
||||
::ExternalSync.find_by(
|
||||
source: external_sync_source,
|
||||
source_id: sanitized_remote_id,
|
||||
object: model_class.name,
|
||||
)
|
||||
end
|
||||
|
||||
def sanitized_remote_id
|
||||
@sanitized_remote_id ||= ::ExternalSync.sanitized_source_id(remote_id)
|
||||
end
|
||||
|
||||
def corrected_entry
|
||||
return if obsolete_entry.blank?
|
||||
obsolete_entry.update!(source_id: sanitized_remote_id)
|
||||
obsolete_entry
|
||||
end
|
||||
|
||||
def obsolete_entry
|
||||
@obsolete_entry ||= begin
|
||||
if Rails.application.config.db_case_sensitive
|
||||
case_sensitive_entry
|
||||
else
|
||||
case_insensitive_entry
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def case_sensitive_entry
|
||||
::ExternalSync.where(
|
||||
source: external_sync_source,
|
||||
object: model_class.name,
|
||||
).where('LOWER(source_id) = LOWER(?)', remote_id).first
|
||||
end
|
||||
|
||||
def case_insensitive_entry
|
||||
::ExternalSync.find_by(
|
||||
def entry
|
||||
@entry ||= ::ExternalSync.find_by(
|
||||
source: external_sync_source,
|
||||
source_id: remote_id,
|
||||
object: model_class.name,
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
class Sequencer
|
||||
class Unit
|
||||
module Import
|
||||
module Common
|
||||
module RemoteId
|
||||
class CaseInsensitive < Sequencer::Unit::Base
|
||||
|
||||
uses :remote_id
|
||||
provides :remote_id
|
||||
|
||||
def process
|
||||
state.provide(:remote_id) do
|
||||
remote_id.downcase
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/sequencer/unit/import/common/remote_id/case_sensitive.rb
Normal file
21
lib/sequencer/unit/import/common/remote_id/case_sensitive.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
class Sequencer
|
||||
class Unit
|
||||
module Import
|
||||
module Common
|
||||
module RemoteId
|
||||
class CaseSensitive < Sequencer::Unit::Base
|
||||
|
||||
uses :remote_id
|
||||
provides :remote_id
|
||||
|
||||
def process
|
||||
state.provide(:remote_id) do
|
||||
Digest::SHA2.hexdigest(state.use(:remote_id))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,27 +2,6 @@ require 'rails_helper'
|
|||
|
||||
RSpec.describe ExternalSync do
|
||||
|
||||
context '#sanitized_source_id' do
|
||||
|
||||
let(:source_id) { 'AbCdEfG124' }
|
||||
|
||||
it 'sanitizes source ids' do
|
||||
sanitized_source_id = described_class.sanitized_source_id(source_id)
|
||||
expect(sanitized_source_id).to_not eq(source_id)
|
||||
end
|
||||
|
||||
it 'returns case insenstive value' do
|
||||
sanitized_source_id = described_class.sanitized_source_id(source_id)
|
||||
expect(sanitized_source_id).to eq(sanitized_source_id.downcase)
|
||||
end
|
||||
|
||||
it 'avoids case sensitive collitions' do
|
||||
sanitized_source_id = described_class.sanitized_source_id(source_id)
|
||||
sanitized_source_id_downcased = described_class.sanitized_source_id(source_id.downcase)
|
||||
expect(sanitized_source_id).to_not eq(sanitized_source_id_downcased)
|
||||
end
|
||||
end
|
||||
|
||||
context '#changed?' do
|
||||
|
||||
it 'keeps ActiveRecord instance unchanged on local but no remote changes' do
|
||||
|
|
|
@ -9,7 +9,7 @@ RSpec.describe Sequencer::Unit::Import::Common::Model::Lookup::ExternalSync, seq
|
|||
|
||||
ExternalSync.create(
|
||||
source: external_sync_source,
|
||||
source_id: ExternalSync.sanitized_source_id(remote_id),
|
||||
source_id: remote_id,
|
||||
o_id: user.id,
|
||||
object: user.class,
|
||||
)
|
||||
|
@ -22,68 +22,4 @@ RSpec.describe Sequencer::Unit::Import::Common::Model::Lookup::ExternalSync, seq
|
|||
|
||||
expect(provided[:instance]).to eq(user)
|
||||
end
|
||||
|
||||
context 'obsolete plain remote_id' do
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:external_sync_source) { 'test' }
|
||||
let(:remote_id) { 'AbCdEfG' }
|
||||
|
||||
it 'finds model_class instance' do
|
||||
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
|
||||
|
||||
it 'corrects external sync entry' do
|
||||
entry = ExternalSync.create(
|
||||
source: external_sync_source,
|
||||
source_id: remote_id,
|
||||
o_id: user.id,
|
||||
object: user.class,
|
||||
)
|
||||
|
||||
process(
|
||||
remote_id: remote_id,
|
||||
model_class: user.class,
|
||||
external_sync_source: external_sync_source,
|
||||
)
|
||||
|
||||
entry.reload
|
||||
|
||||
expect(entry.source_id).to eq(ExternalSync.sanitized_source_id(remote_id))
|
||||
end
|
||||
|
||||
it 'operates case agnostic' do
|
||||
entry = ExternalSync.create(
|
||||
source: external_sync_source,
|
||||
source_id: remote_id.downcase,
|
||||
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)
|
||||
|
||||
entry.reload
|
||||
|
||||
expect(entry.source_id).to eq(ExternalSync.sanitized_source_id(remote_id))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Sequencer::Unit::Import::Common::RemoteId::CaseInsensitive, sequencer: :unit do
|
||||
|
||||
it 'overwrites the remote_id with the downcased version of it' do
|
||||
|
||||
remote_id = 'SomeRandom@EmailExample.com'
|
||||
|
||||
provided = process(
|
||||
remote_id: remote_id,
|
||||
)
|
||||
|
||||
expect(provided[:remote_id]).to eq(remote_id.downcase)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Sequencer::Unit::Import::Common::RemoteId::CaseSensitive, sequencer: :unit do
|
||||
|
||||
it 'overwrites the remote_id with the the SHA-2 hashed version of it' do
|
||||
|
||||
remote_id = 'Zammad!'
|
||||
hashed = '07071585f063b37b8288021f541d8c3cee3265f34e258c8b0bd926378ce03c97'
|
||||
|
||||
provided = process(
|
||||
remote_id: remote_id,
|
||||
)
|
||||
|
||||
expect(provided[:remote_id]).to eq(hashed)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue