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