2022-12-19 08:35:28 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe CategoryGuardian do
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:can_create_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
describe "can_post_in_category?" do
|
2023-10-06 09:00:22 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
2022-12-19 08:35:28 +08:00
|
|
|
context "when not restricted category" do
|
|
|
|
it "returns false for anonymous user" do
|
|
|
|
expect(Guardian.new.can_post_in_category?(category)).to eq(false)
|
|
|
|
end
|
|
|
|
it "returns true for admin" do
|
|
|
|
expect(Guardian.new(admin).can_post_in_category?(category)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for regular user" do
|
|
|
|
expect(Guardian.new(user).can_post_in_category?(category)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when restricted category" do
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2022-12-23 11:18:29 +08:00
|
|
|
fab!(:category) do
|
|
|
|
Fabricate(
|
|
|
|
:private_category,
|
|
|
|
group: group,
|
|
|
|
permission_type: CategoryGroup.permission_types[:readonly],
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2022-12-19 08:35:28 +08:00
|
|
|
fab!(:group_user) { Fabricate(:group_user, group: group, user: user) }
|
|
|
|
|
|
|
|
it "returns false for anonymous user" do
|
|
|
|
expect(Guardian.new.can_post_in_category?(category)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for member of group with readonly access" do
|
|
|
|
expect(Guardian.new(user).can_post_in_category?(category)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2023-05-10 19:45:13 +08:00
|
|
|
it "returns false if everyone has readonly access" do
|
|
|
|
everyone = Group.find(Group::AUTO_GROUPS[:everyone])
|
|
|
|
everyone.add(user)
|
|
|
|
category = Fabricate(:category)
|
|
|
|
Fabricate(
|
|
|
|
:category_group,
|
|
|
|
category: category,
|
|
|
|
group: everyone,
|
|
|
|
permission_type: CategoryGroup.permission_types[:readonly],
|
|
|
|
)
|
|
|
|
expect(Guardian.new(user).can_post_in_category?(category)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-12-19 08:35:28 +08:00
|
|
|
it "returns true for admin" do
|
|
|
|
expect(Guardian.new(admin).can_post_in_category?(category)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for member of group with create_post access" do
|
2022-12-23 11:18:29 +08:00
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:private_category,
|
|
|
|
group: group,
|
|
|
|
permission_type: CategoryGroup.permission_types[:create_post],
|
|
|
|
)
|
|
|
|
expect(Guardian.new(user).can_post_in_category?(category)).to eq(true)
|
2022-12-19 08:35:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for member of group with full access" do
|
2022-12-23 11:18:29 +08:00
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:private_category,
|
|
|
|
group: group,
|
|
|
|
permission_type: CategoryGroup.permission_types[:full],
|
|
|
|
)
|
|
|
|
expect(Guardian.new(user).can_post_in_category?(category)).to eq(true)
|
2022-12-19 08:35:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|