2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require "rails_helper"
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
describe ::DiscoursePoll::PollsController do
|
|
|
|
routes { ::DiscoursePoll::Engine.routes }
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
2018-06-07 14:13:15 +08:00
|
|
|
let(:poll) { Fabricate(:post, topic: topic, user: user, raw: "[poll]\n- A\n- B\n[/poll]") }
|
|
|
|
let(:multi_poll) { Fabricate(:post, topic: topic, user: user, raw: "[poll min=1 max=2 type=multiple public=true]\n- A\n- B\n[/poll]") }
|
2018-11-19 21:50:00 +08:00
|
|
|
let(:public_poll_on_vote) { Fabricate(:post, topic: topic, user: user, raw: "[poll public=true results=on_vote]\n- A\n- B\n[/poll]") }
|
|
|
|
let(:public_poll_on_close) { Fabricate(:post, topic: topic, user: user, raw: "[poll public=true results=on_close]\n- A\n- B\n[/poll]") }
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
describe "#vote" do
|
|
|
|
|
|
|
|
it "works" do
|
2019-05-17 14:16:02 +08:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message = MessageBus.track_publish(channel) do
|
2018-10-15 10:38:02 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
2018-12-06 04:27:49 +08:00
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
|
|
|
|
2019-05-17 14:16:02 +08:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-06 04:27:49 +08: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 14:16:02 +08:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message = MessageBus.track_publish(channel) do
|
2018-12-06 04:27:49 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
|
|
|
|
2019-05-17 14:16:02 +08:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-06 08:20:36 +08:00
|
|
|
expect(message.user_ids).to contain_exactly(user.id, user2.id)
|
2018-12-06 04:27:49 +08:00
|
|
|
expect(message.group_ids).to eq(nil)
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2018-12-06 04:27:49 +08: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 14:16:02 +08:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message = MessageBus.track_publish(channel) do
|
2018-12-06 04:27:49 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
2018-10-15 10:38:02 +08:00
|
|
|
end.first
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2018-12-06 04:27:49 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["poll"]["name"]).to eq("poll")
|
2015-05-04 22:01:57 +08:00
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
2015-05-01 22:33:24 +08:00
|
|
|
expect(json["vote"]).to eq(["5c24fc1df56d764b550ceae1b9319125"])
|
2018-10-15 10:38:02 +08:00
|
|
|
|
2019-05-17 14:16:02 +08:00
|
|
|
expect(message.channel).to eq(channel)
|
2018-12-06 04:27:49 +08:00
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
expect(message.group_ids).to contain_exactly(group.id)
|
2015-05-01 22:33:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires at least 1 valid option" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["A", "B"]
|
|
|
|
}, format: :json
|
2015-05-01 22:33:24 +08:00
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-05-01 22:33:24 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.requires_at_least_1_valid_option"))
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports vote changes" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
2015-05-04 22:01:57 +08:00
|
|
|
expect(json["poll"]["voters"]).to eq(1)
|
2015-04-24 01:33:29 +08:00
|
|
|
expect(json["poll"]["options"][0]["votes"]).to eq(0)
|
|
|
|
expect(json["poll"]["options"][1]["votes"]).to eq(1)
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
it "works on closed topics" do
|
2015-04-24 01:33:29 +08:00
|
|
|
topic.update_attribute(:closed, true)
|
2018-02-27 07:19:44 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures topic is not archived" do
|
|
|
|
topic.update_attribute(:archived, true)
|
2018-02-27 07:19:44 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["A"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.topic_must_be_open_to_vote"))
|
|
|
|
end
|
|
|
|
|
2015-06-02 04:31:47 +08:00
|
|
|
it "ensures post is not trashed" do
|
|
|
|
poll.trash!
|
2018-02-27 07:19:44 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["A"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-06-02 04:31:47 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
|
|
|
|
end
|
|
|
|
|
2018-02-27 07:19:44 +08: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 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2018-02-27 07:19:44 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.user_cant_post_in_topic"))
|
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
it "checks the name of the poll" do
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "foobar", options: ["A"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
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-19 02:52:58 +08:00
|
|
|
closed_poll = create_post(raw: "[poll status=closed]\n- A\n- B\n[/poll]")
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
put :vote, params: {
|
|
|
|
post_id: closed_poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.poll_must_be_open_to_vote"))
|
|
|
|
end
|
|
|
|
|
2016-04-16 05:26:18 +08:00
|
|
|
it "doesn't discard anonymous votes when someone votes" do
|
2018-11-19 21:50:00 +08: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-16 05:26:18 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
put :vote, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-04-16 05:26:18 +08:00
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
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-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#toggle_status" do
|
|
|
|
|
|
|
|
it "works for OP" do
|
2019-05-17 14:16:02 +08:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message = MessageBus.track_publish(channel) do
|
2018-10-15 10:38:02 +08:00
|
|
|
put :toggle_status, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", status: "closed"
|
|
|
|
}, format: :json
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2018-10-15 10:38:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end.first
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["poll"]["status"]).to eq("closed")
|
2019-05-17 14:16:02 +08:00
|
|
|
expect(message.channel).to eq(channel)
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works for staff" do
|
|
|
|
log_in(:moderator)
|
|
|
|
|
2019-05-17 14:16:02 +08:00
|
|
|
channel = "/polls/#{poll.topic_id}"
|
|
|
|
|
|
|
|
message = MessageBus.track_publish(channel) do
|
2018-10-15 10:38:02 +08:00
|
|
|
put :toggle_status, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", status: "closed"
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end.first
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["poll"]["status"]).to eq("closed")
|
2019-05-17 14:16:02 +08:00
|
|
|
expect(message.channel).to eq(channel)
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
2015-06-02 04:31:47 +08:00
|
|
|
it "ensures post is not trashed" do
|
|
|
|
poll.trash!
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
put :toggle_status, params: {
|
|
|
|
post_id: poll.id, poll_name: "poll", status: "closed"
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).not_to eq(200)
|
2015-06-02 04:31:47 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
|
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2017-08-15 03:33:47 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
describe "#voters" do
|
2017-08-15 03:33:47 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
let(:first) { "5c24fc1df56d764b550ceae1b9319125" }
|
|
|
|
let(:second) { "e89dec30bbd9bf50fabf6a05b4324edf" }
|
2017-08-15 03:33:47 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
it "correctly handles offset" do
|
2017-08-15 03:33:47 +08:00
|
|
|
user1 = log_in
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
put :vote, params: {
|
|
|
|
post_id: multi_poll.id, poll_name: "poll", options: [first]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-15 03:33:47 +08:00
|
|
|
|
|
|
|
user2 = log_in
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
put :vote, params: {
|
|
|
|
post_id: multi_poll.id, poll_name: "poll", options: [first]
|
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-15 03:33:47 +08:00
|
|
|
|
|
|
|
user3 = log_in
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
put :vote, params: {
|
2018-11-19 21:50:00 +08:00
|
|
|
post_id: multi_poll.id, poll_name: "poll", options: [first, second]
|
2017-08-31 12:06:56 +08:00
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
get :voters, params: {
|
2018-11-19 21:50:00 +08:00
|
|
|
poll_name: 'poll', post_id: multi_poll.id, limit: 2
|
2017-08-31 12:06:56 +08:00
|
|
|
}, format: :json
|
|
|
|
|
2018-06-07 14:13:15 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-15 03:33:47 +08:00
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
# no user3 cause voter_limit is 2
|
2018-11-19 21:50:00 +08: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-17 09:15:29 +08:00
|
|
|
|
2018-11-19 21:50:00 +08: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-17 09:15:29 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-08-17 09:15:29 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
get :voters, params: {
|
|
|
|
poll_name: "poll", post_id: public_poll_on_vote.id
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json["voters"][first].size).to eq(1)
|
|
|
|
|
2019-05-17 14:16:02 +08:00
|
|
|
_user2 = log_in
|
2018-11-19 21:50:00 +08:00
|
|
|
|
|
|
|
get :voters, params: {
|
|
|
|
poll_name: "poll", post_id: public_poll_on_vote.id
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
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-17 09:15:29 +08:00
|
|
|
|
|
|
|
get :voters, params: {
|
2018-11-19 21:50:00 +08:00
|
|
|
poll_name: "poll", post_id: public_poll_on_close.id
|
2018-08-17 09:15:29 +08:00
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json["voters"][first].size).to eq(1)
|
2017-08-15 03:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|