From ff6b4256e1eddf2d46d09a2d0560a9659b7a7c49 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 4 Dec 2019 11:30:26 +0100 Subject: [PATCH] Fixes #2826 - Invalid stored Twitter credentials block migration. --- ...50249_issue_2460_fix_corrupted_twitter_ids.rb | 16 +++++++++++----- .../issue_2460_fix_corrupted_twitter_ids_spec.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/db/migrate/20191107050249_issue_2460_fix_corrupted_twitter_ids.rb b/db/migrate/20191107050249_issue_2460_fix_corrupted_twitter_ids.rb index e903d4919..7d1c5709c 100644 --- a/db/migrate/20191107050249_issue_2460_fix_corrupted_twitter_ids.rb +++ b/db/migrate/20191107050249_issue_2460_fix_corrupted_twitter_ids.rb @@ -4,13 +4,19 @@ class Issue2460FixCorruptedTwitterIds < ActiveRecord::Migration[5.2] Channel.where(area: 'Twitter::Account').each do |channel| - client = Twitter::REST::Client.new do |config| - config.consumer_key = channel.options['auth']['consumer_key'] - config.consumer_secret = channel.options['auth']['consumer_secret'] - config.access_token = channel.options['auth']['oauth_token'] - config.access_token_secret = channel.options['auth']['oauth_token_secret'] + begin + client = Twitter::REST::Client.new do |config| + config.consumer_key = channel.options['auth']['consumer_key'] + config.consumer_secret = channel.options['auth']['consumer_secret'] + config.access_token = channel.options['auth']['oauth_token'] + config.access_token_secret = channel.options['auth']['oauth_token_secret'] + end + rescue => e + Rails.logger.error "Error while trying to update corrupted Twitter User ID: #{e.message}" end + next if client.blank? + channel.options['user']['id'] = client.user.id.to_s channel.save! diff --git a/spec/db/migrate/issue_2460_fix_corrupted_twitter_ids_spec.rb b/spec/db/migrate/issue_2460_fix_corrupted_twitter_ids_spec.rb index ca67353b5..af2d85bae 100644 --- a/spec/db/migrate/issue_2460_fix_corrupted_twitter_ids_spec.rb +++ b/spec/db/migrate/issue_2460_fix_corrupted_twitter_ids_spec.rb @@ -15,5 +15,15 @@ RSpec.describe Issue2460FixCorruptedTwitterIds, type: :db_migration do .to change { twitter_channel.reload.options[:user][:id] } .to(twitter_api_user_id.to_s) end + + context 'with invalid credentials stored' do + + before { allow(Twitter::REST::Client).to receive(:new).and_raise(Twitter::Error::Unauthorized.new('Could not authenticate you.')) } + + it 'skips the Channel' do + expect { migrate } + .not_to change { twitter_channel.reload.options[:user][:id] } + end + end end end