mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
FEATURE: introduces minimum trust level for polls (#5391)
* FEATURE: introduces minimum trust level for polls This commit makes `poll_enabled` less misleading and introduces `poll_minimum_trust_level_to_create`. If poll are enabled they will always be cooked, and if you have the required trust level you can create polls. As a side effect, it also fixes a bug where rebaking a post created by staff member when `poll_enabled=false` would end up not cooking it. It also adds more tests to ensure settings are respected. * admins should be whitelisted * checks for admin in post validation * test for >= instead of == trust level
This commit is contained in:

committed by
Régis Hanol

parent
f466791a15
commit
63bab32816
@ -336,4 +336,100 @@ describe PostsController do
|
||||
|
||||
end
|
||||
|
||||
describe "disabled polls" do
|
||||
before do
|
||||
SiteSetting.poll_enabled = false
|
||||
end
|
||||
|
||||
it "doesn’t cook the poll" do
|
||||
post :create, params: {
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
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
|
||||
|
||||
describe "insufficient trust level" do
|
||||
before do
|
||||
SiteSetting.poll_minimum_trust_level_to_create = 2
|
||||
end
|
||||
|
||||
it "invalidates the post" do
|
||||
log_in_user(Fabricate(:user, trust_level: 1))
|
||||
|
||||
post :create, params: {
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).not_to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["errors"][0]).to eq(
|
||||
I18n.t("poll.insufficient_trust_level_to_create",
|
||||
current: user.trust_level,
|
||||
required: SiteSetting.poll_minimum_trust_level_to_create
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "equal trust level" do
|
||||
before do
|
||||
SiteSetting.poll_minimum_trust_level_to_create = 2
|
||||
end
|
||||
|
||||
it "validates the post" do
|
||||
log_in_user(Fabricate(:user, trust_level: 2))
|
||||
|
||||
post :create, params: {
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
end
|
||||
end
|
||||
|
||||
describe "superior trust level" do
|
||||
before do
|
||||
SiteSetting.poll_minimum_trust_level_to_create = 2
|
||||
end
|
||||
|
||||
it "validates the post" do
|
||||
log_in_user(Fabricate(:user, trust_level: 3))
|
||||
|
||||
post :create, params: {
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "admin with insufficient trust level" do
|
||||
before do
|
||||
SiteSetting.poll_minimum_trust_level_to_create = 2
|
||||
end
|
||||
|
||||
it "validates the post" do
|
||||
log_in_user(Fabricate(:user, admin: true, trust_level: 1))
|
||||
|
||||
post :create, params: {
|
||||
title: title, raw: "[poll]\n- A\n- B\n[/poll]"
|
||||
}, format: :json
|
||||
|
||||
expect(response).to be_success
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json["cooked"]).to match("data-poll-")
|
||||
expect(json["polls"]["poll"]).to be
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user