FIX: Forcing permissions of seeded categories shouldn't fail

FIX: Forcing permissions of seeded categories shouldn’t fail

Less restrictive permissions of subcategories could make the seeding of categories fail.

diff --git a/lib/seed_data/categories.rb b/lib/seed_data/categories.rb
index 8460328..adf077b 100644
--- a/lib/seed_data/categories.rb
+++ b/lib/seed_data/categories.rb
@@ -120,7 +120,7 @@ module SeedData
 
         if force_permissions
           category.set_permissions(permissions)
-          category.save! if category.changed?
+          category.save!(validate: false) if category.changed?
         end
       end
     end
diff --git a/spec/lib/seed_data/categories_spec.rb b/spec/lib/seed_data/categories_spec.rb
index 76482e6..c36e5f4 100644
--- a/spec/lib/seed_data/categories_spec.rb
+++ b/spec/lib/seed_data/categories_spec.rb
@@ -71,6 +71,25 @@ describe SeedData::Categories do
         expect(category.category_groups.count).to eq(1)
         expect(category.category_groups.first).to have_attributes(permissions(:staff, :full))
       end
+
+      it "overwrites permissions even when subcategory has less restrictive permissions" do
+        category = Category.last
+        category.set_permissions(everyone: :full)
+        category.save!
+
+        group = Fabricate(:group)
+
+        subcategory = Fabricate(:category, name: "child", parent_category_id: category.id)
+        subcategory.set_permissions(group => :full)
+        subcategory.save!
+
+        expect { create_category }
+          .to change { CategoryGroup.count }.by(1)
+
+        category.reload
+        expect(category.category_groups.count).to eq(1)
+        expect(category.category_groups.first).to have_attributes(permissions(:staff, :full))
+      end
     end
 
     it "does not override permissions of existing category when not forced" do

GitHub sha: dc90133d

1 Like