search_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Search do
  4. let(:query) { 'test_phrase' }
  5. let(:current_user) { create(:agent) }
  6. let(:objects) { [User, Organization, Ticket] }
  7. let(:options) { {} }
  8. let(:instance) { described_class.new(current_user:, query:, objects:, options:) }
  9. describe '#execute' do
  10. let(:customer) { create(:customer, firstname: query) }
  11. let(:organization) { create(:organization, name: query) }
  12. before do
  13. customer
  14. organization
  15. end
  16. it 'returns combined result with found items' do
  17. expect(instance.execute.result).to include(
  18. User => include(objects: [customer], total_count: 1),
  19. Organization => include(objects: [organization], total_count: 1),
  20. Ticket => include(objects: be_blank, total_count: 0)
  21. )
  22. end
  23. it 'lists models in the result in a specific order' do
  24. expect(instance.execute.result.keys).to eq [Ticket, User, Organization]
  25. end
  26. it 'lists flattened results in correct order' do
  27. expect(instance.execute.flattened).to eq [customer, organization]
  28. end
  29. context 'when objects are restricted' do
  30. let(:objects) { [User] }
  31. it 'searches given model only' do
  32. expect(instance.execute.result.keys).to eq [User]
  33. end
  34. end
  35. end
  36. describe '#search_single_model' do
  37. before do
  38. allow(SearchIndexBackend).to receive(:search_by_index)
  39. allow(User).to receive(:search)
  40. allow(Ticket).to receive(:search)
  41. end
  42. context 'when ElasticSearch is available' do
  43. before { allow(SearchIndexBackend).to receive(:enabled?).and_return(true) }
  44. context 'when direct index query allowed' do
  45. it 'uses SearchIndexBackend' do
  46. instance.send(:search_single_model, User)
  47. expect(SearchIndexBackend).to have_received(:search_by_index)
  48. end
  49. end
  50. context 'when direct index query not allowed' do
  51. it 'uses model#search' do
  52. instance.send(:search_single_model, Ticket)
  53. expect(Ticket).to have_received(:search)
  54. end
  55. end
  56. end
  57. context 'when ElasticSearch not available' do
  58. before { allow(SearchIndexBackend).to receive(:enabled?).and_return(false) }
  59. context 'when direct index query allowed' do
  60. it 'uses model#search' do
  61. instance.send(:search_single_model, User)
  62. expect(User).to have_received(:search)
  63. end
  64. end
  65. context 'when direct index query not allowed' do
  66. it 'uses model#search' do
  67. instance.send(:search_single_model, Ticket)
  68. expect(Ticket).to have_received(:search)
  69. end
  70. end
  71. end
  72. context 'with given options' do
  73. let(:options) { { limit: 123, offset: 1024 } }
  74. before { allow(SearchIndexBackend).to receive(:enabled?).and_return(true) }
  75. it 'forwards limit and offset arguments to model#search' do
  76. instance.send(:search_single_model, User)
  77. expect(SearchIndexBackend)
  78. .to have_received(:search_by_index)
  79. .with(anything, anything, include(limit: 123, offset: 1024))
  80. end
  81. it 'forwards limit and offset arguments to SearchIndexBackend' do
  82. instance.send(:search_single_model, Ticket)
  83. expect(Ticket)
  84. .to have_received(:search)
  85. .with(include(limit: 123, offset: 1024))
  86. end
  87. end
  88. end
  89. end