FIX: Improve avatar loading, and add tests

Follow-up from 4e2cc9c
This commit is contained in:
David Taylor 2018-12-04 15:09:32 +00:00
parent 22001b3c50
commit e117deb2ba
2 changed files with 36 additions and 2 deletions

View File

@ -72,7 +72,7 @@ class Auth::ManagedAuthenticator < Auth::Authenticator
credentials: auth_token[:credentials] || {},
extra: auth_token[:extra] || {}
)
retrieve_avatar(result.user, auth_token[:info][:image])
retrieve_avatar(result.user, auth_token.dig(:info, :image))
end
result.email_valid = true if result.email
@ -94,7 +94,7 @@ class Auth::ManagedAuthenticator < Auth::Authenticator
def after_create_account(user, auth)
data = auth[:extra_data]
create_association!(data.merge(user: user))
retrieve_avatar(user, data&.[]("info")&.[]("image"))
retrieve_avatar(user, data.dig(:info, :image))
end
def retrieve_avatar(user, url)

View File

@ -114,6 +114,40 @@ describe Auth::ManagedAuthenticator do
expect(result.user.id).to eq(user.id)
end
end
describe "avatar on update" do
let(:user) { Fabricate(:user) }
let!(:associated) { UserAssociatedAccount.create!(user: user, provider_name: 'myauth', provider_uid: "1234") }
it "schedules the job upon update correctly" do
# No image supplied, do not schedule
expect { result = authenticator.after_authenticate(hash) }
.to change { Jobs::DownloadAvatarFromUrl.jobs.count }.by(0)
# Image supplied, schedule
expect { result = authenticator.after_authenticate(hash.deep_merge(info: { image: "https://some.domain/image.jpg" })) }
.to change { Jobs::DownloadAvatarFromUrl.jobs.count }.by(1)
# User already has profile picture, don't schedule
user.user_avatar = Fabricate(:user_avatar, custom_upload: Fabricate(:upload))
user.save!
expect { result = authenticator.after_authenticate(hash.deep_merge(info: { image: "https://some.domain/image.jpg" })) }
.to change { Jobs::DownloadAvatarFromUrl.jobs.count }.by(0)
end
end
describe "avatar on create" do
let(:user) { Fabricate(:user) }
it "doesn't schedule with no image" do
expect { result = authenticator.after_create_account(user, extra_data: hash) }
.to change { Jobs::DownloadAvatarFromUrl.jobs.count }.by(0)
end
it "schedules with image" do
expect { result = authenticator.after_create_account(user, extra_data: hash.deep_merge(info: { image: "https://some.domain/image.jpg" })) }
.to change { Jobs::DownloadAvatarFromUrl.jobs.count }.by(1)
end
end
end
describe 'description_for_user' do