From d52abe23244722e4459955182ff007c3ea083ecf Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Fri, 22 Mar 2024 12:41:28 -0500 Subject: [PATCH] FIX: Set has_children correctly in Category.preload_user_fields! (#26327) --- app/models/category.rb | 2 +- spec/requests/categories_controller_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/category.rb b/app/models/category.rb index 66458783069..1dae5366199 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -241,7 +241,7 @@ class Category < ActiveRecord::Base # Categories with children with_children = Category - .secured(@guardian) + .secured(guardian) .where(parent_category_id: category_ids) .pluck(:parent_category_id) .to_set diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index d8948831207..e27d9520046 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -1140,6 +1140,26 @@ RSpec.describe CategoriesController do expect(category["permission"]).to eq(CategoryGroup.permission_types[:full]) expect(category["has_children"]).to eq(true) end + + context "with a read restricted child category" do + before_all { subcategory.update!(read_restricted: true) } + + it "indicates to an admin that the category has a child" do + sign_in(admin) + + get "/categories/find.json", params: { ids: [category.id] } + category = response.parsed_body["categories"].first + expect(category["has_children"]).to eq(true) + end + + it "indicates to a normal user that the category has no child" do + sign_in(user) + + get "/categories/find.json", params: { ids: [category.id] } + category = response.parsed_body["categories"].first + expect(category["has_children"]).to eq(false) + end + end end describe "#search" do