scope_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe KnowledgeBase::AnswerPolicy::Scope, current_user_id: 1 do
  4. subject(:scope) { described_class.new(user, original_collection) }
  5. let(:original_collection) { KnowledgeBase::Answer }
  6. before do # populate DB
  7. create_list(:'knowledge_base/answer', 2, published_at: Time.zone.yesterday)
  8. create_list(:'knowledge_base/answer', 2, published_at: Time.zone.yesterday, archived_at: Time.zone.now)
  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 unpublished and archived answers' do
  16. expect(scope.resolve)
  17. .to match_array(original_collection.where(<<~QUERY, now: Time.zone.now))
  18. published_at < :now AND (archived_at IS NULL OR archived_at > :now)
  19. QUERY
  20. end
  21. end
  22. context 'without "knowledge_base.editor" permissions' do
  23. let(:user) { create(:admin) }
  24. before { roles.each { |r| r.permissions.delete(permission) } }
  25. it 'removes unpublished and archived answers' do
  26. expect(scope.resolve)
  27. .to match_array(original_collection.where(<<~QUERY, now: Time.zone.now))
  28. published_at < :now AND (archived_at IS NULL OR archived_at > :now)
  29. QUERY
  30. end
  31. end
  32. context 'with "knowledge_base.editor" permissions' do
  33. let(:user) { create(:user) }
  34. before { roles.first.permissions << permission }
  35. it 'returns the given collection (unfiltered)' do
  36. expect(scope.resolve).to eq(original_collection)
  37. end
  38. end
  39. end
  40. end