session_collections_test.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. organization_id: nil,
  43. active: true,
  44. roles: roles,
  45. )
  46. customer1.roles = roles
  47. customer1.save
  48. collection_client1 = Sessions::Backend::Collections.new(agent1, {}, nil, 'aaa-1', 3)
  49. collection_client2 = Sessions::Backend::Collections.new(agent2, {}, nil, 'bbb-2', 3)
  50. collection_client3 = Sessions::Backend::Collections.new(customer1, {}, nil, 'ccc-2', 3)
  51. # get whole collections
  52. result1 = collection_client1.push
  53. assert(result1, 'check collections')
  54. assert(check_if_collection_exists(result1, :Group), 'check collections - after init')
  55. assert(check_if_collection_exists(result1, :Role), 'check collections - after init')
  56. assert(check_if_collection_exists(result1, :Signature), 'check collections - after init')
  57. assert(check_if_collection_exists(result1, :EmailAddress), 'check collections - after init')
  58. sleep 1
  59. result2 = collection_client2.push
  60. assert(result2, 'check collections')
  61. assert(check_if_collection_exists(result2, :Group), 'check collections - after init')
  62. assert(check_if_collection_exists(result2, :Role), 'check collections - after init')
  63. assert(check_if_collection_exists(result2, :Signature), 'check collections - after init')
  64. assert(check_if_collection_exists(result2, :EmailAddress), 'check collections - after init')
  65. assert_equal(result1, result2, 'check collections')
  66. result3 = collection_client3.push
  67. assert(result3, 'check collections')
  68. assert(check_if_collection_exists(result3, :Group), 'check collections - after init')
  69. assert(check_if_collection_exists(result3, :Role), 'check collections - after init')
  70. assert(!check_if_collection_exists(result3, :Signature), 'check collections - after init')
  71. assert(!check_if_collection_exists(result3, :EmailAddress), 'check collections - after init')
  72. # next check should be empty
  73. result1 = collection_client1.push
  74. assert(result1.empty?, 'check collections - recall')
  75. sleep 0.4
  76. result2 = collection_client2.push
  77. assert(result2.empty?, 'check collections - recall')
  78. result3 = collection_client3.push
  79. assert(result3.empty?, 'check collections - recall')
  80. # change collection
  81. group = Group.first
  82. group.touch
  83. sleep 4
  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 0.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. result3 = collection_client3.push
  93. assert(result3, 'check collections - after touch')
  94. assert(check_if_collection_exists(result3, :Group), 'check collections - after touch')
  95. # change collection
  96. org = Organization.create(name: 'SomeOrg::' + rand(999_999).to_s, active: true, member_ids: [customer1.id])
  97. sleep 4
  98. # get whole collections
  99. result1 = collection_client1.push
  100. assert(result1, 'check collections - after create')
  101. assert(check_if_collection_exists(result1, :Organization, { id: org.id, member_ids: [customer1.id] }), 'check collections - after create with attributes')
  102. sleep 0.3
  103. result2 = collection_client2.push
  104. assert(result2, 'check collections - after create')
  105. assert(check_if_collection_exists(result2, :Organization), 'check collections - after create')
  106. # user has no organization, so collection should be empty
  107. result3 = collection_client3.push
  108. assert(result3, 'check collections - after create')
  109. assert(!check_if_collection_exists(result3, :Organization), 'check collections - after create')
  110. # next check should be empty
  111. sleep 1
  112. result1 = collection_client1.push
  113. assert(result1.empty?, 'check collections - recall')
  114. result2 = collection_client2.push
  115. assert(result2.empty?, 'check collections - recall')
  116. result3 = collection_client3.push
  117. assert(result3.empty?, 'check collections - recall')
  118. end
  119. def check_if_collection_exists(results, collection, attributes = nil)
  120. results.each {|result|
  121. next if !result
  122. next if !result[:collection]
  123. next if !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. }
  137. nil
  138. end
  139. end