long_polling_spec.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'LongPolling', type: :request do
  4. let(:agent) do
  5. create(:agent)
  6. end
  7. before do
  8. Sessions.sessions.each do |client_id|
  9. Sessions.destroy(client_id)
  10. end
  11. Sessions.spool_delete
  12. end
  13. describe 'request handling' do
  14. it 'receive without client_id - no user login' do
  15. get '/api/v1/message_receive', params: { data: {} }, as: :json
  16. expect(response).to have_http_status(:unprocessable_entity)
  17. expect(json_response).to be_a_kind_of(Hash)
  18. expect(json_response['error']).to eq('Invalid client_id received!')
  19. end
  20. it 'send without client_id - no user login' do
  21. get '/api/v1/message_send', params: { data: {} }, as: :json
  22. expect(response).to have_http_status(:ok)
  23. expect(json_response).to be_a_kind_of(Hash)
  24. expect(json_response['client_id']).to be_a_uuid
  25. client_id = json_response['client_id']
  26. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
  27. expect(response).to have_http_status(:ok)
  28. expect(json_response).to be_a_kind_of(Hash)
  29. expect(json_response['client_id']).to be_a_uuid
  30. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  31. expect(response).to have_http_status(:unprocessable_entity)
  32. expect(json_response).to be_a_kind_of(Hash)
  33. expect(json_response['error']).to eq('Invalid client_id received!')
  34. end
  35. it 'receive without client_id' do
  36. authenticated_as(agent)
  37. get '/api/v1/message_receive', params: { data: {} }, as: :json
  38. expect(response).to have_http_status(:unprocessable_entity)
  39. expect(json_response).to be_a_kind_of(Hash)
  40. expect(json_response['error']).to eq('Invalid client_id received!')
  41. end
  42. it 'receive without wrong client_id' do
  43. authenticated_as(agent)
  44. get '/api/v1/message_receive', params: { client_id: 'not existing', data: {} }, as: :json
  45. expect(response).to have_http_status(:unprocessable_entity)
  46. expect(json_response).to be_a_kind_of(Hash)
  47. expect(json_response['error']).to eq('Invalid client_id received!')
  48. end
  49. it 'send without client_id' do
  50. authenticated_as(agent)
  51. get '/api/v1/message_send', params: { data: {} }, as: :json
  52. expect(response).to have_http_status(:ok)
  53. expect(json_response).to be_a_kind_of(Hash)
  54. expect(json_response['client_id']).to be_a_uuid
  55. end
  56. it 'send with client_id' do
  57. Sessions.create('123456', {}, { type: 'ajax' })
  58. authenticated_as(agent)
  59. get '/api/v1/message_send', params: { client_id: '123456', data: {} }, as: :json
  60. expect(response).to have_http_status(:ok)
  61. expect(json_response).to be_a_kind_of(Hash)
  62. expect(json_response).to eq({})
  63. end
  64. it 'send event spool and receive data' do
  65. # here we use a token for the authentication because the basic auth way with username and password
  66. # will update the user by every request and return a different result for the test
  67. authenticated_as(agent, token: create(:token, action: 'api', user_id: agent.id))
  68. get '/api/v1/message_send', params: { data: { event: 'login' } }, as: :json
  69. expect(response).to have_http_status(:ok)
  70. expect(json_response['client_id']).to be_a_uuid
  71. client_id = json_response['client_id']
  72. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  73. expect(response).to have_http_status(:ok)
  74. expect(json_response).to be_a_kind_of(Array)
  75. expect(json_response).to eq([{ 'data' => { 'success' => true }, 'event' => 'ws:login' }])
  76. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
  77. expect(response).to have_http_status(:ok)
  78. expect(json_response).to be_a_kind_of(Hash)
  79. expect(json_response).to eq({})
  80. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  81. expect(response).to have_http_status(:ok)
  82. expect(json_response).to be_a_kind_of(Array)
  83. expect(json_response[0]['event']).to eq('spool:sent')
  84. expect(json_response[0]['event']).to eq('spool:sent')
  85. expect(json_response.count).to eq(1)
  86. spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
  87. expect(spool_list).to eq([])
  88. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'broadcast', spool: true, recipient: { user_id: [agent.id] }, data: { taskbar_id: 9_391_633 } } }, as: :json
  89. expect(response).to have_http_status(:ok)
  90. expect(json_response).to be_a_kind_of(Hash)
  91. expect(json_response).to eq({})
  92. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  93. expect(response).to have_http_status(:ok)
  94. expect(json_response).to be_a_kind_of(Hash)
  95. expect(json_response).to eq({ 'event' => 'pong' })
  96. travel 2.seconds
  97. spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
  98. expect(spool_list).to eq([])
  99. spool_list = Sessions.spool_list(nil, agent.id)
  100. expect(spool_list).to eq([{ message: { 'taskbar_id' => 9_391_633 }, type: 'direct' }])
  101. end
  102. it 'automatically cleans-up old spool entries' do
  103. authenticated_as(agent)
  104. Sessions.spool_create({ data: 'my message', event: 'broadcast' })
  105. # Message found
  106. travel 2.seconds
  107. expect(Sessions.spool_list(nil, agent.id)).to eq([{ message: 'my message', type: 'broadcast' }])
  108. # Message expired. In this case spool_list needs to also delete it.
  109. travel 4.days
  110. expect(Sessions.spool_list(nil, agent.id)).to eq([])
  111. # Verify that the message was correctly deleted
  112. travel(-4.days)
  113. expect(Sessions.spool_list(nil, agent.id)).to eq([])
  114. end
  115. end
  116. end