Add safe navigation operator to stop 500 errors (#13646)

This commit is contained in:
Jacob Herrington 2021-05-04 10:37:37 -05:00 committed by GitHub
parent fc88db1f67
commit e8df3b0915
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View file

@ -6,7 +6,9 @@ module ProfileHelper
return {} if user_identities.blank?
urls = user_identities.pluck(:auth_data_dump).each_with_object({}) do |data, hash|
if (data_urls = data.dig(:info, :urls))
# TODO: [@jacobherrington] There are some examples in production of the `auth_data_dump` value being
# `nil`. This appears to be the case around ~0.6% of the time.
if (data_urls = data&.dig(:info, :urls))
hash.merge!(data_urls)
end
end

View file

@ -36,6 +36,21 @@ FactoryBot.define do
end
end
trait :with_broken_identity do
# Mimics a situation that can occur in production
transient { identities { Authentication::Providers.available } }
after(:create) do |user, options|
options.identities.each do |provider|
auth = OmniAuth.config.mock_auth.fetch(provider.to_sym)
create(
:identity,
user: user, provider: provider, uid: auth.uid, auth_data_dump: nil,
)
end
end
end
trait :super_admin do
after(:build) { |user| user.add_role(:super_admin) }
end

View file

@ -28,6 +28,16 @@ describe ProfileHelper do
end
end
context "when a user has a broken social authentication provider linked" do
let(:user) do
create(:user, :with_broken_identity, identities: ["github"])
end
it "ignores that auth provider" do
expect(actual).to eq({})
end
end
context "when a user has multiple social authentication providers linked" do
let(:user) do
omniauth_mock_github_payload