Skip to content

Commit ae66ef0

Browse files
code clean up and added test cases
1 parent 0c21df3 commit ae66ef0

File tree

7 files changed

+108
-56
lines changed

7 files changed

+108
-56
lines changed

app/controllers/identifiers_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class IdentifiersController < ApplicationController
88
# DELETE /users/identifiers
99
# rubocop:disable Metrics/AbcSize
1010
def destroy
11-
# byebug
1211
authorize Identifier
1312
user = User.find(current_user.id)
1413
identifier = Identifier.find(params[:id])

app/controllers/users/omniauth_callbacks_controller.rb

+9-21
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,7 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
1212
end
1313
end
1414

15-
16-
# def openid_connect
17-
# @user = User.from_omniauth(request.env["omniauth.auth"])
18-
19-
# if @user.present?
20-
# sign_in_and_redirect @user, event: :authentication
21-
# set_flash_message(:notice, :success, kind: "OpenID Connect") if is_navigational_format?
22-
# else
23-
# session["devise.openid_connect_data"] = request.env["omniauth.auth"]
24-
# redirect_to new_user_registration_url
25-
# end
26-
# end
27-
28-
29-
30-
3115
#This is for the OpenidConnect CILogon
32-
3316
def openid_connect
3417
# First or create
3518
auth = request.env['omniauth.auth']
@@ -40,7 +23,7 @@ def openid_connect
4023
#If email is missing we need to request the user to register with DMP.
4124
#User email can be missing if the user email id is set to private or trusted clients only we won't get the value.
4225
#USer email id is one of the mandatory field which is must required.
43-
flash[:notice] = 'Please try sign-up with DMP assistant.'
26+
flash[:notice] = 'Something went wrong, Please try signing-up here.'
4427
redirect_to new_user_registration_path
4528
elsif current_user.nil?
4629
# We need to register
@@ -51,7 +34,6 @@ def openid_connect
5134
value: auth.uid,
5235
attrs: auth,
5336
identifiable: user)
54-
5537
end
5638
sign_in_and_redirect user, event: :authentication
5739
elsif user.nil?
@@ -61,12 +43,18 @@ def openid_connect
6143
attrs: auth,
6244
identifiable: current_user)
6345

64-
flash[:notice] = 'linked succesfully'
65-
redirect_to root_path
46+
flash[:notice] = 'Linked succesfully'
47+
redirect_to root_path
6648
end
6749
end
6850

51+
def orcid
52+
handle_omniauth(IdentifierScheme.for_authentication.find_by(name: 'orcid'))
53+
end
6954

55+
def shibboleth
56+
handle_omniauth(IdentifierScheme.for_authentication.find_by(name: 'shibboleth'))
57+
end
7058

7159
# Processes callbacks from an omniauth provider and directs the user to
7260
# the appropriate page:

app/models/user.rb

+17-27
Original file line numberDiff line numberDiff line change
@@ -177,39 +177,29 @@ class User < ApplicationRecord
177177
##
178178
# Load the user based on the scheme and id provided by the Omniauth call
179179
def self.from_omniauth(auth)
180-
# byebug
181180
Identifier.by_scheme_name(auth.provider.downcase.to_s, 'User')
182181
.where(value: auth.uid)
183182
.first&.identifiable
184-
# end
185-
186-
187-
# Rails.logger.info "OmniAuth Auth Hash: #{auth.inspect}"
188-
# where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
189-
# user.provider = auth.provider
190-
# user.uid = auth.uid
191-
# user.email = auth.info.email
192-
# user.password = Devise.friendly_token[0,20]
193-
# end
194-
# # # .where(value: auth.info.eppn) #need to add a cilogon condition for this
195-
# # .first&.identifiable
196-
# # .where(value: auth.uid).first_or_create do |user|
197-
# # user.email = auth.info.email
198-
# # user.password = Devise.friendly_token[0, 20]
199-
# # user.name = auth.info.name # if the User model has a name
200-
# # end
201183
end
202184

203185

204-
# def self.from_omniauth(auth)
205-
# Rails.logger.info "OmniAuth Auth Hash: #{auth.inspect}"
206-
# where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
207-
# user.provider = auth.provider
208-
# user.uid = auth.uid
209-
# user.email = auth.info.email if !auth.info.email_verified.nil?
210-
# user.password = Devise.friendly_token[0,20]
211-
# end
212-
# end
186+
# Handle user creation from provider
187+
def self.create_from_provider_data(provider_data)
188+
user = User.find_by email: provider_data.info.email
189+
190+
return user if user
191+
192+
user = User.new(
193+
firstname: provider_data.info.first_name,
194+
surname: provider_data.info.last_name,
195+
email: provider_data.info.email,
196+
# We don't know which organization to setup so we will use other
197+
org: Org.find_by(is_other: true),
198+
accept_terms: true,
199+
password: Devise.friendly_token[0, 20]
200+
)
201+
user.save
202+
end
213203

