From a306078ebb8a6fc17ab014c63c51bf57ad5b1573 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 27 Mar 2017 15:22:07 +0200 Subject: [PATCH] Fixed issue #896 - Passwords of imported users get changed. --- app/models/user.rb | 1 + lib/import/helper.rb | 7 ++++-- lib/import/otrs.rb | 1 + lib/import/zendesk.rb | 1 + spec/lib/import/helper_spec.rb | 29 ++++++++++++++++++++----- spec/models/user_spec.rb | 18 ++++++++++++++- test/integration/otrs_import_test.rb | 1 + test/integration/zendesk_import_test.rb | 1 + 8 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index cd6aa9deb..70f772f2c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -980,6 +980,7 @@ raise 'Minimum one user need to have admin permissions' def ensure_password return if password_empty? + return if Setting.get('import_mode') return if PasswordHash.crypted?(password) self.password = PasswordHash.crypt(password) end diff --git a/lib/import/helper.rb b/lib/import/helper.rb index 696f1c279..596508809 100644 --- a/lib/import/helper.rb +++ b/lib/import/helper.rb @@ -9,13 +9,16 @@ module Import raise 'System is not in import mode!' end - # log + def check_system_init_done + return true if !Setting.get('system_init_done') + raise 'System is already system_init_done!' + end + def log(message) thread_no = Thread.current[:thread_no] || '-' Rails.logger.info "thread##{thread_no}: #{message}" end - # utf8 convert def utf8_encode(data) data.each { |key, value| next if !value diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index b471eff30..1fdc96503 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -57,6 +57,7 @@ module Import def checks check_import_mode + check_system_init_done connection_test end diff --git a/lib/import/zendesk.rb b/lib/import/zendesk.rb index 7d0d4c8ae..110124103 100644 --- a/lib/import/zendesk.rb +++ b/lib/import/zendesk.rb @@ -62,6 +62,7 @@ module Import::Zendesk def checks check_import_mode + check_system_init_done connection_test end end diff --git a/spec/lib/import/helper_spec.rb b/spec/lib/import/helper_spec.rb index 78ea79db8..1ac356672 100644 --- a/spec/lib/import/helper_spec.rb +++ b/spec/lib/import/helper_spec.rb @@ -4,13 +4,30 @@ require 'lib/import/helper_examples' RSpec.describe Import::Helper do it_behaves_like 'Import::Helper' - it 'checks if import_mode is active' do - expect(Setting).to receive(:get).with('import_mode').and_return(true) - expect( described_class.check_import_mode ).to be true + context 'import mode' do + + it 'checks if import_mode is active' do + expect(Setting).to receive(:get).with('import_mode').and_return(true) + expect( described_class.check_import_mode ).to be true + end + + it 'throws an exception if import_mode is disabled' do + expect(Setting).to receive(:get).with('import_mode').and_return(false) + expect { described_class.check_import_mode }.to raise_error(RuntimeError) + end end - it 'throws an exception if import_mode is disabled' do - expect(Setting).to receive(:get).with('import_mode').and_return(false) - expect { described_class.check_import_mode }.to raise_error(RuntimeError) + context 'system init' do + + it 'checks if system_init_done is active' do + expect(Setting).to receive(:get).with('system_init_done').and_return(false) + expect( described_class.check_system_init_done ).to be true + end + + it 'throws an exception if system_init_done is disabled' do + expect(Setting).to receive(:get).with('system_init_done').and_return(true) + expect { described_class.check_system_init_done }.to raise_error(RuntimeError) + end end + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3625352d4..7c932eebb 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -104,7 +104,7 @@ RSpec.describe User do end end - context '.by_reset_token' do + context '#by_reset_token' do it 'returns a User instance for existing tokens' do token = create(:token_password_reset) @@ -133,4 +133,20 @@ RSpec.describe User do end end + context 'import' do + + it "doesn't change imported passwords" do + + # mock settings calls + expect(Setting).to receive(:get).with('import_mode').and_return(true) + allow(Setting).to receive(:get) + + user = build(:user, password: '{sha2}dd9c764fa7ea18cd992c8600006d3dc3ac983d1ba22e9ba2d71f6207456be0ba') # zammad + expect { + user.save + }.to_not change { + user.password + } + end + end end diff --git a/test/integration/otrs_import_test.rb b/test/integration/otrs_import_test.rb index 47d078278..307a949ae 100644 --- a/test/integration/otrs_import_test.rb +++ b/test/integration/otrs_import_test.rb @@ -13,6 +13,7 @@ class OtrsImportTest < ActiveSupport::TestCase Setting.set('import_otrs_endpoint', ENV['IMPORT_OTRS_ENDPOINT']) Setting.set('import_otrs_endpoint_key', ENV['IMPORT_OTRS_ENDPOINT_KEY']) Setting.set('import_mode', true) + Setting.set('system_init_done', false) Import::OTRS.start # check settings items diff --git a/test/integration/zendesk_import_test.rb b/test/integration/zendesk_import_test.rb index dcff85b07..709ff1f2f 100644 --- a/test/integration/zendesk_import_test.rb +++ b/test/integration/zendesk_import_test.rb @@ -17,6 +17,7 @@ class ZendeskImportTest < ActiveSupport::TestCase Setting.set('import_zendesk_endpoint_key', ENV['IMPORT_ZENDESK_ENDPOINT_KEY']) Setting.set('import_zendesk_endpoint_username', ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME']) Setting.set('import_mode', true) + Setting.set('system_init_done', false) Import::Zendesk.start # check statistic count