2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require "rails_helper"
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe ::DiscoursePoll::PollsController do
|
2015-04-23 13:33:29 -04:00
|
|
|
routes { ::DiscoursePoll::Engine.routes }
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2018-06-07 02:13:15 -04:00
|
|
|
let(:poll) { Fabricate(:post, topic: topic, user: user, raw: "[poll]\n- A\n- B\n[/poll]") }
|
|
|
|
let(:multi_poll) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
user: user,
|
|
|
|
raw: "[poll min=1 max=2 type=multiple public=true]\n- A\n- B\n[/poll]",
|
|
|
|
)
|
2023-01-06 15:42:16 -05:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:public_poll_on_vote) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
user: user,
|
|
|
|
raw: "[poll public=true results=on_vote]\n- A\n- B\n[/poll]",
|
|
|
|
)
|
2023-01-06 15:42:16 -05:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:public_poll_on_close) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
user: user,
|
|
|
|
raw: "[poll public=true results=on_close]\n- A\n- B\n[/poll]",
|
|
|
|
)
|
2023-01-06 15:42:16 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
describe "#vote" do
|
|
|
|
it "works" do
|
2019-05-17 02:16:02 -04:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message =
|
|
|
|
MessageBus
|
|
|
|
.track_publish(channel) do
|
2018-10-14 22:38:02 -04:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
2018-12-05 15:27:49 -05:00
|
|
|
end
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
expect(message.group_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works in PM" do
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
topic =
|
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user2),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
poll = Fabricate(:post, topic: topic, user: user, raw: "[poll]\n- A\n- B\n[/poll]")
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message =
|
|
|
|
MessageBus
|
|
|
|
.track_publish(channel) do
|
2018-12-05 15:27:49 -05:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
end
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-05 19:20:36 -05:00
|
|
|
expect(message.user_ids).to contain_exactly(user.id, user2.id)
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(message.group_ids).to eq(nil)
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2018-12-05 15:27:49 -05:00
|
|
|
it "works in secure categories" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add_owner(user)
|
|
|
|
category = Fabricate(:private_category, group: group)
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
poll = Fabricate(:post, topic: topic, user: user, raw: "[poll]\n- A\n- B\n[/poll]")
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message =
|
|
|
|
MessageBus
|
|
|
|
.track_publish(channel) do
|
2018-12-05 15:27:49 -05:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
2018-10-14 22:38:02 -04:00
|
|
|
end
|
|
|
|
.first
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
2015-05-04 10:01:57 -04:00
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
2015-05-01 10:33:24 -04:00
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
2018-10-14 22:38:02 -04:00
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-05 15:27:49 -05:00
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
expect(message.group_ids).to contain_exactly(group.id)
|
2015-05-01 10:33:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires at least 1 valid option" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote, params: { post_id: poll.id, poll_name: "poll", options: %w[A B] }, format: :json
|
2015-05-01 10:33:24 -04:00
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-05-01 10:33:24 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.requires_at_least_1_valid_option"))
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports vote changes" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["e89dec30bbd9bf50fabf6a05b4324edf"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-05-04 10:01:57 -04:00
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["poll"]["options"][0]["votes"]).to eq(0)
|
|
|
|
expect(json["poll"]["options"][1]["votes"]).to eq(1)
|
|
|
|
end
|
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
it "supports removing votes" do
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
delete :remove_vote, params: { post_id: poll.id, poll_name: "poll" }, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["poll"]["voters"]).to eq(0)
|
|
|
|
expect(json["poll"]["options"][0]["votes"]).to eq(0)
|
|
|
|
expect(json["poll"]["options"][1]["votes"]).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "works on closed topics" do
|
2015-04-23 13:33:29 -04:00
|
|
|
topic.update_attribute(:closed, true)
|
2018-02-26 18:19:44 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures topic is not archived" do
|
|
|
|
topic.update_attribute(:archived, true)
|
2018-02-26 18:19:44 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote, params: { post_id: poll.id, poll_name: "poll", options: ["A"] }, format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.topic_must_be_open_to_vote"))
|
|
|
|
end
|
|
|
|
|
2015-06-01 16:31:47 -04:00
|
|
|
it "ensures post is not trashed" do
|
|
|
|
poll.trash!
|
2018-02-26 18:19:44 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote, params: { post_id: poll.id, poll_name: "poll", options: ["A"] }, format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-06-01 16:31:47 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
|
|
|
|
end
|
|
|
|
|
2018-02-26 18:19:44 -05:00
|
|
|
it "ensures user can post in topic" do
|
|
|
|
Guardian.any_instance.expects(:can_create_post?).returns(false)
|
|
|
|
|
|
|
|
put :vote, params: { post_id: poll.id, poll_name: "poll", options: ["A"] }, format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-02-26 18:19:44 -05:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.user_cant_post_in_topic"))
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
it "checks the name of the poll" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote, params: { post_id: poll.id, poll_name: "foobar", options: ["A"] }, format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.no_poll_with_this_name", name: "foobar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures poll is open" do
|
2017-07-18 14:52:58 -04:00
|
|
|
closed_poll = create_post(raw: "[poll status=closed]\n- A\n- B\n[/poll]")
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: closed_poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.poll_must_be_open_to_vote"))
|
|
|
|
end
|
|
|
|
|
2020-01-28 07:30:04 -05:00
|
|
|
it "ensures user has required trust level" do
|
|
|
|
poll = create_post(raw: "[poll groups=#{Fabricate(:group).name}]\n- A\n- B\n[/poll]")
|
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2020-02-17 06:46:14 -05:00
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
I18n.t("js.poll.results.groups.title", groups: poll.polls.first.groups),
|
|
|
|
)
|
2020-01-28 07:30:04 -05:00
|
|
|
end
|
|
|
|
|
2016-04-15 17:26:18 -04:00
|
|
|
it "doesn't discard anonymous votes when someone votes" do
|
2018-11-19 08:50:00 -05:00
|
|
|
the_poll = poll.polls.first
|
|
|
|
the_poll.update_attribute(:anonymous_voters, 17)
|
|
|
|
the_poll.poll_options[0].update_attribute(:anonymous_votes, 11)
|
|
|
|
the_poll.poll_options[1].update_attribute(:anonymous_votes, 6)
|
2016-04-15 17:26:18 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: ["5c24fc1df56d764b550ceae1b9319125"],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2016-04-15 17:26:18 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2016-04-15 17:26:18 -04:00
|
|
|
expect(json["poll"]["voters"]).to eq(18)
|
|
|
|
expect(json["poll"]["options"][0]["votes"]).to eq(12)
|
|
|
|
expect(json["poll"]["options"][1]["votes"]).to eq(6)
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#toggle_status" do
|
|
|
|
it "works for OP" do
|
2019-05-17 02:16:02 -04:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message =
|
|
|
|
MessageBus
|
|
|
|
.track_publish(channel) do
|
2018-10-14 22:38:02 -04:00
|
|
|
put :toggle_status,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
status: "closed",
|
|
|
|
},
|
|
|
|
format: :json
|
2023-01-06 15:42:16 -05:00
|
|
|
|
2018-10-14 22:38:02 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
.first
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["poll"]["status"]).to eq("closed")
|
2019-05-17 02:16:02 -04:00
|
|
|
expect(message.channel).to eq(channel)
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works for staff" do
|
|
|
|
log_in(:moderator)
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message =
|
|
|
|
MessageBus
|
|
|
|
.track_publish(channel) do
|
2018-10-14 22:38:02 -04:00
|
|
|
put :toggle_status,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
status: "closed",
|
|
|
|
},
|
|
|
|
format: :json
|
2023-01-06 15:42:16 -05:00
|
|
|
|
2018-10-14 22:38:02 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
.first
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-04-23 13:33:29 -04:00
|
|
|
expect(json["poll"]["status"]).to eq("closed")
|
2019-05-17 02:16:02 -04:00
|
|
|
expect(message.channel).to eq(channel)
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
2015-06-01 16:31:47 -04:00
|
|
|
it "ensures post is not trashed" do
|
|
|
|
poll.trash!
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :toggle_status,
|
|
|
|
params: {
|
|
|
|
post_id: poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
status: "closed",
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).not_to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-06-01 16:31:47 -04:00
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2017-08-14 15:33:47 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "#voters" do
|
|
|
|
let(:first) { "5c24fc1df56d764b550ceae1b9319125" }
|
|
|
|
let(:second) { "e89dec30bbd9bf50fabf6a05b4324edf" }
|
2017-08-14 15:33:47 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "correctly handles offset" do
|
2017-08-14 15:33:47 -04:00
|
|
|
user1 = log_in
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: multi_poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [first],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-14 15:33:47 -04:00
|
|
|
|
|
|
|
user2 = log_in
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: multi_poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [first],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-14 15:33:47 -04:00
|
|
|
|
|
|
|
user3 = log_in
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
2018-11-19 08:50:00 -05:00
|
|
|
post_id: multi_poll.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [first, second],
|
2017-08-31 00:06:56 -04:00
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
get :voters, params: { poll_name: "poll", post_id: multi_poll.id, limit: 2 }, format: :json
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 02:13:15 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-14 15:33:47 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-08-14 15:33:47 -04:00
|
|
|
|
|
|
|
# no user3 cause voter_limit is 2
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(json["voters"][first].map { |h| h["id"] }).to contain_exactly(user1.id, user2.id)
|
|
|
|
expect(json["voters"][second].map { |h| h["id"] }).to contain_exactly(user3.id)
|
|
|
|
end
|
2018-08-16 21:15:29 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensures voters can only be seen after casting a vote" do
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: public_poll_on_vote.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [first],
|
|
|
|
},
|
|
|
|
format: :json
|
2018-08-16 21:15:29 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(response.status).to eq(200)
|
2018-08-16 21:15:29 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
get :voters, params: { poll_name: "poll", post_id: public_poll_on_vote.id }, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
expect(json["voters"][first].size).to eq(1)
|
|
|
|
|
2019-05-17 02:16:02 -04:00
|
|
|
_user2 = log_in
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
get :voters, params: { poll_name: "poll", post_id: public_poll_on_vote.id }, format: :json
|
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
expect(response.status).to eq(400)
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: public_poll_on_vote.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [second],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
get :voters, params: { poll_name: "poll", post_id: public_poll_on_vote.id }, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
expect(json["voters"][first].size).to eq(1)
|
|
|
|
expect(json["voters"][second].size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures voters can only be seen when poll is closed" do
|
|
|
|
put :vote,
|
|
|
|
params: {
|
|
|
|
post_id: public_poll_on_close.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
options: [first],
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
get :voters, params: { poll_name: "poll", post_id: public_poll_on_close.id }, format: :json
|
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
expect(response.status).to eq(400)
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
put :toggle_status,
|
|
|
|
params: {
|
|
|
|
post_id: public_poll_on_close.id,
|
|
|
|
poll_name: "poll",
|
|
|
|
status: "closed",
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-16 21:15:29 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
get :voters, params: { poll_name: "poll", post_id: public_poll_on_close.id }, format: :json
|
2018-08-16 21:15:29 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
expect(json["voters"][first].size).to eq(1)
|
2017-08-14 15:33:47 -04:00
|
|
|
end
|
2021-07-06 13:46:34 -04:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|