Skip to content

Commit d731743

Browse files
Automatically confirm user when authenticated via Google SSO
Changes: * Gemfile.lock - Redcarpet upgraded to 3.6.1 to resolve test warnings * Users::OmniauthCallbacksController - Set confirmed_at when successful authentication via Google SSO and user is not yet confirmed * Updated integration tests * Updated Test config to set default host for ActionMailer - Similar to other environments (e.g. Development, Stage and Production)
1 parent 9c3d0fd commit d731743

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ GEM
336336
rb-fsevent (0.11.1)
337337
rb-inotify (0.10.1)
338338
ffi (~> 1.0)
339-
redcarpet (3.5.1)
339+
redcarpet (3.6.1)
340340
redis (4.7.1)
341341
regexp_parser (2.5.0)
342342
responders (3.0.1)

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def google_sign_in(access_token)
6262
user = User.find_by_email data['email']
6363
user ||= new_user_from_google(data)
6464
update_user_info_from_google user, data
65+
# Automatically mark user as "confirmed" if logging in via Google SSO
66+
user.confirmed_at = Time.now.in_time_zone(LAST_COMPETITION_TIME_ZONE) if user.confirmed_at.blank?
6567
user.save
6668
user
6769
end

config/environments/test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
# ActionMailer::Base.deliveries array.
4444
config.action_mailer.delivery_method = :test
4545

46+
config.action_mailer.default_url_options = { host: ENV['DOMAIN'] || 'localhost' }
47+
4648
# Print deprecation notices to the stderr.
4749
config.active_support.deprecation = :stderr
4850

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class Users::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTest
6+
GOOGLE_USER = 'test@google.com'
7+
CONFIRM_DATE = Time.new(2025, 1, 1)
8+
9+
setup do
10+
OmniAuth.config.test_mode = true
11+
end
12+
13+
test 'should confirm a new user' do
14+
OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new(
15+
{
16+
provider: 'google',
17+
uid: '1234567890',
18+
info: {
19+
email: GOOGLE_USER,
20+
name: 'Google User',
21+
image: 'google avatar',
22+
},
23+
},
24+
)
25+
26+
assert_difference('User.count', 1) do
27+
User.find_by_email(GOOGLE_USER).tap do |user|
28+
assert_nil user
29+
end
30+
31+
get '/users/auth/google/callback'
32+
33+
User.find_by_email(GOOGLE_USER).tap do |user|
34+
assert user.confirmed?
35+
end
36+
end
37+
end
38+
39+
test 'should confirm an existing un-confirmed user' do
40+
OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new(
41+
{
42+
provider: 'google',
43+
uid: '1234567890',
44+
info: {
45+
email: GOOGLE_USER,
46+
name: 'Google User',
47+
image: 'google avatar',
48+
},
49+
},
50+
)
51+
52+
assert_difference('User.count', 1) do
53+
User.create!(email: GOOGLE_USER, password: Devise.friendly_token[0, 20])
54+
55+
User.find_by_email(GOOGLE_USER).tap do |user|
56+
assert_not user.confirmed?
57+
end
58+
59+
get '/users/auth/google/callback'
60+
61+
User.find_by_email(GOOGLE_USER).tap do |user|
62+
assert user.confirmed?
63+
end
64+
end
65+
end
66+
67+
test 'should not re-confirm an existing confirmed user' do
68+
OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new(
69+
{
70+
provider: 'google',
71+
uid: '1234567890',
72+
info: {
73+
email: GOOGLE_USER,
74+
name: 'Google User',
75+
image: 'google avatar',
76+
},
77+
},
78+
)
79+
80+
assert_difference('User.count', 1) do
81+
User.create!(email: GOOGLE_USER, password: Devise.friendly_token[0, 20], confirmed_at: CONFIRM_DATE)
82+
83+
User.find_by_email(GOOGLE_USER).tap do |user|
84+
assert user.confirmed?
85+
end
86+
87+
get '/users/auth/google/callback'
88+
89+
User.find_by_email(GOOGLE_USER).tap do |user|
90+
assert user.confirmed?
91+
end
92+
93+
User.find_by_email(GOOGLE_USER).tap do |user|
94+
assert_equal user.confirmed_at, CONFIRM_DATE
95+
end
96+
end
97+
end
98+
end

0 commit comments

Comments
 (0)