session_collections_test.rb 7.7 KB

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