From e57d2f5cb806de69fe904846383c347ce0f77d1f Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 26 May 2017 11:33:12 +0800 Subject: [PATCH] FIX: Don't do anything if avatar url returns an invalid status code. --- app/models/user_avatar.rb | 2 ++ spec/models/user_avatar_spec.rb | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index 87e674d0cff..0c49693ddbf 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -75,6 +75,8 @@ class UserAvatar < ActiveRecord::Base follow_redirect: true ) + return unless tempfile + ext = FastImage.type(tempfile).to_s tempfile.rewind diff --git a/spec/models/user_avatar_spec.rb b/spec/models/user_avatar_spec.rb index 066cb97ab6f..46269b6a341 100644 --- a/spec/models/user_avatar_spec.rb +++ b/spec/models/user_avatar_spec.rb @@ -1,10 +1,8 @@ require 'rails_helper' describe UserAvatar do - let(:avatar){ - user = Fabricate(:user) - user.create_user_avatar! - } + let(:user) { Fabricate(:user) } + let(:avatar) { user.create_user_avatar! } it 'can update gravatars' do temp = Tempfile.new('test') @@ -18,7 +16,7 @@ describe UserAvatar do expect(avatar.gravatar_upload).not_to eq(nil) end - context '#import_url_for_user' do + context '.import_url_for_user' do it 'creates user_avatar record if missing' do user = Fabricate(:user) @@ -44,7 +42,20 @@ describe UserAvatar do user.reload expect(user.uploaded_avatar_id).to eq(1) - expect(user.user_avatar.custom_upload_id).not_to eq(nil) + expect(user.user_avatar.custom_upload_id).to eq(Upload.last.id) + end + + describe 'when avatar url returns an invalid status code' do + it 'should not do anything' do + url = "http://thisfakesomething.something.com/" + stub_request(:head, url).to_return(status: 404) + UserAvatar.import_url_for_user(url, user) + + user.reload + + expect(user.uploaded_avatar_id).to eq(nil) + expect(user.user_avatar.custom_upload_id).to eq(nil) + end end end end