scope_spec.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe KnowledgeBasePolicy::Scope do
  4. subject(:scope) { described_class.new(user, original_collection) }
  5. let(:original_collection) { KnowledgeBase }
  6. before do # populate DB
  7. create_list(:knowledge_base, 2, active: true)
  8. create_list(:knowledge_base, 2, active: false)
  9. end
  10. describe '#resolve' do
  11. let(:roles) { user.roles }
  12. let(:permission) { Permission.find_by(name: 'knowledge_base.editor') }
  13. context 'without user' do
  14. let(:user) { nil }
  15. it 'removes only inactive knowledge bases' do
  16. expect(scope.resolve).to eq(original_collection.where(active: true))
  17. end
  18. end
  19. context 'without "knowledge_base.editor" permissions' do
  20. let(:user) { create(:admin) }
  21. before { roles.each { |r| r.permissions.delete(permission) } }
  22. it 'removes only inactive knowledge bases' do
  23. expect(scope.resolve).to eq(original_collection.where(active: true))
  24. end
  25. end
  26. context 'with "knowledge_base.editor" permissions' do
  27. let(:user) { create(:user) }
  28. before { roles.first.permissions << permission }
  29. it 'returns the given collection (unfiltered)' do
  30. expect(scope.resolve).to eq(original_collection)
  31. end
  32. end
  33. end
  34. end