session_collections_test.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # encoding: utf-8
  2. # rubocop:disable Next, CyclomaticComplexity
  3. require 'test_helper'
  4. class SessionCollectionsTest < ActiveSupport::TestCase
  5. test 'c collections' do
  6. UserInfo.current_user_id = 1
  7. # create users
  8. roles = Role.where( name: [ 'Agent', 'Admin'] )
  9. groups = Group.all
  10. agent1 = User.create_or_update(
  11. login: 'session-collections-agent-1',
  12. firstname: 'Session',
  13. lastname: 'collections 1',
  14. email: 'session-collections-agent-1@example.com',
  15. password: 'agentpw',
  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. active: true,
  31. roles: roles,
  32. groups: groups,
  33. )
  34. agent2.roles = roles
  35. agent2.save
  36. roles = Role.where( name: [ 'Customer'] )
  37. customer1 = User.create_or_update(
  38. login: 'session-collections-customer-1',
  39. firstname: 'Session',
  40. lastname: 'collections 2',
  41. email: 'session-collections-customer-1@example.com',
  42. password: 'customerpw',
  43. organization_id: nil,
  44. active: true,
  45. roles: roles,
  46. )
  47. customer1.roles = roles
  48. customer1.save
  49. collection_client1 = Sessions::Backend::Collections.new(agent1, nil, 'aaa-1', 3)
  50. collection_client2 = Sessions::Backend::Collections.new(agent2, nil, 'bbb-2', 3)
  51. collection_client3 = Sessions::Backend::Collections.new(customer1, nil, 'ccc-2', 3)
  52. # get whole collections
  53. result1 = collection_client1.push
  54. assert( result1, 'check collections' )
  55. assert( check_if_collection_exists(result1, :Group), 'check collections - after init' )
  56. assert( check_if_collection_exists(result1, :Role), 'check collections - after init' )
  57. assert( check_if_collection_exists(result1, :Signature), 'check collections - after init' )
  58. assert( check_if_collection_exists(result1, :EmailAddress), 'check collections - after init' )
  59. sleep 1
  60. result2 = collection_client2.push
  61. assert( result2, 'check collections' )
  62. assert( check_if_collection_exists(result2, :Group), 'check collections - after init' )
  63. assert( check_if_collection_exists(result2, :Role), 'check collections - after init' )
  64. assert( check_if_collection_exists(result2, :Signature), 'check collections - after init' )
  65. assert( check_if_collection_exists(result2, :EmailAddress), 'check collections - after init' )
  66. assert_equal( result1, result2, 'check collections' )
  67. result3 = collection_client3.push
  68. assert( result3, 'check collections' )
  69. assert( check_if_collection_exists(result3, :Group), 'check collections - after init' )
  70. assert( check_if_collection_exists(result3, :Role), 'check collections - after init' )
  71. assert( !check_if_collection_exists(result3, :Signature), 'check collections - after init' )
  72. assert( !check_if_collection_exists(result3, :EmailAddress), 'check collections - after init' )
  73. # next check should be empty
  74. result1 = collection_client1.push
  75. assert( result1.empty?, 'check collections - recall' )
  76. sleep 0.4
  77. result2 = collection_client2.push
  78. assert( result2.empty?, 'check collections - recall' )
  79. result3 = collection_client3.push
  80. assert( result3.empty?, 'check collections - recall' )
  81. # change collection
  82. group = Group.first
  83. group.touch
  84. sleep 4
  85. # get whole collections
  86. result1 = collection_client1.push
  87. assert( result1, 'check collections - after touch' )
  88. assert( check_if_collection_exists(result1, :Group), 'check collections - after touch' )
  89. sleep 0.1
  90. result2 = collection_client2.push
  91. assert( result2, 'check collections - after touch' )
  92. assert( check_if_collection_exists(result2, :Group), 'check collections - after touch' )
  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(999_999).to_s, active: true, member_ids: [customer1.id] )
  98. sleep 4
  99. # get whole collections
  100. result1 = collection_client1.push
  101. assert( result1, 'check collections - after create' )
  102. assert( check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] } ), 'check collections - after create with attributes' )
  103. sleep 0.3
  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. # user has no organization, so collection should be empty
  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 1
  113. result1 = collection_client1.push
  114. assert( result1.empty?, 'check collections - recall' )
  115. result2 = collection_client2.push
  116. assert( result2.empty?, 'check collections - recall' )
  117. result3 = collection_client3.push
  118. assert( result3.empty?, 'check collections - recall' )
  119. end
  120. def check_if_collection_exists(results, collection, attributes = nil)
  121. results.each {|result|
  122. next if !result
  123. if result[:collection] && result[:collection][collection]
  124. # check just if collection exists
  125. return true if !attributes
  126. # check if objetc with attributes in collection exists
  127. result[:collection][collection].each {|item|
  128. match_all = true
  129. attributes.each {|key, value|
  130. if item[ key.to_s ] != value
  131. match_all = false
  132. end
  133. }
  134. return true if match_all
  135. }
  136. end
  137. }
  138. nil
  139. end
  140. end