session_basic_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 'c session create / update' do
  39. # create users
  40. roles = Role.where(name: ['Agent'])
  41. groups = Group.all
  42. UserInfo.current_user_id = 1
  43. agent1 = User.create_or_update(
  44. login: 'session-agent-1',
  45. firstname: 'Session',
  46. lastname: 'Agent 1',
  47. email: 'session-agent1@example.com',
  48. password: 'agentpw',
  49. active: true,
  50. roles: roles,
  51. groups: groups,
  52. )
  53. agent1.roles = roles
  54. agent1.save
  55. # create sessions
  56. client_id1 = '123456789'
  57. Sessions.create(client_id1, {}, { type: 'websocket' })
  58. # check if session exists
  59. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  60. # check session data
  61. data = Sessions.get(client_id1)
  62. assert(data[:meta], 'check if meta exists')
  63. assert(data[:user], 'check if user exists')
  64. assert_equal(data[:user]['id'], nil, 'check if user id is correct')
  65. # recreate session
  66. Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
  67. # check if session exists
  68. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  69. # check session data
  70. data = Sessions.get(client_id1)
  71. assert(data[:meta], 'check if meta exists')
  72. assert(data[:user], 'check if user exists')
  73. assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
  74. # destroy session
  75. Sessions.destory(client_id1)
  76. # check if session exists
  77. assert(!Sessions.session_exists?(client_id1), 'check if session exists')
  78. end
  79. test 'c collections group' do
  80. require 'sessions/backend/collections/group.rb'
  81. UserInfo.current_user_id = 2
  82. user = User.lookup(id: 1)
  83. collection_client1 = Sessions::Backend::Collections::Group.new(user, {}, false, '123-1', 3)
  84. collection_client2 = Sessions::Backend::Collections::Group.new(user, {}, false, '234-2', 3)
  85. # get whole collections
  86. result1 = collection_client1.push
  87. assert(!result1.empty?, 'check collections')
  88. sleep 0.6
  89. result2 = collection_client2.push
  90. assert(!result2.empty?, 'check collections')
  91. assert_equal(result1, result2, 'check collections')
  92. # next check should be empty
  93. result1 = collection_client1.push
  94. assert(!result1, 'check collections - recall')
  95. sleep 1
  96. result2 = collection_client2.push
  97. assert(!result2, 'check collections - recall')
  98. # change collection
  99. group = Group.first
  100. group.touch
  101. sleep 4
  102. # get whole collections
  103. result1 = collection_client1.push
  104. assert(!result1.empty?, 'check collections - after touch')
  105. result2 = collection_client2.push
  106. assert(!result2.empty?, 'check collections - after touch')
  107. assert_equal(result1, result2, 'check collections')
  108. # check again after touch
  109. result1 = collection_client1.push
  110. assert(!result1, 'check collections - after touch - recall')
  111. result2 = collection_client2.push
  112. assert(!result2, 'check collections - after touch - recall')
  113. assert_equal(result1, result2, 'check collections')
  114. # change collection
  115. group = Group.create(name: 'SomeGroup::' + rand(999_999).to_s, active: true)
  116. sleep 4
  117. # get whole collections
  118. result1 = collection_client1.push
  119. assert(!result1.empty?, 'check collections - after create')
  120. result2 = collection_client2.push
  121. assert(!result2.empty?, 'check collections - after create')
  122. assert_equal(result1, result2, 'check collections')
  123. # check again after create
  124. sleep 4
  125. result1 = collection_client1.push
  126. assert(!result1, 'check collections - after create - recall')
  127. result2 = collection_client2.push
  128. assert(!result2, 'check collections - after create - recall')
  129. assert_equal(result1, result2, 'check collections')
  130. # change collection
  131. group.destroy
  132. sleep 4
  133. # get whole collections
  134. result1 = collection_client1.push
  135. assert(!result1.empty?, 'check collections - after destroy')
  136. result2 = collection_client2.push
  137. assert(!result2.empty?, 'check collections - after destroy')
  138. assert_equal(result1, result2, 'check collections')
  139. # check again after destroy
  140. sleep 4
  141. result1 = collection_client1.push
  142. assert(!result1, 'check collections - after destroy - recall')
  143. result2 = collection_client2.push
  144. assert(!result2, 'check collections - after destroy - recall')
  145. assert_equal(result1, result2, 'check collections')
  146. end
  147. user = User.lookup(id: 1)
  148. roles = Role.where(name: %w(Agent Admin))
  149. user.roles = roles
  150. user.save
  151. test 'c collections organization' do
  152. require 'sessions/backend/collections/organization.rb'
  153. UserInfo.current_user_id = 2
  154. user = User.lookup(id: 1)
  155. org = Organization.create( name: 'SomeOrg1::' + rand(999_999).to_s, active: true )
  156. collection_client1 = Sessions::Backend::Collections::Organization.new(user, {}, false, '123-1', 3)
  157. collection_client2 = Sessions::Backend::Collections::Organization.new(user, {}, false, '234-2', 3)
  158. # get whole collections - should be nil, no org exists!
  159. result1 = collection_client1.push
  160. assert(!result1.empty?, 'check collections')
  161. result2 = collection_client2.push
  162. assert(!result2.empty?, 'check collections')
  163. assert_equal(result1, result2, 'check collections')
  164. # next check - should still be nil, no org exists!
  165. result1 = collection_client1.push
  166. assert(!result1, 'check collections - recall')
  167. sleep 0.6
  168. result2 = collection_client2.push
  169. assert(!result2, 'check collections - recall')
  170. # change collection
  171. org = Organization.create(name: 'SomeOrg2::' + rand(999_999).to_s, active: true)
  172. sleep 4
  173. # get whole collections
  174. result1 = collection_client1.push
  175. assert(!result1.empty?, 'check collections - after create')
  176. result2 = collection_client2.push
  177. assert(!result2.empty?, 'check collections - after create')
  178. assert_equal(result1, result2, 'check collections')
  179. sleep 4
  180. # next check should be empty
  181. result1 = collection_client1.push
  182. assert(!result1, 'check collections - after create recall')
  183. result2 = collection_client2.push
  184. assert(!result2, 'check collections - after create recall')
  185. organization = Organization.first
  186. organization.touch
  187. sleep 4
  188. # get whole collections
  189. result1 = collection_client1.push
  190. assert(!result1.empty?, 'check collections - after touch')
  191. result2 = collection_client2.push
  192. assert(!result1.empty?, 'check collections - after touch')
  193. assert_equal(result1, result2, 'check collections')
  194. end
  195. test 'c rss' do
  196. user = User.lookup(id: 1)
  197. collection_client1 = Sessions::Backend::Rss.new(user, {}, false, '123-1')
  198. # get whole collections
  199. result1 = collection_client1.push
  200. #puts "RSS1: #{result1.inspect}"
  201. assert(!result1.empty?, 'check rss')
  202. sleep 1
  203. # next check should be empty
  204. result1 = collection_client1.push
  205. #puts "R1: #{result1.inspect}"
  206. assert(!result1, 'check rss - recall')
  207. end
  208. test 'c activity stream' do
  209. # create users
  210. roles = Role.where(name: %w(Agent Admin))
  211. groups = Group.all
  212. UserInfo.current_user_id = 2
  213. agent1 = User.create_or_update(
  214. login: 'activity-stream-agent-1',
  215. firstname: 'Session',
  216. lastname: 'activity stream ' + rand(99_999).to_s,
  217. email: 'activity-stream-agent1@example.com',
  218. password: 'agentpw',
  219. active: true,
  220. roles: roles,
  221. groups: groups,
  222. )
  223. agent1.roles = roles
  224. assert(agent1.save, 'create/update agent1')
  225. as_client1 = Sessions::Backend::ActivityStream.new(agent1, {}, false, '123-1', 3)
  226. # get as stream
  227. result1 = as_client1.push
  228. assert(result1, 'check as agent1')
  229. sleep 1
  230. # next check should be empty
  231. result1 = as_client1.push
  232. assert(!result1, 'check as agent1 - recall')
  233. # next check should be empty
  234. sleep 4
  235. result1 = as_client1.push
  236. assert(!result1, 'check as agent1 - recall 2')
  237. agent1.update_attribute(:email, 'activity-stream-agent11@example.com')
  238. ticket = Ticket.create(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
  239. sleep 4
  240. # get as stream
  241. result1 = as_client1.push
  242. assert( result1, 'check as agent1 - recall 3')
  243. end
  244. test 'c ticket_create' do
  245. UserInfo.current_user_id = 2
  246. user = User.lookup(id: 1)
  247. ticket_create_client1 = Sessions::Backend::TicketCreate.new(user, {}, false, '123-1', 3)
  248. # get as stream
  249. result1 = ticket_create_client1.push
  250. assert(result1, 'check ticket_create')
  251. sleep 0.6
  252. # next check should be empty
  253. result1 = ticket_create_client1.push
  254. assert(!result1, 'check ticket_create - recall')
  255. # next check should be empty
  256. sleep 0.6
  257. result1 = ticket_create_client1.push
  258. assert(!result1, 'check ticket_create - recall 2')
  259. Group.create(name: 'SomeTicketCreateGroup::' + rand(999_999).to_s, active: true)
  260. sleep 4
  261. # get as stream
  262. result1 = ticket_create_client1.push
  263. assert(result1, 'check ticket_create - recall 3')
  264. end
  265. end