has_search_index_backend_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'HasSearchIndexBackend', performs_jobs: true, searchindex: true, type: :model do
  4. describe 'Updating referenced data between ticket and organizations' do
  5. let(:organization) { create(:organization, name: 'Tomato42') }
  6. let(:user) { create(:customer, organization: organization) }
  7. let(:ticket) { create(:ticket, customer: user) }
  8. let(:article) do
  9. article = create(:ticket_article, ticket_id: ticket.id)
  10. create(:store,
  11. object: 'Ticket::Article',
  12. o_id: article.id,
  13. data: Rails.root.join('test/data/elasticsearch/es-normal.txt').binread,
  14. filename: 'es-normal.txt')
  15. article
  16. end
  17. before do
  18. article && organization
  19. searchindex_model_reload([Ticket, Organization])
  20. end
  21. it 'finds added tickets' do
  22. result = SearchIndexBackend.search('organization.name:Tomato42', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
  23. expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
  24. end
  25. it 'finds added ticket article attachments' do
  26. result = SearchIndexBackend.search('text66', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
  27. expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
  28. end
  29. context 'when renaming the organization' do
  30. before do
  31. organization.update(name: 'Cucumber43 Ltd.')
  32. organization.search_index_update_associations
  33. perform_enqueued_jobs
  34. SearchIndexBackend.refresh
  35. end
  36. it 'finds added tickets by organization name in sub hash' do
  37. result = SearchIndexBackend.search('organization.name:Cucumber43', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
  38. expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
  39. end
  40. it 'finds added tickets by organization name' do
  41. result = SearchIndexBackend.search('Cucumber43', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
  42. expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
  43. end
  44. it 'still finds attachments' do
  45. result = SearchIndexBackend.search('text66', 'Ticket', sort_by: ['updated_at'], order_by: ['desc'])
  46. expect(result).to eq([{ id: ticket.id.to_s, type: 'Ticket' }])
  47. end
  48. end
  49. it 'does return organization_id as referenced indexable attribute' do
  50. result = organization.search_index_indexable_attributes(Ticket)
  51. expect(result).to include({ name: 'organization_id', ref_name: 'organization' })
  52. end
  53. it 'does not return updated_by_id as referenced indexable attribute' do
  54. result = user.search_index_indexable_attributes(Ticket)
  55. expect(result).not_to include({ name: 'updated_by_id', ref_name: 'updated_by' })
  56. end
  57. it 'does include organization_id as relevant search index attribute' do
  58. expect(Ticket).to be_search_index_attribute_relevant('organization_id')
  59. end
  60. it 'does exclude updated_by as relevant search index attribute' do
  61. expect(Ticket).not_to be_search_index_attribute_relevant('updated_by_id')
  62. end
  63. end
  64. describe 'Updating group settings causes huge numbers of delayed jobs #4306', performs_jobs: false do
  65. let(:ticket) { create(:ticket, customer: create(:customer, :with_org)) }
  66. before do
  67. ticket
  68. Delayed::Job.destroy_all
  69. end
  70. it 'does not create any jobs if nothing has changed' do
  71. expect { ticket.update(title: ticket.title) }.not_to change(Delayed::Job, :count)
  72. end
  73. it 'does not create any jobs for the organization if the organization has not changed at the ticket' do
  74. ticket.update(title: SecureRandom.uuid)
  75. expect(Delayed::Job.where("handler LIKE '%SearchIndexJob%' AND handler LIKE '%Organization%'").count).to eq(0)
  76. end
  77. it 'does create jobs for the organization if the organization has changed at the ticket' do
  78. ticket.update(customer: create(:customer, :with_org))
  79. expect(Delayed::Job.where("handler LIKE '%SearchIndexJob%' AND handler LIKE '%Organization%'").count).to be > 0
  80. end
  81. end
  82. describe 'Search doesnt show tickets belonging to secondary organization #4425' do
  83. let(:organization_a) { create(:organization, shared: true) }
  84. let(:organization_b) { create(:organization, shared: false) }
  85. let(:customer_a) { create(:customer, :with_org, organizations: [organization_a, organization_b]) }
  86. let(:customer_b) { create(:customer, :with_org, organizations: [organization_a, organization_b]) }
  87. let(:ticket_customer_a_shared) do
  88. ticket = create(:ticket, title: 'findme', customer: customer_a, organization: organization_a)
  89. create(:ticket_article, ticket: ticket)
  90. ticket
  91. end
  92. let(:ticket_customer_a_nonshared) do
  93. ticket = create(:ticket, title: 'findme', customer: customer_a, organization: organization_b)
  94. create(:ticket_article, ticket: ticket)
  95. ticket
  96. end
  97. let(:ticket_customer_b_shared) do
  98. ticket = create(:ticket, title: 'findme', customer: customer_b, organization: organization_a)
  99. create(:ticket_article, ticket: ticket)
  100. ticket
  101. end
  102. let(:ticket_customer_b_nonshared) do
  103. ticket = create(:ticket, title: 'findme', customer: customer_b, organization: organization_b)
  104. create(:ticket_article, ticket: ticket)
  105. ticket
  106. end
  107. before do
  108. ticket_customer_a_shared
  109. ticket_customer_a_nonshared
  110. ticket_customer_b_shared
  111. ticket_customer_b_nonshared
  112. searchindex_model_reload([Ticket, User, Organization])
  113. end
  114. context 'with ES' do
  115. it 'customer does find shared tickets', :aggregate_failures do
  116. result = Ticket.search(
  117. current_user: customer_a,
  118. query: 'findme',
  119. full: true,
  120. )
  121. expect(result).to include(ticket_customer_a_shared)
  122. expect(result).to include(ticket_customer_a_nonshared)
  123. expect(result).to include(ticket_customer_b_shared)
  124. expect(result).not_to include(ticket_customer_b_nonshared)
  125. end
  126. end
  127. context 'with DB', searchindex: false do
  128. it 'customer does find shared tickets', :aggregate_failures do
  129. result = Ticket.search(
  130. current_user: customer_a,
  131. query: 'findme',
  132. full: true,
  133. )
  134. expect(result).to include(ticket_customer_a_shared)
  135. expect(result).to include(ticket_customer_a_nonshared)
  136. expect(result).to include(ticket_customer_b_shared)
  137. expect(result).not_to include(ticket_customer_b_nonshared)
  138. end
  139. end
  140. end
  141. end