elasticsearch_active_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Elasticsearch', aggregate_failures: true, performs_jobs: true, searchindex: true do
  4. let(:group) { create(:group) }
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:customers) do
  7. (1..6).map do |i|
  8. name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
  9. customer = create(:customer,
  10. firstname: 'ActiveTest',
  11. lastname: name,
  12. login: "#{name}-customer#{i}@example.com",
  13. active: i.even?)
  14. customer
  15. end
  16. end
  17. let(:organizations) do
  18. (1..6).map do |i|
  19. name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
  20. create(:organization, name: "TestOrg-#{name}", active: i.even?)
  21. end
  22. end
  23. let(:tickets) do
  24. (1..6).map do |i|
  25. create(:ticket, title: "Ticket-#{i}", group: group)
  26. end
  27. end
  28. context 'with users' do
  29. before do
  30. agent
  31. customers
  32. searchindex_model_reload([User])
  33. end
  34. it 'active users appear before inactive users in search results' do
  35. result = User.search(current_user: agent, query: 'ActiveTest')
  36. expect(result).to be_present
  37. expected_names = %w[Active-6 Active-4 Active-2 Inactive-5 Inactive-3 Inactive-1]
  38. actual_names = result.map(&:lastname)
  39. expect(actual_names).to match(expected_names)
  40. end
  41. end
  42. context 'with organizations' do
  43. before do
  44. agent
  45. organizations
  46. searchindex_model_reload([Organization])
  47. end
  48. it 'active organizations appear before inactive organizations in search results' do
  49. result = Organization.search(current_user: agent, query: 'TestOrg')
  50. expect(result).to be_present
  51. expected_names = %w[TestOrg-Active-6 TestOrg-Active-4 TestOrg-Active-2 TestOrg-Inactive-5 TestOrg-Inactive-3 TestOrg-Inactive-1]
  52. actual_names = result.map(&:name)
  53. expect(actual_names).to match(expected_names)
  54. end
  55. end
  56. context 'with tickets' do
  57. before do
  58. agent
  59. tickets
  60. searchindex_model_reload([Ticket])
  61. end
  62. it 'ordering of tickets are not affected by the lack of active flags' do
  63. result = Ticket.search(current_user: agent, query: 'Ticket')
  64. expect(result).to be_present
  65. expected_titles = tickets.map(&:title)
  66. actual_titles = result.map(&:title).reverse
  67. expect(actual_titles).to match(expected_titles)
  68. end
  69. end
  70. end