-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testcases for the Ominiauth controller openid connect #869
Changes from 14 commits
eab41b0
050b7db
22fde67
e95c10c
377f62c
45e54e3
84c2ef7
58d4ee8
9678eda
a3b5ab6
7f70edf
0aece6b
3649013
7eb53a1
87b2fa3
c21e69d
8303962
8d3bd79
bf0d279
ea4e25a
57da037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= _('CILogon') %> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,62 +1,136 @@ | ||||||||||||||||||||||||
# frozen_string_literal: true | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
require 'rails_helper' | ||||||||||||||||||||||||
require 'byebug' | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# RSpec.describe Users::OmniauthCallbacksController, type: :controller do | ||||||||||||||||||||||||
# describe '#openid_connect' do | ||||||||||||||||||||||||
# let(:auth) do | ||||||||||||||||||||||||
# OmniAuth::AuthHash.new( | ||||||||||||||||||||||||
# provider: 'openid_connect', | ||||||||||||||||||||||||
# uid: '123545', | ||||||||||||||||||||||||
# info: { | ||||||||||||||||||||||||
# email: '[email protected]' | ||||||||||||||||||||||||
# } | ||||||||||||||||||||||||
# ) | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# before do | ||||||||||||||||||||||||
# OmniAuth.config.test_mode = true | ||||||||||||||||||||||||
# OmniAuth.config.mock_auth[:openid_connect] = auth | ||||||||||||||||||||||||
# request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect] | ||||||||||||||||||||||||
# request.env['devise.mapping'] = Devise.mappings[:user] # If using Devise | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# let(:user) { create(:user) } # Defining the user | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# context 'when the email is missing and user does not exist' do | ||||||||||||||||||||||||
# before do | ||||||||||||||||||||||||
# allow(User).to receive(:from_omniauth).and_return(nil) | ||||||||||||||||||||||||
# allow(auth.info).to receive(:email).and_return(nil) | ||||||||||||||||||||||||
# get :openid_connect | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# it 'redirects to the registration page with a flash message' do | ||||||||||||||||||||||||
# expect(flash[:notice]).to eq('Something went wrong, Please try signing-up here.') | ||||||||||||||||||||||||
# expect(response).to redirect_to(new_user_registration_path) | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# context 'with correct credentials' do | ||||||||||||||||||||||||
# before do | ||||||||||||||||||||||||
# create(:org, managed: false, is_other: true) | ||||||||||||||||||||||||
# @org = create(:org, managed: true) | ||||||||||||||||||||||||
# @identifier_scheme = create(:identifier_scheme, | ||||||||||||||||||||||||
# name: 'openid_connect', | ||||||||||||||||||||||||
# description: 'CILogon', | ||||||||||||||||||||||||
# active: true, | ||||||||||||||||||||||||
# identifier_prefix: 'https://www.cilogon.org/') | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Rails.application.env_config['devise.mapping'] = Devise.mappings[:user] | ||||||||||||||||||||||||
# Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect] | ||||||||||||||||||||||||
# allow(User).to receive(:from_omniauth).and_return(user) | ||||||||||||||||||||||||
# # get :openid_connect | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# it 'links account from external credentials' do | ||||||||||||||||||||||||
# expect(flash[:notice]).to eq('Linked successfully') | ||||||||||||||||||||||||
# expect(response).to redirect_to(root_path) | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
RSpec.describe Users::OmniauthCallbacksController, type: :controller do | ||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
# Enable test mode for OmniAuth | ||||||||||||||||||||||||
OmniAuth.config.test_mode = true | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Setup Devise mapping | ||||||||||||||||||||||||
@request.env["devise.mapping"] = Devise.mappings[:user] | ||||||||||||||||||||||||
create(:org, managed: false, is_other: true) | ||||||||||||||||||||||||
@org = create(:org, managed: true) | ||||||||||||||||||||||||
@identifier_scheme = create(:identifier_scheme, | ||||||||||||||||||||||||
name: 'openid_connect', | ||||||||||||||||||||||||
description: 'CILogon', | ||||||||||||||||||||||||
active: true, | ||||||||||||||||||||||||
identifier_prefix: 'https://www.cilogon.org/') | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Mock OmniAuth data for OpenID Connect with necessary info | ||||||||||||||||||||||||
OmniAuth.config.mock_auth[:openid_connect] = OmniAuth::AuthHash.new({ | ||||||||||||||||||||||||
provider: 'openid_connect', | ||||||||||||||||||||||||
uid: '12345', | ||||||||||||||||||||||||
info: { | ||||||||||||||||||||||||
email: '[email protected]', | ||||||||||||||||||||||||
first_name: 'Test', | ||||||||||||||||||||||||
last_name: 'User', | ||||||||||||||||||||||||
name: 'Test User' | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to define this here since it is already on Lines 126 to 136 in 4da5fdd
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Assign the mocked authentication hash to the request environment | ||||||||||||||||||||||||
@request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:openid_connect] | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
describe 'GET #openid_connect' do | ||||||||||||||||||||||||
let(:auth) { request.env['omniauth.auth'] } | ||||||||||||||||||||||||
let!(:identifier_scheme) { IdentifierScheme.create(name: auth.provider) } | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
context 'when the email is missing and the user does not exist' do | ||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
# Simulate missing email | ||||||||||||||||||||||||
OmniAuth.config.mock_auth[:openid_connect].info.email = nil | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we reset the email after this test in case we use it in another test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this test case, we want to ensure it returns a 'nil' value. Replacing the values will not guarantee the nil results. |
||||||||||||||||||||||||
@request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:openid_connect] | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it 'redirects to the registration page with a flash message' do | ||||||||||||||||||||||||
get :openid_connect | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
expect(response).to redirect_to(new_user_registration_path) | ||||||||||||||||||||||||
expect(flash[:notice]).to eq('Something went wrong, Please try signing-up here.') | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
context 'when the user is not signed in but already exists' do | ||||||||||||||||||||||||
# let!(:user) { User.create(email: auth.info.email, password: 'password123') } | ||||||||||||||||||||||||
let!(:user) { User.create(email: '[email protected]', firstname: 'Test', surname: 'User', org: @org) } | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
def User.from_omniauth(_auth) | ||||||||||||||||||||||||
User.find_by(email: '[email protected]') | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it 'signs in the existing user' do | ||||||||||||||||||||||||
get :openid_connect | ||||||||||||||||||||||||
# expect(subject.current_user).to eq(user) | ||||||||||||||||||||||||
expect(response).to redirect_to(root_path) | ||||||||||||||||||||||||
expect(flash[:notice]).to be_nil | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
context 'when the user is signed in and needs to link their OpenID Connect account' do | ||||||||||||||||||||||||
let!(:user) { User.create(email: '[email protected]', firstname: 'Test', surname: 'User', org: @org) } | ||||||||||||||||||||||||
let(:current_user) { create(:user) } | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
sign_in current_user | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Ensure from_omniauth returns nil, indicating no user is associated with the auth | ||||||||||||||||||||||||
# User.define_singleton_method(:from_omniauth) do |_auth| | ||||||||||||||||||||||||
# nil | ||||||||||||||||||||||||
# end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it "links identifier to current user, sets flash notice, and redirects to root path" do | ||||||||||||||||||||||||
expect { | ||||||||||||||||||||||||
get :openid_connect | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated this |
||||||||||||||||||||||||
current_user.reload # Ensure we have the latest state of the user | ||||||||||||||||||||||||
}.to change(current_user.identifiers, :count).by(1) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
expect(flash[:notice]).to eq('Linked successfully') | ||||||||||||||||||||||||
expect(response).to redirect_to(root_path) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
context 'when the user found via omniauth is different from the current_user' do | ||||||||||||||||||||||||
let(:current_user) { create(:user) } | ||||||||||||||||||||||||
let!(:different_user) { create(:user, email: '[email protected]') } # Ensure different_user is created before test runs | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
sign_in current_user | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Mocking the from_omniauth method to return a different user | ||||||||||||||||||||||||
# We use `let!` to ensure `different_user` is accessible here | ||||||||||||||||||||||||
User.define_singleton_method(:from_omniauth) do |_auth| | ||||||||||||||||||||||||
User.find_by(email: '[email protected]') | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it "sets flash alert and redirects to edit user registration path" do | ||||||||||||||||||||||||
get :openid_connect | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
expect(flash[:alert]).to eq( | ||||||||||||||||||||||||
"The current #{@identifier_scheme.description} iD has been already linked to a user with email #{different_user.email}" | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be updated to match the changes you made in app/controllers/users/omniauth_callbacks_controller.rb:57 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String value is going to be same as expected here. I have changed the way of passing the variable into this string. |
||||||||||||||||||||||||
) | ||||||||||||||||||||||||
expect(response).to redirect_to(edit_user_registration_path) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
context 'when an unknown error occurs' do | ||||||||||||||||||||||||
before do | ||||||||||||||||||||||||
def User.from_omniauth(_auth) | ||||||||||||||||||||||||
raise StandardError.new('Unexpected error') | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it 'handles the error and raises an exception' do | ||||||||||||||||||||||||
expect { | ||||||||||||||||||||||||
get :openid_connect | ||||||||||||||||||||||||
}.to raise_error(StandardError, 'Unexpected error') | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spec/spec_helper.rb:124 of yashu-sso-link-accounts also has
OmniAuth.config.test_mode = true
. Do we need the same statement here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated this