session_basic_test.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class SessionBasicTest < ActiveSupport::TestCase
  4. test 'a cache' do
  5. Sessions::CacheIn.set( 'last_run_test' , true, { :expires_in => 2.seconds } )
  6. result = Sessions::CacheIn.get( 'last_run_test' )
  7. assert_equal( true, result, "check 1" )
  8. # should not be expired
  9. result = Sessions::CacheIn.expired( 'last_run_test' )
  10. assert_equal( false, result, "check 1 - expired" )
  11. # should be expired
  12. sleep 3
  13. result = Sessions::CacheIn.expired( 'last_run_test' )
  14. assert_equal( true, result, "check 1 - expired" )
  15. # renew expire
  16. result = Sessions::CacheIn.get( 'last_run_test', :re_expire => true )
  17. assert_equal( true, result, "check 1 - re_expire" )
  18. # should not be expired
  19. result = Sessions::CacheIn.expired( 'last_run_test' )
  20. assert_equal( false, result, "check 1 - expired" )
  21. # ignore expired
  22. sleep 3
  23. result = Sessions::CacheIn.get( 'last_run_test', :ignore_expire => true )
  24. assert_equal( true, result, "check 1 - ignore_expire" )
  25. # should be expired
  26. result = Sessions::CacheIn.expired( 'last_run_test' )
  27. assert_equal( true, result, "check 2" )
  28. result = Sessions::CacheIn.get( 'last_run_test' )
  29. assert_equal( nil, result, "check 2" )
  30. # check delete cache
  31. Sessions::CacheIn.set( 'last_run_delete' , true, { :expires_in => 5.seconds } )
  32. result = Sessions::CacheIn.get( 'last_run_delete' )
  33. assert_equal( true, result, "check 1" )
  34. Sessions::CacheIn.delete( 'last_run_delete' )
  35. result = Sessions::CacheIn.get( 'last_run_delete' )
  36. assert_equal( nil, nil, "check delete" )
  37. end
  38. test 'b collections group' do
  39. require 'sessions/backend/collections/group.rb'
  40. UserInfo.current_user_id = 1
  41. user = User.lookup(:id => 1)
  42. collection_client1 = Sessions::Backend::Collections::Group.new(user, false, '123-1')
  43. collection_client2 = Sessions::Backend::Collections::Group.new(user, false, '234-2')
  44. # get whole collections
  45. result1 = collection_client1.push
  46. assert( !result1.empty?, "check collections" )
  47. sleep 1
  48. result2 = collection_client2.push
  49. assert( !result2.empty?, "check collections" )
  50. assert_equal( result1, result2, "check collections" )
  51. # next check should be empty
  52. result1 = collection_client1.push
  53. assert( !result1, "check collections - recall" )
  54. sleep 1
  55. result2 = collection_client2.push
  56. assert( !result2, "check collections - recall" )
  57. # change collection
  58. group = Group.first
  59. group.touch
  60. sleep 16
  61. # get whole collections
  62. result1 = collection_client1.push
  63. assert( !result1.empty?, "check collections - after touch" )
  64. sleep 1
  65. result2 = collection_client2.push
  66. assert( !result2.empty?, "check collections - after touch" )
  67. assert_equal( result1, result2, "check collections" )
  68. # check again after touch
  69. result1 = collection_client1.push
  70. assert( !result1, "check collections - after touch - recall" )
  71. sleep 1
  72. result2 = collection_client2.push
  73. assert( !result2, "check collections - after touch - recall" )
  74. assert_equal( result1, result2, "check collections" )
  75. # change collection
  76. group = Group.create( :name => 'SomeGroup::' + rand(999999).to_s, :active => true )
  77. sleep 12
  78. # get whole collections
  79. result1 = collection_client1.push
  80. assert( !result1.empty?, "check collections - after create" )
  81. sleep 1
  82. result2 = collection_client2.push
  83. assert( !result2.empty?, "check collections - after create" )
  84. assert_equal( result1, result2, "check collections" )
  85. # check again after create
  86. sleep 14
  87. result1 = collection_client1.push
  88. assert( !result1, "check collections - after create - recall" )
  89. sleep 1
  90. result2 = collection_client2.push
  91. assert( !result2, "check collections - after create - recall" )
  92. assert_equal( result1, result2, "check collections" )
  93. # change collection
  94. group.destroy
  95. sleep 14
  96. # get whole collections
  97. result1 = collection_client1.push
  98. assert( !result1.empty?, "check collections - after destroy" )
  99. sleep 1
  100. result2 = collection_client2.push
  101. assert( !result2.empty?, "check collections - after destroy" )
  102. assert_equal( result1, result2, "check collections" )
  103. # check again after destroy
  104. sleep 12
  105. result1 = collection_client1.push
  106. assert( !result1, "check collections - after destroy - recall" )
  107. sleep 1
  108. result2 = collection_client2.push
  109. assert( !result2, "check collections - after destroy - recall" )
  110. assert_equal( result1, result2, "check collections" )
  111. end
  112. user = User.lookup(:id => 1)
  113. roles = Role.where( :name => [ 'Agent', 'Admin'] )
  114. user.roles = roles
  115. user.save
  116. test 'b collections organization' do
  117. require 'sessions/backend/collections/organization.rb'
  118. UserInfo.current_user_id = 1
  119. user = User.lookup(:id => 1)
  120. org = Organization.create( :name => 'SomeOrg1::' + rand(999999).to_s, :active => true )
  121. collection_client1 = Sessions::Backend::Collections::Organization.new(user, false, '123-1')
  122. collection_client2 = Sessions::Backend::Collections::Organization.new(user, false, '234-2')
  123. # get whole collections - should be nil, no org exists!
  124. result1 = collection_client1.push
  125. assert( !result1.empty?, "check collections" )
  126. sleep 1
  127. result2 = collection_client2.push
  128. assert( !result2.empty?, "check collections" )
  129. assert_equal( result1, result2, "check collections" )
  130. # next check - should still be nil, no org exists!
  131. result1 = collection_client1.push
  132. assert( !result1, "check collections - recall" )
  133. sleep 1
  134. result2 = collection_client2.push
  135. assert( !result2, "check collections - recall" )
  136. # change collection
  137. org = Organization.create( :name => 'SomeOrg2::' + rand(999999).to_s, :active => true )
  138. sleep 16
  139. # get whole collections
  140. result1 = collection_client1.push
  141. assert( !result1.empty?, "check collections - after create" )
  142. sleep 1
  143. result2 = collection_client2.push
  144. assert( !result2.empty?, "check collections - after create" )
  145. assert_equal( result1, result2, "check collections" )
  146. sleep 16
  147. # next check should be empty
  148. result1 = collection_client1.push
  149. assert( !result1, "check collections - after create recall" )
  150. result2 = collection_client2.push
  151. assert( !result2, "check collections - after create recall" )
  152. organization = Organization.first
  153. organization.touch
  154. sleep 16
  155. # get whole collections
  156. result1 = collection_client1.push
  157. assert( !result1.empty?, "check collections - after touch" )
  158. result2 = collection_client2.push
  159. assert( !result1.empty?, "check collections - after touch" )
  160. assert_equal( result1, result2, "check collections" )
  161. end
  162. test 'b rss' do
  163. user = User.lookup(:id => 1)
  164. collection_client1 = Sessions::Backend::Rss.new(user, false, '123-1')
  165. # get whole collections
  166. result1 = collection_client1.push
  167. #puts "RSS1: #{result1.inspect}"
  168. assert( !result1.empty?, "check rss" )
  169. sleep 1
  170. # next check should be empty
  171. result1 = collection_client1.push
  172. #puts "R1: #{result1.inspect}"
  173. assert( !result1, "check rss - recall" )
  174. end
  175. test 'b activity stream' do
  176. UserInfo.current_user_id = 1
  177. # create users
  178. roles = Role.where( :name => [ 'Agent', 'Admin'] )
  179. groups = Group.all
  180. UserInfo.current_user_id = 1
  181. agent1 = User.create_or_update(
  182. :login => 'activity-stream-agent-1',
  183. :firstname => 'Session',
  184. :lastname => 'activity stream ' + rand(99999).to_s,
  185. :email => 'activity-stream-agent1@example.com',
  186. :password => 'agentpw',
  187. :active => true,
  188. :roles => roles,
  189. :groups => groups,
  190. )
  191. agent1.roles = roles
  192. agent1.save
  193. as_client1 = Sessions::Backend::ActivityStream.new(agent1, false, '123-1')
  194. # get as stream
  195. result1 = as_client1.push
  196. assert( result1, "check as" )
  197. sleep 1
  198. # next check should be empty
  199. result1 = as_client1.push
  200. assert( !result1, "check as - recall" )
  201. # next check should be empty
  202. sleep 60
  203. result1 = as_client1.push
  204. assert( !result1, "check as - recall 2" )
  205. agent1.update_attribute( :email, 'activity-stream-agent11@example.com' )
  206. ticket = Ticket.create(:title => '12323', :group_id => 1, :priority_id => 1, :state_id => 1, :customer_id => 1 )
  207. sleep 32
  208. # get as stream
  209. result1 = as_client1.push
  210. assert( result1, "check as - recall 3" )
  211. end
  212. test 'b ticket_create' do
  213. UserInfo.current_user_id = 1
  214. user = User.lookup(:id => 1)
  215. ticket_create_client1 = Sessions::Backend::TicketCreate.new(user, false, '123-1')
  216. # get as stream
  217. result1 = ticket_create_client1.push
  218. assert( result1, "check ticket_create" )
  219. sleep 1
  220. # next check should be empty
  221. result1 = ticket_create_client1.push
  222. assert( !result1, "check ticket_create - recall" )
  223. # next check should be empty
  224. sleep 10
  225. result1 = ticket_create_client1.push
  226. assert( !result1, "check ticket_create - recall 2" )
  227. Group.create( :name => 'SomeTicketCreateGroup::' + rand(999999).to_s, :active => true )
  228. sleep 26
  229. # get as stream
  230. result1 = ticket_create_client1.push
  231. assert( result1, "check ticket_create - recall 3" )
  232. end
  233. end