mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 07:11:34 +08:00
FIX: Validate number of votes allowed per poll per user. (#15001)
* DEV: Remove spec that we no longer need. As far as we know, the migration has been successful for a number of years. * FIX: Validate number of votes allowed per poll per user.
This commit is contained in:

committed by
GitHub

parent
254689b1fb
commit
1d0faedfbc
131
plugins/poll/spec/lib/poll_spec.rb
Normal file
131
plugins/poll/spec/lib/poll_spec.rb
Normal file
@ -0,0 +1,131 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe DiscoursePoll::Poll do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:user_2) { Fabricate(:user) }
|
||||
|
||||
fab!(:post_with_regular_poll) do
|
||||
Fabricate(:post, raw: <<~RAW)
|
||||
[poll]
|
||||
* 1
|
||||
* 2
|
||||
[/poll]
|
||||
RAW
|
||||
end
|
||||
|
||||
fab!(:post_with_multiple_poll) do
|
||||
Fabricate(:post, raw: <<~RAW)
|
||||
[poll type=multiple min=2 max=3]
|
||||
* 1
|
||||
* 2
|
||||
* 3
|
||||
* 4
|
||||
* 5
|
||||
[/poll]
|
||||
RAW
|
||||
end
|
||||
|
||||
describe '.vote' do
|
||||
it 'should only allow one vote per user for a regular poll' do
|
||||
poll = post_with_regular_poll.polls.first
|
||||
|
||||
expect do
|
||||
DiscoursePoll::Poll.vote(
|
||||
user,
|
||||
post_with_regular_poll.id,
|
||||
"poll",
|
||||
poll.poll_options.map(&:digest)
|
||||
)
|
||||
end.to raise_error(DiscoursePoll::Error, I18n.t("poll.one_vote_per_user"))
|
||||
end
|
||||
|
||||
it 'should clean up bad votes for a regular poll' do
|
||||
poll = post_with_regular_poll.polls.first
|
||||
|
||||
PollVote.create!(
|
||||
poll: poll,
|
||||
poll_option: poll.poll_options.first,
|
||||
user: user
|
||||
)
|
||||
|
||||
PollVote.create!(
|
||||
poll: poll,
|
||||
poll_option: poll.poll_options.last,
|
||||
user: user
|
||||
)
|
||||
|
||||
DiscoursePoll::Poll.vote(
|
||||
user,
|
||||
post_with_regular_poll.id,
|
||||
"poll",
|
||||
[poll.poll_options.first.digest]
|
||||
)
|
||||
|
||||
expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id))
|
||||
.to contain_exactly(poll.poll_options.first.id)
|
||||
end
|
||||
|
||||
it 'allows user to vote on multiple options correctly for a multiple poll' do
|
||||
poll = post_with_multiple_poll.polls.first
|
||||
poll_options = poll.poll_options
|
||||
|
||||
[
|
||||
poll_options.first,
|
||||
poll_options.second,
|
||||
poll_options.third,
|
||||
].each do |poll_option|
|
||||
PollVote.create!(poll: poll, poll_option: poll_option, user: user)
|
||||
end
|
||||
|
||||
DiscoursePoll::Poll.vote(
|
||||
user,
|
||||
post_with_multiple_poll.id,
|
||||
"poll",
|
||||
[poll_options.first.digest, poll_options.second.digest]
|
||||
)
|
||||
|
||||
DiscoursePoll::Poll.vote(
|
||||
user_2,
|
||||
post_with_multiple_poll.id,
|
||||
"poll",
|
||||
[poll_options.third.digest, poll_options.fourth.digest]
|
||||
)
|
||||
|
||||
expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id))
|
||||
.to contain_exactly(poll_options.first.id, poll_options.second.id)
|
||||
|
||||
expect(PollVote.where(poll: poll, user: user_2).pluck(:poll_option_id))
|
||||
.to contain_exactly(poll_options.third.id, poll_options.fourth.id)
|
||||
end
|
||||
|
||||
it 'should respect the min/max votes per user for a multiple poll' do
|
||||
poll = post_with_multiple_poll.polls.first
|
||||
|
||||
expect do
|
||||
DiscoursePoll::Poll.vote(
|
||||
user,
|
||||
post_with_multiple_poll.id,
|
||||
"poll",
|
||||
poll.poll_options.map(&:digest)
|
||||
)
|
||||
end.to raise_error(
|
||||
DiscoursePoll::Error,
|
||||
I18n.t("poll.max_vote_per_user", count: poll.max)
|
||||
)
|
||||
|
||||
expect do
|
||||
DiscoursePoll::Poll.vote(
|
||||
user,
|
||||
post_with_multiple_poll.id,
|
||||
"poll",
|
||||
[poll.poll_options.first.digest]
|
||||
)
|
||||
end.to raise_error(
|
||||
DiscoursePoll::Error,
|
||||
I18n.t("poll.min_vote_per_user", count: poll.min)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user