GH#retrieve_avatar: simplify conditional and restructured testing

This commit is contained in:
Kyle Zhao 2017-08-22 23:46:50 -04:00
parent 49f0119c12
commit 5868508e98
2 changed files with 21 additions and 15 deletions

View File

@ -118,14 +118,11 @@ class Auth::GithubAuthenticator < Auth::Authenticator
scope: "user:email"
end
protected
private
def retrieve_avatar(user, data)
return unless user
return if user.user_avatar&.custom_upload_id.present?
return unless data[:image].present? && user && user.user_avatar&.custom_upload_id.blank?
if (avatar_url = data[:image]).present?
Jobs.enqueue(:download_avatar_from_url, url: avatar_url, user_id: user.id, override_gravatar: false)
end
Jobs.enqueue(:download_avatar_from_url, url: data[:image], user_id: user.id, override_gravatar: false)
end
end

View File

@ -184,27 +184,36 @@ describe Auth::GithubAuthenticator do
end
describe 'avatar retrieval' do
let(:job_klass) { Jobs::DownloadAvatarFromUrl }
before { SiteSetting.queue_jobs = true }
context 'when user has a custom avatar' do
let(:user_avatar) { Fabricate(:user_avatar, custom_upload: Fabricate(:upload)) }
let(:user_with_custom_avatar) { Fabricate(:user, user_avatar: user_avatar) }
it 'does not enqueue a download_avatar_from_url job' do
Jobs.expects(:enqueue).with(:download_avatar_from_url, anything).never
Sidekiq::Testing.fake!
result = authenticator.after_authenticate(auth_token_for(user_with_custom_avatar))
expect {
authenticator.after_authenticate(auth_token_for(user_with_custom_avatar))
}.to_not change(job_klass.jobs, :size)
end
end
context 'when user does not have a custom avatar' do
it 'enqueues a download_avatar_from_url job' do
Jobs.expects(:enqueue).with(
:download_avatar_from_url,
url: "https://avatars3.githubusercontent.com/u/#{user.username}",
user_id: user.id,
override_gravatar: false
)
Sidekiq::Testing.fake!
result = authenticator.after_authenticate(auth_token_for(user))
expect {
authenticator.after_authenticate(auth_token_for(user))
}.to change(job_klass.jobs, :size).by(1)
job_args = job_klass.jobs.last['args'].first
expect(job_args['url']).to eq("https://avatars3.githubusercontent.com/u/#{user.username}")
expect(job_args['user_id']).to eq(user.id)
expect(job_args['override_gravatar']).to eq(false)
end
end
end