2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-05-22 15:37:02 +08:00
|
|
|
|
|
|
|
describe UserAvatar do
|
2017-05-26 11:33:12 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:avatar) { user.create_user_avatar! }
|
2014-05-22 15:37:02 +08:00
|
|
|
|
|
|
|
it 'can update gravatars' do
|
|
|
|
temp = Tempfile.new('test')
|
2014-05-26 19:17:20 +08:00
|
|
|
temp.binmode
|
|
|
|
# tiny valid png
|
|
|
|
temp.write(Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw=="))
|
|
|
|
temp.rewind
|
2014-05-22 15:37:02 +08:00
|
|
|
FileHelper.expects(:download).returns(temp)
|
|
|
|
avatar.update_gravatar!
|
|
|
|
temp.unlink
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(avatar.gravatar_upload).not_to eq(nil)
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
2016-09-19 13:10:02 +08:00
|
|
|
|
2017-05-26 11:33:12 +08:00
|
|
|
context '.import_url_for_user' do
|
2016-09-19 13:10:02 +08: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
|
|
|
|
user = Fabricate(:user, uploaded_avatar_id: 1)
|
|
|
|
user.user_avatar.update_columns(gravatar_upload_id: 1)
|
|
|
|
|
|
|
|
FileHelper.stubs(:download).returns(file_from_fixtures("logo.png"))
|
|
|
|
UserAvatar.import_url_for_user("logo.png", user, override_gravatar: false)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.uploaded_avatar_id).to eq(1)
|
2017-05-26 11:33:12 +08:00
|
|
|
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
|
2018-03-28 14:44:42 +08:00
|
|
|
stub_request(:get, "http://thisfakesomething.something.com/")
|
|
|
|
.to_return(status: 500, body: "", headers: {})
|
|
|
|
|
2017-05-26 11:33:12 +08:00
|
|
|
url = "http://thisfakesomething.something.com/"
|
2018-02-24 19:35:57 +08:00
|
|
|
|
2017-05-26 11:33:12 +08:00
|
|
|
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
|
2016-09-19 13:10:02 +08:00
|
|
|
end
|
|
|
|
end
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|