2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe CategorySerializer do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:admin)
|
|
|
|
fab!(:group)
|
2024-09-04 09:38:46 +08:00
|
|
|
fab!(:category)
|
|
|
|
fab!(:category_moderation_group) { Fabricate(:category_moderation_group, category:, group:) }
|
2019-04-18 05:12:32 +08:00
|
|
|
|
|
|
|
it "includes the reviewable by group name if enabled" do
|
2020-07-15 00:36:19 +08:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
2019-04-18 05:12:32 +08:00
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
2024-09-04 09:38:46 +08:00
|
|
|
expect(json[:moderating_group_ids]).to eq([group.id])
|
2019-04-18 05:12:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include the reviewable by group name if disabled" do
|
2020-07-15 00:36:19 +08:00
|
|
|
SiteSetting.enable_category_group_moderation = false
|
2019-04-18 05:12:32 +08:00
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
2024-09-04 09:38:46 +08:00
|
|
|
expect(json[:moderating_group_ids]).to be_blank
|
2019-04-18 05:12:32 +08:00
|
|
|
end
|
2019-03-16 19:48:57 +08:00
|
|
|
|
|
|
|
it "includes custom fields" do
|
2019-03-25 10:29:56 +08:00
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
|
|
|
expect(json[:custom_fields]).to be_empty
|
|
|
|
|
2019-03-16 19:48:57 +08:00
|
|
|
category.custom_fields["enable_marketplace"] = true
|
|
|
|
category.save_custom_fields
|
|
|
|
|
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
|
|
|
expect(json[:custom_fields]).to be_present
|
|
|
|
end
|
2020-04-29 00:05:53 +08:00
|
|
|
|
2020-06-24 15:07:52 +08:00
|
|
|
it "does not include the default notification level when there is no user" do
|
2020-04-29 00:05:53 +08:00
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
2020-06-24 15:07:52 +08:00
|
|
|
expect(json.key?(:notification_level)).to eq(false)
|
2020-04-29 00:05:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "user notification level" do
|
|
|
|
it "includes the user's notification level" do
|
|
|
|
CategoryUser.set_notification_level_for_category(
|
|
|
|
user,
|
|
|
|
NotificationLevels.all[:watching],
|
|
|
|
category.id,
|
|
|
|
)
|
|
|
|
json = described_class.new(category, scope: Guardian.new(user), root: false).as_json
|
|
|
|
expect(json[:notification_level]).to eq(NotificationLevels.all[:watching])
|
|
|
|
end
|
|
|
|
end
|
2021-06-03 01:18:45 +08:00
|
|
|
|
2022-03-31 14:39:01 +08:00
|
|
|
describe "#group_permissions" do
|
2022-04-08 11:14:06 +08:00
|
|
|
fab!(:private_group) do
|
|
|
|
Fabricate(:group, visibility_level: Group.visibility_levels[:staff], name: "bbb")
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
fab!(:user_group) do
|
|
|
|
Fabricate(:group, visibility_level: Group.visibility_levels[:members], name: "ccc").tap do |g|
|
|
|
|
g.add(user)
|
2022-03-31 14:39:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
before do
|
|
|
|
group.update!(name: "aaa")
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
category.set_permissions(
|
|
|
|
:everyone => :readonly,
|
|
|
|
group.name => :readonly,
|
|
|
|
user_group.name => :full,
|
|
|
|
private_group.name => :full,
|
|
|
|
)
|
2021-06-03 01:18:45 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
category.save!
|
|
|
|
end
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
it "does not include the attribute for an anon user" do
|
|
|
|
json = described_class.new(category, scope: Guardian.new, root: false).as_json
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
expect(json[:group_permissions]).to eq(nil)
|
|
|
|
end
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
it "does not include the attribute for a regular user" do
|
|
|
|
json = described_class.new(category, scope: Guardian.new(user), root: false).as_json
|
2022-03-31 14:39:01 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
expect(json[:group_permissions]).to eq(nil)
|
|
|
|
end
|
2022-04-07 17:21:29 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
it "returns the right category group permissions for a user that can edit the category" do
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups = true
|
|
|
|
user.update!(moderator: true)
|
2022-04-07 17:21:29 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
json = described_class.new(category, scope: Guardian.new(user), root: false).as_json
|
2022-04-07 17:21:29 +08:00
|
|
|
|
2022-04-08 11:14:06 +08:00
|
|
|
expect(json[:group_permissions]).to eq(
|
|
|
|
[
|
|
|
|
{ permission_type: CategoryGroup.permission_types[:readonly], group_name: group.name },
|
|
|
|
{
|
|
|
|
permission_type: CategoryGroup.permission_types[:full],
|
|
|
|
group_name: private_group.name,
|
|
|
|
},
|
|
|
|
{ permission_type: CategoryGroup.permission_types[:full], group_name: user_group.name },
|
|
|
|
{ permission_type: CategoryGroup.permission_types[:readonly], group_name: "everyone" },
|
|
|
|
],
|
|
|
|
)
|
2022-03-31 14:39:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "available groups" do
|
2021-06-03 01:18:45 +08:00
|
|
|
it "not included for a regular user" do
|
|
|
|
json = described_class.new(category, scope: Guardian.new(user), root: false).as_json
|
|
|
|
expect(json[:available_groups]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "included for an admin" do
|
|
|
|
json = described_class.new(category, scope: Guardian.new(admin), root: false).as_json
|
|
|
|
expect(json[:available_groups]).to eq(Group.order(:name).pluck(:name) - ["everyone"])
|
|
|
|
end
|
|
|
|
end
|
2019-03-16 19:48:57 +08:00
|
|
|
end
|