2023-07-25 09:46:57 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 07:12:25 +02:00
|
|
|
RSpec.describe 'OmniAuth callbacks' do
|
2023-07-25 09:46:57 +02:00
|
|
|
shared_examples 'omniauth provider callbacks' do |provider|
|
2023-12-18 11:26:09 +01:00
|
|
|
subject { post send :"user_#{provider}_omniauth_callback_path" }
|
2023-07-25 09:46:57 +02:00
|
|
|
|
|
|
|
context 'with full information in response' do
|
|
|
|
before do
|
|
|
|
mock_omniauth(provider, {
|
|
|
|
provider: provider.to_s,
|
|
|
|
uid: '123',
|
|
|
|
info: {
|
|
|
|
verified: 'true',
|
|
|
|
email: 'user@host.example',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a matching user' do
|
|
|
|
it 'creates a user and an identity and redirects to root path' do
|
|
|
|
expect { subject }
|
|
|
|
.to change(User, :count)
|
|
|
|
.by(1)
|
|
|
|
.and change(Identity, :count)
|
|
|
|
.by(1)
|
|
|
|
.and change(LoginActivity, :count)
|
|
|
|
.by(1)
|
|
|
|
|
|
|
|
expect(User.last.email).to eq('user@host.example')
|
|
|
|
expect(Identity.find_by(user: User.last).uid).to eq('123')
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a matching user and no matching identity' do
|
|
|
|
before do
|
|
|
|
Fabricate(:user, email: 'user@host.example')
|
|
|
|
end
|
|
|
|
|
2024-02-14 15:57:49 +01:00
|
|
|
context 'when ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH is set to true' do
|
|
|
|
around do |example|
|
|
|
|
ClimateControl.modify ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH: 'true' do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
2023-07-25 09:46:57 +02:00
|
|
|
|
2024-02-14 15:57:49 +01:00
|
|
|
it 'matches the existing user, creates an identity, and redirects to root path' do
|
|
|
|
expect { subject }
|
|
|
|
.to not_change(User, :count)
|
|
|
|
.and change(Identity, :count)
|
|
|
|
.by(1)
|
|
|
|
.and change(LoginActivity, :count)
|
|
|
|
.by(1)
|
|
|
|
|
|
|
|
expect(Identity.find_by(user: User.last).uid).to eq('123')
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH is not set to true' do
|
2024-02-14 22:49:45 +01:00
|
|
|
it 'does not match the existing user or create an identity, and redirects to login page' do
|
2024-02-14 15:57:49 +01:00
|
|
|
expect { subject }
|
|
|
|
.to not_change(User, :count)
|
|
|
|
.and not_change(Identity, :count)
|
|
|
|
.and not_change(LoginActivity, :count)
|
2024-02-14 22:49:45 +01:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_url)
|
2024-02-14 15:57:49 +01:00
|
|
|
end
|
2023-07-25 09:46:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a matching user and a matching identity' do
|
|
|
|
before do
|
|
|
|
user = Fabricate(:user, email: 'user@host.example')
|
|
|
|
Fabricate(:identity, user: user, uid: '123', provider: provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches the existing records and redirects to root path' do
|
|
|
|
expect { subject }
|
|
|
|
.to not_change(User, :count)
|
|
|
|
.and not_change(Identity, :count)
|
|
|
|
.and change(LoginActivity, :count)
|
|
|
|
.by(1)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a response missing email address' do
|
|
|
|
before do
|
|
|
|
mock_omniauth(provider, {
|
|
|
|
provider: provider.to_s,
|
|
|
|
uid: '123',
|
|
|
|
info: {
|
|
|
|
verified: 'true',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the auth setup page' do
|
|
|
|
expect { subject }
|
|
|
|
.to change(User, :count)
|
|
|
|
.by(1)
|
|
|
|
.and change(Identity, :count)
|
|
|
|
.by(1)
|
|
|
|
.and change(LoginActivity, :count)
|
|
|
|
.by(1)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(auth_setup_path(missing_email: '1'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user cannot be built' do
|
|
|
|
before do
|
2024-02-14 15:16:07 +01:00
|
|
|
allow(User).to receive(:find_for_omniauth).and_return(User.new)
|
2023-07-25 09:46:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the new user signup page' do
|
|
|
|
expect { subject }
|
|
|
|
.to not_change(User, :count)
|
|
|
|
.and not_change(Identity, :count)
|
|
|
|
.and not_change(LoginActivity, :count)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_registration_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#openid_connect', if: ENV['OIDC_ENABLED'] == 'true' && ENV['OIDC_SCOPE'].present? do
|
|
|
|
include_examples 'omniauth provider callbacks', :openid_connect
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#cas', if: ENV['CAS_ENABLED'] == 'true' do
|
|
|
|
include_examples 'omniauth provider callbacks', :cas
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#saml', if: ENV['SAML_ENABLED'] == 'true' do
|
|
|
|
include_examples 'omniauth provider callbacks', :saml
|
|
|
|
end
|
|
|
|
end
|