-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtind_download_request_spec.rb
More file actions
252 lines (206 loc) · 8.84 KB
/
tind_download_request_spec.rb
File metadata and controls
252 lines (206 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require 'forms_helper'
describe TindDownloadController, type: :request do
before do
allow(BerkeleyLibrary::TIND::Config).to receive(:api_key).and_return('not-a-real-api-key')
Rails.cache.clear
end
let(:collection_name) { 'Abraham Lincoln Papers' }
attr_reader :patron_id
attr_reader :patron
attr_reader :user
context 'specs for unauthenticated user' do
before do
logout! # just to be sure
end
describe 'form' do
it 'redirects to login' do
get(form_path = tind_download_path)
login_with_callback_url = "#{login_path}?#{URI.encode_www_form(url: form_path)}"
expect(response).to redirect_to(login_with_callback_url)
end
end
describe 'find_collection' do
it 'redirects to login' do
get tind_download_find_collection_path
login_with_callback_url = "#{login_path}?#{URI.encode_www_form(url: tind_download_find_collection_path)}"
expect(response).to redirect_to(login_with_callback_url)
end
end
describe 'download' do
it 'POST redirects to login' do
post tind_download_download_path, params: { collection_name:, export_format: 'csv' }
login_with_callback_url = "#{login_path}?#{URI.encode_www_form(url: tind_download_download_path)}"
expect(response).to redirect_to(login_with_callback_url)
end
it 'GET redirects to login' do
params = { collection_name:, export_format: 'csv' }
get tind_download_download_path, params: params
callback_url = "#{tind_download_download_path}?#{URI.encode_www_form(params)}"
login_with_callback_url = "#{login_path}?#{URI.encode_www_form(url: callback_url)}"
expect(response).to redirect_to(login_with_callback_url)
end
end
end
context 'specs for non-staff user' do
before do
patron_id = Alma::Type.sample_id_for(Alma::Type::FACULTY)
login_as_patron(patron_id)
end
after do
logout!
end
describe 'form' do
it 'returns 403' do
get tind_download_path
expect(response).to have_http_status :forbidden
end
end
describe 'find_collection' do
it 'returns 403' do
get tind_download_find_collection_path
expect(response).to have_http_status :forbidden
end
end
describe 'download' do
it 'returns 403 for a POST' do
post tind_download_download_path, params: { collection_name:, export_format: 'csv' }
expect(response).to have_http_status :forbidden
end
it 'returns 403 for a GET' do
get tind_download_download_path, params: { collection_name:, export_format: 'csv' }
expect(response).to have_http_status :forbidden
end
end
end
context 'specs for staff user' do
before do
patron_id = Alma::Type.sample_id_for(Alma::Type::STAFF)
login_as_patron(patron_id)
stub_request(:get, 'https://digicoll.lib.berkeley.edu/api/v1/collections?depth=100').to_return(
status: 200,
body: File.new('spec/data/tind_download/collections.json')
)
end
after do
logout!
end
describe 'form' do
it 'is displayed' do
get tind_download_path
expect(response).to have_http_status :ok
expect(response.body).to include('TIND Metadata Download')
end
end
describe 'find_collection' do
it 'requires a collection' do
get tind_download_find_collection_path
expect(response).to have_http_status :bad_request
end
it 'returns the matching collection name list' do
get tind_download_find_collection_path, params: { collection_name: 'Abraham' }
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
expect(result).to be_a(Array)
expect(result).to contain_exactly('Abraham Lincoln Papers', 'Veterans of the Abraham Lincoln Brigade')
end
it 'returns an empty list for an unmatched name' do
get tind_download_find_collection_path, params: { collection_name: 'axolotl' }
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
expect(result).to be_a(Array)
expect(result).to be_empty
end
end
describe 'download' do
describe 'valid collection' do
before do
search_url = 'https://digicoll.lib.berkeley.edu/api/v1/search'
search_params = { c: collection_name, format: 'xml' }
search_params_with_search_id = search_params.merge(search_id: 'DnF1ZXJ5VGhlbkZldGNoBQAAAAABsY')
result_1 = File.read('spec/data/tind_download/tind-abraham-lincoln-1.xml')
result_2 = File.read('spec/data/tind_download/tind-abraham-lincoln-2.xml')
stub_request(:get, search_url).with(query: search_params).to_return(status: 200, body: result_1)
stub_request(:get, search_url).with(query: search_params_with_search_id).to_return(status: 200, body: result_2)
end
def verify_ods(expected_path, body)
Dir.mktmpdir(File.basename(__FILE__, 'rb')) do |dir|
actual_path = File.join(dir, File.basename(expected_path))
File.binwrite(actual_path, body)
ss_expected = Roo::Spreadsheet.open(expected_path, file_warning: :warning)
ss_actual = Roo::Spreadsheet.open(actual_path, file_warning: :warning)
first_column, first_row, last_column, last_row = verify_row_geometry(ss_actual, ss_expected)
aggregate_failures(:values) do
(first_row..last_row).each do |row|
(first_column..last_column).each do |col|
verify_value(ss_actual, row, col, ss_expected)
end
end
end
end
end
def verify_csv(expected_path, body)
expected = File.read(expected_path, encoding: 'UTF-8')
expect(body).to eq(expected)
end
def verify_row_geometry(ss_actual, ss_expected)
row_and_col_attrs = %i[first_row first_column last_row last_column]
row_and_col_attrs.each do |attr|
expected = ss_expected.send(attr)
actual = ss_actual.send(attr)
expect(actual).to eq(expected), "Expected #{attr} to be #{expected}, but was #{actual}"
end
first_row, first_column, last_row, last_column = row_and_col_attrs.map { |attr| ss_expected.send(attr) }
[first_column, first_row, last_column, last_row]
end
def verify_value(ss_actual, row, col, ss_expected)
expected_value = ss_expected.cell(row, col)
actual_value = ss_actual.cell(row, col)
msg = -> { "Expected value at (#{[row, col].join(', ')}) to be #{expected_value.inspect}, but was #{actual_value.inspect}" }
expect(actual_value).to eq(expected_value), msg
end
BerkeleyLibrary::TIND::Export::ExportFormat.each do |fmt|
describe fmt do
let(:ext) { fmt.value.downcase }
let(:expected_filename) { "abraham-lincoln-papers.#{ext}" }
let(:expected_path) { File.join('spec/data/tind_download', expected_filename) }
let(:verify_method) { :"verify_#{ext}" }
it 'supports POST' do
post tind_download_download_path, params: { collection_name:, export_format: ext }
expect(response).to have_http_status :ok
send(verify_method, expected_path, body)
end
it 'supports GET' do
get tind_download_download_path, params: { collection_name:, export_format: ext }
expect(response).to have_http_status :ok
send(verify_method, expected_path, body)
end
end
end
end
describe 'invalid collection' do
let(:invalid_collection) { 'Not a collection' }
before do
search_url = 'https://digicoll.lib.berkeley.edu/api/v1/search'
search_params = { c: invalid_collection, format: 'xml' }
body = File.read('spec/data/tind_download/tind-not-a-collection.json')
stub_request(:get, search_url).with(query: search_params)
.to_return(status: 500, body:, headers: { 'Content-Type' => 'applicaton/json' })
end
BerkeleyLibrary::TIND::Export::ExportFormat.each do |fmt|
let(:ext) { fmt.value.downcase }
let(:params) { { collection_name: invalid_collection, export_format: ext } }
describe fmt do
it 'GET returns 404' do
get tind_download_download_path, params: { collection_name: invalid_collection, export_format: ext }
expect(response).to have_http_status :not_found
end
it 'POST returns 404' do
post tind_download_download_path, params: { collection_name: invalid_collection, export_format: ext }
expect(response).to have_http_status :not_found
end
end
end
end
end
end
end