mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Cache categories in Site model take 2.
Follow-up to aa4f0aee67.
Fixed the security problem in the previous attempt.
This commit is contained in:
@@ -58,30 +58,65 @@ describe Site do
|
||||
expect(Site.new(guardian).categories.last.notification_level).to eq(1)
|
||||
end
|
||||
|
||||
it "omits categories users can not write to from the category list" do
|
||||
category = Fabricate(:category)
|
||||
user = Fabricate(:user)
|
||||
describe '#categories' do
|
||||
fab!(:category) { Fabricate(:category) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:guardian) { Guardian.new(user) }
|
||||
|
||||
expect(Site.new(Guardian.new(user)).categories.count).to eq(2)
|
||||
after do
|
||||
Site.clear_cache
|
||||
end
|
||||
|
||||
category.set_permissions(everyone: :create_post)
|
||||
category.save
|
||||
it "omits read restricted categories" do
|
||||
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
|
||||
SiteSetting.uncategorized_category_id, category.id
|
||||
)
|
||||
|
||||
guardian = Guardian.new(user)
|
||||
category.update!(read_restricted: true)
|
||||
|
||||
expect(Site.new(guardian)
|
||||
.categories
|
||||
.keep_if { |c| c.name == category.name }
|
||||
.first
|
||||
.permission)
|
||||
.not_to eq(CategoryGroup.permission_types[:full])
|
||||
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
|
||||
SiteSetting.uncategorized_category_id
|
||||
)
|
||||
end
|
||||
|
||||
# If a parent category is not visible, the child categories should not be returned
|
||||
category.set_permissions(staff: :full)
|
||||
category.save
|
||||
it "includes categories that a user's group can see" do
|
||||
group = Fabricate(:group)
|
||||
category.update!(read_restricted: true)
|
||||
category.groups << group
|
||||
|
||||
sub_category = Fabricate(:category, parent_category_id: category.id)
|
||||
expect(Site.new(guardian).categories).not_to include(sub_category)
|
||||
expect(Site.new(guardian).categories.map(&:id)).to contain_exactly(
|
||||
SiteSetting.uncategorized_category_id
|
||||
)
|
||||
|
||||
group.add(user)
|
||||
|
||||
expect(Site.new(Guardian.new(user)).categories.map(&:id)).to contain_exactly(
|
||||
SiteSetting.uncategorized_category_id, category.id
|
||||
)
|
||||
end
|
||||
|
||||
it "omits categories users can not write to from the category list" do
|
||||
expect(Site.new(guardian).categories.count).to eq(2)
|
||||
|
||||
category.set_permissions(everyone: :create_post)
|
||||
category.save!
|
||||
|
||||
guardian = Guardian.new(user)
|
||||
|
||||
expect(Site.new(guardian)
|
||||
.categories
|
||||
.keep_if { |c| c.name == category.name }
|
||||
.first
|
||||
.permission)
|
||||
.not_to eq(CategoryGroup.permission_types[:full])
|
||||
|
||||
# If a parent category is not visible, the child categories should not be returned
|
||||
category.set_permissions(staff: :full)
|
||||
category.save!
|
||||
|
||||
sub_category = Fabricate(:category, parent_category_id: category.id)
|
||||
expect(Site.new(guardian).categories).not_to include(sub_category)
|
||||
end
|
||||
end
|
||||
|
||||
it "omits groups user can not see" do
|
||||
|
||||
@@ -10,13 +10,34 @@ describe SiteSerializer do
|
||||
category.custom_fields["enable_marketplace"] = true
|
||||
category.save_custom_fields
|
||||
|
||||
data = MultiJson.dump(described_class.new(Site.new(guardian), scope: guardian, root: false))
|
||||
expect(data).not_to include("enable_marketplace")
|
||||
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
||||
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
||||
|
||||
expect(c1[:preloaded_custom_fields]).to eq(nil)
|
||||
|
||||
Site.preloaded_category_custom_fields << "enable_marketplace"
|
||||
|
||||
data = MultiJson.dump(described_class.new(Site.new(guardian), scope: guardian, root: false))
|
||||
expect(data).to include("enable_marketplace")
|
||||
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
||||
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
||||
|
||||
expect(c1[:preloaded_custom_fields]["enable_marketplace"]).to eq("t")
|
||||
end
|
||||
|
||||
it "includes category tags" do
|
||||
tag = Fabricate(:tag)
|
||||
tag_group = Fabricate(:tag_group)
|
||||
tag_group_2 = Fabricate(:tag_group)
|
||||
|
||||
category.tags << tag
|
||||
category.tag_groups << tag_group
|
||||
category.update!(required_tag_group: tag_group_2)
|
||||
|
||||
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
|
||||
c1 = serialized[:categories].find { |c| c[:id] == category.id }
|
||||
|
||||
expect(c1[:allowed_tags]).to contain_exactly(tag.name)
|
||||
expect(c1[:allowed_tag_groups]).to contain_exactly(tag_group.name)
|
||||
expect(c1[:required_tag_group_name]).to eq(tag_group_2.name)
|
||||
end
|
||||
|
||||
it "returns correct notification level for categories" do
|
||||
|
||||
Reference in New Issue
Block a user