search_knowledge_base_backend_spec.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe SearchKnowledgeBaseBackend do
  4. include_context 'basic Knowledge Base'
  5. let(:instance) { described_class.new options }
  6. let(:user) { create(:admin) }
  7. let(:options) do
  8. {
  9. knowledge_base: knowledge_base,
  10. locale: primary_locale,
  11. scope: nil
  12. }
  13. end
  14. def handle_elasticsearch(enabled)
  15. if enabled
  16. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  17. else
  18. Setting.set('es_url', nil)
  19. end
  20. end
  21. context 'with ES', searchindex: true do
  22. describe '#search' do
  23. context 'when highlight enabled' do
  24. let(:options) do
  25. {
  26. knowledge_base: knowledge_base,
  27. locale: primary_locale,
  28. scope: nil,
  29. highlight_enabled: true
  30. }
  31. end
  32. before do
  33. published_answer
  34. handle_elasticsearch(true)
  35. end
  36. # https://github.com/zammad/zammad/issues/3070
  37. it 'lists item with an attachment' do
  38. expect(instance.search('Hello World', user: user)).to be_present
  39. end
  40. context 'with big attachment' do
  41. before do
  42. url = "#{Setting.get('es_url')}/_all/_settings?preserve_existing=true"
  43. SearchIndexBackend.make_request_and_validate(url, data: { index: { 'highlight.max_analyzed_offset': 1000 } }, method: :put)
  44. end
  45. let :published_answer do
  46. create(:knowledge_base_answer, :published, :with_attachment, attachment: File.open('spec/fixtures/files/upload/lipsum.pdf'), category: category)
  47. end
  48. it 'lists item with an attachment' do
  49. expect(instance.search('Suspendisse', user: user)).to be_present
  50. end
  51. end
  52. end
  53. end
  54. end
  55. context 'with paging' do
  56. let(:answers) do
  57. Array.new(20) do |nth|
  58. create(:knowledge_base_answer, :published, :with_attachment, category: category, translation_attributes: { title: "#{search_phrase} #{nth}" })
  59. end
  60. end
  61. let(:search_phrase) { 'paging test' }
  62. let(:options) do
  63. {
  64. knowledge_base: knowledge_base,
  65. locale: primary_locale,
  66. scope: nil,
  67. order_by: { id: :desc }
  68. }
  69. end
  70. shared_examples 'verify paging' do |elasticsearch:|
  71. context "when elastic search is #{elasticsearch}", searchindex: elasticsearch do
  72. before do
  73. answers
  74. handle_elasticsearch(elasticsearch)
  75. end
  76. it 'first page is first 5 answers' do
  77. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 1, per_page: 5 }))
  78. first_5 = answers.reverse.slice(0, 5)
  79. expect(results.pluck(:id)).to eq first_5.map { |answer| answer.translations.first.id }
  80. end
  81. it 'second page is next 5 answers' do
  82. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 2, per_page: 5 }))
  83. next_5 = answers.reverse.slice(5, 5)
  84. expect(results.pluck(:id)).to eq next_5.map { |answer| answer.translations.first.id }
  85. end
  86. it 'last page may be partial' do
  87. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 4, per_page: 6 }))
  88. last_page = answers.reverse.slice(18, 6)
  89. expect(results.pluck(:id)).to eq last_page.map { |answer| answer.translations.first.id }
  90. end
  91. it '5th page is empty' do
  92. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 5, per_page: 5 }))
  93. expect(results).to be_blank
  94. end
  95. end
  96. end
  97. include_examples 'verify paging', elasticsearch: true
  98. include_examples 'verify paging', elasticsearch: false
  99. end
  100. context 'with successful API response' do
  101. before do
  102. published_answer
  103. end
  104. shared_examples 'verify response' do |elasticsearch:|
  105. it "ID is an Integer when ES=#{elasticsearch}", searchindex: elasticsearch do
  106. handle_elasticsearch(elasticsearch)
  107. first_result = instance.search(published_answer.translations.first.title, user: user).first
  108. expect(first_result[:id]).to be_a(Integer)
  109. end
  110. end
  111. include_examples 'verify response', elasticsearch: true
  112. include_examples 'verify response', elasticsearch: false
  113. end
  114. context 'with user trait and object state' do
  115. def expected_visibility_instance(ui_identifier)
  116. options = {
  117. knowledge_base: knowledge_base,
  118. locale: primary_locale,
  119. scope: nil,
  120. flavor: ui_identifier
  121. }
  122. described_class.new options
  123. end
  124. shared_examples 'verify given search backend' do |permissions:, ui:|
  125. is_visible = permissions == :all || permissions == ui
  126. prefix = is_visible ? 'lists' : 'does not list'
  127. [true, false].each do |elasticsearch|
  128. it "#{prefix} in #{ui} interface when ES=#{elasticsearch}", searchindex: elasticsearch do
  129. instance = expected_visibility_instance ui
  130. object
  131. handle_elasticsearch(elasticsearch)
  132. expect(instance.search(object.translations.first.title, user: user)).to is_visible ? be_present : be_blank
  133. end
  134. end
  135. end
  136. shared_examples 'verify given permissions' do |scope:, trait:, admin:, agent:|
  137. context "with #{trait} #{scope}" do
  138. let(:object) { create("knowledge_base_#{scope}", trait, knowledge_base: knowledge_base) }
  139. include_examples 'verify given user', user_id: :admin, permissions: admin
  140. include_examples 'verify given user', user_id: :agent, permissions: agent
  141. end
  142. end
  143. shared_examples 'verify given user' do |user_id:, permissions:|
  144. context "with #{user_id}" do
  145. let(:user) { create(user_id) }
  146. include_examples 'verify given search backend', permissions: permissions, ui: :agent
  147. include_examples 'verify given search backend', permissions: permissions, ui: :public
  148. end
  149. end
  150. include_examples 'verify given permissions', scope: :answer, trait: :published, admin: :all, agent: :all
  151. include_examples 'verify given permissions', scope: :answer, trait: :internal, admin: :all, agent: :agent
  152. include_examples 'verify given permissions', scope: :answer, trait: :draft, admin: :all, agent: :none
  153. include_examples 'verify given permissions', scope: :answer, trait: :archived, admin: :all, agent: :none
  154. include_examples 'verify given permissions', scope: :category, trait: :empty, admin: :all, agent: :none
  155. include_examples 'verify given permissions', scope: :category, trait: :containing_published, admin: :all, agent: :all
  156. include_examples 'verify given permissions', scope: :category, trait: :containing_internal, admin: :all, agent: :agent
  157. include_examples 'verify given permissions', scope: :category, trait: :containing_draft, admin: :all, agent: :none
  158. include_examples 'verify given permissions', scope: :category, trait: :containing_archived, admin: :all, agent: :none
  159. context 'with granular permissions' do
  160. before do
  161. KnowledgeBase::PermissionsUpdate
  162. .new(category)
  163. .update! Role.find_by(name: 'Agent') => 'none'
  164. end
  165. context 'with reader with limited access to answer' do
  166. let(:object) { internal_answer }
  167. let(:user) { create(:agent) }
  168. include_examples 'verify given search backend', permissions: :none, ui: :agent
  169. end
  170. context 'with editor with full access to answer' do
  171. let(:object) { internal_answer }
  172. let(:user) { create(:admin) }
  173. include_examples 'verify given search backend', permissions: :all, ui: :agent
  174. include_examples 'verify given search backend', permissions: :all, ui: :public
  175. end
  176. context 'with reader with limited access to category' do
  177. let(:object) { internal_answer.category }
  178. let(:user) { create(:agent) }
  179. include_examples 'verify given search backend', permissions: :none, ui: :agent
  180. end
  181. context 'with editor with full access to category' do
  182. let(:object) { internal_answer.category }
  183. let(:user) { create(:admin) }
  184. include_examples 'verify given search backend', permissions: :all, ui: :agent
  185. include_examples 'verify given search backend', permissions: :all, ui: :public
  186. end
  187. context 'with unauthorized user and public answer' do
  188. let(:object) { published_answer }
  189. let(:user) { nil }
  190. include_examples 'verify given search backend', permissions: :all, ui: :public
  191. end
  192. context 'with unauthorized user and internal answer' do
  193. let(:object) { internal_answer }
  194. let(:user) { nil }
  195. include_examples 'verify given search backend', permissions: :none, ui: :public
  196. end
  197. end
  198. end
  199. end