session_basic_test.rb 9.0 KB

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