Prevent creation of duplicate poll voters

Note that, when taking votes from an erased user, since poll answers
don't belong to poll voters, we were not migrating them in the
`take_votes_from` method (and we aren't migrating them now either).
This commit is contained in:
Javi Martín
2024-05-11 18:13:35 +02:00
parent 175e990bb4
commit 9a8bfac5bd
6 changed files with 68 additions and 3 deletions

View File

@@ -0,0 +1,27 @@
require "rails_helper"
describe Officing::VotersController do
describe "POST create" do
it "does not create two records with two simultaneous requests", :race_condition do
officer = create(:poll_officer)
poll = create(:poll, officers: [officer])
user = create(:user, :level_two)
sign_in(officer.user)
2.times.map do
Thread.new do
begin
post :create, params: {
voter: { poll_id: poll.id, user_id: user.id },
format: :js
}
rescue ActionDispatch::IllegalStateError
end
end
end.each(&:join)
expect(Poll::Voter.count).to eq 1
end
end
end