mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
REFACTOR: use tables instead of custom fields for polls (#6359)
Co-authored-by: Guo Xiang Tan <tgx_world@hotmail.com>
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
require "rails_helper"
|
||||
require_relative "../helpers"
|
||||
|
||||
describe PostsController do
|
||||
let!(:user) { log_in }
|
||||
@ -19,7 +18,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
|
||||
it "works on any post" do
|
||||
@ -32,7 +31,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
|
||||
it "schedules auto-close job" do
|
||||
@ -45,9 +44,8 @@ describe PostsController do
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["polls"][name]["close"]).to be
|
||||
|
||||
expect(Jobs.scheduled_for(:close_poll, post_id: Post.last.id, poll_name: name)).to be
|
||||
expect(Poll.find_by(post_id: json["id"]).close_at).to be
|
||||
expect(Jobs.scheduled_for(:close_poll, post_id: json["id"], poll_name: name)).to be
|
||||
end
|
||||
|
||||
it "should have different options" do
|
||||
@ -55,7 +53,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll]\n- A\n- A\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options"))
|
||||
end
|
||||
@ -65,7 +63,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll]\n- A\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_2_options"))
|
||||
end
|
||||
@ -79,7 +77,7 @@ describe PostsController do
|
||||
title: title, raw: raw
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_less_options", count: SiteSetting.poll_maximum_options))
|
||||
end
|
||||
@ -89,7 +87,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"))
|
||||
end
|
||||
@ -103,7 +101,7 @@ describe PostsController do
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["cooked"]).to include("<script>")
|
||||
expect(json["polls"]["<script>alert('xss')</script>"]).to be
|
||||
expect(Poll.find_by(post_id: json["id"]).name).to eq("<script>alert('xss')</script>")
|
||||
end
|
||||
|
||||
it "also works whe there is a link starting with '[poll'" do
|
||||
@ -114,7 +112,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
|
||||
it "prevents pollception" do
|
||||
@ -125,8 +123,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["1"]).to_not be
|
||||
expect(json["polls"]["2"]).to be
|
||||
expect(Poll.where(post_id: json["id"]).count).to eq(1)
|
||||
end
|
||||
|
||||
describe "edit window" do
|
||||
@ -150,7 +147,7 @@ describe PostsController do
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][2]["html"]).to eq("C")
|
||||
expect(json["post"]["polls"][0]["options"][2]["html"]).to eq("C")
|
||||
end
|
||||
|
||||
it "resets the votes" do
|
||||
@ -191,26 +188,14 @@ describe PostsController do
|
||||
|
||||
describe "with no vote" do
|
||||
|
||||
it "OP can change the options" do
|
||||
it "can change the options" do
|
||||
put :update, params: {
|
||||
id: post_id, post: { raw: new_option }
|
||||
}, format: :json
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||
end
|
||||
|
||||
it "staff can change the options" do
|
||||
log_in_user(Fabricate(:moderator))
|
||||
|
||||
put :update, params: {
|
||||
id: post_id, post: { raw: new_option }
|
||||
}, format: :json
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||
expect(json["post"]["polls"][0]["options"][1]["html"]).to eq("C")
|
||||
end
|
||||
|
||||
it "support changes on the post" do
|
||||
@ -228,54 +213,19 @@ describe PostsController do
|
||||
DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user)
|
||||
end
|
||||
|
||||
it "OP cannot change the options" do
|
||||
it "cannot change the options" do
|
||||
put :update, params: {
|
||||
id: post_id, post: { raw: new_option }
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t(
|
||||
"poll.edit_window_expired.op_cannot_edit_options",
|
||||
"poll.edit_window_expired.cannot_edit_default_poll_with_votes",
|
||||
minutes: poll_edit_window_mins
|
||||
))
|
||||
end
|
||||
|
||||
it "staff can change the options and votes are merged" do
|
||||
log_in_user(Fabricate(:moderator))
|
||||
|
||||
put :update, params: {
|
||||
id: post_id, post: { raw: new_option }
|
||||
}, format: :json
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||
expect(json["post"]["polls"]["poll"]["voters"]).to eq(1)
|
||||
expect(json["post"]["polls"]["poll"]["options"][0]["votes"]).to eq(1)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["votes"]).to eq(0)
|
||||
end
|
||||
|
||||
it "staff can change the options and anonymous votes are merged" do
|
||||
post = Post.find_by(id: post_id)
|
||||
default_poll = post.custom_fields["polls"]["poll"]
|
||||
add_anonymous_votes(post, default_poll, 7, "5c24fc1df56d764b550ceae1b9319125" => 7)
|
||||
|
||||
log_in_user(Fabricate(:moderator))
|
||||
|
||||
put :update, params: {
|
||||
id: post_id, post: { raw: new_option }
|
||||
}, format: :json
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
|
||||
expect(json["post"]["polls"]["poll"]["voters"]).to eq(8)
|
||||
expect(json["post"]["polls"]["poll"]["options"][0]["votes"]).to eq(8)
|
||||
expect(json["post"]["polls"]["poll"]["options"][1]["votes"]).to eq(0)
|
||||
end
|
||||
|
||||
it "support changes on the post" do
|
||||
put :update, params: { id: post_id, post: { raw: updated } }, format: :json
|
||||
expect(response.status).to eq(200)
|
||||
@ -298,7 +248,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo"))
|
||||
end
|
||||
@ -308,7 +258,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll name='foo']\n- A\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_2_options", name: "foo"))
|
||||
end
|
||||
@ -325,8 +275,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(json["polls"]["foo"]).to be
|
||||
expect(Poll.where(post_id: json["id"]).count).to eq(2)
|
||||
end
|
||||
|
||||
it "should have a name" do
|
||||
@ -334,7 +283,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_without_name"))
|
||||
end
|
||||
@ -344,7 +293,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_with_same_name", name: "foo"))
|
||||
end
|
||||
@ -381,7 +330,7 @@ describe PostsController do
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
expect(response).not_to be_successful
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(I18n.t("poll.insufficient_rights_to_create"))
|
||||
end
|
||||
@ -402,7 +351,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
@ -421,7 +370,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
@ -440,7 +389,7 @@ describe PostsController do
|
||||
expect(response.status).to eq(200)
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
expect(Poll.exists?(post_id: json["id"])).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user