internal_assets_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe KnowledgeBase::InternalAssets do
  4. include_context 'basic Knowledge Base' do
  5. before do
  6. draft_answer
  7. internal_answer
  8. published_answer
  9. end
  10. end
  11. describe '#collect_assets' do
  12. subject(:assets) { described_class.new(user).collect_assets }
  13. context 'when for KB editor' do
  14. let(:user) { create(:user, roles: Role.where(name: 'Admin')) }
  15. it 'returns assets for all KB objects' do
  16. expect(assets).to include_assets_of(knowledge_base, category, draft_answer, internal_answer, published_answer)
  17. end
  18. context 'when has editor permission' do
  19. before do
  20. KnowledgeBase::PermissionsUpdate.new(category).update! user.roles.first => 'editor'
  21. end
  22. it 'returns assets for all KB objects' do
  23. expect(assets).to include_assets_of(knowledge_base, category, draft_answer, internal_answer, published_answer)
  24. end
  25. end
  26. context 'when has reader permission' do
  27. before do
  28. KnowledgeBase::PermissionsUpdate.new(category).update! user.roles.first => 'reader'
  29. end
  30. it 'returns assets for internally visible KB objects' do
  31. expect(assets)
  32. .to include_assets_of(knowledge_base, category, internal_answer, published_answer)
  33. .and not_include_assets_of(draft_answer)
  34. end
  35. end
  36. context 'when has none permission' do
  37. before do
  38. KnowledgeBase::PermissionsUpdate.new(category).update! user.roles.first => 'none'
  39. end
  40. it 'does not return assets for internally visible KB objects' do
  41. published_answer.destroy # make sure public item does not make category visible
  42. expect(assets)
  43. .to include_assets_of(knowledge_base)
  44. .and not_include_assets_of(category, draft_answer, internal_answer, published_answer)
  45. end
  46. it 'returns assets for published answer and it\'s category' do
  47. expect(assets)
  48. .to include_assets_of(knowledge_base, category, published_answer)
  49. .and not_include_assets_of(draft_answer, internal_answer)
  50. end
  51. end
  52. end
  53. context 'when for agent' do
  54. let(:user) { create(:agent) }
  55. it 'returns assets for all KB objects' do
  56. expect(assets)
  57. .to include_assets_of(knowledge_base, category, internal_answer, published_answer)
  58. .and not_include_assets_of(draft_answer)
  59. end
  60. end
  61. context 'when for customer' do
  62. let(:user) { create(:customer) }
  63. it 'returns assets for all KB objects' do
  64. expect(assets)
  65. .to include_assets_of(knowledge_base)
  66. .and not_include_assets_of(category, draft_answer, internal_answer, published_answer)
  67. end
  68. end
  69. context 'when filtering by categories' do
  70. subject(:assets) { described_class.new(user, categories_filter: other_category).collect_assets }
  71. before { published_answer_in_other_category }
  72. let(:user) { create(:agent) }
  73. it 'returns assets for all KB objects' do
  74. expect(assets)
  75. .to include_assets_of(knowledge_base, other_category, published_answer_in_other_category)
  76. .and not_include_assets_of(draft_answer, category, internal_answer, published_answer)
  77. end
  78. end
  79. end
  80. describe '#all_answer_ids' do
  81. let(:user) { create(:agent) }
  82. it 'returns available answer ids' do
  83. expect(described_class.new(user).all_answer_ids).to contain_exactly(
  84. internal_answer.id,
  85. published_answer.id,
  86. )
  87. end
  88. end
  89. describe '#all_category_ids' do
  90. let(:user) { create(:agent) }
  91. it 'returns available category id' do
  92. expect(described_class.new(user).all_category_ids).to contain_exactly(
  93. published_answer.category.id
  94. )
  95. end
  96. end
  97. end