docbrown/spec/models/identity_spec.rb
Fernando Valverde fcc5c0d2a1
Add Sign in with Apple (#11934)
* Add gem omniauth-apple

* Integrate omniauth-apple

* Integrate callback

* Add fields

* Add tests, fix bugs and make it all work

* Show only enabled providers for the current user

* Add default profile image for Apple

* Remove localhost patch

* Bring over the changed Apple username if the user changes it

* More specs fixed

* Incorporate feedback from PR

* Fix specs

* Simplify code and fix spec

* Fix Broadcast generators to take into account the new provider

* Fix spec

* Generate a truly unique apple_username

* Fix user specs

* Add omniauth-apple-0.0.2 to vendor cache

* Fix merge conflict and spec

* Update VCR fastly sloan cassette

* Revert "Generate a truly unique apple_username"

This reverts commit 2462875575b0bbd6b3c1d56b25afcd3189671608.

* Fix user specs

* Fix specs

* Fix specs

* Hide Connect Apple button behind a feature flag

* Revert "Hide Connect Apple button behind a feature flag"

This reverts commit 105bde0373389a4eb9b6e948f60734c7e0e99cba.

* Fix line lengths

* Fix spec

* ES tag

* CSRF bypass for Apple callback

* custom user_nickname in Apple provider with small tweaks + omniauth-apple bump

* Fixes username specs

* Makes Apple users default image Users::ProfileImageGenerator

* Fallback to mascot_image_url in test environment to avoid breaking Travis

* Fixes Apple CSRF error + makes default nickname more readable

* Trigger Travis

* Better devise config

* Apple SiteConfig entires in /admin/config

* Fixing specs

* Adds beta_access? to Authentication::Providers::Provider

* Fixes specs

* Codeclimate double quote fix in Gemfile

* Fixes /admin/config allowed params & adds feature flag for provider beta_access?

* Remove Enfile & adds temporary docs

* Adds custom apple auth provider settings

* Fix authenticator spec

* Fix configs spec (use last instead of first to avoid apple special case)

* Remove dangling fields from /admin/config

* updates feature flag

* More test fixes

* Hide config behind feature flag too

* omniauth-apple bump

* Takes care of edge case fallback

* Reverse apple_username update

* Adds auth_time to info hash in apple omniauth mock

* Switch to next instead of nesting for feature flag

* Fixes CVE-2015-9284

* Fixes specs after auth providers initiatior refactor from GET to POST

* Spec fixes

* More spec fixes

* Fix Rails codebase reference link

Co-authored-by: rhymes <rhymesete@gmail.com>
Co-authored-by: rhymes <rhymes@hey.com>
2020-12-18 10:22:33 -06:00

181 lines
6.1 KiB
Ruby

require "rails_helper"
RSpec.describe Identity, type: :model do
let(:identity) { create(:identity, user: create(:user), uid: SecureRandom.hex) }
describe "validations" do
describe "builtin validations" do
subject { identity }
it { is_expected.to belong_to(:user) }
it { is_expected.to validate_presence_of(:provider) }
it { is_expected.to validate_presence_of(:uid) }
it { is_expected.to validate_presence_of(:user_id) }
it { is_expected.to validate_uniqueness_of(:uid).scoped_to(:provider) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:provider) }
it { is_expected.to validate_inclusion_of(:provider).in_array(Authentication::Providers.available.map(&:to_s)) }
it { is_expected.to serialize(:auth_data_dump) }
end
end
describe ".build_build_from_omniauth" do
let(:user) { create(:user) }
before do
omniauth_mock_providers_payload
end
context "with Apple payload" do
let(:auth_payload) { OmniAuth.config.mock_auth[:apple] }
let(:provider) { Authentication::Providers::Apple.new(auth_payload) }
it "initializes a new identity from the auth payload" do
identity = described_class.build_from_omniauth(provider)
expect(identity.new_record?).to be(true)
expect(identity.provider).to eq("apple")
expect(identity.uid).to eq(auth_payload.uid)
expect(identity.token).to eq(auth_payload.credentials.token)
expect(identity.secret).to be_nil
expect(identity.auth_data_dump).to eq(provider.payload)
end
it "finds an existing identity" do
payload = provider.payload
existing_identity = described_class.create!(
user: user,
provider: payload.provider,
uid: payload.uid,
token: payload.credentials.token,
secret: nil,
auth_data_dump: payload,
)
identity = described_class.build_from_omniauth(provider)
expect(identity).to eq(existing_identity)
end
end
context "with Github payload" do
let(:auth_payload) { OmniAuth.config.mock_auth[:github] }
let(:provider) { Authentication::Providers::Github.new(auth_payload) }
it "initializes a new identity from the auth payload" do
identity = described_class.build_from_omniauth(provider)
expect(identity.new_record?).to be(true)
expect(identity.provider).to eq("github")
expect(identity.uid).to eq(auth_payload.uid)
expect(identity.token).to eq(auth_payload.credentials.token)
expect(identity.secret).to eq(auth_payload.credentials.secret)
expect(identity.auth_data_dump).to eq(provider.payload)
end
it "finds an existing identity" do
payload = provider.payload
existing_identity = described_class.create!(
user: user,
provider: payload.provider,
uid: payload.uid,
token: payload.credentials.token,
secret: payload.credentials.secret,
auth_data_dump: payload,
)
identity = described_class.build_from_omniauth(provider)
expect(identity).to eq(existing_identity)
end
end
context "with Facebook payload" do
let(:auth_payload) { OmniAuth.config.mock_auth[:facebook] }
let(:provider) { Authentication::Providers::Facebook.new(auth_payload) }
it "initializes a new identity from the auth payload" do
identity = described_class.build_from_omniauth(provider)
expect(identity.new_record?).to be(true)
expect(identity.provider).to eq("facebook")
expect(identity.uid).to eq(auth_payload.uid)
expect(identity.token).to eq(auth_payload.credentials.token)
expect(identity.secret).to eq(auth_payload.credentials.secret)
expect(identity.auth_data_dump).to eq(provider.payload)
end
it "finds an existing identity" do
payload = provider.payload
existing_identity = described_class.create!(
user: user,
provider: payload.provider,
uid: payload.uid,
token: payload.credentials.token,
secret: payload.credentials.secret,
auth_data_dump: payload,
)
identity = described_class.build_from_omniauth(provider)
expect(identity).to eq(existing_identity)
end
end
context "with Twitter payload" do
let(:auth_payload) { OmniAuth.config.mock_auth[:twitter] }
let(:provider) { Authentication::Providers::Twitter.new(auth_payload) }
it "initializes a new identity from the auth payload" do
identity = described_class.build_from_omniauth(provider)
expect(identity.new_record?).to be(true)
expect(identity.provider).to eq("twitter")
expect(identity.uid).to eq(auth_payload.uid)
expect(identity.token).to eq(auth_payload.credentials.token)
expect(identity.secret).to eq(auth_payload.credentials.secret)
expect(identity.auth_data_dump).to eq(provider.payload)
end
it "finds an existing identity" do
payload = provider.payload
existing_identity = described_class.create!(
user: user,
provider: payload.provider,
uid: payload.uid,
token: payload.credentials.token,
secret: payload.credentials.secret,
auth_data_dump: payload,
)
identity = described_class.build_from_omniauth(provider)
expect(identity).to eq(existing_identity)
end
it "does not store the access token in auth_data_dump" do
expect(auth_payload.extra.access_token).not_to be_nil
identity = described_class.build_from_omniauth(provider)
expect(identity.auth_data_dump.extra.access_token).to be_nil
end
end
end
describe "#email" do
let(:auth_payload) { OmniAuth.config.mock_auth[:github] }
let(:provider) { Authentication::Providers::Github.new(auth_payload) }
before do
omniauth_mock_providers_payload
end
it "returns the email associated with the identity" do
identity = described_class.build_from_omniauth(provider)
expect(identity.email).to eq(identity.auth_data_dump.info.email)
end
end
end