Skip to content

Commit 35fd625

Browse files
committed
Fix ActiveRecord::RecordNotUnique errors
When someone does a double submit we can get uniqueness exceptions from the database. In these circumstances just redirect to the thank you page as the first request will have sent the email.
1 parent 8dfe249 commit 35fd625

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

app/controllers/signatures_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def handle_new_signature(petition)
149149
format.html { render :new }
150150
end
151151
end
152+
rescue ActiveRecord::RecordNotUnique => e
153+
redirect_to thank_you_petition_signatures_url(petition)
152154
end
153155

154156
def validate_sponsor

spec/controllers/signatures_controller_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,19 @@ def do_post(options = {})
430430
expect(response).to render_template(:new)
431431
end
432432
end
433+
434+
context "when a race condition occurs" do
435+
let(:exception) { ActiveRecord::RecordNotUnique.new("PG::UniqueViolation") }
436+
before do
437+
FactoryGirl.create(:validated_signature, signature_params.merge(petition_id: petition.id))
438+
allow_any_instance_of(Signature).to receive(:save).and_raise(exception)
439+
end
440+
441+
it "redirects to the thank you page" do
442+
do_post(stage: 'done')
443+
expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/signatures/thank-you")
444+
end
445+
end
433446
end
434447

435448
context "when the petition is closed" do

0 commit comments

Comments
 (0)