Skip to content

Commit

Permalink
Merge pull request #506 from alphagov/fix-record-not-unique-errors
Browse files Browse the repository at this point in the history
Fix ActiveRecord::RecordNotUnique errors
  • Loading branch information
h-lame authored Jul 1, 2016
2 parents 8dfe249 + 35fd625 commit adc720a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/signatures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def handle_new_signature(petition)
format.html { render :new }
end
end
rescue ActiveRecord::RecordNotUnique => e
redirect_to thank_you_petition_signatures_url(petition)
end

def validate_sponsor
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/signatures_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ def do_post(options = {})
expect(response).to render_template(:new)
end
end

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)
end

it "redirects to the thank you page" do
do_post(stage: 'done')
expect(response).to redirect_to("https://petition.parliament.uk/petitions/#{petition.id}/signatures/thank-you")
end
end
end

context "when the petition is closed" do
Expand Down

0 comments on commit adc720a

Please sign in to comment.