session_enhanced_test.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class SessionEnhancedTest < ActiveSupport::TestCase
  4. test 'check clients and send messages' do
  5. # create users
  6. roles = Role.where(name: ['Agent'])
  7. groups = Group.all
  8. UserInfo.current_user_id = 1
  9. agent1 = User.create_or_update(
  10. login: 'session-agent-1',
  11. firstname: 'Session',
  12. lastname: 'Agent 1',
  13. email: 'session-agent1@example.com',
  14. password: 'agentpw',
  15. active: true,
  16. roles: roles,
  17. groups: groups,
  18. )
  19. agent1.save!
  20. agent2 = User.create_or_update(
  21. login: 'session-agent-2',
  22. firstname: 'Session',
  23. lastname: 'Agent 2',
  24. email: 'session-agent2@example.com',
  25. password: 'agentpw',
  26. active: true,
  27. roles: roles,
  28. groups: groups,
  29. )
  30. agent2.save!
  31. agent3 = User.create_or_update(
  32. login: 'session-agent-3',
  33. firstname: 'Session',
  34. lastname: 'Agent 3',
  35. email: 'session-agent3@example.com',
  36. password: 'agentpw',
  37. active: true,
  38. roles: roles,
  39. groups: groups,
  40. )
  41. agent3.save!
  42. # create sessions
  43. client_id1 = 'a1234'
  44. client_id2 = 'a123456'
  45. client_id3 = 'aabc'
  46. Sessions.destroy(client_id1)
  47. Sessions.destroy(client_id2)
  48. Sessions.destroy(client_id3)
  49. Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
  50. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  51. Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })
  52. # check if session exists
  53. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  54. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  55. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  56. # check if session still exists after idle cleanup
  57. sleep 1
  58. Sessions.destroy_idle_sessions(3)
  59. assert(Sessions.session_exists?(client_id1), 'check if session exists after 1 sec')
  60. assert(Sessions.session_exists?(client_id2), 'check if session exists after 1 sec')
  61. assert(Sessions.session_exists?(client_id3), 'check if session exists after 1 sec')
  62. # check if session still exists after idle cleanup with touched sessions
  63. sleep 4
  64. Sessions.touch(client_id1)
  65. Sessions.touch(client_id2)
  66. Sessions.touch(client_id3)
  67. Sessions.destroy_idle_sessions(3)
  68. assert(Sessions.session_exists?(client_id1), 'check if session exists after touch')
  69. assert(Sessions.session_exists?(client_id2), 'check if session exists after touch')
  70. assert(Sessions.session_exists?(client_id3), 'check if session exists after touch')
  71. # check session data
  72. data = Sessions.get(client_id1)
  73. assert(data[:meta], 'check if meta exists')
  74. assert(data[:user], 'check if user exists')
  75. assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
  76. data = Sessions.get(client_id2)
  77. assert(data[:meta], 'check if meta exists')
  78. assert(data[:user], 'check if user exists')
  79. assert_equal(data[:user]['id'], agent2.id, 'check if user id is correct')
  80. data = Sessions.get(client_id3)
  81. assert(data[:meta], 'check if meta exists')
  82. assert(data[:user], 'check if user exists')
  83. assert_equal(data[:user]['id'], agent3.id, 'check if user id is correct')
  84. # send data to one client
  85. Sessions.send(client_id1, { msg: 'äöüß123' })
  86. Sessions.send(client_id1, { msg: 'äöüß1234' })
  87. messages = Sessions.queue(client_id1)
  88. assert_equal(3, messages.count, 'messages count')
  89. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  90. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  91. assert_equal('äöüß123', messages[1]['msg'], 'messages 2')
  92. assert_equal('äöüß1234', messages[2]['msg'], 'messages 3')
  93. messages = Sessions.queue(client_id2)
  94. assert_equal(messages.count, 1, 'messages count')
  95. assert_equal('ws:login', messages[0]['event'], 'messages 1')
  96. assert_equal(true, messages[0]['data']['success'], 'messages 1')
  97. messages = Sessions.queue(client_id3)
  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. # broadcast to all clients
  102. Sessions.broadcast({ msg: 'ooo123123123123123123' })
  103. messages = Sessions.queue(client_id1)
  104. assert_equal(messages.count, 1, 'messages count')
  105. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  106. messages = Sessions.queue(client_id2)
  107. assert_equal(messages.count, 1, 'messages count')
  108. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  109. messages = Sessions.queue(client_id3)
  110. assert_equal(messages.count, 1, 'messages count')
  111. assert_equal('ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1')
  112. # send dedicated message to user
  113. Sessions.send_to(agent1.id, { msg: 'ooo1231231231231231234' })
  114. messages = Sessions.queue(client_id1)
  115. assert_equal(messages.count, 1, 'messages count')
  116. assert_equal('ooo1231231231231231234', messages[0]['msg'], 'messages send 1')
  117. messages = Sessions.queue(client_id2)
  118. assert_equal(messages.count, 0, 'messages count')
  119. messages = Sessions.queue(client_id3)
  120. assert_equal(messages.count, 0, 'messages count')
  121. # start jobs
  122. jobs = Thread.new do
  123. Sessions.jobs
  124. end
  125. sleep 6
  126. # check client threads
  127. assert(Sessions.thread_client_exists?(client_id1), 'check if client is running')
  128. assert(Sessions.thread_client_exists?(client_id2), 'check if client is running')
  129. assert(Sessions.thread_client_exists?(client_id3), 'check if client is running')
  130. # check if session still exists after idle cleanup
  131. travel 10.seconds
  132. Sessions.destroy_idle_sessions(2)
  133. travel 2.seconds
  134. # check client sessions
  135. assert_not(Sessions.session_exists?(client_id1), 'check if session is removed')
  136. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  137. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  138. sleep 6
  139. # check client threads
  140. assert_not(Sessions.thread_client_exists?(client_id1), 'check if client is running')
  141. assert_not(Sessions.thread_client_exists?(client_id2), 'check if client is running')
  142. assert_not(Sessions.thread_client_exists?(client_id3), 'check if client is running')
  143. # exit jobs
  144. jobs.exit
  145. jobs.join
  146. travel_back
  147. end
  148. test 'check client and backends' do
  149. # create users
  150. roles = Role.where(name: ['Agent'])
  151. groups = Group.all
  152. organization = Organization.create(
  153. name: "SomeOrg::#{SecureRandom.uuid}", active: true,
  154. updated_by_id: 1,
  155. created_by_id: 1,
  156. )
  157. UserInfo.current_user_id = 1
  158. agent1 = User.create_or_update(
  159. login: 'session-agent-1',
  160. firstname: 'Session',
  161. lastname: 'Agent 1',
  162. email: 'session-agent1@example.com',
  163. password: 'agentpw',
  164. active: true,
  165. organization: organization,
  166. roles: roles,
  167. groups: groups,
  168. )
  169. agent1.save!
  170. agent2 = User.create_or_update(
  171. login: 'session-agent-2',
  172. firstname: 'Session',
  173. lastname: 'Agent 2',
  174. email: 'session-agent2@example.com',
  175. password: 'agentpw',
  176. active: true,
  177. organization: organization,
  178. roles: roles,
  179. groups: groups,
  180. )
  181. agent2.save!
  182. agent3 = User.create_or_update(
  183. login: 'session-agent-3',
  184. firstname: 'Session',
  185. lastname: 'Agent 3',
  186. email: 'session-agent3@example.com',
  187. password: 'agentpw',
  188. active: true,
  189. organization: organization,
  190. roles: roles,
  191. groups: groups,
  192. )
  193. agent3.save!
  194. # create sessions
  195. client_id1_0 = 'b1234-1'
  196. client_id1_1 = 'b1234-2'
  197. client_id2 = 'b123456'
  198. client_id3 = 'c123456'
  199. Sessions.destroy(client_id1_0)
  200. Sessions.destroy(client_id1_1)
  201. Sessions.destroy(client_id2)
  202. Sessions.destroy(client_id3)
  203. # start jobs
  204. jobs = Thread.new do
  205. Sessions.jobs
  206. end
  207. sleep 5
  208. Sessions.create(client_id1_0, agent1.attributes, { type: 'websocket' })
  209. sleep 6.5
  210. Sessions.create(client_id1_1, agent1.attributes, { type: 'websocket' })
  211. sleep 3.2
  212. Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
  213. sleep 3.2
  214. Sessions.create(client_id3, agent3.attributes, { type: 'websocket' })
  215. # check if session exists
  216. assert(Sessions.session_exists?(client_id1_0), 'check if session exists')
  217. assert(Sessions.session_exists?(client_id1_1), 'check if session exists')
  218. assert(Sessions.session_exists?(client_id2), 'check if session exists')
  219. assert(Sessions.session_exists?(client_id3), 'check if session exists')
  220. # check if session still exists after idle cleanup
  221. travel 10.seconds
  222. Sessions.destroy_idle_sessions(2)
  223. travel 2.seconds
  224. # check client sessions
  225. assert_not(Sessions.session_exists?(client_id1_0), 'check if session is removed')
  226. assert_not(Sessions.session_exists?(client_id1_1), 'check if session is removed')
  227. assert_not(Sessions.session_exists?(client_id2), 'check if session is removed')
  228. assert_not(Sessions.session_exists?(client_id3), 'check if session is removed')
  229. # exit jobs
  230. jobs.exit
  231. jobs.join
  232. travel_back
  233. end
  234. end