session_basic_test.rb 8.4 KB

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