answer_policy_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'policies/knowledge_base_policy_examples'
  4. describe KnowledgeBase::AnswerPolicy do
  5. subject(:policy) { described_class.new(user, record) }
  6. let(:record) { create(:knowledge_base_answer) }
  7. let(:user) { create(:user) }
  8. shared_context 'with answer visibility' do |visible:, visible_internally:|
  9. before do
  10. allow(record).to receive_messages(visible?: visible, visible_internally?: visible_internally)
  11. end
  12. end
  13. describe '#show?' do
  14. context 'when visible and visible internally' do
  15. include_examples 'with answer visibility', visible: true, visible_internally: true
  16. include_examples 'with KB policy check', editor: true, reader: true, none: true, method: :show?
  17. end
  18. context 'when visible internally only' do
  19. include_examples 'with answer visibility', visible: false, visible_internally: true
  20. include_examples 'with KB policy check', editor: true, reader: true, none: false, method: :show?
  21. end
  22. context 'when not visible' do
  23. include_examples 'with answer visibility', visible: false, visible_internally: false
  24. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :show?
  25. end
  26. end
  27. describe '#show_public?' do
  28. context 'when visible and visible internally' do
  29. include_examples 'with answer visibility', visible: true, visible_internally: true
  30. include_examples 'with KB policy check', editor: true, reader: true, none: true, method: :show_public?
  31. end
  32. context 'when visible internally only' do
  33. include_examples 'with answer visibility', visible: false, visible_internally: true
  34. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :show_public?
  35. end
  36. context 'when not visible' do
  37. include_examples 'with answer visibility', visible: false, visible_internally: false
  38. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :show_public?
  39. end
  40. end
  41. describe '#update?' do
  42. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :update?
  43. end
  44. describe '#create?' do
  45. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :create?
  46. end
  47. describe '#destroy?' do
  48. include_examples 'with KB policy check', editor: true, reader: false, none: false, method: :destroy?
  49. end
  50. end