Skip to content

Commit 08b2175

Browse files
authored
Merge pull request #4993 from Benjamin-Couey/3845-friendly-message-on-invalid-token
3845 friendly message on invalid token
2 parents 007fad6 + 3e6fd8b commit 08b2175

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

app/controllers/application_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class ApplicationController < ActionController::Base
1414

1515
rescue_from ActiveRecord::RecordNotFound, with: :not_found!
1616

17+
rescue_from ActionController::InvalidAuthenticityToken do
18+
flash[:error] = "Your session expired. This could be due to leaving a page open for a long time, or having multiple tabs open. Try resubmitting."
19+
redirect_back fallback_location: root_path
20+
end
21+
1722
def current_organization
1823
return @current_organization if @current_organization
1924
return nil unless current_role

spec/system/authorization_system_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,46 @@
1616

1717
expect(current_path).to eql "/dashboard"
1818
end
19+
20+
context "Submitting a form with an invalid CSRF token" do
21+
before(:all) do
22+
ActionController::Base.allow_forgery_protection = true
23+
end
24+
25+
context "When logging in" do
26+
it "should redirect back and show a helpful message" do
27+
visit "/users/sign_in"
28+
fill_in "user_email", with: user.email
29+
fill_in "user_password", with: DEFAULT_USER_PASSWORD
30+
first('input[name="authenticity_token"]', visible: false).set("NOTAVALIDCSRFTOKEN")
31+
page.execute_script("$(\"meta[name='csrf-token']\").attr('content', 'NOTAVALIDCSRFTOKEN');")
32+
click_button "Log in"
33+
expect(current_path).to eql "/users/sign_in"
34+
expect(page).to have_content "Your session expired. This could be due to leaving a page open for a long time, or having multiple tabs open. Try resubmitting."
35+
end
36+
end
37+
38+
context "When logged in and creating a distribution" do
39+
before do
40+
create(:partner, organization: organization, name: "Test Partner")
41+
storage_location = create(:storage_location, organization: organization, name: "Test Storage Location")
42+
setup_storage_location(storage_location)
43+
end
44+
it "should redirect back and show a helpful message" do
45+
sign_in(user)
46+
visit new_distribution_path
47+
select "Test Partner", from: "Partner"
48+
select "Test Storage Location", from: "From storage location"
49+
first('input[name="authenticity_token"]', visible: false).set("NOTAVALIDCSRFTOKEN")
50+
page.execute_script("$(\"meta[name='csrf-token']\").attr('content', 'NOTAVALIDCSRFTOKEN');")
51+
click_button "Save"
52+
expect(current_path).to eql new_distribution_path
53+
expect(page).to have_content "Your session expired. This could be due to leaving a page open for a long time, or having multiple tabs open. Try resubmitting."
54+
end
55+
end
56+
57+
after(:all) do
58+
ActionController::Base.allow_forgery_protection = false
59+
end
60+
end
1961
end

0 commit comments

Comments
 (0)