session_basic_test.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 activity stream' do
  44. # create users
  45. roles = Role.where(name: %w[Agent Admin])
  46. groups = Group.all
  47. agent1 = User.create_or_update(
  48. login: 'activity-stream-agent-1',
  49. firstname: 'Session',
  50. lastname: "activity stream #{rand(99_999)}",
  51. email: 'activity-stream-agent1@example.com',
  52. password: 'agentpw',
  53. active: true,
  54. roles: roles,
  55. groups: groups,
  56. updated_by_id: 1,
  57. created_by_id: 1,
  58. )
  59. # create min. on activity record
  60. random_name = "Random:#{rand(9_999_999_999)}"
  61. Group.create_or_update(
  62. name: random_name,
  63. updated_by_id: 1,
  64. created_by_id: 1,
  65. )
  66. as_client1 = Sessions::Backend::ActivityStream.new(agent1, {}, false, '123-1', 3)
  67. # get as stream
  68. result1 = as_client1.push
  69. assert(result1, 'check as agent1')
  70. travel 1.second
  71. # next check should be empty
  72. result1 = as_client1.push
  73. assert_not(result1, 'check as agent1 - recall')
  74. # next check should be empty
  75. travel 4.seconds
  76. result1 = as_client1.push
  77. assert_not(result1, 'check as agent1 - recall 2')
  78. agent1.update!(email: 'activity-stream-agent11@example.com')
  79. Ticket.create!(
  80. title: '12323',
  81. group_id: 1,
  82. priority_id: 1,
  83. state_id: 1,
  84. customer_id: 1,
  85. updated_by_id: 1,
  86. created_by_id: 1,
  87. )
  88. travel 4.seconds
  89. # get as stream
  90. result1 = as_client1.push
  91. assert( result1, 'check as agent1 - recall 3')
  92. travel_back
  93. end
  94. end