search_knowledge_base_backend_spec.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (C) 2012-2022 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. context 'with ES', searchindex: true do
  15. before do
  16. configure_elasticsearch(required: true, rebuild: true) do
  17. published_answer
  18. end
  19. end
  20. describe '#search' do
  21. context 'when highlight enabled' do
  22. let(:options) do
  23. {
  24. knowledge_base: knowledge_base,
  25. locale: primary_locale,
  26. scope: nil,
  27. highlight_enabled: true
  28. }
  29. end
  30. # https://github.com/zammad/zammad/issues/3070
  31. it 'lists item with an attachment' do
  32. expect(instance.search('Hello World', user: user)).to be_present
  33. end
  34. end
  35. end
  36. end
  37. context 'with paging' do
  38. let(:answers) do
  39. Array.new(20) do |nth|
  40. create(:knowledge_base_answer, :published, :with_attachment, category: category, translation_attributes: { title: "#{search_phrase} #{nth}" })
  41. end
  42. end
  43. let(:search_phrase) { 'paging test' }
  44. let(:options) do
  45. {
  46. knowledge_base: knowledge_base,
  47. locale: primary_locale,
  48. scope: nil,
  49. order_by: { id: :desc }
  50. }
  51. end
  52. shared_examples 'verify paging' do |elasticsearch:|
  53. context "when elastic search is #{elasticsearch}", searchindex: elasticsearch do
  54. before do
  55. answers
  56. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  57. end
  58. it 'first page is first 5 answers' do
  59. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 1, per_page: 5 }))
  60. first_5 = answers.reverse.slice(0, 5)
  61. expect(results.pluck(:id)).to eq first_5.map { |answer| answer.translations.first.id }
  62. end
  63. it 'second page is next 5 answers' do
  64. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 2, per_page: 5 }))
  65. next_5 = answers.reverse.slice(5, 5)
  66. expect(results.pluck(:id)).to eq next_5.map { |answer| answer.translations.first.id }
  67. end
  68. it 'last page may be partial' do
  69. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 4, per_page: 6 }))
  70. last_page = answers.reverse.slice(18, 6)
  71. expect(results.pluck(:id)).to eq last_page.map { |answer| answer.translations.first.id }
  72. end
  73. it '5th page is empty' do
  74. results = instance.search(search_phrase, user: user, pagination: build(:pagination, params: { page: 5, per_page: 5 }))
  75. expect(results).to be_blank
  76. end
  77. end
  78. end
  79. include_examples 'verify paging', elasticsearch: true
  80. include_examples 'verify paging', elasticsearch: false
  81. end
  82. context 'with successful API response' do
  83. shared_examples 'verify response' do |elasticsearch:|
  84. it "ID is an Integer when ES=#{elasticsearch}", searchindex: elasticsearch do
  85. published_answer
  86. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  87. first_result = instance.search(published_answer.translations.first.title, user: user).first
  88. expect(first_result[:id]).to be_a(Integer)
  89. end
  90. end
  91. include_examples 'verify response', elasticsearch: true
  92. include_examples 'verify response', elasticsearch: false
  93. end
  94. context 'with user trait and object state' do
  95. def expected_visibility_instance(ui_identifier)
  96. options = {
  97. knowledge_base: knowledge_base,
  98. locale: primary_locale,
  99. scope: nil,
  100. flavor: ui_identifier
  101. }
  102. described_class.new options
  103. end
  104. shared_examples 'verify given search backend' do |permissions:, ui:, elasticsearch:|
  105. is_visible = permissions == :all || permissions == ui
  106. prefix = is_visible ? 'lists' : 'does not list'
  107. it "#{prefix} in #{ui} interface when ES=#{elasticsearch}", searchindex: elasticsearch do
  108. instance = expected_visibility_instance ui
  109. object
  110. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  111. expect(instance.search(object.translations.first.title, user: user)).to is_visible ? be_present : be_blank
  112. end
  113. end
  114. shared_examples 'verify given permissions' do |scope:, trait:, admin:, agent:|
  115. context "with #{trait} #{scope}" do
  116. let(:object) { create("knowledge_base_#{scope}", trait, knowledge_base: knowledge_base) }
  117. include_examples 'verify given user', user_id: :admin, permissions: admin
  118. include_examples 'verify given user', user_id: :agent, permissions: agent
  119. end
  120. end
  121. shared_examples 'verify given user' do |user_id:, permissions:|
  122. context "with #{user_id}" do
  123. let(:user) { create(user_id) }
  124. include_examples 'verify given search backend', permissions: permissions, ui: :agent, elasticsearch: true
  125. include_examples 'verify given search backend', permissions: permissions, ui: :agent, elasticsearch: false
  126. include_examples 'verify given search backend', permissions: permissions, ui: :public, elasticsearch: true
  127. include_examples 'verify given search backend', permissions: permissions, ui: :public, elasticsearch: false
  128. end
  129. end
  130. include_examples 'verify given permissions', scope: :answer, trait: :published, admin: :all, agent: :all
  131. include_examples 'verify given permissions', scope: :answer, trait: :internal, admin: :all, agent: :agent
  132. include_examples 'verify given permissions', scope: :answer, trait: :draft, admin: :all, agent: :none
  133. include_examples 'verify given permissions', scope: :answer, trait: :archived, admin: :all, agent: :none
  134. include_examples 'verify given permissions', scope: :category, trait: :empty, admin: :all, agent: :none
  135. include_examples 'verify given permissions', scope: :category, trait: :containing_published, admin: :all, agent: :all
  136. include_examples 'verify given permissions', scope: :category, trait: :containing_internal, admin: :all, agent: :agent
  137. include_examples 'verify given permissions', scope: :category, trait: :containing_draft, admin: :all, agent: :none
  138. include_examples 'verify given permissions', scope: :category, trait: :containing_archived, admin: :all, agent: :none
  139. end
  140. end