Skip to content

Commit 86d2cec

Browse files
committed
Fix user serialization issues under Rails 7.0.3
1 parent 60c7af4 commit 86d2cec

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

app/controllers/sessions_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def index
1010
def callback
1111
logger.debug({ msg: 'Received omniauth callback', omniauth: auth_hash })
1212

13-
session[User::SESSION_KEY] = User.from_omniauth(auth_hash)
13+
user = User.from_omniauth(auth_hash)
14+
session[User::SESSION_KEY] = user.serializable_hash
1415
redirect_to(omniauth_origin, allow_other_host: true)
1516
end
1617

app/models/user.rb

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class User
2+
include ActiveModel::Model
3+
include ActiveModel::Attributes
24
include ActiveModel::Serialization
35
include BerkeleyLibrary::Logging
46

@@ -18,27 +20,24 @@ class User
1820
# Accessors
1921

2022
# @return [String]
21-
attr_reader :uid
23+
attribute :uid, :string
2224

2325
# @return [String]
24-
attr_reader :display_name
26+
attribute :display_name, :string
2527

2628
# @return [String] The user's email address.
27-
attr_reader :email
29+
attribute :email, :string
2830

2931
# @return [Boolean] True if the user is a GALC administrator, false otherwise.
30-
attr_reader :galc_admin
32+
attribute :galc_admin, :boolean
3133

3234
alias galc_admin? galc_admin
3335

3436
# ------------------------------------------------------------
3537
# Initializer
3638

3739
def initialize(uid: nil, display_name: nil, email: nil, galc_admin: false)
38-
@uid = uid
39-
@display_name = display_name
40-
@email = email
41-
@galc_admin = galc_admin
40+
super
4241
end
4342

4443
# ------------------------------------------------------------
@@ -67,11 +66,6 @@ def from_session(session)
6766
def galc_admin?(cal_groups)
6867
cal_groups && cal_groups.include?(GALC_ADMIN_GROUP)
6968
end
70-
71-
def attribute_names
72-
# Hack to leverage ActiveModel::Serialization#attribute_names
73-
@attribute_names ||= User.new.attribute_names
74-
end
7569
end
7670

7771
# ------------------------------------------------------------
@@ -81,15 +75,4 @@ def attribute_names
8175
def authenticated?
8276
!uid.nil?
8377
end
84-
85-
# ------------------------------
86-
# ActiveModel::Serialization
87-
88-
# Default attribute hash. Used by ActiveModel::Serialization to
89-
# implement `#attribute_names` and `#serializable_hash`.
90-
#
91-
# @return [Hash<String, Object>] a hash of default attribute values
92-
def attributes
93-
SERIALIZED_ATTRS.stringify_keys
94-
end
9578
end

spec/models/user_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,16 @@
7676
end
7777
end
7878
end
79+
80+
describe :serializable_hash do
81+
it 'serializes the user' do
82+
attrs = { uid: '5551213', display_name: 'Natalie Noe', email: '[email protected]', galc_admin: true }
83+
84+
user = User.new(**attrs)
85+
expected_hash = attrs.transform_keys(&:to_s)
86+
87+
expect(user.serializable_hash).to eq(expected_hash)
88+
end
89+
end
90+
7991
end

spec/requests/sessions_spec.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,26 @@ def callback_url_from_cas_redirect(loc)
7979
it 'initializes a user from the auth response and stores it in the session' do
8080
post login_path, params: { origin: origin_url }
8181

82-
user = session[User::SESSION_KEY]
83-
expect(user).to be_nil # just to be sure
82+
user_attrs = session[User::SESSION_KEY]
83+
expect(user_attrs).to be_nil # just to be sure
8484

8585
# NOTE: We can't use follow_redirect! b/c the Rack::Test mock client
8686
# thinks all requests are local; so we just pretend we hit the
8787
# CAS login URL and it redirected the browser back here.
8888
get callback_url_from_cas_redirect(response.headers['Location'])
8989

90-
user = session[User::SESSION_KEY]
91-
expect(user).to be_a(User)
92-
{
90+
expected_attrs = {
9391
uid: '5551215',
9492
display_name: 'Rachel Roe',
9593
9694
galc_admin: true
97-
}.each do |attr, v_expected|
95+
}.with_indifferent_access
96+
97+
user = User.from_session(session)
98+
99+
expected_attrs.each do |attr, v_expected|
98100
v_actual = user.send(attr)
99-
expect(v_actual).to eq(v_expected), "Wrong value for #{attr}; expected #{v_expected}, was #{v_actual}"
101+
expect(v_actual).to eq(v_expected), "Wrong value for #{attr}; expected #{v_expected.inspect}, was #{v_actual.inspect}"
100102
end
101103

102104
expect(response).to redirect_to(origin_url)

0 commit comments

Comments
 (0)