Skip to content

Commit 1424aa2

Browse files
committed
Created custom alma_record_not_found_error, raising 403 if Alma patron record is not active or missing
1 parent 563bb99 commit 1424aa2

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Error
2+
# Raised when Alma returns a not-found style response.
3+
# Subclassing ActiveRecord::RecordNotFound preserves existing 404 handling.
4+
class AlmaRecordNotFoundError < ActiveRecord::RecordNotFound
5+
end
6+
end

app/models/alma/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def find_if_active(id)
4242
find_if_exists(id).tap do |rec|
4343
return nil unless rec && rec.active?
4444
end
45+
rescue Error::AlmaRecordNotFoundError => e
46+
return if e.message.include?('Alma query failed with response: 404')
47+
48+
raise
4549
end
4650
end
4751

app/services/alma_services.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def authenticate_alma_patron?(alma_user_id, alma_password)
5050
def get_user(alma_user_id)
5151
params = { view: 'full', expand: 'fees' }
5252
connection.get(user_uri_for(alma_user_id), params).tap do |res|
53-
raise ActiveRecord::RecordNotFound, "Alma query failed with response: #{res.status}" unless res.status == 200
53+
raise Error::AlmaRecordNotFoundError, "Alma query failed with response: #{res.status}" unless res.status == 200
5454
end
5555
end
5656

5757
def save(alma_user_id, user)
5858
# noinspection RubyArgCount
5959
connection.put(user_uri_for(alma_user_id), user.to_json, { 'Content-Type' => 'application/json' }).tap do |res|
60-
raise ActiveRecord::RecordNotFound, 'Failed to save.' unless res.status == 200
60+
raise Error::AlmaRecordNotFoundError, 'Failed to save.' unless res.status == 200
6161
end
6262

6363
'Saved user.' # TODO: what is this for?
@@ -85,7 +85,7 @@ def fee_uri_for(alma_id, fee_id)
8585

8686
def fetch_all(alma_user_id)
8787
res = connection.get(fees_uri_for(alma_user_id))
88-
raise ActiveRecord::RecordNotFound, 'No fees could be found.' unless res.status == 200
88+
raise Error::AlmaRecordNotFoundError, 'No fees could be found.' unless res.status == 200
8989

9090
JSON.parse(res.body)
9191
end
@@ -97,7 +97,7 @@ def credit(alma_user_id, pp_ref_number, fee)
9797
payment_uri = URIs.append(fee_uri_for(alma_user_id, fee.id), '?', URI.encode_www_form(params))
9898

9999
connection.post(payment_uri).tap do |res|
100-
raise ActiveRecord::RecordNotFound, "Alma query failed with response: #{res.status}" unless res.status == 200
100+
raise Error::AlmaRecordNotFoundError, "Alma query failed with response: #{res.status}" unless res.status == 200
101101
end
102102
end
103103
end
@@ -119,14 +119,14 @@ def fetch_sets(env, offset = 0)
119119
}
120120

121121
res = connection(env).get(URIs.append(alma_api_url, 'conf/sets'), params)
122-
raise ActiveRecord::RecordNotFound, 'No item sets could be found..' unless res.status == 200
122+
raise Error::AlmaRecordNotFoundError, 'No item sets could be found..' unless res.status == 200
123123

124124
JSON.parse(res.body)
125125
end
126126

127127
def fetch_set(env, id)
128128
res = connection(env).get(URIs.append(alma_api_url, "conf/sets/#{id}"))
129-
raise ActiveRecord::RecordNotFound, "No set with ID #{id} found..." unless res.status == 200
129+
raise Error::AlmaRecordNotFoundError, "No set with ID #{id} found..." unless res.status == 200
130130

131131
JSON.parse(res.body)
132132
end
@@ -137,15 +137,15 @@ def fetch_members(set_id, env, offset = 0)
137137
limit: 100
138138
}
139139
res = connection(env).get(URIs.append(alma_api_url, "conf/sets/#{set_id}/members"), params)
140-
raise ActiveRecord::RecordNotFound, 'No item sets could be found.' unless res.status == 200
140+
raise Error::AlmaRecordNotFoundError, 'No item sets could be found.' unless res.status == 200
141141

142142
JSON.parse(res.body)
143143
end
144144

145145
def fetch_item(env, mms_id, holding_id, item_pid)
146146
uri = URIs.append(alma_api_url, "bibs/#{mms_id}/holdings/#{holding_id}/items/#{item_pid}")
147147
res = connection(env).get(uri)
148-
raise ActiveRecord::RecordNotFound, 'Item could be found.' unless res.status == 200
148+
raise Error::AlmaRecordNotFoundError, 'Item could be found.' unless res.status == 200
149149

150150
JSON.parse(res.body)
151151
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<h1>Forbidden</h1>
2-
<p>Your patron record cannot be found. Please contact the <a href="https://www.lib.berkeley.edu/find/privileges-desk">Privileges Desk</a>.</p>
2+
<p>This form is only available to active library patrons. Please contact the <a href="https://www.lib.berkeley.edu/find/privileges-desk">Privileges Desk</a>.</p>

spec/forms_helper.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,23 @@ def show_path(id)
5656
with_patron_login(patron_id) do
5757
get new_form_path
5858
expect(response).to have_http_status(:forbidden)
59-
expected_msg = /Your patron record cannot be found/
59+
expected_msg = /This form is only available to active library patrons/
6060
expect(response.body).to match(expected_msg)
6161
end
6262
end
6363

64+
it 'forbids users when active patron lookup returns Alma 404' do
65+
patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
66+
allow(AlmaServices::Patron).to receive(:get_user).with(patron_id)
67+
.and_raise(Error::AlmaRecordNotFoundError,
68+
'Alma query failed with response: 404')
69+
70+
with_patron_login(patron_id) do
71+
get new_form_path
72+
expect(response).to have_http_status(:forbidden)
73+
end
74+
end
75+
6476
describe 'with a valid user' do
6577
attr_reader :patron_id
6678
attr_reader :patron

spec/models/user_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,23 @@
235235
end
236236
end
237237

238+
describe :primary_patron_record do
239+
it 'returns nil when active patron lookup returns nil' do
240+
user = User.new(uid: 'does_not_exist')
241+
allow(Alma::User).to receive(:find_if_active).with('does_not_exist')
242+
.and_return(nil)
243+
244+
expect(user.primary_patron_record).to be_nil
245+
end
246+
247+
it 're-raises active patron lookup errors' do
248+
user = User.new(uid: 'fake_uid')
249+
allow(Alma::User).to receive(:find_if_active).with('fake_uid')
250+
.and_raise(Error::AlmaRecordNotFoundError,
251+
'Alma query failed with response: 500')
252+
253+
expect { user.primary_patron_record }.to raise_error(Error::AlmaRecordNotFoundError)
254+
end
255+
end
256+
238257
end

0 commit comments

Comments
 (0)