diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb index c9e12d415d..3c7bc43827 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -5,7 +5,7 @@ # jquery 1.9 ajax does error callback if 200 returns empty body. so use 204 instead. describe "update" do it "has a 200 status code when creating a new one" do - xhr :put, :update, :id => '2007020969', :format => :js + put :update, xhr: true, params: { id: '2007020969', format: :js } expect(response).to be_success expect(response.code).to eq "200" expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 1 @@ -16,7 +16,7 @@ allow(@controller).to receive_message_chain(:current_or_guest_user, :persisted?).and_return(true) allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :where, :exists?).and_return(false) allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :create).and_return(false) - xhr :put, :update, :id => 'iamabooboo', :format => :js + put :update, xhr: true, params: { id: 'iamabooboo', format: :js } expect(response.code).to eq "500" end end @@ -28,7 +28,7 @@ end it "has a 200 status code when delete is success" do - xhr :delete, :destroy, :id => '2007020969', :format => :js + delete :destroy, xhr: true, params: { id: '2007020969', format: :js } expect(response).to be_success expect(response.code).to eq "200" expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 0 @@ -39,7 +39,7 @@ allow(@controller).to receive_message_chain(:current_or_guest_user, :existing_bookmark_for).and_return(bm) allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :find_by).and_return(double('bookmark', delete: nil, destroyed?: false)) - xhr :delete, :destroy, :id => 'pleasekillme', :format => :js + delete :destroy, xhr: true, params: { id: 'pleasekillme', format: :js } expect(response.code).to eq "500" end @@ -55,7 +55,7 @@ end it 'finds the user from the encrypted token' do - get :index, encrypted_user_id: token + get :index, params: { encrypted_user_id: token } expect(controller.send(:token_user).id).to eq user.id end @@ -63,7 +63,7 @@ let(:current_time) { Time.zone.now - 2.hours } it 'is expired' do - get :index, encrypted_user_id: token + get :index, params: { encrypted_user_id: token } expect do controller.send(:token_user) end.to raise_error(Blacklight::Exceptions::ExpiredSessionToken) diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 9b53156fb0..a0ea13af0d 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -16,39 +16,39 @@ describe "preferred view" do it "should save the view choice" do - get :index, q: 'foo', view: 'gallery' + get :index, params: { q: 'foo', view: 'gallery' } expect(session[:preferred_view]).to eq 'gallery' end end # check each user manipulated parameter it "should have docs and facets for query with results", :integration => true do - get :index, q: user_query + get :index, params: { q: user_query } expect(assigns(:response).docs).to_not be_empty assert_facets_have_values(assigns(:response).aggregations) end it "should have docs and facets for existing facet value", :integration => true do - get :index, f: {"format" => 'Book'} + get :index, params: { f: {"format" => 'Book'} } expect(assigns(:response).docs).to_not be_empty assert_facets_have_values(assigns(:response).aggregations) end it "should have docs and facets for non-default results per page", :integration => true do num_per_page = 7 - get :index, :per_page => num_per_page + get :index, params: { per_page: num_per_page } expect(assigns(:response).docs).to have(num_per_page).items assert_facets_have_values(assigns(:response).aggregations) end it "should have docs and facets for second page", :integration => true do page = 2 - get :index, :page => page + get :index, params: { page: page } expect(assigns(:response).docs).to_not be_empty expect(assigns(:response).params[:start].to_i).to eq (page-1) * @controller.blacklight_config[:default_solr_params][:rows] assert_facets_have_values(assigns(:response).aggregations) end it "should have no docs or facet values for query without results", :integration => true do - get :index, q: 'sadfdsafasdfsadfsadfsadf' # query for no results + get :index, params: { q: 'sadfdsafasdfsadfsadfsadf' } # query for no results expect(assigns(:response).docs).to be_empty assigns(:response).aggregations.each do |key, facet| @@ -57,19 +57,19 @@ end it "should show 0 results when the user asks for an invalid value to a custom facet query", :integration => true do - get :index, f: {example_query_facet_field: 'bogus'} # bogus custom facet value + get :index, params: { f: { example_query_facet_field: 'bogus' } } # bogus custom facet value expect(assigns(:response).docs).to be_empty end it "should return results (possibly 0) when the user asks for a valid value to a custom facet query", :integration => true do - get :index, f: {example_query_facet_field: 'years_10'} # valid custom facet value with some results + get :index, params: { f: { example_query_facet_field: 'years_10' } } # valid custom facet value with some results expect(assigns(:response).docs).to_not be_empty - get :index, f: {example_query_facet_field: 'years_5'} # valid custom facet value with NO results + get :index, params: { f: {example_query_facet_field: 'years_5' } } # valid custom facet value with NO results expect(assigns(:response).docs).to be_empty end it "should have a spelling suggestion for an appropriately poor query", :integration => true do - get :index, :q => 'boo' + get :index, params: { q: 'boo' } expect(assigns(:response).spelling.words).to_not be_nil end @@ -78,7 +78,7 @@ allow(controller).to receive(:search_results) end it "includes search hash with key :q" do - get :index, q: user_query + get :index, params: { q: user_query } expect(session[:search]).to_not be_nil expect(session[:search].keys).to include 'id' @@ -114,7 +114,7 @@ describe "with format :rss" do it "should get the feed", :integration => true do - get :index, :format => 'rss' + get :index, params: { format: 'rss' } expect(response).to be_success end end @@ -122,7 +122,7 @@ describe "with format :json" do render_views before do - get :index, :format => 'json' + get :index, params: { format: 'json' } expect(response).to be_success end let(:json) { JSON.parse(response.body)['response'] } @@ -168,25 +168,25 @@ it "should not render when the config is false" do blacklight_config.index.respond_to.yaml = false - expect { get :index, format: 'yaml' }.to raise_error ActionController::RoutingError + expect { get :index, params: { format: 'yaml' } }.to raise_error ActionController::RoutingError end it "renders the default when the config is true" do # TODO: this should really stub a template and see if it gets rendered, # but how to do that is non-obvious.. blacklight_config.index.respond_to.yaml = true - expect { get :index, format: 'yaml' }.to raise_error ActionView::MissingTemplate + expect { get :index, params: { format: 'yaml' } }.to raise_error ActionView::MissingTemplate end it "should pass a hash to the render call" do blacklight_config.index.respond_to.yaml = { nothing: true, layout: false } - get :index, format: 'yaml' + get :index, params: { format: 'yaml' } expect(response.body).to be_blank end it "should evaluate a proc" do blacklight_config.index.respond_to.yaml = lambda { render text: "" } - get :index, format: 'yaml' + get :index, params: { format: 'yaml' } expect(response.body).to be_empty end @@ -196,7 +196,7 @@ end blacklight_config.index.respond_to.yaml = :render_some_yaml - get :index, format: 'yaml' + get :index, params: { format: 'yaml' } expect(response.body).to be_blank end end @@ -207,37 +207,37 @@ doc_id = '2007020969' it "should persist the search session id value into session[:search]" do - put :track, :id => doc_id, :counter => 3, search_id: "123" + put :track, params: { id: doc_id, counter: 3, search_id: "123" } expect(session[:search]['id']).to eq "123" end it "should set counter value into session[:search]" do - put :track, :id => doc_id, :counter => 3 + put :track, params: { id: doc_id, counter: 3 } expect(session[:search]['counter']).to eq "3" end it "should record the current per_page setting" do - put :track, :id => doc_id, :counter => 3, :per_page => 15 + put :track, params: { id: doc_id, counter: 3, per_page: 15 } expect(session[:search]['per_page']).to eq "15" end it "should redirect to show action for doc id" do - put :track, :id => doc_id, :counter => 3 + put :track, params: { id: doc_id, counter: 3 } assert_redirected_to(solr_document_path(doc_id)) end it "HTTP status code for redirect should be 303" do - put :track, :id => doc_id, :counter => 3 + put :track, params: { id: doc_id, counter: 3 } expect(response.status).to eq 303 end it "should redirect to the path given in the redirect param" do - put :track, :id => doc_id, :counter => 3, redirect: '/xyz' + put :track, params: { id: doc_id, counter: 3, redirect: '/xyz' } assert_redirected_to("/xyz") end it "should redirect to the path of the uri given in the redirect param" do - put :track, :id => doc_id, :counter => 3, redirect: 'http://localhost:3000/xyz' + put :track, params: { id: doc_id, counter: 3, redirect: 'http://localhost:3000/xyz' } assert_redirected_to("/xyz") end end @@ -249,14 +249,14 @@ describe "with format :html" do it "should get document", :integration => true do - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:document]).to_not be_nil end end describe "with format :json" do it "should get the feed" do - get :show, id: doc_id, format: 'json' + get :show, params: { id: doc_id, format: 'json' } expect(response).to be_success json = JSON.parse response.body expect(json["response"]["document"].keys).to match_array(["author_t", "opensearch_display", "marc_display", "published_display", "author_display", "lc_callnum_display", "title_t", "pub_date", "pub_date_sort", "subtitle_display", "format", "url_suppl_display", "material_type_display", "title_display", "subject_addl_t", "subject_t", "isbn_t", "id", "title_addl_t", "subject_geo_facet", "subject_topic_facet", "author_addl_t", "language_facet", "subtitle_t", "timestamp"]) @@ -278,23 +278,23 @@ end it "should set previous document if counter present in session" do session[:search] = @search_session.merge('counter' => 2) - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:previous_document]).to_not be_nil end it "should not set previous or next document if session is blank" do - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:previous_document]).to be_nil expect(assigns[:next_document]).to be_nil end it "should not set previous or next document if session[:search]['counter'] is nil" do session[:search] = {} - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:previous_document]).to be_nil expect(assigns[:next_document]).to be_nil end it "should set next document if counter present in session" do session[:search] = @search_session.merge('counter' => 2) - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:next_document]).to_not be_nil end @@ -302,7 +302,7 @@ allow(controller).to receive(:get_previous_and_next_documents_for_search) { raise Blacklight::Exceptions::InvalidRequest.new "Error" } - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(assigns[:previous_document]).to be_nil expect(assigns[:next_document]).to be_nil end @@ -310,7 +310,7 @@ # NOTE: status code is always 200 in isolation mode ... it "HTTP status code for GET should be 200", :integration => true do - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(response).to be_success end it "should render show.html.erb" do @@ -318,7 +318,7 @@ @mock_document = double() allow(@mock_document).to receive_messages(:export_formats => {}) allow(controller).to receive_messages(fetch: [@mock_response, @mock_document]) - get :show, :id => doc_id + get :show, params: { id: doc_id } expect(response).to render_template(:show) end @@ -330,7 +330,7 @@ allow(controller).to receive_messages(:find => @mock_response ) end before(:each) do - get :show, :id => doc_id + get :show, params: { id: doc_id } @document = assigns[:document] end it "should be a SolrDocument" do @@ -381,7 +381,7 @@ def export_as_mock end it "should respond to an extension-registered format properly" do - get :show, :id => doc_id, :format => "mock" + get :show, params: { id: doc_id, format: 'mock' } expect(response).to be_success expect(response.body).to match /mock_export/ end @@ -405,11 +405,11 @@ def export_as_mock end it "should return an opensearch description" do - get :opensearch, :format => 'xml' + get :opensearch, params: { format: 'xml' } expect(response).to be_success end it "should return valid JSON" do - get :opensearch,:format => 'json', :q => "a" + get :opensearch, params: { format: 'json', q: 'a' } expect(response).to be_success end end @@ -425,15 +425,15 @@ def export_as_mock end describe "email" do it "should give error if no TO parameter" do - post :email, :id => doc_id + post :email, params: { id: doc_id } expect(request.flash[:error]).to eq "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 :email, :id => doc_id, :to => 'test_bad_email' + post :email, params: { id: doc_id, to: 'test_bad_email' } expect(request.flash[:error]).to eq "You must enter a valid email address" end it "should not give error if no Message parameter is set" do - post :email, :id => doc_id, :to => 'test_email@projectblacklight.org' + post :email, params: { id: doc_id, to: 'test_email@projectblacklight.org' } expect(request.flash[:error]).to be_nil end it "should redirect back to the record upon success" do @@ -441,35 +441,35 @@ def export_as_mock allow(mock_mailer).to receive(:deliver) allow(RecordMailer).to receive(:email_record).with(anything, { :to => 'test_email@projectblacklight.org', :message => 'xyz' }, hash_including(:host => 'test.host')).and_return mock_mailer - post :email, :id => doc_id, :to => 'test_email@projectblacklight.org', :message => 'xyz' + post :email, params: { id: doc_id, to: 'test_email@projectblacklight.org', message: 'xyz' } expect(request.flash[:error]).to be_nil expect(request).to redirect_to(solr_document_path(doc_id)) end it "should render email_success for XHR requests" do - xhr :post, :email, :id => doc_id, :to => 'test_email@projectblacklight.org' + post :email, xhr: true, params: { id: doc_id, to: 'test_email@projectblacklight.org' } expect(request).to render_template 'email_success' expect(request.flash[:success]).to eq "Email Sent" end end describe "sms" do it "should give error if no phone number is given" do - post :sms, :id => doc_id, :carrier => 'att' + post :sms, params: { id: doc_id, carrier: 'att' } expect(request.flash[:error]).to eq "You must enter a recipient's phone number in order to send this message" end it "should give an error when a carrier is not provided" do - post :sms, :id => doc_id, :to => '5555555555', :carrier => '' + post :sms, params: { id: doc_id, to: '5555555555', carrier: '' } expect(request.flash[:error]).to eq "You must select a carrier" end it "should give an error when the phone number is not 10 digits" do - post :sms, :id => doc_id, :to => '555555555', :carrier => 'txt.att.net' + post :sms, params: { id: doc_id, to: '555555555', carrier: 'txt.att.net' } expect(request.flash[:error]).to eq "You must enter a valid 10 digit phone number" end it "should give an error when the carrier is not in our list of carriers" do - post :sms, :id => doc_id, :to => '5555555555', :carrier => 'no-such-carrier' + post :sms, params: { id: doc_id, to: '5555555555', carrier: 'no-such-carrier' } expect(request.flash[:error]).to eq "You must enter a valid carrier" end it "should allow punctuation in phone number" do - post :sms, :id => doc_id, :to => '(555) 555-5555', :carrier => 'txt.att.net' + post :sms, params: { id: doc_id, to: '(555) 555-5555', carrier: 'txt.att.net' } expect(request.flash[:error]).to be_nil expect(request).to redirect_to(solr_document_path(doc_id)) end @@ -477,16 +477,16 @@ def export_as_mock mock_mailer = double allow(mock_mailer).to receive(:deliver) expect(RecordMailer).to receive(:sms_record).with(anything, { to: '5555555555@txt.att.net' }, hash_including(:host => 'test.host')).and_return mock_mailer - post :sms, :id => doc_id, :to => '5555555555', :carrier => 'txt.att.net' + post :sms, params: { id: doc_id, to: '5555555555', carrier: 'txt.att.net' } end it "should redirect back to the record upon success" do - post :sms, :id => doc_id, :to => '5555555555', :carrier => 'txt.att.net' + post :sms, params: { id: doc_id, to: '5555555555', carrier: 'txt.att.net' } expect(request.flash[:error]).to eq nil expect(request).to redirect_to(solr_document_path(doc_id)) end it "should render sms_success template for XHR requests" do - xhr :post, :sms, :id => doc_id, :to => '5555555555', :carrier => 'txt.att.net' + post :sms, xhr: true, params: { id: doc_id, to: '5555555555', carrier: 'txt.att.net' } expect(request).to render_template 'sms_success' expect(request.flash[:success]).to eq "SMS Sent" end @@ -497,7 +497,7 @@ def export_as_mock it "should return status 404 for a record that doesn't exist" do @mock_response = double(documents: []) allow(controller).to receive_messages(:find => @mock_response) - get :show, :id=>"987654321" + get :show, params: { id: "987654321" } expect(response.status).to eq 404 expect(response.content_type).to eq Mime::HTML end @@ -505,7 +505,7 @@ def export_as_mock @mock_response = double(documents: []) allow(controller).to receive_messages(:find => @mock_response) - get :show, :id=>"987654321", :format => "xml" + get :show, params: { id: "987654321", format: "xml" } expect(response.status).to eq 404 expect(response.content_type).to eq Mime::XML end @@ -515,7 +515,7 @@ def export_as_mock allow(Rails.env).to receive_messages(:test? => false) allow(controller).to receive(:search_results) { |*args| raise fake_error } expect(controller.logger).to receive(:error).with(fake_error) - get :index, :q=>"+" + get :index, params: { q: '+' } expect(response.redirect_url).to eq root_url expect(request.flash[:notice]).to eq "Sorry, I don't understand your search." @@ -528,7 +528,7 @@ def export_as_mock allow(controller).to receive(:search_results) { |*args| raise fake_error } allow(controller.flash).to receive(:sweep) allow(controller).to receive(:flash).and_return(:notice => I18n.t('blacklight.search.errors.request_error')) - expect { get :index, q: "+" }.to raise_error Blacklight::Exceptions::InvalidRequest + expect { get :index, params: { q: '+' } }.to raise_error Blacklight::Exceptions::InvalidRequest end end @@ -549,13 +549,13 @@ def export_as_mock describe "facet" do describe "requesting js" do it "should be successful" do - xhr :get, :facet, id: 'format' + get :facet, xhr: true, params: { id: 'format' } expect(response).to be_successful end end describe "requesting html" do it "should be successful" do - get :facet, id: 'format' + get :facet, params: { id: 'format' } expect(response).to be_successful expect(assigns[:response]).to be_kind_of Blacklight::Solr::Response expect(assigns[:facet]).to be_kind_of Blacklight::Configuration::FacetField @@ -566,7 +566,7 @@ def export_as_mock describe "requesting json" do render_views it "is successful" do - get :facet, id: 'format', format: 'json' + get :facet, params: { id: 'format', format: 'json' } expect(response).to be_successful json = JSON.parse(response.body) expect(json["response"]["facets"]["items"].first["value"]).to eq 'Book' @@ -701,14 +701,14 @@ def export_as_mock describe "search_action_url" do it "should be the same as the catalog url" do - get :index, :page => 1 + get :index, params: { page: 1 } expect(controller.send(:search_action_url, q: "xyz")).to eq root_url(q: "xyz") end end describe "search_facet_url" do it "should be the same as the catalog url" do - get :index, :page => 1 + get :index, params: { page: 1 } expect(controller.send(:search_facet_url, id: "some_facet", page: 5)).to eq facet_catalog_url(id: "some_facet") end end diff --git a/spec/controllers/saved_searches_controller_spec.rb b/spec/controllers/saved_searches_controller_spec.rb index 862019f594..33ffd2bc46 100644 --- a/spec/controllers/saved_searches_controller_spec.rb +++ b/spec/controllers/saved_searches_controller_spec.rb @@ -17,22 +17,17 @@ describe "save" do it "should let you save a search" do - request.env["HTTP_REFERER"] = "where_i_came_from" session[:history] = [@one.id] - post :save, :id => @one.id + post :save, params: { id: @one.id } expect(response).to redirect_to "where_i_came_from" end it "should not let you save a search that isn't in your search history" do session[:history] = [@one.id] expect { - post :save, :id => @two.id + post :save, params: { id: @two.id } }.to raise_error ActiveRecord::RecordNotFound end - end - - - end diff --git a/spec/controllers/search_history_controller_spec.rb b/spec/controllers/search_history_controller_spec.rb index bae69717be..3138bf37de 100644 --- a/spec/controllers/search_history_controller_spec.rb +++ b/spec/controllers/search_history_controller_spec.rb @@ -37,7 +37,4 @@ expect(@searches).to be_empty end end - - - end diff --git a/spec/controllers/suggest_controller_spec.rb b/spec/controllers/suggest_controller_spec.rb index 42414e67de..b06e7e5a62 100644 --- a/spec/controllers/suggest_controller_spec.rb +++ b/spec/controllers/suggest_controller_spec.rb @@ -5,11 +5,11 @@ routes { Blacklight::Engine.routes } describe 'GET index' do it 'returns JSON' do - get :index, format: 'json' + get :index, params: { format: 'json' } expect(response.body).to eq [].to_json end it 'returns suggestions' do - get :index, format: 'json', q: 'new' + get :index, params: { format: 'json', q: 'new' } json = JSON.parse(response.body) expect(json.count).to eq 3 expect(json.first['term']).to eq 'new jersey' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 36bc761c86..9a5d95e77d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -66,4 +66,8 @@ config.include(ControllerLevelHelpers, type: :view) config.before(:each, type: :view) { initialize_controller_helpers(view) } + + unless Rails.version > '5' + config.include BackportTest, type: :controller + end end diff --git a/spec/support/backport_test.rb b/spec/support/backport_test.rb new file mode 100644 index 0000000000..cee114cc57 --- /dev/null +++ b/spec/support/backport_test.rb @@ -0,0 +1,38 @@ +# Backport the Rails 5 controller test methods to Rails 4 +module BackportTest + def delete(*args) + (action, rest) = *args + rest ||= {} + if rest[:xhr] + @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + end + super(action, rest[:params]) + end + + def get(*args) + (action, rest) = *args + rest ||= {} + if rest[:xhr] + @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + end + super(action, rest[:params]) + end + + def post(*args) + (action, rest) = *args + rest ||= {} + if rest[:xhr] + @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + end + super(action, rest[:params]) + end + + def put(*args) + (action, rest) = *args + rest ||= {} + if rest[:xhr] + @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + end + super(action, rest[:params]) + end +end