session_basic_test.rb 8.4 KB

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