session_enhanced_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # encoding: utf-8
  2. # rubocop:disable Next, UselessAssignment
  3. require 'test_helper'
  4. class SessionEnhancedTest < ActiveSupport::TestCase
  5. test 'a check clients and send messages' do
  6. # create users
  7. roles = Role.where( name: [ 'Agent'] )
  8. groups = Group.all
  9. UserInfo.current_user_id = 1
  10. agent1 = User.create_or_update(
  11. login: 'session-agent-1',
  12. firstname: 'Session',
  13. lastname: 'Agent 1',
  14. email: 'session-agent1@example.com',
  15. password: 'agentpw',
  16. active: true,
  17. roles: roles,
  18. groups: groups,
  19. )
  20. agent1.roles = roles
  21. agent1.save
  22. agent2 = User.create_or_update(
  23. login: 'session-agent-2',
  24. firstname: 'Session',
  25. lastname: 'Agent 2',
  26. email: 'session-agent2@example.com',
  27. password: 'agentpw',
  28. active: true,
  29. roles: roles,
  30. groups: groups,
  31. )
  32. agent2.roles = roles
  33. agent2.save
  34. agent3 = User.create_or_update(
  35. login: 'session-agent-3',
  36. firstname: 'Session',
  37. lastname: 'Agent 3',
  38. email: 'session-agent3@example.com',
  39. password: 'agentpw',
  40. active: true,
  41. roles: roles,
  42. groups: groups,
  43. )
  44. agent3.roles = roles
  45. agent3.save
  46. # create sessions
  47. client_id1 = '1234'
  48. client_id2 = '123456'
  49. client_id3 = 'abc'
  50. Sessions.destory(client_id1)
  51. Sessions.destory(client_id2)
  52. Sessions.destory(client_id3)
  53. Sessions.create( client_id1, agent1.attributes, { type: 'websocket' } )
  54. Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } )
  55. Sessions.create( client_id3, agent3.attributes, { type: 'ajax' } )
  56. # check if session exists
  57. assert( Sessions.session_exists?(client_id1), 'check if session exists' )
  58. assert( Sessions.session_exists?(client_id2), 'check if session exists' )
  59. assert( Sessions.session_exists?(client_id3), 'check if session exists' )
  60. # check if session still exists after idle cleanup
  61. sleep 1
  62. Sessions.destory_idle_sessions(5)
  63. assert( Sessions.session_exists?(client_id1), 'check if session exists after 1 sec' )
  64. assert( Sessions.session_exists?(client_id2), 'check if session exists after 1 sec' )
  65. assert( Sessions.session_exists?(client_id3), 'check if session exists after 1 sec' )
  66. # check if session still exists after idle cleanup with touched sessions
  67. sleep 6
  68. Sessions.touch(client_id1)
  69. Sessions.touch(client_id2)
  70. Sessions.touch(client_id3)
  71. Sessions.destory_idle_sessions(5)
  72. assert( Sessions.session_exists?(client_id1), 'check if session exists after touch' )
  73. assert( Sessions.session_exists?(client_id2), 'check if session exists after touch' )
  74. assert( Sessions.session_exists?(client_id3), 'check if session exists after touch' )
  75. # check session data
  76. data = Sessions.get(client_id1)
  77. assert( data[:meta], 'check if meta exists' )
  78. assert( data[:user], 'check if user exists' )
  79. assert_equal( data[:user]['id'], agent1.id, 'check if user id is correct' )
  80. data = Sessions.get(client_id2)
  81. assert( data[:meta], 'check if meta exists' )
  82. assert( data[:user], 'check if user exists' )
  83. assert_equal( data[:user]['id'], agent2.id, 'check if user id is correct' )
  84. data = Sessions.get(client_id3)
  85. assert( data[:meta], 'check if meta exists' )
  86. assert( data[:user], 'check if user exists' )
  87. assert_equal( data[:user]['id'], agent3.id, 'check if user id is correct' )
  88. # send data to one client
  89. Sessions.send( client_id1, { msg: 'äöüß123' } )
  90. Sessions.send( client_id1, { msg: 'äöüß1234' } )
  91. messages = Sessions.queue(client_id1)
  92. assert_equal( 3, messages.count, 'messages count')
  93. assert_equal( 'ws:login', messages[0]['event'], 'messages 1')
  94. assert_equal( true, messages[0]['data']['success'], 'messages 1')
  95. assert_equal( 'äöüß123', messages[1]['msg'], 'messages 2')
  96. assert_equal( 'äöüß1234', messages[2]['msg'], 'messages 3')
  97. messages = Sessions.queue(client_id2)
  98. assert_equal( messages.count, 1, 'messages count')
  99. assert_equal( 'ws:login', messages[0]['event'], 'messages 1')
  100. assert_equal( true, messages[0]['data']['success'], 'messages 1')
  101. messages = Sessions.queue(client_id3)
  102. assert_equal( messages.count, 1, 'messages count')
  103. assert_equal( 'ws:login', messages[0]['event'], 'messages 1')
  104. assert_equal( true, messages[0]['data']['success'], 'messages 1')
  105. # broadcast to all clients
  106. Sessions.broadcast( { msg: 'ooo123123123123123123' } )
  107. messages = Sessions.queue(client_id1)
  108. assert_equal( messages.count, 1, 'messages count')
  109. assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  110. messages = Sessions.queue(client_id2)
  111. assert_equal( messages.count, 1, 'messages count')
  112. assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  113. messages = Sessions.queue(client_id3)
  114. assert_equal( messages.count, 1, 'messages count')
  115. assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  116. # send dedicated message to user
  117. Sessions.send_to( agent1.id, { msg: 'ooo1231231231231231234' } )
  118. messages = Sessions.queue(client_id1)
  119. assert_equal( messages.count, 1, 'messages count')
  120. assert_equal( 'ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
  121. messages = Sessions.queue(client_id2)
  122. assert_equal( messages.count, 0, 'messages count')
  123. messages = Sessions.queue(client_id3)
  124. assert_equal( messages.count, 0, 'messages count')
  125. # start jobs
  126. jobs = Thread.new {
  127. Sessions.jobs
  128. }
  129. sleep 5
  130. #jobs.join
  131. # check client threads
  132. assert( Sessions.thread_client_exists?(client_id1), 'check if client is running' )
  133. assert( Sessions.thread_client_exists?(client_id2), 'check if client is running' )
  134. assert( Sessions.thread_client_exists?(client_id3), 'check if client is running' )
  135. # check if session still exists after idle cleanup
  136. sleep 6
  137. client_ids = Sessions.destory_idle_sessions(5)
  138. # check client sessions
  139. assert( !Sessions.session_exists?(client_id1), 'check if session is removed' )
  140. assert( !Sessions.session_exists?(client_id2), 'check if session is removed' )
  141. assert( !Sessions.session_exists?(client_id3), 'check if session is removed' )
  142. sleep 6
  143. # check client threads
  144. assert( !Sessions.thread_client_exists?(client_id1), 'check if client is running' )
  145. assert( !Sessions.thread_client_exists?(client_id2), 'check if client is running' )
  146. assert( !Sessions.thread_client_exists?(client_id3), 'check if client is running' )
  147. # exit jobs
  148. jobs.exit
  149. end
  150. test 'b check client and backends' do
  151. # create users
  152. roles = Role.where( name: [ 'Agent'] )
  153. groups = Group.all
  154. UserInfo.current_user_id = 1
  155. agent1 = User.create_or_update(
  156. login: 'session-agent-1',
  157. firstname: 'Session',
  158. lastname: 'Agent 1',
  159. email: 'session-agent1@example.com',
  160. password: 'agentpw',
  161. active: true,
  162. roles: roles,
  163. groups: groups,
  164. )
  165. agent1.roles = roles
  166. agent1.save
  167. agent2 = User.create_or_update(
  168. login: 'session-agent-2',
  169. firstname: 'Session',
  170. lastname: 'Agent 2',
  171. email: 'session-agent2@example.com',
  172. password: 'agentpw',
  173. active: true,
  174. roles: roles,
  175. groups: groups,
  176. )
  177. agent2.roles = roles
  178. agent2.save
  179. org = Organization.create( name: 'SomeOrg::' + rand(999_999).to_s, active: true )
  180. # create sessions
  181. client_id1_0 = '1234-1'
  182. client_id1_1 = '1234-2'
  183. client_id2 = '123456'
  184. Sessions.destory(client_id1_0)
  185. Sessions.destory(client_id1_1)
  186. Sessions.destory(client_id2)
  187. # start jobs
  188. jobs = Thread.new {
  189. Sessions.jobs
  190. }
  191. sleep 5
  192. Sessions.create( client_id1_0, agent1.attributes, { type: 'websocket' } )
  193. sleep 6.5
  194. Sessions.create( client_id1_1, agent1.attributes, { type: 'websocket' } )
  195. sleep 3.2
  196. Sessions.create( client_id2, agent2.attributes, { type: 'ajax' } )
  197. # check if session exists
  198. assert( Sessions.session_exists?(client_id1_0), 'check if session exists' )
  199. assert( Sessions.session_exists?(client_id1_1), 'check if session exists' )
  200. assert( Sessions.session_exists?(client_id2), 'check if session exists' )
  201. sleep 11
  202. # check collections
  203. collections = {
  204. 'Group' => true,
  205. 'Organization' => true,
  206. 'User' => nil,
  207. }
  208. check_if_collection_reset_message_exists(client_id1_0, collections, 'init')
  209. check_if_collection_reset_message_exists(client_id1_1, collections, 'init')
  210. check_if_collection_reset_message_exists(client_id2, collections, 'init')
  211. collections = {
  212. 'Group' => nil,
  213. 'Organization' => nil,
  214. 'User' => nil,
  215. }
  216. check_if_collection_reset_message_exists(client_id1_0, collections, 'init2')
  217. check_if_collection_reset_message_exists(client_id1_1, collections, 'init2')
  218. check_if_collection_reset_message_exists(client_id2, collections, 'init2')
  219. sleep 11
  220. collections = {
  221. 'Group' => nil,
  222. 'Organization' => nil,
  223. 'User' => nil,
  224. }
  225. check_if_collection_reset_message_exists(client_id1_0, collections, 'init3')
  226. check_if_collection_reset_message_exists(client_id1_1, collections, 'init3')
  227. check_if_collection_reset_message_exists(client_id2, collections, 'init3')
  228. # change collection
  229. group = Group.first
  230. group.touch
  231. sleep 11
  232. # check collections
  233. collections = {
  234. 'Group' => true,
  235. 'Organization' => nil,
  236. 'User' => nil,
  237. }
  238. check_if_collection_reset_message_exists(client_id1_0, collections, 'update')
  239. check_if_collection_reset_message_exists(client_id1_1, collections, 'update')
  240. check_if_collection_reset_message_exists(client_id2, collections, 'update')
  241. # check if session still exists after idle cleanup
  242. sleep 6
  243. client_ids = Sessions.destory_idle_sessions(5)
  244. # check client sessions
  245. assert( !Sessions.session_exists?(client_id1_0), 'check if session is removed' )
  246. assert( !Sessions.session_exists?(client_id1_1), 'check if session is removed' )
  247. assert( !Sessions.session_exists?(client_id2), 'check if session is removed' )
  248. end
  249. def check_if_collection_reset_message_exists(client_id, collections_orig, type)
  250. messages = Sessions.queue(client_id)
  251. #puts "cid: #{client_id}"
  252. #puts "m: #{messages.inspect}"
  253. collections_result = {}
  254. messages.each {|message|
  255. #puts ""
  256. #puts "message: #{message.inspect}"
  257. if message['event'] == 'resetCollection'
  258. #puts "rc: "
  259. if message['data']
  260. message['data'].each {|key, value|
  261. #puts "rc: #{key}"
  262. collections_result[key] = true
  263. }
  264. end
  265. end
  266. }
  267. #puts "c: #{collections_result.inspect}"
  268. collections_orig.each {|key, value|
  269. assert_equal( collections_orig[key], collections_result[key], "collection message for #{key} #{type}-check (client_id #{client_id})" )
  270. }
  271. end
  272. end