From f47fa99b47f10c085ef32573472c5d3cb0d6d200 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 27 Jan 2025 16:16:23 -0800 Subject: [PATCH] Adds user.rb tests to boost coverage --- app/models/user.rb | 4 +++- spec/models/user_spec.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 spec/models/user_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index e2c1859..6ae8126 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,11 @@ class User < ApplicationRecord - # Connects this user object to Blacklights Bookmarks. include Blacklight::User + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :omniauthable, omniauth_providers: [:calnet] + class << self # Finds or creates a user record given data from the omniauth request hash def from_calnet(auth) @@ -21,6 +22,7 @@ def fake_email "fake-#{SecureRandom.hex[0, 16]}@noemail.com" end end + # Method added by Blacklight; Blacklight uses #to_s on your # user class to get a user-displayable login/identifier for # the account. diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..287b2b2 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe User do + describe 'initialization from calnet' do + # @todo Is this still valid since the switch from berkeleyEduOfficialEmail? + it 'assigns the user their berkeleyEduOfficialEmail' do + uid = 12_345 + User.from_calnet(OmniAuth::AuthHash.new({ + 'uid' => uid, + 'extra' => { + 'berkeleyEduOfficialEmail' => 'official@berkeley.edu' + } + })) + + user = User.find_by(calnet_uid: uid) + expect(user.email).to eq 'official@berkeley.edu' + end + end +end