Skip to content

Commit bd2ef35

Browse files
Respond to Copilot feedback
- Add env vars to README - Refactor guard clause for env into separate method, expand to include EXL_INST_ID - Fix typo in method comment - Restore logger to original state using begin/ensure blocks for those tests - Adds tests for updated guard clause - Update test names to reflect empty list return, not nil return - More robust test for env vars includes a confirmation of initial success - Adds test helper for scrubbing cassettes of cookie values - Scrubs cassettes
1 parent bd445a4 commit bd2ef35

8 files changed

Lines changed: 109 additions & 63 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ See `Optional Environment Variables` for more information.
7676
### Required Environment Variables
7777

7878
- `ALMA_OPENURL`: The base URL for Alma openurls found in CDI records.
79+
- `EXL_INST_ID`: The Ex Libris Institution ID. Used for constructing URLs.
80+
- `MIT_ALMA_URL`: The base URL for MIT Libraries' Alma instance (used to generate SRU API lookups).
7981
- `MIT_PRIMO_URL`: The base URL for MIT Libraries' Primo instance (used to generate record links).
8082
- `PRIMO_API_KEY`: The Primo Search API key.
8183
- `PRIMO_API_URL`: The Primo Search API base URL.

app/models/alma_sru.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class InvalidAlmaId < StandardError; end
2424
#
2525
# It accepts an "alma_client" argument for use when testing, but this is not used in normal operations.
2626
def self.lookup(raw_identifier, alma_client: nil)
27-
return [] unless alma_base_url
27+
return [] unless alma_sru_enabled?
2828

2929
# Validate the raw identifier received. This will raise an InvalidAlmaId if validation fails.
3030
identifier = validate_alma_id(raw_identifier)
@@ -73,7 +73,7 @@ def self.parse_response(raw_response, reference_identifier)
7373

7474
# validate_alma_id ensures we are only submitting valid Alma IDs to the SRU endpoint.
7575
#
76-
# It needs to do two thigns:
76+
# It needs to do two things:
7777
# 1. Remove the "alma" prefix if one is present. Otherwise, no manipulation of the submitted value should occur.
7878
# 2. Enforce the formatting requirements for a valid alma identifier (start with "99", and end with "6761").
7979
def self.validate_alma_id(raw)
@@ -129,12 +129,22 @@ def self.alma_base_url
129129
ENV.fetch('MIT_ALMA_URL', nil)
130130
end
131131

132+
def self.alma_sru_enabled?
133+
return false if alma_base_url.to_s.empty? || exl_inst_id.to_s.empty?
134+
135+
true
136+
end
137+
132138
def self.alma_sru_url(identifier)
133-
# example identifier: 990000959610106761
134-
"#{alma_base_url}/view/sru/#{ENV.fetch('EXL_INST_ID')}?version=1.2&operation=searchRetrieve&recordSchema=marcxml" \
139+
# example identifier: 9935177389906761
140+
"#{alma_base_url}/view/sru/#{exl_inst_id}?version=1.2&operation=searchRetrieve&recordSchema=marcxml" \
135141
"&query=alma.all_for_ui=#{identifier}"
136142
end
137143

144+
def self.exl_inst_id
145+
ENV.fetch('EXL_INST_ID', nil)
146+
end
147+
138148
def self.setup(url, alma_client)
139149
alma_client || HTTP.persistent(url)
140150
end

test/models/alma_sru_test.rb

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,50 @@ class AlmaSruTest < ActiveSupport::TestCase
6767

6868
test 'lookup returns empty list for non-existent records' do
6969
output = StringIO.new
70+
original_logger = Rails.logger
7071
Rails.logger = Logger.new(output)
7172

72-
VCR.use_cassette('alma sru nonexistent record') do
73-
needle = 'alma9900000000006761'
73+
begin
74+
VCR.use_cassette('alma sru nonexistent record') do
75+
needle = 'alma9900000000006761'
7476

75-
result = AlmaSru.lookup(needle)
77+
result = AlmaSru.lookup(needle)
7678

77-
assert_equal(result, [])
79+
assert_equal(result, [])
7880

79-
# This condition gets logged for local investigation due to control field mismatch
80-
assert_match('Alma lookup failure: Control field mismatch', output.string)
81+
# This condition gets logged for local investigation due to control field mismatch
82+
assert_match('Alma lookup failure: Control field mismatch', output.string)
83+
end
84+
ensure
85+
Rails.logger = original_logger
8186
end
8287
end
8388

