2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserAvatar do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-05-25 23:33:12 -04:00
|
|
|
let(:avatar) { user.create_user_avatar! }
|
2014-05-22 03:37:02 -04:00
|
|
|
|
2018-08-05 22:42:03 -04:00
|
|
|
describe '#update_gravatar!' do
|
|
|
|
let(:temp) { Tempfile.new('test') }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:upload) { Fabricate(:upload, user: user) }
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
describe "when working" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
temp.binmode
|
|
|
|
# tiny valid png
|
2019-07-30 23:16:03 -04:00
|
|
|
temp.write(Base64.decode64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="))
|
2018-10-18 05:02:54 -04:00
|
|
|
temp.rewind
|
|
|
|
FileHelper.expects(:download).returns(temp)
|
|
|
|
end
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
after do
|
|
|
|
temp.unlink
|
|
|
|
end
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
it 'can update gravatars' do
|
|
|
|
freeze_time Time.now
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
expect { avatar.update_gravatar! }.to change { Upload.count }.by(1)
|
|
|
|
expect(avatar.gravatar_upload).to eq(Upload.last)
|
|
|
|
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
|
|
|
|
expect(user.reload.uploaded_avatar).to eq(nil)
|
2018-12-13 00:26:07 -05:00
|
|
|
|
|
|
|
expect do
|
|
|
|
avatar.destroy
|
|
|
|
end.to_not change { Upload.count }
|
2019-07-30 23:16:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates gravatars even if uploads have been disabled" do
|
|
|
|
SiteSetting.authorized_extensions = ""
|
2018-12-13 00:26:07 -05:00
|
|
|
|
2019-07-30 23:16:03 -04:00
|
|
|
expect { avatar.update_gravatar! }.to change { Upload.count }.by(1)
|
|
|
|
expect(avatar.gravatar_upload).to eq(Upload.last)
|
2018-10-18 05:02:54 -04:00
|
|
|
end
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
describe 'when user has an existing custom upload' do
|
|
|
|
it "should not change the user's uploaded avatar" do
|
|
|
|
user.update!(uploaded_avatar: upload)
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
avatar.update!(
|
|
|
|
custom_upload: upload,
|
|
|
|
gravatar_upload: Fabricate(:upload, user: user)
|
|
|
|
)
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
avatar.update_gravatar!
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
expect(upload.reload).to eq(upload)
|
|
|
|
expect(user.reload.uploaded_avatar).to eq(upload)
|
|
|
|
expect(avatar.reload.custom_upload).to eq(upload)
|
|
|
|
expect(avatar.gravatar_upload).to eq(Upload.last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user has an existing gravatar' do
|
|
|
|
it "should update the user's uploaded avatar correctly" do
|
|
|
|
user.update!(uploaded_avatar: upload)
|
|
|
|
avatar.update!(gravatar_upload: upload)
|
|
|
|
|
|
|
|
avatar.update_gravatar!
|
|
|
|
|
2018-12-13 00:26:07 -05:00
|
|
|
# old upload to be cleaned up via clean_up_uploads
|
|
|
|
expect(Upload.find_by(id: upload.id)).not_to eq(nil)
|
2018-10-18 05:02:54 -04:00
|
|
|
|
|
|
|
new_upload = Upload.last
|
|
|
|
|
|
|
|
expect(user.reload.uploaded_avatar).to eq(new_upload)
|
|
|
|
expect(avatar.reload.gravatar_upload).to eq(new_upload)
|
|
|
|
end
|
2018-08-05 22:42:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
describe "when failing" do
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
it "always update 'last_gravatar_download_attempt'" do
|
|
|
|
freeze_time Time.now
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
FileHelper.expects(:download).raises(SocketError)
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 21:51:34 -04:00
|
|
|
expect do
|
|
|
|
expect { avatar.update_gravatar! }.to raise_error(SocketError)
|
|
|
|
end.to_not change { Upload.count }
|
2018-08-05 22:42:03 -04:00
|
|
|
|
2018-10-18 05:02:54 -04:00
|
|
|
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
|
2018-08-05 22:42:03 -04:00
|
|
|
end
|
2018-10-18 05:02:54 -04:00
|
|
|
|
2018-08-05 22:42:03 -04:00
|
|
|
end
|
2018-10-18 05:02:54 -04:00
|
|
|
|
2018-10-22 20:43:14 -04:00
|
|
|
describe "404 should be silent, nothing to do really" do
|
|
|
|
|
|
|
|
it "does nothing when avatar is 404" do
|
2020-04-07 17:35:42 -04:00
|
|
|
SecureRandom.stubs(:urlsafe_base64).returns("5555")
|
2018-10-22 20:43:14 -04:00
|
|
|
freeze_time Time.now
|
|
|
|
|
2020-04-07 17:35:42 -04:00
|
|
|
stub_request(:get, "https://www.gravatar.com/avatar/#{avatar.user.email_hash}.png?d=404&reset_cache=5555&s=360").
|
2018-10-22 20:43:14 -04:00
|
|
|
to_return(status: 404, body: "", headers: {})
|
|
|
|
|
|
|
|
expect do
|
|
|
|
avatar.update_gravatar!
|
|
|
|
end.to_not change { Upload.count }
|
|
|
|
|
|
|
|
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 13:39:56 -05:00
|
|
|
it "should not raise an error when there's no primary_email" do
|
|
|
|
avatar.user.primary_email.destroy
|
|
|
|
avatar.user.reload
|
|
|
|
|
|
|
|
# If raises an error, test fails
|
|
|
|
avatar.update_gravatar!
|
|
|
|
end
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
2016-09-19 01:10:02 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe '.import_url_for_user' do
|
2016-09-19 01:10:02 -04:00
|
|
|
|
|
|
|
it 'creates user_avatar record if missing' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_avatar.destroy
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
FileHelper.stubs(:download).returns(file_from_fixtures("logo.png"))
|
|
|
|
|
|
|
|
UserAvatar.import_url_for_user("logo.png", user)
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.uploaded_avatar_id).not_to eq(nil)
|
|
|
|
expect(user.user_avatar.custom_upload_id).to eq(user.uploaded_avatar_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can leave gravatar alone' do
|
2019-01-31 21:26:08 -05:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
|
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
user.user_avatar.update_columns(gravatar_upload_id: upload.id)
|
2016-09-19 01:10:02 -04:00
|
|
|
|
2018-08-13 02:48:15 -04:00
|
|
|
stub_request(:get, "http://thisfakesomething.something.com/")
|
|
|
|
.to_return(status: 200, body: file_from_fixtures("logo.png"), headers: {})
|
|
|
|
|
|
|
|
url = "http://thisfakesomething.something.com/"
|
|
|
|
|
|
|
|
expect do
|
|
|
|
UserAvatar.import_url_for_user(url, user, override_gravatar: false)
|
|
|
|
end.to change { Upload.count }.by(1)
|
2016-09-19 01:10:02 -04:00
|
|
|
|
|
|
|
user.reload
|
2019-01-31 21:26:08 -05:00
|
|
|
expect(user.uploaded_avatar_id).to eq(upload.id)
|
|
|
|
|
|
|
|
last_id = Upload.last.id
|
|
|
|
|
|
|
|
expect(last_id).not_to eq(upload.id)
|
|
|
|
expect(user.user_avatar.custom_upload_id).to eq(last_id)
|
2017-05-25 23:33:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when avatar url returns an invalid status code' do
|
|
|
|
it 'should not do anything' do
|
2018-03-28 02:44:42 -04:00
|
|
|
stub_request(:get, "http://thisfakesomething.something.com/")
|
|
|
|
.to_return(status: 500, body: "", headers: {})
|
|
|
|
|
2017-05-25 23:33:12 -04:00
|
|
|
url = "http://thisfakesomething.something.com/"
|
2018-02-24 06:35:57 -05:00
|
|
|
|
2018-08-13 02:48:15 -04:00
|
|
|
expect do
|
|
|
|
UserAvatar.import_url_for_user(url, user)
|
|
|
|
end.to_not change { Upload.count }
|
2017-05-25 23:33:12 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.uploaded_avatar_id).to eq(nil)
|
|
|
|
expect(user.user_avatar.custom_upload_id).to eq(nil)
|
|
|
|
end
|
2016-09-19 01:10:02 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-31 00:46:22 -04:00
|
|
|
|
|
|
|
describe "ensure_consistency!" do
|
|
|
|
|
|
|
|
it "will clean up dangling avatars" do
|
|
|
|
upload1 = Fabricate(:upload)
|
|
|
|
upload2 = Fabricate(:upload)
|
|
|
|
|
|
|
|
user_avatar = Fabricate(:user).user_avatar
|
|
|
|
|
|
|
|
user_avatar.update_columns(
|
|
|
|
gravatar_upload_id: upload1.id,
|
|
|
|
custom_upload_id: upload2.id
|
|
|
|
)
|
|
|
|
|
|
|
|
upload1.destroy!
|
|
|
|
upload2.destroy!
|
|
|
|
|
|
|
|
user_avatar.reload
|
|
|
|
expect(user_avatar.gravatar_upload_id).to eq(nil)
|
|
|
|
expect(user_avatar.custom_upload_id).to eq(nil)
|
|
|
|
|
|
|
|
user_avatar.update_columns(
|
|
|
|
gravatar_upload_id: upload1.id,
|
|
|
|
custom_upload_id: upload2.id
|
|
|
|
)
|
|
|
|
|
|
|
|
UserAvatar.ensure_consistency!
|
|
|
|
|
|
|
|
user_avatar.reload
|
|
|
|
expect(user_avatar.gravatar_upload_id).to eq(nil)
|
|
|
|
expect(user_avatar.custom_upload_id).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|