214204
def self.to_csv(users)
215205
User::AtCsv.new(users).to_csv

app/views/shared/_sign_in_form.html.erb

+1-6
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,10 @@
4040
<p class="text-center fontsize-h4">- <%= _('or') %> -</p>
4141
<div class="form-group">
4242
<span class="center-block btn-group-justified">
43-
<% #target = user_openid_connect_omniauth_authorize_path %>
44-
<%#= link_to _('Sign in with your institutional credentials'), target, method: :post, class: 'btn btn-default' %>
45-
<%= link_to "Sign in with CILogon", user_openid_connect_omniauth_authorize_path, method: :post, data: { turbo: false }, class: 'btn btn-default' %>
46-
<%#= button_to 'Login with CILogon', user_openid_connect_omniauth_authorize_path, method: :post, class: 'btn btn-default' %>
43+
<%= link_to _('Sign in with ORCID iD'), user_openid_connect_omniauth_authorize_path, method: :post, data: { turbo: false }, class: 'btn btn-default' %>
4744
</span>
4845
</div>
4946
<% else %>
50-
<%#= debug session %>
51-
<%#= f.hidden_field :openid_connect_id, :value => session['devise.openid_connect_data']['uid'] %>
5247
<% end %>
5348
<% end %>
5449

config/database.yml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ development:
1515
# Do not set this db to the same as development or production.
1616
test:
1717
<<: *defaults
18+
username: <%= ENV['DATABASE_USER'] %>
19+
password: <%= ENV['DATABASE_PASSWORD'] %>
20+
host: <%= ENV['DATABASE_URL'] || '127.0.0.1' %>
1821
url: <%= Rails.application.secrets.database_test_url %>
1922

2023
uat:

config/initializers/cookie_size.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
module ActionDispatch
44
class Cookies
55
# Increase the MAX_COOKIE_SIZE to 8KB (8192 bytes)
6-
MAX_COOKIE_SIZE = 4600
6+
# MAX_COOKIE_SIZE = 4600
77
end
88
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe UsersController, type: :controller do
4+
describe '#openid_connect' do
5+
let(:auth) do
6+
OmniAuth::AuthHash.new(
7+
provider: 'provider_name',
8+
uid: '123545',
9+
info: {
10+
email: 'test@example.com'
11+
}
12+
)
13+
end
14+
15+
before do
16+
request.env['omniauth.auth'] = auth
17+
end
18+
19+
context 'when the email is missing and user does not exist' do
20+
before do
21+
allow(User).to receive(:from_omniauth).and_return(nil)
22+
allow(auth.info).to receive(:email).and_return(nil)
23+
get :openid_connect
24+
end
25+
26+
it 'redirects to the registration page with a flash message' do
27+
expect(flash[:notice]).to eq('Something went wrong, Please try signing-up here.')
28+
expect(response).to redirect_to(new_user_registration_path)
29+
end
30+
end
31+
32+
context 'when current_user is nil and user is nil' do
33+
before do
34+
allow(User).to receive(:from_omniauth).and_return(nil)
35+
allow(User).to receive(:create_from_provider_data).and_return(create(:user))
36+
allow(IdentifierScheme).to receive(:find_by_name).and_return(create(:identifier_scheme))
37+
get :openid_connect
38+
end
39+
40+
it 'creates a new user and identifier, and redirects after signing in' do
41+
expect(User).to have_received(:create_from_provider_data).with(auth)
42+
expect(response).to redirect_to(root_path) # Assuming redirect after sign_in_and_redirect
43+
end
44+
end
45+
46+
context 'when current_user is nil but user exists' do
47+
let(:user) { create(:user) }
48+
49+
before do
50+
allow(User).to receive(:from_omniauth).and_return(user)
51+
get :openid_connect
52+
end
53+
54+
it 'signs in the user and redirects' do
55+
expect(controller.current_user).to eq(user)
56+
expect(response).to redirect_to(root_path) # Assuming redirect after sign_in_and_redirect
57+
end
58+
end
59+
60+
context 'when user is nil but current_user exists' do
61+
let(:current_user) { create(:user) }
62+
63+
before do
64+
allow(controller).to receive(:current_user).and_return(current_user)
65+
allow(User).to receive(:from_omniauth).and_return(nil)
66+
allow(IdentifierScheme).to receive(:find_by_name).and_return(create(:identifier_scheme))
67+
get :openid_connect
68+
end
69+
70+
it 'creates a new identifier and redirects to root with a flash notice' do
71+
expect(Identifier).to have_received(:create)
72+
expect(flash[:notice]).to eq('Linked successfully')
73+
expect(response).to redirect_to(root_path)
74+
end
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)