2019-05-03 06:17:27 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
|
RSpec.describe PostsController do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
let!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2015-04-24 01:33:29 +08:00
|
|
|
|
let!(:title) { "Testing Poll Plugin" }
|
|
|
|
|
|
2023-11-28 22:34:02 +08:00
|
|
|
|
before do
|
|
|
|
|
SiteSetting.min_first_post_typing_time = 0
|
|
|
|
|
log_in_user(user)
|
|
|
|
|
end
|
2015-08-05 10:39:38 +08:00
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
|
describe "polls" do
|
|
|
|
|
it "works" do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
Group.refresh_automatic_groups!
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "works on any post" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post_1 = Fabricate(:post)
|
|
|
|
|
|
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
topic_id: post_1.topic.id,
|
|
|
|
|
raw: "[poll]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-05-07 17:31:20 +08:00
|
|
|
|
it "schedules auto-close job" do
|
2018-12-13 16:39:36 +08:00
|
|
|
|
freeze_time
|
2018-05-07 17:31:20 +08:00
|
|
|
|
name = "auto_close"
|
2020-03-11 05:13:17 +08:00
|
|
|
|
close_date = 1.month.from_now.round
|
2018-05-07 17:31:20 +08:00
|
|
|
|
|
2018-12-13 16:39:36 +08:00
|
|
|
|
expect do
|
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll name=#{name} close=#{close_date.iso8601}]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
end.to change { Jobs::ClosePoll.jobs.size }.by(1) & change { Poll.count }.by(1)
|
2018-05-07 17:31:20 +08:00
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2018-12-13 16:39:36 +08:00
|
|
|
|
post_id = json["id"]
|
|
|
|
|
|
2020-03-11 05:13:17 +08:00
|
|
|
|
expect(Poll.find_by(post_id: post_id).close_at).to eq_time(close_date)
|
2018-12-13 16:39:36 +08:00
|
|
|
|
|
|
|
|
|
job = Jobs::ClosePoll.jobs.first
|
|
|
|
|
job_args = job["args"].first
|
|
|
|
|
|
|
|
|
|
expect(job_args["post_id"]).to eq(post_id)
|
|
|
|
|
expect(job_args["poll_name"]).to eq(name)
|
2018-05-07 17:31:20 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
|
it "should have different options" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- A\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options"))
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-24 00:51:05 +08:00
|
|
|
|
it "accepts different Chinese options" do
|
|
|
|
|
SiteSetting.default_locale = "zh_CN"
|
|
|
|
|
|
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll]\n- Microsoft Edge(新)\n- Microsoft Edge(旧)\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-05 22:03:27 +08:00
|
|
|
|
it "should have at least 1 options" do
|
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n[/poll]" }, format: :json
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2020-02-05 22:03:27 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_1_option"))
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-05-01 22:44:51 +08:00
|
|
|
|
it "should have at most 'SiteSetting.poll_maximum_options' options" do
|
2019-05-13 10:16:17 +08:00
|
|
|
|
raw = +"[poll]\n"
|
2015-05-01 22:44:51 +08:00
|
|
|
|
(SiteSetting.poll_maximum_options + 1).times { |n| raw << "\n- #{n}" }
|
2017-07-19 02:31:22 +08:00
|
|
|
|
raw << "\n[/poll]"
|
2015-05-01 22:44:51 +08:00
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create, params: { title: title, raw: raw }, format: :json
|
2015-05-01 22:44:51 +08:00
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-09-28 03:36:57 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
|
I18n.t("poll.default_poll_must_have_less_options", count: SiteSetting.poll_maximum_options),
|
|
|
|
|
)
|
2015-05-01 22:44:51 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-06-02 01:28:05 +08:00
|
|
|
|
it "should have valid parameters" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-06-02 01:28:05 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"),
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-12 02:09:17 +08:00
|
|
|
|
it "prevents self-xss" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2023-06-20 09:49:22 +08:00
|
|
|
|
expect(json["cooked"]).to include("data-poll-name=\"<script>alert('xss')</script>\"")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.find_by(post_id: json["id"]).name).to eq(
|
|
|
|
|
"<script>alert('xss')</script>",
|
|
|
|
|
)
|
2015-05-12 02:09:17 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
|
it "also works when there is a link starting with '[poll'" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-05-13 23:50:25 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2015-05-13 23:50:25 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
|
it "prevents poll-inception" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-05-14 05:12:53 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.where(post_id: json["id"]).count).to eq(1)
|
2015-05-14 05:12:53 +08:00
|
|
|
|
end
|
|
|
|
|
|
2020-10-02 15:21:24 +08:00
|
|
|
|
it "accepts polls with titles" do
|
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll]\n# What's up?\n- one\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
poll = Poll.last
|
|
|
|
|
expect(poll).to_not be_nil
|
|
|
|
|
expect(poll.title).to eq("What’s up?")
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
|
describe "edit window" do
|
|
|
|
|
describe "within the first 5 minutes" do
|
|
|
|
|
let(:post_id) do
|
2017-07-24 21:17:42 +08:00
|
|
|
|
freeze_time(4.minutes.ago) do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
|
response.parsed_body["id"]
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "can be changed" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
put :update,
|
|
|
|
|
params: {
|
|
|
|
|
id: post_id,
|
|
|
|
|
post: {
|
|
|
|
|
raw: "[poll]\n- A\n- B\n- C\n[/poll]",
|
|
|
|
|
},
|
2023-01-07 04:42:16 +08:00
|
|
|
|
},
|
2017-08-31 12:06:56 +08:00
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(json["post"]["polls"][0]["options"][2]["html"]).to eq("C")
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
2024-02-29 12:57:53 +08:00
|
|
|
|
it "does not clear votes when poll has no change" do
|
|
|
|
|
DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"])
|
|
|
|
|
put :update,
|
|
|
|
|
params: {
|
|
|
|
|
id: post_id,
|
|
|
|
|
post: {
|
|
|
|
|
raw: "[poll]\n- A\n- B\n[/poll]\n This poll has no change, but the raw does.",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
json = response.parsed_body
|
|
|
|
|
expect(json["post"]["polls_votes"]["poll"]).to match_array(
|
|
|
|
|
"5c24fc1df56d764b550ceae1b9319125",
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "resets the votes when poll is changed" do
|
2021-10-05 16:38:49 +08:00
|
|
|
|
DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"])
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
|
|
put :update,
|
|
|
|
|
params: {
|
|
|
|
|
id: post_id,
|
|
|
|
|
post: {
|
|
|
|
|
raw: "[poll]\n- A\n- B\n- C\n[/poll]",
|
|
|
|
|
},
|
2023-01-07 04:42:16 +08:00
|
|
|
|
},
|
2017-08-31 12:06:56 +08:00
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-05-07 00:52:09 +08:00
|
|
|
|
expect(json["post"]["polls_votes"]).to_not be
|
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-10 15:15:29 +08:00
|
|
|
|
describe "after the poll edit window has expired" do
|
2017-07-19 02:31:22 +08:00
|
|
|
|
let(:poll) { "[poll]\n- A\n- B\n[/poll]" }
|
|
|
|
|
let(:new_option) { "[poll]\n- A\n- C\n[/poll]" }
|
|
|
|
|
let(:updated) { "before\n\n[poll]\n- A\n- B\n[/poll]\n\nafter" }
|
2015-09-15 01:27:54 +08:00
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
|
let(:post_id) do
|
2017-07-24 21:17:42 +08:00
|
|
|
|
freeze_time(6.minutes.ago) do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create, params: { title: title, raw: poll }, format: :json
|
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
|
response.parsed_body["id"]
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-10 15:15:29 +08:00
|
|
|
|
let(:poll_edit_window_mins) { 6 }
|
|
|
|
|
|
|
|
|
|
before { SiteSetting.poll_edit_window_mins = poll_edit_window_mins }
|
|
|
|
|
|
2015-09-26 00:54:15 +08:00
|
|
|
|
describe "with no vote" do
|
2018-11-19 21:50:00 +08:00
|
|
|
|
it "can change the options" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
put :update, params: { id: post_id, post: { raw: new_option } }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(json["post"]["polls"][0]["options"][1]["html"]).to eq("C")
|
2015-09-26 00:54:15 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "support changes on the post" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
put :update, params: { id: post_id, post: { raw: updated } }, format: :json
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-09-26 00:54:15 +08:00
|
|
|
|
expect(json["post"]["cooked"]).to match("before")
|
|
|
|
|
end
|
2015-09-15 01:27:54 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with at least one vote" do
|
|
|
|
|
before do
|
2021-10-05 16:38:49 +08:00
|
|
|
|
DiscoursePoll::Poll.vote(user, post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"])
|
2015-09-15 01:27:54 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
it "cannot change the options" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
put :update, params: { id: post_id, post: { raw: new_option } }, format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2016-08-10 15:15:29 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
|
I18n.t(
|
2018-11-19 21:50:00 +08:00
|
|
|
|
"poll.edit_window_expired.cannot_edit_default_poll_with_votes",
|
2016-08-10 15:15:29 +08:00
|
|
|
|
minutes: poll_edit_window_mins,
|
2023-01-07 04:42:16 +08:00
|
|
|
|
),
|
2016-08-10 15:15:29 +08:00
|
|
|
|
)
|
2015-09-26 00:54:15 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-09-15 01:27:54 +08:00
|
|
|
|
it "support changes on the post" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
put :update, params: { id: post_id, post: { raw: updated } }, format: :json
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-09-15 01:27:54 +08:00
|
|
|
|
expect(json["post"]["cooked"]).to match("before")
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "named polls" do
|
|
|
|
|
it "should have different options" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw:
|
|
|
|
|
"[poll name=" \
|
|
|
|
|
"foo" \
|
|
|
|
|
"]\n- A\n- A\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
|
I18n.t("poll.named_poll_must_have_different_options", name: "foo"),
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-05 22:03:27 +08:00
|
|
|
|
it "should have at least 1 option" do
|
|
|
|
|
post :create, params: { title: title, raw: "[poll name='foo']\n[/poll]" }, format: :json
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2020-02-05 22:03:27 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(
|
|
|
|
|
I18n.t("poll.named_poll_must_have_at_least_1_option", name: "foo"),
|
|
|
|
|
)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "multiple polls" do
|
|
|
|
|
it "works" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.where(post_id: json["id"]).count).to eq(2)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should have a name" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_without_name"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "should have unique name" do
|
2017-08-31 12:06:56 +08:00
|
|
|
|
post :create,
|
|
|
|
|
params: {
|
|
|
|
|
title: title,
|
|
|
|
|
raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]",
|
|
|
|
|
},
|
|
|
|
|
format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2015-04-24 01:33:29 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_with_same_name", name: "foo"))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-12-04 21:47:11 +08:00
|
|
|
|
describe "disabled polls" do
|
|
|
|
|
before { SiteSetting.poll_enabled = false }
|
|
|
|
|
|
|
|
|
|
it "doesn’t cook the poll" do
|
2017-12-05 07:03:56 +08:00
|
|
|
|
log_in_user(Fabricate(:user, admin: true, trust_level: 4))
|
|
|
|
|
|
2017-12-04 21:47:11 +08:00
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2017-12-04 21:47:11 +08:00
|
|
|
|
expect(json["cooked"]).to eq(
|
|
|
|
|
"<p>[poll]</p>\n<ul>\n<li>A</li>\n<li>B<br>\n[/poll]</li>\n</ul>",
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-12-05 01:12:17 +08:00
|
|
|
|
describe "regular user with insufficient trust level" do
|
2024-01-23 09:35:14 +08:00
|
|
|
|
before { SiteSetting.poll_create_allowed_groups = Group::AUTO_GROUPS[:trust_level_2] }
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
it "invalidates the post" do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
log_in_user(Fabricate(:user, trust_level: 1, refresh_auto_groups: true))
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(response).not_to be_successful
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2017-12-05 01:12:17 +08:00
|
|
|
|
expect(json["errors"][0]).to eq(I18n.t("poll.insufficient_rights_to_create"))
|
2017-12-04 21:47:11 +08:00
|
|
|
|
end
|
2020-03-03 03:29:40 +08:00
|
|
|
|
|
|
|
|
|
it "skips the check in PMs with bots" do
|
|
|
|
|
user = Fabricate(:user, trust_level: 1)
|
|
|
|
|
topic =
|
|
|
|
|
Fabricate(
|
|
|
|
|
:private_message_topic,
|
|
|
|
|
topic_allowed_users: [
|
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user),
|
|
|
|
|
Fabricate.build(:topic_allowed_user, user: Discourse.system_user),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
Fabricate(:post, topic_id: topic.id, user_id: Discourse::SYSTEM_USER_ID)
|
|
|
|
|
|
|
|
|
|
log_in_user(user)
|
|
|
|
|
|
|
|
|
|
post :create, params: { topic_id: topic.id, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
|
expect(response.parsed_body["errors"]).to eq(nil)
|
2020-03-03 03:29:40 +08:00
|
|
|
|
end
|
2017-12-04 21:47:11 +08:00
|
|
|
|
end
|
|
|
|
|
|
2017-12-05 01:12:17 +08:00
|
|
|
|
describe "regular user with equal trust level" do
|
2024-01-23 09:35:14 +08:00
|
|
|
|
before { SiteSetting.poll_create_allowed_groups = Group::AUTO_GROUPS[:trust_level_2] }
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
it "validates the post" do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
log_in_user(Fabricate(:user, trust_level: 2, refresh_auto_groups: true))
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2017-12-04 21:47:11 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2017-12-04 21:47:11 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-12-05 01:12:17 +08:00
|
|
|
|
describe "regular user with superior trust level" do
|
2024-01-23 09:35:14 +08:00
|
|
|
|
before { SiteSetting.poll_create_allowed_groups = Group::AUTO_GROUPS[:trust_level_2] }
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
it "validates the post" do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
log_in_user(Fabricate(:user, trust_level: 3, refresh_auto_groups: true))
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2017-12-04 21:47:11 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2017-12-04 21:47:11 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-12-05 07:03:56 +08:00
|
|
|
|
describe "staff with insufficient trust level" do
|
2024-01-23 09:35:14 +08:00
|
|
|
|
before { SiteSetting.poll_create_allowed_groups = Group::AUTO_GROUPS[:trust_level_2] }
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
it "validates the post" do
|
2017-12-05 07:03:56 +08:00
|
|
|
|
log_in_user(Fabricate(:user, moderator: true, trust_level: 1))
|
2017-12-04 21:47:11 +08:00
|
|
|
|
|
|
|
|
|
post :create, params: { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }, format: :json
|
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
|
json = response.parsed_body
|
2017-12-04 21:47:11 +08:00
|
|
|
|
expect(json["cooked"]).to match("data-poll-")
|
2018-11-19 21:50:00 +08:00
|
|
|
|
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
2017-12-04 21:47:11 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2022-05-05 14:54:10 +08:00
|
|
|
|
|
|
|
|
|
describe "staff editing posts of users with insufficient trust level" do
|
2024-01-23 09:35:14 +08:00
|
|
|
|
before { SiteSetting.poll_create_allowed_groups = Group::AUTO_GROUPS[:trust_level_2] }
|
2022-05-05 14:54:10 +08:00
|
|
|
|
|
|
|
|
|
it "validates the post" do
|
2023-11-28 22:34:02 +08:00
|
|
|
|
log_in_user(Fabricate(:user, trust_level: 1, refresh_auto_groups: true))
|
2022-05-05 14:54:10 +08:00
|
|
|
|
|
|
|
|
|
post :create, params: { title: title, raw: title }, format: :json
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
post_id = response.parsed_body["id"]
|
|
|
|
|
|
|
|
|
|
log_in_user(Fabricate(:admin))
|
|
|
|
|
|
|
|
|
|
put :update,
|
|
|
|
|
params: {
|
|
|
|
|
id: post_id,
|
|
|
|
|
post: {
|
|
|
|
|
raw: "#{title}\n[poll]\n- A\n- B\n- C\n[/poll]",
|
|
|
|
|
},
|
2023-01-07 04:42:16 +08:00
|
|
|
|
},
|
2022-05-05 14:54:10 +08:00
|
|
|
|
format: :json
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.parsed_body["post"]["polls"][0]["options"][2]["html"]).to eq("C")
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
end
|