session_collections_test.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. sleep 1
  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, result2, 'check collections')
  68. result3 = collection_client3.push
  69. assert(result3, 'check collections')
  70. assert(check_if_collection_exists(result3, :Group), 'check collections - after init')
  71. assert(check_if_collection_exists(result3, :Role), 'check collections - after init')
  72. assert(!check_if_collection_exists(result3, :Signature), 'check collections - after init')
  73. assert(!check_if_collection_exists(result3, :EmailAddress), 'check collections - after init')
  74. # next check should be empty
  75. result1 = collection_client1.push
  76. assert(result1.empty?, 'check collections - recall')
  77. sleep 0.4
  78. result2 = collection_client2.push
  79. assert(result2.empty?, 'check collections - recall')
  80. result3 = collection_client3.push
  81. assert(result3.empty?, 'check collections - recall')
  82. # change collection
  83. group = Group.first
  84. group.touch
  85. sleep 3
  86. # get whole collections
  87. result1 = collection_client1.push
  88. assert(result1, 'check collections - after touch')
  89. assert(check_if_collection_exists(result1, :Group), 'check collections - after touch')
  90. sleep 0.1
  91. result2 = collection_client2.push
  92. assert(result2, 'check collections - after touch')
  93. assert(check_if_collection_exists(result2, :Group), 'check collections - after touch')
  94. result3 = collection_client3.push
  95. assert(result3, 'check collections - after touch')
  96. assert(check_if_collection_exists(result3, :Group), 'check collections - after touch')
  97. # next check should be empty
  98. sleep 0.5
  99. result1 = collection_client1.push
  100. assert(result1.empty?, 'check collections - recall')
  101. result2 = collection_client2.push
  102. assert(result2.empty?, 'check collections - recall')
  103. result3 = collection_client3.push
  104. assert(result3.empty?, 'check collections - recall')
  105. end
  106. def check_if_collection_exists(results, collection, attributes = nil)
  107. results.each {|result|
  108. next if !result
  109. next if !result[:collection]
  110. next if !result[:collection][collection]
  111. # check just if collection exists
  112. return true if !attributes
  113. # check if objetc with attributes in collection exists
  114. result[:collection][collection].each {|item|
  115. match_all = true
  116. attributes.each {|key, value|
  117. # sort array, database result maybe unsorted
  118. item_attributes = item[ key.to_s ]
  119. if item[ key.to_s ].class == Array
  120. item_attributes.sort!
  121. end
  122. if value.class == Array
  123. value.sort!
  124. end
  125. # compare values
  126. if item_attributes != value
  127. #p "FAILED: #{key} -> #{item_attributes.inspect} vs. #{value.inspect}"
  128. match_all = false
  129. end
  130. }
  131. return true if match_all
  132. }
  133. }
  134. nil
  135. end
  136. end