Skip to content

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 21 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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 Aug 29, 2024
050b7db
Code cleanup - specs controllers omniauth
200455939-yashu Aug 29, 2024
22fde67
Issue fix for the multiple accounts
200455939-yashu Aug 30, 2024
e95c10c
Resolving conflicts after the yashu-sso-link-accounts
200455939-yashu Aug 30, 2024
377f62c
Merge remote-tracking branch 'origin/yashu-sso-2user-accounts-issue-f…
200455939-yashu Aug 30, 2024
45e54e3
Resolving the conflicts after merge
200455939-yashu Aug 30, 2024
84c2ef7
adding test cases for the linked successfylly and 2 users condition
200455939-yashu Aug 30, 2024
58d4ee8
Translation related changes
200455939-yashu Aug 30, 2024
9678eda
Merge remote-tracking branch 'origin/yashu-sso-link-accounts' into ya…
200455939-yashu Aug 30, 2024
a3b5ab6
Spelling correction
200455939-yashu Aug 30, 2024
7f70edf
Removing the byebug and the updates related to translations.
200455939-yashu Sep 3, 2024
0aece6b
Adding the changelog
200455939-yashu Sep 3, 2024
3649013
commit after Resolving the conflicts in Changelog
200455939-yashu Sep 3, 2024
7eb53a1
Review Changes
200455939-yashu Sep 4, 2024
87b2fa3
Merge branch 'yashu-sso-link-accounts' into yashu-controller-spec
200455939-yashu Sep 5, 2024
c21e69d
Merge branch 'yashu-sso-link-accounts' into yashu-controller-spec
200455939-yashu Sep 6, 2024
8303962
Review changes 2 O
200455939-yashu Sep 9, 2024
8d3bd79
Resolving the conflicts after merging the link accounts branch
200455939-yashu Sep 9, 2024
bf0d279
Review changes 2.1
200455939-yashu Sep 10, 2024
ea4e25a
Removing conflict HEAD
200455939-yashu Sep 10, 2024
57da037
Make rubocop happy
aaronskiba Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- Test cases for CILogon(openid_connection) changes in Omniauth controller - [#869](https://github.com/portagenetwork/roadmap/pull/869/)

- Implemented openid_connection SSO with CILogon [#872](https://github.com/portagenetwork/roadmap/pull/872)

- Create GET "/api/ca_dashboard/stats" endpoint to fetch Plan, User, and Org-related statistics [#852](https://github.com/portagenetwork/roadmap/pull/852)
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ def openid_connect
attrs: auth,
identifiable: current_user)

flash[:notice] = _('Linked succesfully')
flash[:notice] = _('Linked successfully')

redirect_to root_path
elsif user.id != current_user.id
# If a user was found but does NOT match the current user then the identifier has
# already been attached to another account (likely the user has 2 accounts)
flash[:alert] = format(_('The current %{description} iD has been already linked to a user with email %{email}'),
description: identifier_scheme.description, email: user.email)
redirect_to edit_user_registration_path
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
Expand Down
1 change: 1 addition & 0 deletions app/views/translation_io_exports/_cilogon.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= _('CILogon') %>
198 changes: 139 additions & 59 deletions spec/controllers/omniauth_callbacks_controller_spec.rb
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
Copy link
Collaborator

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?

Copy link
Author

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. 

@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
Loading