From de85bb0a3951f56d68dbc95bb671ef4ef17b4d24 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 1 Oct 2018 14:20:50 +0800 Subject: [PATCH] FIX: Don't update user_profile URLs unless upload is persisted. --- lib/upload_recovery.rb | 2 +- spec/lib/upload_recovery_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb index 53762ee5fc2..fc64839a557 100644 --- a/lib/upload_recovery.rb +++ b/lib/upload_recovery.rb @@ -64,7 +64,7 @@ class UploadRecovery puts "#{background}" else recover_user_profile_background(sha1, user_profile.user_id) do |upload| - user_profile.update!("#{column}" => upload.url) + user_profile.update!("#{column}" => upload.url) if upload.persisted? end end end diff --git a/spec/lib/upload_recovery_spec.rb b/spec/lib/upload_recovery_spec.rb index f8e00ac4ecc..5e8e4500a92 100644 --- a/spec/lib/upload_recovery_spec.rb +++ b/spec/lib/upload_recovery_spec.rb @@ -36,6 +36,7 @@ RSpec.describe UploadRecovery do after do [upload, upload2].each do |u| + next if u public_path = "#{Discourse.store.public_dir}#{u.url}" [ @@ -140,5 +141,31 @@ RSpec.describe UploadRecovery do expect(user_profile.profile_background).to eq(upload.url) expect(user_profile.card_background).to eq(upload.url) end + + describe 'for a bad upload' do + it 'should not update the urls' do + user_profile = user.user_profile + upload.destroy! + + profile_background = user_profile.profile_background.sub("default", "X") + card_background = user_profile.card_background.sub("default", "X") + + user_profile.update_columns( + profile_background: profile_background, + card_background: card_background + ) + + SiteSetting.authorized_extensions = '' + + expect do + upload_recovery.recover_user_profile_backgrounds + end.to_not change { Upload.count } + + user_profile.reload + + expect(user_profile.profile_background).to eq(profile_background) + expect(user_profile.card_background).to eq(card_background) + end + end end end