docbrown/spec/system/authentication/user_logs_in_with_github_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

203 lines
5.8 KiB
Ruby

require "rails_helper"
RSpec.describe "Authenticating with GitHub" do
let(:sign_in_link) { "Continue with GitHub" }
before do
omniauth_mock_github_payload
allow(SiteConfig).to receive(:authentication_providers).and_return(Authentication::Providers.available)
end
context "when a user is new" do
context "when using valid credentials" do
it "creates a new user" do
expect do
visit sign_up_path
click_on(sign_in_link, match: :first)
end.to change(User, :count).by(1)
end
it "logs in and redirects to the onboarding" do
visit sign_up_path
click_on(sign_in_link, match: :first)
expect(page).to have_current_path("/onboarding", ignore_query: true)
expect(page.html).to include("onboarding-container")
end
it "remembers the user" do
visit sign_up_path
click_on(sign_in_link, match: :first)
user = User.last
expect(user.remember_token).to be_present
expect(user.remember_created_at).to be_present
end
end
context "when trying to register with an already existing username" do
it "creates a new user with a temporary username" do
username = OmniAuth.config.mock_auth[:github].extra.raw_info.username
user = create(:user, username: username.delete("."))
expect do
visit sign_up_path
click_on(sign_in_link, match: :first)
end.to change(User, :count).by(1)
expect(page).to have_current_path("/onboarding", ignore_query: true)
expect(User.last.username).to include(user.username)
end
end
context "when using invalid credentials" do
let(:params) do
'{"callback_url"=>"http://localhost:3000/users/auth/github/callback", "state"=>"navbar_basic"}'
end
before do
omniauth_setup_invalid_credentials(:github)
allow(DatadogStatsClient).to receive(:increment)
end
after do
OmniAuth.config.on_failure = OmniauthHelpers.const_get("OMNIAUTH_DEFAULT_FAILURE_HANDLER")
end
it "does not create a new user" do
expect do
visit sign_up_path
click_on(sign_in_link, match: :first)
end.not_to change(User, :count)
end
it "does not log in" do
visit sign_up_path
click_on(sign_in_link, match: :first)
expect(page).to have_current_path("/users/sign_in")
expect(page).to have_button(sign_in_link)
end
it "notifies Datadog about a callback error" do
error = OmniAuth::Strategies::OAuth2::CallbackError.new(
"Callback error", "Error reason", "https://example.com/error"
)
omniauth_setup_authentication_error(error, params)
visit sign_up_path
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
it "notifies Datadog about an OAuth unauthorized error" do
request = double
allow(request).to receive(:code).and_return(401)
allow(request).to receive(:message).and_return("unauthorized")
error = OAuth::Unauthorized.new(request)
omniauth_setup_authentication_error(error, params)
visit sign_up_path
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
it "notifies Datadog even with no OmniAuth error present" do
error = nil
omniauth_setup_authentication_error(error, params)
visit sign_up_path
click_on(sign_in_link, match: :first)
args = omniauth_failure_args(error, "github", params)
expect(DatadogStatsClient).to have_received(:increment).with(
"omniauth.failure", *args
)
end
end
context "when a validation failure occurrs" do
before do
# A User is invalid if their name is more than 100 chars long
OmniAuth.config.mock_auth[:github].extra.raw_info.name = "X" * 101
end
it "does not create a new user" do
expect do
visit sign_up_path
click_on(sign_in_link, match: :first)
end.not_to change(User, :count)
end
it "redirects to the registration page" do
visit sign_up_path
click_on(sign_in_link, match: :first)
expect(page).to have_current_path("/users/sign_up")
end
it "reports errors" do
allow(Honeybadger).to receive(:notify)
visit sign_up_path
click_on(sign_in_link, match: :first)
expect(Honeybadger).to have_received(:notify)
end
end
end
context "when a user already exists" do
let!(:auth_payload) { OmniAuth.config.mock_auth[:github] }
let(:user) { create(:user, :with_identity, identities: [:github]) }
before do
auth_payload.info.email = user.email
end
after do
sign_out user
end
context "when using valid credentials" do
it "logs in" do
visit sign_up_path
click_on(sign_in_link, match: :first)
expect(page).to have_current_path("/?signin=true")
end
end
context "when already signed in" do
it "redirects to the feed" do
sign_in user
visit user_github_omniauth_authorize_path
expect(page).to have_current_path("/?signin=true")
end
end
end
context "when community is in invite only mode" do
before do
allow(SiteConfig).to receive(:invite_only_mode).and_return(true)
end
it "doesn't present the authentication option" do
visit sign_up_path(state: "new-user")
expect(page).not_to have_text(sign_in_link)
expect(page).to have_text("invite only")
end
end
end