2024-05-23 10:19:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe FlagGuardian do
|
|
|
|
fab!(:user)
|
|
|
|
fab!(:admin)
|
2024-05-29 12:39:58 +08:00
|
|
|
fab!(:moderator)
|
2024-05-24 13:23:59 +08:00
|
|
|
|
|
|
|
after(:each) { Flag.reset_flag_settings! }
|
2024-08-06 08:50:12 +08:00
|
|
|
describe "#can_create_flag?" do
|
|
|
|
it "returns true for admin and when custom flags limit is not reached" do
|
|
|
|
SiteSetting.custom_flags_limit = 1
|
|
|
|
|
|
|
|
expect(Guardian.new(admin).can_create_flag?).to eq(true)
|
|
|
|
expect(Guardian.new(user).can_create_flag?).to eq(false)
|
|
|
|
|
|
|
|
Fabricate(:flag)
|
|
|
|
|
|
|
|
expect(Guardian.new(admin).can_create_flag?).to eq(false)
|
|
|
|
expect(Guardian.new(user).can_create_flag?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2024-05-23 10:19:07 +08:00
|
|
|
|
|
|
|
describe "#can_edit_flag?" do
|
2024-05-29 12:39:58 +08:00
|
|
|
it "returns true for admin and false for moderator and regular user" do
|
2024-05-24 13:23:59 +08:00
|
|
|
flag = Fabricate(:flag)
|
2024-05-23 10:19:07 +08:00
|
|
|
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(true)
|
2024-05-29 12:39:58 +08:00
|
|
|
expect(Guardian.new(moderator).can_edit_flag?(flag)).to eq(false)
|
2024-05-23 10:19:07 +08:00
|
|
|
expect(Guardian.new(user).can_edit_flag?(flag)).to eq(false)
|
2024-05-24 13:23:59 +08:00
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when flag is system" do
|
|
|
|
expect(Guardian.new(admin).can_edit_flag?(Flag.system.first)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when flag was already used with post action" do
|
2024-05-24 13:23:59 +08:00
|
|
|
flag = Fabricate(:flag)
|
2024-05-23 10:19:07 +08:00
|
|
|
Fabricate(:post_action, post_action_type_id: flag.id)
|
|
|
|
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(false)
|
2024-05-24 13:23:59 +08:00
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when flag was already used with reviewable" do
|
2024-05-24 13:23:59 +08:00
|
|
|
flag = Fabricate(:flag)
|
2024-05-23 10:19:07 +08:00
|
|
|
Fabricate(:reviewable_score, reviewable_score_type: flag.id)
|
|
|
|
expect(Guardian.new(admin).can_edit_flag?(flag)).to eq(false)
|
2024-05-24 13:23:59 +08:00
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
end
|
2024-05-29 12:39:58 +08:00
|
|
|
|
|
|
|
describe "#can_toggle_flag?" do
|
|
|
|
it "returns true for admin and false for regular user" do
|
|
|
|
expect(Guardian.new(admin).can_toggle_flag?).to eq(true)
|
|
|
|
expect(Guardian.new(user).can_toggle_flag?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2024-06-05 11:27:06 +08:00
|
|
|
|
|
|
|
describe "#can_reorder_flag?" do
|
|
|
|
it "returns true for admin and false for regular user and notify_user" do
|
|
|
|
expect(Guardian.new(admin).can_reorder_flag?(Flag.system.last)).to eq(true)
|
|
|
|
expect(
|
|
|
|
Guardian.new(admin).can_reorder_flag?(Flag.system.find_by(name_key: "notify_user")),
|
|
|
|
).to eq(false)
|
|
|
|
expect(Guardian.new(user).can_reorder_flag?(Flag.system.last)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|