forked from DMPRoadmap/roadmap
-
Notifications
You must be signed in to change notification settings - Fork 3
Testcases for the Ominiauth controller openid connect #869
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
eab41b0
Testcases for the Ominiauth controller openid connect
200455939-yashu 050b7db
Code cleanup - specs controllers omniauth
200455939-yashu 22fde67
Issue fix for the multiple accounts
200455939-yashu e95c10c
Resolving conflicts after the yashu-sso-link-accounts
200455939-yashu 377f62c
Merge remote-tracking branch 'origin/yashu-sso-2user-accounts-issue-f…
200455939-yashu 45e54e3
Resolving the conflicts after merge
200455939-yashu 84c2ef7
adding test cases for the linked successfylly and 2 users condition
200455939-yashu 58d4ee8
Translation related changes
200455939-yashu 9678eda
Merge remote-tracking branch 'origin/yashu-sso-link-accounts' into ya…
200455939-yashu a3b5ab6
Spelling correction
200455939-yashu 7f70edf
Removing the byebug and the updates related to translations.
200455939-yashu 0aece6b
Adding the changelog
200455939-yashu 3649013
commit after Resolving the conflicts in Changelog
200455939-yashu 7eb53a1
Review Changes
200455939-yashu 87b2fa3
Merge branch 'yashu-sso-link-accounts' into yashu-controller-spec
200455939-yashu c21e69d
Merge branch 'yashu-sso-link-accounts' into yashu-controller-spec
200455939-yashu 8303962
Review changes 2 O
200455939-yashu 8d3bd79
Resolving the conflicts after merging the link accounts branch
200455939-yashu bf0d279
Review changes 2.1
200455939-yashu ea4e25a
Removing conflict HEAD
200455939-yashu 57da037
Make rubocop happy
aaronskiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= _('CILogon') %> |
198 changes: 139 additions & 59 deletions
198
spec/controllers/omniauth_callbacks_controller_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,142 @@ | ||
# 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: 'test@example.com' | ||
# } | ||
# ) | ||
# 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 | ||
# 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: 'user@organization.ca', | ||
first_name: 'Test', | ||
last_name: 'User', | ||
name: 'Test User' | ||
} | ||
}) | ||
|
||
# Assign the mocked authentication hash to the request environment | ||
@request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect] | ||
end | ||
|
||
after do | ||
# Reset the `from_omniauth` method after each test | ||
User.define_singleton_method(:from_omniauth) do |auth| | ||
User.find_by(email: auth.info.email) | ||
end | ||
end | ||
|
||
describe 'POST #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 | ||
@request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect] | ||
end | ||
|
||
it 'redirects to the registration page with a flash message' do | ||
post :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: 'user@organization.ca', firstname: 'Test', surname: 'User', org: @org) } | ||
|
||
before do | ||
def User.from_omniauth(_auth) | ||
User.find_by(email: 'user@organization.ca') | ||
end | ||
end | ||
|
||
it 'signs in the existing user' do | ||
post :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: 'user@organization.ca', 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 do | ||
post :openid_connect | ||
current_user.reload # Ensure we have the latest state of the user | ||
end.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) } | ||
# Ensure different_user is created before test runs | ||
let!(:different_user) do | ||
create(:user, email: 'different_user@example.com') | ||
end | ||
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: 'different_user@example.com') | ||
end | ||
end | ||
|
||
it 'sets flash alert and redirects to edit user registration path' do | ||
post :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}" | ||
) | ||
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, 'Unexpected error' | ||
end | ||
end | ||
|
||
it 'handles the error and raises an exception' do | ||
expect do | ||
post :openid_connect | ||
end.to raise_error(StandardError, 'Unexpected error') | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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.