Skip to content

Commit 67a686e

Browse files
Handle case where alma user lookup returns a 400 (#39)
* Handle case where alma user lookup returns a 400 having Error::AlmaRecordNotFoundError only return res.status * refactored AlmaRecordNotFoundError rescue
1 parent 339ba48 commit 67a686e

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

app/models/alma/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def find_if_active(id)
4343
return nil unless rec && rec.active?
4444
end
4545
rescue Error::AlmaRecordNotFoundError => e
46-
return if e.message.include?('Alma query failed with response: 404')
46+
return if %w[400 404].include? e.message
4747

4848
raise
4949
end

app/services/alma_services.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ 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 Error::AlmaRecordNotFoundError, "Alma query failed with response: #{res.status}" unless res.status == 200
53+
raise Error::AlmaRecordNotFoundError, res.status unless res.status == 200
5454
end
5555
end
5656

@@ -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 Error::AlmaRecordNotFoundError, "Alma query failed with response: #{res.status}" unless res.status == 200
100+
raise Error::AlmaRecordNotFoundError, res.status unless res.status == 200
101101
end
102102
end
103103
end

spec/forms_helper.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,18 @@ def show_path(id)
6464
it 'forbids users when active patron lookup returns Alma 404' do
6565
patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
6666
allow(AlmaServices::Patron).to receive(:get_user).with(patron_id)
67-
.and_raise(Error::AlmaRecordNotFoundError,
68-
'Alma query failed with response: 404')
67+
.and_raise(Error::AlmaRecordNotFoundError, '404')
68+
69+
with_patron_login(patron_id) do
70+
get new_form_path
71+
expect(response).to have_http_status(:forbidden)
72+
end
73+
end
74+
75+
it 'forbids users when active patron lookup returns Alma 400' do
76+
patron_id = Alma::Type.sample_id_for(allowed_patron_types.first)
77+
allow(AlmaServices::Patron).to receive(:get_user).with(patron_id)
78+
.and_raise(Error::AlmaRecordNotFoundError, '400')
6979

7080
with_patron_login(patron_id) do
7181
get new_form_path

spec/models/user_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
user = User.new(uid: 'fake_uid')
249249
allow(Alma::User).to receive(:find_if_active).with('fake_uid')
250250
.and_raise(Error::AlmaRecordNotFoundError,
251-
'Alma query failed with response: 500')
251+
'500')
252252

253253
expect { user.primary_patron_record }.to raise_error(Error::AlmaRecordNotFoundError)
254254
end

0 commit comments

Comments
 (0)