permission_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe KnowledgeBase::Category::Permission do
  4. include_context 'basic Knowledge Base'
  5. describe '#permissions_effective' do
  6. let(:child_category) { create(:knowledge_base_category, knowledge_base: knowledge_base, parent: category) }
  7. let(:child_permission) { create(:knowledge_base_permission, permissionable: child_category, access: 'reader') }
  8. let(:parent_permission) { create(:knowledge_base_permission, permissionable: category) }
  9. context 'when no permissions exist' do
  10. it 'parent category returns nil' do
  11. expect(described_class.new(category).permissions_effective).to be_blank
  12. end
  13. it 'child category returns nil' do
  14. expect(described_class.new(child_category).permissions_effective).to be_blank
  15. end
  16. end
  17. context 'when parent category has permissions' do
  18. before { parent_permission && child_category }
  19. it 'parent category returns parent permission' do
  20. expect(described_class.new(category).permissions_effective).to eq [parent_permission]
  21. end
  22. it 'child category returns parent permission' do
  23. expect(described_class.new(child_category).permissions_effective).to eq [parent_permission]
  24. end
  25. end
  26. context 'when child category has permissions' do
  27. before { child_permission }
  28. it 'parent category returns parent permission' do
  29. expect(described_class.new(category).permissions_effective).to be_blank
  30. end
  31. it 'child category returns parent permission' do
  32. expect(described_class.new(child_category).permissions_effective).to eq [child_permission]
  33. end
  34. end
  35. context 'when both parent and child categories have permissions' do
  36. before { child_permission && parent_permission }
  37. it 'parent category returns parent permission' do
  38. expect(described_class.new(category).permissions_effective).to eq [parent_permission]
  39. end
  40. it 'child category returns parent permission' do
  41. expect(described_class.new(child_category).permissions_effective).to eq [child_permission, parent_permission]
  42. end
  43. end
  44. context 'when both parent child categories have colliding permissions for the same role' do
  45. let(:child_permission_on_same_role) { create(:knowledge_base_permission, permissionable: child_category, access: 'reader', role: parent_permission.role) }
  46. before { parent_permission && child_permission && child_permission_on_same_role }
  47. it 'parent category returns parent permission' do
  48. expect(described_class.new(category).permissions_effective).to eq [parent_permission]
  49. end
  50. it 'child category returns child permission override' do
  51. expect(described_class.new(child_category).permissions_effective).to eq [child_permission, child_permission_on_same_role]
  52. end
  53. end
  54. end
  55. end