elasticsearch_active_test.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. require 'test_helper'
  2. class ElasticsearchActiveTest < ActiveSupport::TestCase
  3. include SearchindexHelper
  4. setup do
  5. configure_elasticsearch(required: true)
  6. rebuild_searchindex
  7. roles = Role.where(name: 'Agent')
  8. @agent = User.create!(
  9. login: 'es-agent@example.com',
  10. firstname: 'E',
  11. lastname: 'S',
  12. email: 'es-agent@example.com',
  13. password: 'agentpw',
  14. active: true,
  15. roles: roles,
  16. updated_by_id: 1,
  17. created_by_id: 1,
  18. )
  19. roles = Role.where(name: 'Customer')
  20. (1..6).each do |i|
  21. name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
  22. User.create!(
  23. login: "#{name}-customer#{i}@example.com",
  24. firstname: 'ActiveTest',
  25. lastname: name,
  26. active: i.even?,
  27. roles: roles,
  28. updated_by_id: 1,
  29. created_by_id: 1,
  30. )
  31. Organization.create!(
  32. name: "TestOrg-#{name}",
  33. active: i.even?,
  34. updated_by_id: 1,
  35. created_by_id: 1,
  36. )
  37. travel 10.seconds
  38. end
  39. # execute background jobs to index created/changed objects
  40. Scheduler.worker(true)
  41. sleep 2 # for ES to come ready/indexed
  42. end
  43. test 'active users appear before inactive users in search results' do
  44. result = User.search(
  45. current_user: @agent,
  46. query: 'ActiveTest',
  47. limit: 15,
  48. )
  49. assert(result.present?, 'result should not be empty')
  50. names = result.map(&:lastname)
  51. correct_names = %w[Active-2 Active-4 Active-6 Inactive-1 Inactive-3 Inactive-5]
  52. assert_equal(correct_names[0, 3], names[0, 3].sort)
  53. assert_equal(correct_names[3, 6], names[3, 6].sort)
  54. end
  55. test 'active organizations appear before inactive organizations in search results' do
  56. result = Organization.search(
  57. current_user: @agent,
  58. query: 'TestOrg',
  59. limit: 15,
  60. )
  61. assert(result.present?, 'result should not be empty')
  62. names = result.map(&:name)
  63. correct_names = %w[TestOrg-Active-2
  64. TestOrg-Active-4
  65. TestOrg-Active-6
  66. TestOrg-Inactive-1
  67. TestOrg-Inactive-3
  68. TestOrg-Inactive-5]
  69. assert_equal(correct_names[0, 3], names[0, 3].sort)
  70. assert_equal(correct_names[3, 6], names[3, 6].sort)
  71. end
  72. test 'ordering of tickets are not affected by the lack of active flags' do
  73. ticket_setup
  74. result = Ticket.search(
  75. current_user: User.find(1),
  76. query: 'ticket',
  77. limit: 15,
  78. )
  79. assert(result.present?, 'result should not be empty')
  80. names = result.map(&:title)
  81. correct_names = %w[Ticket-6 Ticket-5 Ticket-4 Ticket-3 Ticket-2 Ticket-1]
  82. assert_equal(correct_names, names)
  83. end
  84. def ticket_setup
  85. result = Ticket.search(
  86. current_user: User.find(1),
  87. query: 'ticket',
  88. limit: 15,
  89. )
  90. return if result.present?
  91. (1..6).each do |i|
  92. Ticket.create!(
  93. title: "Ticket-#{i}",
  94. group: Group.lookup(name: 'Users'),
  95. customer_id: 1,
  96. state: Ticket::State.lookup(name: 'new'),
  97. priority: Ticket::Priority.lookup(name: '2 normal'),
  98. updated_by_id: 1,
  99. created_by_id: 1,
  100. )
  101. travel 10.seconds
  102. end
  103. # execute background jobs to index created/changed objects
  104. Scheduler.worker(true)
  105. sleep 2 # for ES to come ready/indexed
  106. end
  107. end