elasticsearch_active_test.rb 3.3 KB

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