Merge pull request #5073 from xrav3nz/extract-github-avatar

FEATURE: import Github profile picture
This commit is contained in:
Guo Xiang Tan 2017-08-23 16:18:46 +09:00 committed by GitHub
commit 7c15b27a90
2 changed files with 69 additions and 9 deletions

View File

@ -91,6 +91,8 @@ class Auth::GithubAuthenticator < Auth::Authenticator
end
end
retrieve_avatar(user, data)
result.user = user
result
end
@ -102,6 +104,8 @@ class Auth::GithubAuthenticator < Auth::Authenticator
screen_name: data[:github_screen_name],
github_user_id: data[:github_user_id]
)
retrieve_avatar(user, data)
end
def register_middleware(omniauth)
@ -113,4 +117,12 @@ class Auth::GithubAuthenticator < Auth::Authenticator
},
scope: "user:email"
end
private
def retrieve_avatar(user, data)
return unless data[:image].present? && user && user.user_avatar&.custom_upload_id.blank?
Jobs.enqueue(:download_avatar_from_url, url: data[:image], user_id: user.id, override_gravatar: false)
end
end

View File

@ -6,13 +6,33 @@ require 'rails_helper'
Auth.send(:remove_const, :GithubAuthenticator)
load 'auth/github_authenticator.rb'
def auth_token_for(user)
{
extra: {
all_emails: [{
email: user.email,
primary: true,
verified: true,
}]
},
info: {
email: user.email,
email_verified: true,
nickname: user.username,
name: user.name,
image: "https://avatars3.githubusercontent.com/u/#{user.username}",
},
uid: '100'
}
end
describe Auth::GithubAuthenticator do
let(:authenticator) { described_class.new }
let(:user) { Fabricate(:user) }
context 'after_authenticate' do
it 'can authenticate and create a user record for already existing users' do
user = Fabricate(:user)
hash = {
extra: {
all_emails: [{
@ -30,7 +50,6 @@ describe Auth::GithubAuthenticator do
uid: "100"
}
authenticator = Auth::GithubAuthenticator.new
result = authenticator.after_authenticate(hash)
expect(result.user.id).to eq(user.id)
@ -41,8 +60,6 @@ describe Auth::GithubAuthenticator do
end
it 'will not authenticate for already existing users with an unverified email' do
user = Fabricate(:user)
hash = {
extra: {
all_emails: [{
@ -60,7 +77,6 @@ describe Auth::GithubAuthenticator do
uid: "100"
}
authenticator = Auth::GithubAuthenticator.new
result = authenticator.after_authenticate(hash)
expect(result.user).to eq(nil)
@ -88,7 +104,6 @@ describe Auth::GithubAuthenticator do
uid: "100"
}
authenticator = Auth::GithubAuthenticator.new
result = authenticator.after_authenticate(hash)
expect(result.user).to eq(nil)
@ -120,7 +135,6 @@ describe Auth::GithubAuthenticator do
uid: "100"
}
authenticator = Auth::GithubAuthenticator.new
SiteSetting.email_domains_blacklist = "blacklist.com"
result = authenticator.after_authenticate(hash)
@ -157,7 +171,6 @@ describe Auth::GithubAuthenticator do
uid: "100"
}
authenticator = Auth::GithubAuthenticator.new
SiteSetting.email_domains_whitelist = "whitelist.com"
result = authenticator.after_authenticate(hash)
@ -169,4 +182,39 @@ describe Auth::GithubAuthenticator do
end
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
Sidekiq::Testing.fake!
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
Sidekiq::Testing.fake!
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
end