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

@@ -155,6 +155,16 @@ describe Poll::Answer do
expect(Poll::Voter.count).to be 1
end
it "does not create two voters when calling the method twice at the same time", :race_condition do
answer = create(:poll_answer, question: question, author: author, answer: "Yes")
2.times.map do
Thread.new { answer.save_and_record_voter_participation }
end.each(&:join)
expect(Poll::Voter.count).to be 1
end
end
describe "#destroy_and_remove_voter_participation" do

View File

@@ -744,6 +744,24 @@ describe User do
expect(Poll::Voter.where(user: other_user).count).to eq(0)
expect(Poll::Voter.where(user: user)).to match_array [v1, v2]
end
it "does not reassign votes if the user has already voted" do
poll = create(:poll)
user = create(:user, :level_three)
other_user = create(:user, :level_three)
voter = create(:poll_voter, poll: poll, user: user)
other_voter = create(:poll_voter, poll: poll, user: other_user)
other_poll_voter = create(:poll_voter, poll: create(:poll), user: other_user)
expect(Poll::Voter.where(user: user)).to eq [voter]
expect(Poll::Voter.where(user: other_user)).to match_array [other_voter, other_poll_voter]
user.take_votes_from(other_user)
expect(Poll::Voter.where(user: user)).to match_array [voter, other_poll_voter]
expect(Poll::Voter.where(user: other_user)).to eq []
end
end
describe "#take_votes_if_erased_document" do