Skip to content

Commit

Permalink
Merge pull request #543 from alphagov/fix-race-condition-in-sponsors-…
Browse files Browse the repository at this point in the history
…controller

Fix race condition in sponsors controller
  • Loading branch information
alanth authored Sep 12, 2016
2 parents 141ec52 + ba4ccdb commit 9080c2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/sponsors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def update
format.html { render :show }
end
end
rescue ActiveRecord::RecordNotUnique => e
redirect_to thank_you_petition_sponsor_url(@petition, token: @petition.sponsor_token)
end

def thank_you
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/signatures_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ def do_post(options = {})

context "when a race condition occurs" do
let(:exception) { ActiveRecord::RecordNotUnique.new("PG::UniqueViolation") }

before do
FactoryGirl.create(:validated_signature, signature_params.merge(petition_id: petition.id))
allow_any_instance_of(Signature).to receive(:save).and_raise(exception)
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/sponsors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def do_patch(options = {})
expect(assigns[:stage_manager].stage).to eq 'signer'
end
end

context "when a race condition occurs" do
let(:exception) { ActiveRecord::RecordNotUnique.new("PG::UniqueViolation") }

before do
FactoryGirl.create(:sponsor, :validated, petition: petition)
allow_any_instance_of(Signature).to receive(:save).and_raise(exception)
end

it "redirects to the thank you page" do
do_patch
expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/sponsors/#{petition.sponsor_token}/thank-you")
end
end
end

context 'GET thank-you' do
Expand Down

0 comments on commit 9080c2b

Please sign in to comment.