session_basic_test.rb 7.8 KB

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