search_knowledge_base_backend_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require 'rails_helper'
  2. RSpec.describe SearchKnowledgeBaseBackend do
  3. include_context 'basic Knowledge Base'
  4. let(:instance) { described_class.new options }
  5. let(:user) { create(:admin) }
  6. let(:options) do
  7. {
  8. knowledge_base: knowledge_base,
  9. locale: primary_locale,
  10. scope: nil
  11. }
  12. end
  13. context 'with ES', searchindex: true do
  14. before do
  15. configure_elasticsearch(required: true, rebuild: true) do
  16. published_answer
  17. end
  18. end
  19. describe '#search' do
  20. context 'when highlight enabled' do
  21. let(:options) do
  22. {
  23. knowledge_base: knowledge_base,
  24. locale: primary_locale,
  25. scope: nil,
  26. highlight_enabled: true
  27. }
  28. end
  29. # https://github.com/zammad/zammad/issues/3070
  30. it 'lists item with an attachment' do
  31. expect(instance.search('Hello World', user: user)).to be_present
  32. end
  33. end
  34. end
  35. end
  36. context 'with successful API response' do
  37. shared_examples 'verify response' do |elasticsearch:|
  38. it "ID is an Integer when ES=#{elasticsearch}", searchindex: elasticsearch do
  39. published_answer
  40. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  41. first_result = instance.search(published_answer.translations.first.title, user: user).first
  42. expect(first_result.dig(:id)).to be_a(Integer)
  43. end
  44. end
  45. include_examples 'verify response', elasticsearch: true
  46. include_examples 'verify response', elasticsearch: false
  47. end
  48. context 'with user trait and object state' do
  49. def expected_visibility_instance(ui_identifier)
  50. options = {
  51. knowledge_base: knowledge_base,
  52. locale: primary_locale,
  53. scope: nil,
  54. flavor: ui_identifier
  55. }
  56. described_class.new options
  57. end
  58. shared_examples 'verify given search backend' do |permissions:, ui:, elasticsearch:|
  59. is_visible = permissions == :all || permissions == ui
  60. prefix = is_visible ? 'lists' : 'does not list'
  61. it "#{prefix} in #{ui} interface when ES=#{elasticsearch}", searchindex: elasticsearch do
  62. instance = expected_visibility_instance ui
  63. object
  64. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  65. expect(instance.search(object.translations.first.title, user: user)).to is_visible ? be_present : be_blank
  66. end
  67. end
  68. shared_examples 'verify given permissions' do |scope:, trait:, admin:, agent:|
  69. context "with #{trait} #{scope}" do
  70. let(:object) { create("knowledge_base_#{scope}", trait, knowledge_base: knowledge_base) }
  71. include_examples 'verify given user', user_id: :admin, permissions: admin
  72. include_examples 'verify given user', user_id: :agent, permissions: agent
  73. end
  74. end
  75. shared_examples 'verify given user' do |user_id:, permissions:|
  76. context "with #{user_id}" do
  77. let(:user) { create(user_id) }
  78. include_examples 'verify given search backend', permissions: permissions, ui: :agent, elasticsearch: true
  79. include_examples 'verify given search backend', permissions: permissions, ui: :agent, elasticsearch: false
  80. include_examples 'verify given search backend', permissions: permissions, ui: :public, elasticsearch: true
  81. include_examples 'verify given search backend', permissions: permissions, ui: :public, elasticsearch: false
  82. end
  83. end
  84. include_examples 'verify given permissions', scope: :answer, trait: :published, admin: :all, agent: :all
  85. include_examples 'verify given permissions', scope: :answer, trait: :internal, admin: :all, agent: :agent
  86. include_examples 'verify given permissions', scope: :answer, trait: :draft, admin: :all, agent: :none
  87. include_examples 'verify given permissions', scope: :answer, trait: :archived, admin: :all, agent: :none
  88. include_examples 'verify given permissions', scope: :category, trait: :empty, admin: :all, agent: :none
  89. include_examples 'verify given permissions', scope: :category, trait: :containing_published, admin: :all, agent: :all
  90. include_examples 'verify given permissions', scope: :category, trait: :containing_internal, admin: :all, agent: :agent
  91. include_examples 'verify given permissions', scope: :category, trait: :containing_draft, admin: :all, agent: :none
  92. include_examples 'verify given permissions', scope: :category, trait: :containing_archived, admin: :all, agent: :none
  93. end
  94. end