84-
test 'lookup returns nil if env not set' do
85-
needle = '990002935920106761'
89+
test 'lookup returns empty list if alma URL not set' do
90+
needle = 'alma990014651640106761'
91+
92+
VCR.use_cassette('alma sru single record') do
93+
assert_equal(1, AlmaSru.lookup(needle).length)
94+
end
95+
8696
ClimateControl.modify(MIT_ALMA_URL: nil) do
8797
assert_equal([], AlmaSru.lookup(needle))
8898
end
8999
end
90100

91-
test 'lookup returns nil with non-complying ID' do
101+
test 'lookup returns empty list if exl_inst_id not set' do
102+
needle = 'alma990014651640106761'
103+
104+
VCR.use_cassette('alma sru single record') do
105+
assert_equal(1, AlmaSru.lookup(needle).length)
106+
end
107+
108+
ClimateControl.modify(EXL_INST_ID: nil) do
109+
assert_equal([], AlmaSru.lookup(needle))
110+
end
111+
end
112+
113+
test 'lookup returns empty list with non-complying ID' do
92114
needle = 'foo'
93115

94116
result = AlmaSru.lookup(needle)
@@ -99,34 +121,44 @@ class AlmaSruTest < ActiveSupport::TestCase
99121
test 'lookup survives failing to connect to Alma SRU' do
100122
alma_client = AlmaConnectionError.new
101123
output = StringIO.new
124+
original_logger = Rails.logger
102125
Rails.logger = Logger.new(output)
103126

104127
needle = 'alma990014651640106761'
105128

106-
assert_nothing_raised do
107-
result = AlmaSru.lookup(needle, alma_client: alma_client)
129+
begin
130+
assert_nothing_raised do
131+
result = AlmaSru.lookup(needle, alma_client: alma_client)
108132

109-
assert_equal([], result)
133+
assert_equal([], result)
110134

111-
# This condition gets logged for local investigation
112-
assert_match('Alma SRU connection error', output.string)
135+
# This condition gets logged for local investigation
136+
assert_match('Alma SRU connection error', output.string)
137+
end
138+
ensure
139+
Rails.logger = original_logger
113140
end
114141
end
115142

116143
test 'lookup survives Alma SRU errors' do
117144
alma_client = AlmaErrorResponse.new
118145
output = StringIO.new
146+
original_logger = Rails.logger
119147
Rails.logger = Logger.new(output)
120148

121149
needle = 'alma990014651640106761'
122150

123-
assert_nothing_raised do
124-
result = AlmaSru.lookup(needle, alma_client: alma_client)
151+
begin
152+
assert_nothing_raised do
153+
result = AlmaSru.lookup(needle, alma_client: alma_client)
125154

126-
assert_equal(result, [])
155+
assert_equal(result, [])
127156

128-
# This condition gets logged for local investigation
129-
assert_match('Alma lookup failure: 500', output.string)
157+
# This condition gets logged for local investigation
158+
assert_match('Alma lookup failure: 500', output.string)
159+
end
160+
ensure
161+
Rails.logger = original_logger
130162
end
131163
end
132164

test/test_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
config.filter_sensitive_data('FAKE_THIRDIRON_ID') { ENV.fetch('THIRDIRON_ID').to_s }
3333
config.filter_sensitive_data('FAKE_THIRDIRON_KEY') { ENV.fetch('THIRDIRON_KEY').to_s }
3434
config.filter_sensitive_data('FAKE_OPENALEX_EMAIL') { ENV.fetch('OPENALEX_EMAIL').to_s }
35+
# Filter cookie contents
36+
config.before_record do |interaction|
37+
cookies = interaction.response&.headers&.fetch('Set-Cookie', nil)
38+
next unless cookies
39+
40+
interaction.response.headers['Set-Cookie'] = cookies.map do |cookie|
41+
name = cookie.split('=', 2).first
42+
"#{name}=<FILTERED_COOKIE>"
43+
end
44+
end
3545
end
3646

3747
module ActiveSupport

test/vcr_cassettes/alma_sru_multiple_records.yml

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/alma_sru_no_availability.yml

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/alma_sru_nonexistent_record.yml

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/alma_sru_single_record.yml

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)