Skip to content

Commit ba4ccdb

Browse files
committed
Fix race condition in sponsors controller
Similar to #506, there's a race condition in the sponsors controller that can be triggered by a double submit. We can fix it in a similar fashion as well by redirecting to the thank you url as the email will have already been sent by the first submit.
1 parent afd6b37 commit ba4ccdb

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

app/controllers/sponsors_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def update
3131
format.html { render :show }
3232
end
3333
end
34+
rescue ActiveRecord::RecordNotUnique => e
35+
redirect_to thank_you_petition_sponsor_url(@petition, token: @petition.sponsor_token)
3436
end
3537

3638
def thank_you

spec/controllers/signatures_controller_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ def do_post(options = {})
477477

478478
context "when a race condition occurs" do
479479
let(:exception) { ActiveRecord::RecordNotUnique.new("PG::UniqueViolation") }
480+
480481
before do
481482
FactoryGirl.create(:validated_signature, signature_params.merge(petition_id: petition.id))
482483
allow_any_instance_of(Signature).to receive(:save).and_raise(exception)

spec/controllers/sponsors_controller_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ def do_patch(options = {})
284284
expect(assigns[:stage_manager].stage).to eq 'signer'
285285
end
286286
end
287+
288+
context "when a race condition occurs" do
289+
let(:exception) { ActiveRecord::RecordNotUnique.new("PG::UniqueViolation") }
290+
291+
before do
292+
FactoryGirl.create(:sponsor, :validated, petition: petition)
293+
allow_any_instance_of(Signature).to receive(:save).and_raise(exception)
294+
end
295+
296+
it "redirects to the thank you page" do
297+
do_patch
298+
expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/sponsors/#{petition.sponsor_token}/thank-you")
299+
end
300+
end
287301
end
288302

289303
context 'GET thank-you' do

0 commit comments

Comments
 (0)