Fixes #3167: Active Google channels cause Email channels to loose email addresses.

This commit is contained in:
Thorsten Eckel 2020-08-28 11:41:38 +02:00
parent 3d192af3d0
commit b6ae957ac2
2 changed files with 22 additions and 1 deletions

View file

@ -346,7 +346,15 @@ get instance of channel driver
options[:inbound][:options][:password] = result[:access_token]
options[:outbound][:options][:password] = result[:access_token]
save!
return if new_record?
# ATTENTION: We don't want to execute any other callbacks here
# because `after_initialize` leaks the current scope of the Channel class
# as described here: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-new
# which leads to unexpected effects like:
# Channel.where(area: 'Google::Account').limit(1).find_each { |c| puts Channel.all.to_sql }
# => "SELECT "channels".* FROM "channels" WHERE "channels"."area" = 'Google::Account'"
update_column(:options, options) # rubocop:disable Rails/SkipsModelValidations
rescue => e
logger.error e
raise "Failed to refresh XOAUTH2 access_token of provider '#{options[:auth][:provider]}'! #{e.inspect}"

View file

@ -62,4 +62,17 @@ RSpec.describe 'Gmail XOAUTH2' do # rubocop:disable RSpec/DescribeClass
expect(result[:result]).to eq('ok')
end
end
context 'when non-Google channels are present' do
let!(:email_address) { create(:email_address, channel: create(:channel, area: 'Some::Other')) }
before do
channel
end
it "doesn't remove email address assignments" do
expect { Channel.where(area: 'Google::Account').find_each {} }.not_to change { email_address.reload.channel_id }
end
end
end