session_collections_test.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class SessionCollectionsTest < ActiveSupport::TestCase
  4. test 'c collections' do
  5. UserInfo.current_user_id = 1
  6. # create users
  7. roles = Role.where(name: %w(Agent Admin))
  8. groups = Group.all
  9. agent1 = User.create_or_update(
  10. login: 'session-collections-agent-1',
  11. firstname: 'Session',
  12. lastname: 'collections 1',
  13. email: 'session-collections-agent-1@example.com',
  14. password: 'agentpw',
  15. organization_id: nil,
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. agent1.roles = roles
  21. agent1.save
  22. roles = Role.where(name: ['Agent'])
  23. groups = Group.all
  24. agent2 = User.create_or_update(
  25. login: 'session-collections-agent-2',
  26. firstname: 'Session',
  27. lastname: 'collections 2',
  28. email: 'session-collections-agent-2@example.com',
  29. password: 'agentpw',
  30. organization_id: nil,
  31. active: true,
  32. roles: roles,
  33. groups: groups,
  34. )
  35. agent2.roles = roles
  36. agent2.save
  37. roles = Role.where(name: ['Customer'])
  38. customer1 = User.create_or_update(
  39. login: 'session-collections-customer-1',
  40. firstname: 'Session',
  41. lastname: 'collections 2',
  42. email: 'session-collections-customer-1@example.com',
  43. password: 'customerpw',
  44. organization_id: nil,
  45. active: true,
  46. roles: roles,
  47. )
  48. customer1.roles = roles
  49. customer1.save
  50. collection_client1 = Sessions::Backend::Collections.new(agent1, {}, nil, 'aaa-1', 2)
  51. collection_client2 = Sessions::Backend::Collections.new(agent2, {}, nil, 'bbb-2', 2)
  52. collection_client3 = Sessions::Backend::Collections.new(customer1, {}, nil, 'ccc-2', 2)
  53. # get whole collections
  54. result1 = collection_client1.push
  55. assert(result1, 'check collections')
  56. assert(check_if_collection_exists(result1, :Group), 'check collections - after init')
  57. assert(check_if_collection_exists(result1, :Role), 'check collections - after init')
  58. assert(check_if_collection_exists(result1, :Signature), 'check collections - after init')
  59. assert(check_if_collection_exists(result1, :EmailAddress), 'check collections - after init')
  60. travel 1.second
  61. result2 = collection_client2.push
  62. assert(result2, 'check collections')
  63. assert(check_if_collection_exists(result2, :Group), 'check collections - after init')
  64. assert(check_if_collection_exists(result2, :Role), 'check collections - after init')
  65. assert(check_if_collection_exists(result2, :Signature), 'check collections - after init')
  66. assert(check_if_collection_exists(result2, :EmailAddress), 'check collections - after init')
  67. assert_equal(result1.length, result2.length, 'check collections')
  68. assert_equal(result1[0], result2[0], 'check collections')
  69. assert_equal(result1[1], result2[1], 'check collections')
  70. assert_equal(result1[2], result2[2], 'check collections')
  71. assert_equal(result1[3], result2[3], 'check collections')
  72. assert_equal(result1[4], result2[4], 'check collections')
  73. assert_equal(result1[5], result2[5], 'check collections')
  74. assert_equal(result1[6], result2[6], 'check collections')
  75. assert_equal(result1[7], result2[7], 'check collections')
  76. assert_equal(result1[8], result2[8], 'check collections')
  77. assert_equal(result1[9], result2[9], 'check collections')
  78. assert_equal(result1, result2, 'check collections')
  79. result3 = collection_client3.push
  80. assert(result3, 'check collections')
  81. assert(check_if_collection_exists(result3, :Group), 'check collections - after init')
  82. assert(check_if_collection_exists(result3, :Role), 'check collections - after init')
  83. assert(!check_if_collection_exists(result3, :Signature), 'check collections - after init')
  84. assert(!check_if_collection_exists(result3, :EmailAddress), 'check collections - after init')
  85. # next check should be empty
  86. result1 = collection_client1.push
  87. assert(result1.empty?, 'check collections - recall')
  88. sleep 0.4
  89. result2 = collection_client2.push
  90. assert(result2.empty?, 'check collections - recall')
  91. result3 = collection_client3.push
  92. assert(result3.empty?, 'check collections - recall')
  93. # change collection
  94. group = Group.first
  95. group.touch
  96. travel 3.seconds
  97. # get whole collections
  98. result1 = collection_client1.push
  99. assert(result1, 'check collections - after touch')
  100. assert(check_if_collection_exists(result1, :Group), 'check collections - after touch')
  101. sleep 0.1
  102. result2 = collection_client2.push
  103. assert(result2, 'check collections - after touch')
  104. assert(check_if_collection_exists(result2, :Group), 'check collections - after touch')
  105. result3 = collection_client3.push
  106. assert(result3, 'check collections - after touch')
  107. assert(check_if_collection_exists(result3, :Group), 'check collections - after touch')
  108. # next check should be empty
  109. sleep 0.5
  110. result1 = collection_client1.push
  111. assert(result1.empty?, 'check collections - recall')
  112. result2 = collection_client2.push
  113. assert(result2.empty?, 'check collections - recall')
  114. result3 = collection_client3.push
  115. assert(result3.empty?, 'check collections - recall')
  116. travel_back
  117. end
  118. def check_if_collection_exists(results, collection, attributes = nil)
  119. results.each { |result|
  120. next if !result
  121. next if !result[:collection]
  122. next if !result[:collection][collection]
  123. # check just if collection exists
  124. return true if !attributes
  125. # check if objetc with attributes in collection exists
  126. result[:collection][collection].each { |item|
  127. match_all = true
  128. attributes.each { |key, value|
  129. # sort array, database result maybe unsorted
  130. item_attributes = item[ key.to_s ]
  131. if item[ key.to_s ].class == Array
  132. item_attributes.sort!
  133. end
  134. if value.class == Array
  135. value.sort!
  136. end
  137. # compare values
  138. if item_attributes != value
  139. #p "FAILED: #{key} -> #{item_attributes.inspect} vs. #{value.inspect}"
  140. match_all = false
  141. end
  142. }
  143. return true if match_all
  144. }
  145. }
  146. nil
  147. end
  148. end