session_collections_test.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 => [ '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. :active => true,
  16. :roles => roles,
  17. :groups => groups,
  18. )
  19. agent1.roles = roles
  20. agent1.save
  21. roles = Role.where( :name => [ 'Agent' ] )
  22. groups = Group.all
  23. agent2 = User.create_or_update(
  24. :login => 'session-collections-agent-2',
  25. :firstname => 'Session',
  26. :lastname => 'collections 2',
  27. :email => 'session-collections-agent-2@example.com',
  28. :password => 'agentpw',
  29. :active => true,
  30. :roles => roles,
  31. :groups => groups,
  32. )
  33. agent2.roles = roles
  34. agent2.save
  35. roles = Role.where( :name => [ 'Customer'] )
  36. customer1 = User.create_or_update(
  37. :login => 'session-collections-customer-1',
  38. :firstname => 'Session',
  39. :lastname => 'collections 2',
  40. :email => 'session-collections-customer-1@example.com',
  41. :password => 'customerpw',
  42. :active => true,
  43. :roles => roles,
  44. )
  45. customer1.roles = roles
  46. customer1.save
  47. collection_client1 = Sessions::Backend::Collections.new(agent1, nil, 'aaa-1')
  48. collection_client2 = Sessions::Backend::Collections.new(agent2, nil, 'bbb-2')
  49. collection_client3 = Sessions::Backend::Collections.new(customer1, nil, 'bbb-2')
  50. # get whole collections
  51. result1 = collection_client1.push
  52. assert( result1, "check collections" )
  53. assert( check_if_collection_exists(result1, :Group), "check collections - after init" )
  54. assert( check_if_collection_exists(result1, :Role), "check collections - after init" )
  55. assert( check_if_collection_exists(result1, :Signature), "check collections - after init" )
  56. assert( check_if_collection_exists(result1, :EmailAddress), "check collections - after init" )
  57. sleep 1
  58. result2 = collection_client2.push
  59. assert( result2, "check collections" )
  60. assert( check_if_collection_exists(result2, :Group), "check collections - after init" )
  61. assert( check_if_collection_exists(result2, :Role), "check collections - after init" )
  62. assert( check_if_collection_exists(result2, :Signature), "check collections - after init" )
  63. assert( check_if_collection_exists(result2, :EmailAddress), "check collections - after init" )
  64. assert_equal( result1, result2, "check collections" )
  65. result3 = collection_client3.push
  66. assert( result3, "check collections" )
  67. assert( check_if_collection_exists(result3, :Group), "check collections - after init" )
  68. assert( check_if_collection_exists(result3, :Role), "check collections - after init" )
  69. assert( !check_if_collection_exists(result3, :Signature), "check collections - after init" )
  70. assert( !check_if_collection_exists(result3, :EmailAddress), "check collections - after init" )
  71. # next check should be empty
  72. result1 = collection_client1.push
  73. assert( result1.empty?, "check collections - recall" )
  74. sleep 1
  75. result2 = collection_client2.push
  76. assert( result2.empty?, "check collections - recall" )
  77. sleep 0.2
  78. result3 = collection_client3.push
  79. assert( result3.empty?, "check collections - recall" )
  80. # change collection
  81. group = Group.first
  82. group.touch
  83. sleep 16
  84. # get whole collections
  85. result1 = collection_client1.push
  86. assert( result1, "check collections - after touch" )
  87. assert( check_if_collection_exists(result1, :Group), "check collections - after touch" )
  88. sleep 1
  89. result2 = collection_client2.push
  90. assert( result2, "check collections - after touch" )
  91. assert( check_if_collection_exists(result2, :Group), "check collections - after touch" )
  92. sleep 0.2
  93. result3 = collection_client3.push
  94. assert( result3, "check collections - after touch" )
  95. assert( check_if_collection_exists(result3, :Group), "check collections - after touch" )
  96. # change collection
  97. org = Organization.create( :name => 'SomeOrg::' + rand(999999).to_s, :active => true )
  98. sleep 16
  99. # get whole collections
  100. result1 = collection_client1.push
  101. assert( result1, "check collections - after create" )
  102. assert( check_if_collection_exists(result1, :Organization), "check collections - after create" )
  103. sleep 0.5
  104. result2 = collection_client2.push
  105. assert( result2, "check collections - after create" )
  106. assert( check_if_collection_exists(result2, :Organization), "check collections - after create" )
  107. sleep 0.5
  108. result3 = collection_client3.push
  109. assert( result3, "check collections - after create" )
  110. assert( !check_if_collection_exists(result3, :Organization), "check collections - after create" )
  111. # next check should be empty
  112. sleep 16
  113. result1 = collection_client1.push
  114. assert( result1.empty?, "check collections - recall" )
  115. sleep 1
  116. result2 = collection_client2.push
  117. assert( result2.empty?, "check collections - recall" )
  118. sleep 0.2
  119. result3 = collection_client3.push
  120. assert( result3.empty?, "check collections - recall" )
  121. end
  122. def check_if_collection_exists(results, collection)
  123. results.each {|result|
  124. return true if result && result[:collection] && result[:collection][collection]
  125. }
  126. nil
  127. end
  128. end