-
Notifications
You must be signed in to change notification settings - Fork 253
/
catalog_controller_spec.rb
391 lines (353 loc) · 15 KB
/
catalog_controller_spec.rb
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'rubygems'
require 'marc'
describe CatalogController do
#=begin
# ROUTES and MAPPING
describe "Paths Generated by Custom Routes:" do
# paths generated by custom routes
it "should map {:controller => 'catalog', :id => '888', :action => 'email'} to /catalog/888/email" do
route_for(:controller => 'catalog', :action => 'email', :id => '888').should == '/catalog/888/email'
end
it "should map {:controller => 'catalog', :id => '777', :action => 'sms'} to /catalog/777/sms" do
route_for(:controller => 'catalog', :action => 'sms', :id => '777').should == '/catalog/777/sms'
end
it "should map { :controller => 'catalog', :action => 'show', :id => 666 } to /catalog/666" do
route_for(:controller => 'catalog', :action => 'show', :id => '666').should == '/catalog/666'
end
it "should map {:controller => 'catalog', :id = '444', :action => 'image'} to /catalog/444/image" do
route_for(:controller => 'catalog', :action => 'image', :id => '444').should == '/catalog/444/image'
end
it "should map {:controller => 'catalog', :id = '333', :action => 'status'} to /catalog/333/status" do
route_for(:controller => 'catalog', :action => 'status', :id => '333').should == '/catalog/333/status'
end
it "should map {:controller => 'catalog', :id => '222', :action => 'availability'} to /catalog/222/availability" do
route_for(:controller => 'catalog', :action => 'availability', :id => '222').should == '/catalog/222/availability'
end
it "should map {:controller => 'catalog', :id => '111', :action => 'librarian_view'} to /catalog/111/librarian_view" do
route_for(:controller => 'catalog', :action => 'librarian_view', :id => '111').should == '/catalog/111/librarian_view'
end
end
# parameters generated from routes
describe "Parameters Generated from Routes:" do
it "should map /catalog/888/email to {:controller => 'catalog', :action => 'email', :id => 888}" do
params_from(:get, '/catalog/888/email').should == {:controller => 'catalog', :action => 'email', :id => '888'}
end
it "should map /catalog/777/sms to {:controller => 'catalog', :action => 'sms', :id => 777}" do
params_from(:get, '/catalog/777/sms').should == {:controller => 'catalog', :action => 'sms', :id => '777'}
end
it "should map /catalog/666 to {:controller => 'catalog', :action => 'show', :id => 666}" do
params_from(:get, '/catalog/666').should == {:controller => 'catalog', :action => 'show', :id => '666'}
end
it "should map /catalog/444/image to {:controller => 'catalog', :action => 'image', :id => 444}" do
params_from(:get, '/catalog/444/image').should == {:controller => 'catalog', :action => 'image', :id => '444'}
end
it "should map /catalog/333/status to {:controller => 'catalog', :action => 'status', :id => 333}" do
params_from(:get, '/catalog/333/status').should == {:controller => 'catalog', :action => 'status', :id => '333'}
end
it "should map /catalog/222/availability to {:controller => 'catalog', :action => 'availability', :id => 222}" do
params_from(:get, '/catalog/222/availability').should == {:controller => 'catalog', :action => 'availability', :id => '222'}
end
it "should map /catalog/111/librarian_view to {:controller => 'catalog', :action => 'librarian_view', :id => 111}" do
params_from(:get, '/catalog/111/librarian_view').should == {:controller => 'catalog', :action => 'librarian_view', :id => '111'}
end
end
# INDEX ACTION
describe "index action" do
before(:each) do
@user_query = 'history' # query that will get results
@no_docs_query = 'sadfdsafasdfsadfsadfsadf' # query for no results
@facet_query = {"format" => 'Book'}
end
it "should have no search history if no search criteria" do
session[:history] = []
get :index
session[:history].length.should == 0
end
# check each user manipulated parameter
it "should have docs and facets for query with results" do
get :index, :q => @user_query
assigns[:response].docs.size.should > 1
assert_facets_have_values(assigns[:response].facets)
end
it "should have docs and facets for existing facet value" do
get :index, :f => @facet_query
assigns[:response].docs.size.should > 1
assert_facets_have_values(assigns[:response].facets)
end
it "should have docs and facets for non-default results per page" do
num_per_page = 7
get :index, :per_page => num_per_page
assigns[:response].docs.size.should == num_per_page
assert_facets_have_values(assigns[:response].facets)
end
it "should have docs and facets for second page" do
page = 2
get :index, :page => page
assigns[:response].docs.size.should > 1
assigns[:response].params[:start].to_i.should == (page-1) * Blacklight.config[:default_solr_params][:per_page]
assert_facets_have_values(assigns[:response].facets)
end
it "should have no docs or facet values for query without results" do
get :index, :q => @no_docs_query
assigns[:response].docs.size.should == 0
assigns[:response].facets.each do |facet|
facet.items.size.should == 0
end
end
it "should have a spelling suggestion for an appropriately poor query" do
get :index, :q => 'boo'
assigns[:response].spelling.words.should_not be_nil
end
describe "session" do
it "should include :search key with hash" do
get :index
session[:search].should_not be_nil
session[:search].should be_kind_of(Hash)
end
it "should include search hash with key :q" do
get :index, :q => @user_query
session[:search].should_not be_nil
session[:search].keys.should include(:q)
session[:search][:q].should == @user_query
end
it "should include search hash with key :f" do
get :index, :f => @facet_query
session[:search].should_not be_nil
session[:search].keys.should include(:f)
session[:search][:f].should == @facet_query
end
it "should include search hash with key :per_page" do
get :index, :per_page => 10
session[:search].should_not be_nil
session[:search].keys.should include(:per_page)
session[:search][:per_page].should == "10"
end
it "should include search hash with key :page" do
get :index, :page => 2
session[:search].should_not be_nil
session[:search].keys.should include(:page)
session[:search][:page].should == "2"
end
it "should include search hash with random key" do
# cause a plugin might add an unpredictable one, we want to preserve it.
get :index, :some_weird_key => "value"
session[:search].should_not be_nil
session[:search].keys.should include(:some_weird_key)
session[:search][:some_weird_key].should == "value"
end
end
describe "with index action with arbitrary key" do
before(:each) do
session[:history] = []
get :index, :arbitrary_key_from_plugin => "value"
end
it "should save search history" do
session[:history].length.should_not == 0
end
end
# check with no user manipulation
describe "for default query" do
it "should get documents when no query" do
get :index
assigns[:response].docs.size.should > 1
end
it "should get facets when no query" do
get :index
assert_facets_have_values(assigns[:response].facets)
end
end
it "should get rss feed" do
get :index, :format => 'rss'
response.should be_success
end
it "should render index.html.erb" do
get :index
response.should render_template(:index)
end
# NOTE: status code is always 200 in isolation mode ...
it "HTTP status code for GET should be 200" do
get :index
response.should be_success
end
end # describe index action
describe "update action" do
doc_id = '2007020969'
it "should set counter value into session[:search]" do
put :update, :id => doc_id, :counter => 3
session[:search][:counter].should == "3"
end
it "should redirect to show action for doc id" do
put :update, :id => doc_id, :counter => 3
assert_redirected_to(catalog_path(doc_id))
end
end
# SHOW ACTION
describe "show action" do
doc_id = '2007020969'
it "should get document" do
get :show, :id => doc_id
assigns[:document].should_not be_nil
end
it "should set previous document if counter present in session" do
session[:search] = {:q => "", :counter => 2}
get :show, :id => doc_id
assigns[:previous_document].should_not be_nil
end
it "should not set previous document if counter is 1" do
session[:search] = {:counter => 1}
get :show, :id => doc_id
assigns[:previous_document].should be_nil
end
it "should not set previous or next document if session is blank" do
get :show, :id => doc_id
assigns[:previous_document].should be_nil
assigns[:next_document].should be_nil
end
it "should not set previous or next document if session[:search][:counter] is nil" do
session[:search] = {:q => ""}
get :show, :id => doc_id
assigns[:previous_document].should be_nil
assigns[:next_document].should be_nil
end
it "should set next document if counter present in session" do
session[:search] = {:q => "", :counter => 2}
get :show, :id => doc_id
assigns[:next_document].should_not be_nil
end
it "should not set next document if counter is >= number of docs" do
session[:search] = {:counter => 66666666}
get :show, :id => doc_id
assigns[:next_document].should be_nil
end
# NOTE: status code is always 200 in isolation mode ...
it "HTTP status code for GET should be 200" do
get :show, :id => doc_id
response.should be_success
end
it "should render show.html.erb" do
get :show, :id => doc_id
response.should render_template(:show)
end
describe "@document" do
before(:each) do
get :show, :id => doc_id
@document = assigns[:document]
end
it "should be a SolrDocument" do
@document.should be_instance_of(SolrDocument)
end
end
describe "with dynamic export formats" do
module FakeExtension
def self.extended(document)
document.will_export_as(:mock, "application/mock")
end
def export_as_mock
"mock_export"
end
end
before(:each) do
SolrDocument.use_extension(FakeExtension)
end
it "should respond to an extension-registered format properly" do
get :show, :id => doc_id, :format => "mock"
response.should be_success
response.should have_text("mock_export")
end
after(:each) do
SolrDocument.registered_extensions = nil
end
end # dynamic export formats
end # describe show action
describe "opensearch" do
it "should return an opensearch description" do
get :opensearch, :format => 'xml'
response.should be_success
end
it "should return valid JSON" do
get :opensearch,:format => 'json', :q => "a"
response.should be_success
end
end
#=end
describe "send_email_record" do
doc_id = '2007020969'
describe "email" do
it "should give error if no TO paramater" do
post :send_email_record, :id => doc_id, :style => 'email'
response.flash[:error].should == "You must enter a recipient in order to send this message"
end
it "should give an error if the email address is not valid" do
post :send_email_record, :id => doc_id, :style => 'email', :to => 'test_bad_email'
response.flash[:error].should == "You must enter a valid email address"
end
it "should not give error if no Message paramater is set" do
pending() # see CODEBASE-227
#post :send_email_record, :id => doc_id, :style => 'email', :to => 'test_email@projectblacklight.org'
#response.flash[:error].should be_nil
end
it "should redirect back to the record upon success" do
pending() # see CODEBASE-227
#post :send_email_record, :id => doc_id, :style => 'email', :to => 'test_email@projectblacklight.org'
#response.flash[:error].should be_nil
#response.should redirect_to(catalog_path(doc_id))
end
end
describe "sms" do
it "should give error if no phone number is given" do
post :send_email_record, :id => doc_id, :style => 'sms', :carrier => 'att'
response.flash[:error].should == "You must enter a recipient in order to send this message"
end
it "should give an error when a carrier is not provided" do
post :send_email_record, :id => doc_id, :style => 'sms', :to => '5555555555', :carrier => ''
response.flash[:error].should == "You must select a carrier"
end
it "should give an error when the phone number is not 10 digits" do
post :send_email_record, :id => doc_id, :style => 'sms', :to => '555555555', :carrier => 'att'
response.flash[:error].should == "You must enter a valid 10 digit phone number"
end
it "should redirect back to the record upon success" do
pending() # see CODEBASE-227
#post :send_email_record, :id => doc_id, :style => 'sms', :to => '5555555555', :carrier => 'att'
#response.flash[:error].should be_nil
#response.should redirect_to(catalog_path(doc_id))
end
end
end
describe "facet_limit_for" do
it "should return default value for facet_field not specified" do
controller.facet_limit_for("zzz_unknown_facet_field").should == Blacklight.config[:facet][:limits][nil]
end
it "should return specified value for facet_field specified" do
controller.facet_limit_for("subject_facet").should == Blacklight.config[:facet][:limits]["subject_facet"]
end
it "facet_limit_hash should return hash with key being facet_field and value being configured limit" do
controller.facet_limit_hash.should == Blacklight.config[:facet][:limits]
end
end
describe "errors" do
it "should return status 404 for a record that doesn't exist" do
get :show, :id=>"987654321"
response.redirected_to.should == root_path
response.flash[:notice].should == "Sorry, you have requested a record that doesn't exist."
response.should_not be_success
response.status.should == "404 Not Found"
end
it "should return a status 500 for a bad search" do
get :index, :q=>"+"
response.redirected_to.should == root_path
response.flash[:notice].should == "Sorry, I don't understand your search."
response.should_not be_success
response.status.should == "500 Internal Server Error"
end
end
end
# there must be at least one facet, and each facet must have at least one value
def assert_facets_have_values(facets)
facets.size.should > 1
# should have at least one value for each facet
facets.each do |facet|
facet.items.size.should >= 1
end
end