Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#669 bad page number parameters causing ArgumentError #1144

Merged
merged 6 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base
# Scrub sensitive parameters from your log
# filter_parameter_logging :password

before_filter :set_header_variable, :set_view_path
before_filter :set_header_variable, :set_view_path, :validate_page_param
before_action :configure_permitted_parameters, if: :devise_controller?

def authenticate_active_admin_user!
Expand Down Expand Up @@ -45,4 +45,12 @@ def ssl_required?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :organisation])
end

def validate_page_param
unless params[:page].present? && params[:page].to_i > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use unless with else. Rewrite these with the positive case first.

I also notice your indentation is a little out on the params lines - you need an extra space.

params[:page] = nil
else
params[:page] = params[:page].to_i
end
end
end
15 changes: 15 additions & 0 deletions spec/controllers/applications_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
get :index
expect(assigns[:rss]).to be_nil
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this stray extra line.

it "should try to convert page param to an integer" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my first review I didn't check where these tests were placed. They're under #index rss feed and since this isn't related to the RSS feed these tests should not be here.

But where should they go? I didn't know so I hunted around. I discovered that ApplicationController behaviour that does things like global error checking should be tested by defining an anonymous controller.

So create a new file in this directory called application_controller_spec.rb, with something like this (and removing the tests from this file):

require "spec_helper"

describe ApplicationController do
  controller do
    def index
      render text: nil
    end
  end

  describe "#validate_page_param" do
    it "should try to convert page param to an integer" do
      get :index, page: "2%5B&q"

      expect(controller.params[:page]).to eq(2)
    end

    it "should default to page nil when no page number param is given" do
      get :index, page: "%5B&q"

      expect(controller.params[:page]).to eq(nil)
    end
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do!

VCR.use_cassette('planningalerts') do
get :index, page: "2%5B&q"
end
expect(controller.params[:page]).to eq(2)
end

it "should default to page nil when no page number param is given" do
VCR.use_cassette('planningalerts') do
get :index, page: "%5B&q"
end
expect(controller.params[:page]).to eq(nil)
end

end

describe "error checking on parameters used" do
Expand Down