search_with_details_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Knowledge Base search with details', type: :request, searchindex: true do
  4. include_context 'basic Knowledge Base'
  5. before do
  6. configure_elasticsearch(required: true, rebuild: true) do
  7. published_answer
  8. end
  9. end
  10. let(:endpoint) { '/api/v1/knowledge_bases/search' }
  11. context 'ensure details ID type matches ES ID type' do
  12. it 'for answers' do
  13. post endpoint, params: { query: published_answer.translations.first.title }
  14. expect(json_response['details'][0]['id']).to be_a_kind_of Integer
  15. end
  16. it 'for categories' do
  17. post endpoint, params: { query: category.translations.first.title }
  18. expect(json_response['details'][0]['id']).to be_a_kind_of Integer
  19. end
  20. it 'for knowledge base' do
  21. post endpoint, params: { query: knowledge_base.translations.first.title }
  22. expect(json_response['details'][0]['id']).to be_a_kind_of Integer
  23. end
  24. end
  25. context 'when category translation to one of locales is missing' do
  26. let(:search_phrase) { 'search_phrase' }
  27. let(:alternative_translation) { create('knowledge_base/answer/translation', title: search_phrase, kb_locale: alternative_locale, answer: published_answer) }
  28. before do
  29. alternative_translation
  30. rebuild_searchindex
  31. end
  32. it 'returns answer in locale without category translation' do
  33. post endpoint, params: { query: search_phrase }
  34. expect(json_response['details'][0]['id']).to be alternative_translation.id
  35. end
  36. end
  37. context 'when parent category translation to one of locales is missing' do
  38. let(:search_phrase) { 'search_phrase' }
  39. let(:child_category) { create('knowledge_base/category', parent: category) }
  40. let(:child_category_translation) { create('knowledge_base/category/translation', title: search_phrase, kb_locale: alternative_locale, category: child_category) }
  41. before do
  42. child_category_translation && rebuild_searchindex
  43. end
  44. it 'returns category in locale without category translation', authenticated_as: -> { create(:admin) } do
  45. post endpoint, params: { query: search_phrase }
  46. expect(json_response['details'][0]['subtitle']).to eq category.translation_to(primary_locale).title
  47. end
  48. end
  49. context 'when answer tree is long' do
  50. let(:category1) { create('knowledge_base/category') }
  51. let(:category2) { create('knowledge_base/category', parent: category1) }
  52. let(:category3) { create('knowledge_base/category', parent: category2) }
  53. let(:answer_cut_tree) { create(:knowledge_base_answer, :published, :with_attachment, category: category3) }
  54. let(:category4) { create('knowledge_base/category') }
  55. let(:category5) { create('knowledge_base/category', parent: category4) }
  56. let(:answer_full_tree) { create(:knowledge_base_answer, :published, :with_attachment, category: category5) }
  57. before do
  58. answer_cut_tree && answer_full_tree && rebuild_searchindex
  59. end
  60. it 'returns category with cut tree', authenticated_as: -> { create(:admin) } do
  61. post endpoint, params: { query: answer_cut_tree.translations.first.title }
  62. expect(json_response['details'][0]['subtitle']).to eq("#{category1.translations.first.title} > .. > #{category3.translations.first.title}")
  63. end
  64. it 'returns category with full tree', authenticated_as: -> { create(:admin) } do
  65. post endpoint, params: { query: answer_full_tree.translations.first.title }
  66. expect(json_response['details'][0]['subtitle']).to eq("#{category4.translations.first.title} > #{category5.translations.first.title}")
  67. end
  68. end
  69. context 'when using include_locale parameter' do
  70. context 'when no multiple locales exists' do
  71. it 'no locale added to title' do
  72. post endpoint, params: { query: published_answer.translations.first.title, include_locale: true }
  73. expect(json_response['details'][0]['title']).to not_include('(EN-US)')
  74. end
  75. end
  76. context 'when multiple locales exists' do
  77. before do
  78. # Create a alternative knowledge base locale.
  79. alternative_locale
  80. end
  81. it 'locale added to title' do
  82. post endpoint, params: { query: published_answer.translations.first.title, include_locale: true }
  83. expect(json_response['details'][0]['title']).to include('(EN-US)')
  84. end
  85. end
  86. end
  87. context 'when using paging' do
  88. let(:answers) do
  89. Array.new(20) do |nth|
  90. create(:knowledge_base_answer, :published, :with_attachment, category: category, translation_attributes: { title: "#{search_phrase} #{nth}" })
  91. end
  92. end
  93. let(:search_phrase) { 'paging test' }
  94. before do
  95. configure_elasticsearch(required: true, rebuild: true) do
  96. answers
  97. end
  98. end
  99. it 'returns success' do
  100. post endpoint, params: { query: search_phrase, per_page: 10, page: 0 }
  101. expect(response).to have_http_status(:ok)
  102. end
  103. it 'returns defined amount of items' do
  104. post endpoint, params: { query: search_phrase, per_page: 7, page: 0 }
  105. expect(json_response['result'].count).to be 7
  106. end
  107. end
  108. end