session_basic_test.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class SessionBasicTest < ActiveSupport::TestCase
  4. test 'c session create / update' do
  5. # create users
  6. roles = Role.where(name: %w[Agent])
  7. groups = Group.all
  8. agent1 = User.create_or_update(
  9. login: 'session-agent-1',
  10. firstname: 'Session',
  11. lastname: 'Agent 1',
  12. email: 'session-agent1@example.com',
  13. password: 'agentpw',
  14. active: true,
  15. roles: roles,
  16. groups: groups,
  17. updated_by_id: 1,
  18. created_by_id: 1,
  19. )
  20. # create sessions
  21. client_id1 = '123456789'
  22. Sessions.create(client_id1, {}, { type: 'websocket' })
  23. # check if session exists
  24. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  25. # check session data
  26. data = Sessions.get(client_id1)
  27. assert(data[:meta], 'check if meta exists')
  28. assert(data[:user], 'check if user exists')
  29. assert_nil(data[:user]['id'], 'check if user id is correct')
  30. # recreate session
  31. Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
  32. # check if session exists
  33. assert(Sessions.session_exists?(client_id1), 'check if session exists')
  34. # check session data
  35. data = Sessions.get(client_id1)
  36. assert(data[:meta], 'check if meta exists')
  37. assert(data[:user], 'check if user exists')
  38. assert_equal(data[:user]['id'], agent1.id, 'check if user id is correct')
  39. # destroy session
  40. Sessions.destroy(client_id1)
  41. # check if session exists
  42. assert_not(Sessions.session_exists?(client_id1), 'check if session exists')
  43. end
  44. test 'c activity stream' do
  45. # create users
  46. roles = Role.where(name: %w[Agent Admin])
  47. groups = Group.all
  48. agent1 = User.create_or_update(
  49. login: 'activity-stream-agent-1',
  50. firstname: 'Session',
  51. lastname: "activity stream #{SecureRandom.uuid}",
  52. email: 'activity-stream-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. # create min. on activity record
  61. random_name = "Random:#{SecureRandom.uuid}"
  62. Group.create_or_update(
  63. name: random_name,
  64. updated_by_id: 1,
  65. created_by_id: 1,
  66. )
  67. as_client1 = Sessions::Backend::ActivityStream.new(agent1, {}, false, '123-1', 3)
  68. # get as stream
  69. result1 = as_client1.push
  70. assert(result1, 'check as agent1')
  71. travel 1.second
  72. # next check should be empty
  73. result1 = as_client1.push
  74. assert_not(result1, 'check as agent1 - recall')
  75. # next check should be empty
  76. travel 4.seconds
  77. result1 = as_client1.push
  78. assert_not(result1, 'check as agent1 - recall 2')
  79. agent1.update!(email: 'activity-stream-agent11@example.com')
  80. Ticket.create!(
  81. title: '12323',
  82. group_id: 1,
  83. priority_id: 1,
  84. state_id: 1,
  85. customer_id: 1,
  86. updated_by_id: 1,
  87. created_by_id: 1,
  88. )
  89. travel 4.seconds
  90. # get as stream
  91. result1 = as_client1.push
  92. assert(result1, 'check as agent1 - recall 3')
  93. travel_back
  94. end
  95. end