long_polling_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. require 'rails_helper'
  2. RSpec.describe 'LongPolling', type: :request do
  3. let(:agent) do
  4. create(:agent)
  5. end
  6. before do
  7. Sessions.sessions.each do |client_id|
  8. Sessions.destroy(client_id)
  9. end
  10. Sessions.spool_delete
  11. end
  12. describe 'request handling' do
  13. it 'receive without client_id - no user login' do
  14. get '/api/v1/message_receive', params: { data: {} }, as: :json
  15. expect(response).to have_http_status(:unprocessable_entity)
  16. expect(json_response).to be_a_kind_of(Hash)
  17. expect(json_response['error']).to eq('Invalid client_id receive!')
  18. end
  19. it 'send without client_id - no user login' do
  20. get '/api/v1/message_send', params: { data: {} }, as: :json
  21. expect(response).to have_http_status(:ok)
  22. expect(json_response).to be_a_kind_of(Hash)
  23. expect(json_response['client_id'].to_i).to be_between(1, 9_999_999_999)
  24. client_id = json_response['client_id']
  25. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
  26. expect(response).to have_http_status(:ok)
  27. expect(json_response).to be_a_kind_of(Hash)
  28. expect(json_response['client_id'].to_i).to be_between(1, 9_999_999_999)
  29. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  30. expect(response).to have_http_status(:unprocessable_entity)
  31. expect(json_response).to be_a_kind_of(Hash)
  32. expect(json_response['error']).to eq('Invalid client_id receive!')
  33. end
  34. it 'receive without client_id' do
  35. authenticated_as(agent)
  36. get '/api/v1/message_receive', params: { data: {} }, as: :json
  37. expect(response).to have_http_status(:unprocessable_entity)
  38. expect(json_response).to be_a_kind_of(Hash)
  39. expect(json_response['error']).to eq('Invalid client_id receive!')
  40. end
  41. it 'receive without wrong client_id' do
  42. authenticated_as(agent)
  43. get '/api/v1/message_receive', params: { client_id: 'not existing', data: {} }, as: :json
  44. expect(response).to have_http_status(:unprocessable_entity)
  45. expect(json_response).to be_a_kind_of(Hash)
  46. expect(json_response['error']).to eq('Invalid client_id receive!')
  47. end
  48. it 'send without client_id' do
  49. authenticated_as(agent)
  50. get '/api/v1/message_send', params: { data: {} }, as: :json
  51. expect(response).to have_http_status(:ok)
  52. expect(json_response).to be_a_kind_of(Hash)
  53. expect(json_response['client_id'].to_i).to be_between(1, 9_999_999_999)
  54. end
  55. it 'send with client_id' do
  56. Sessions.create('123456', {}, { type: 'ajax' })
  57. authenticated_as(agent)
  58. get '/api/v1/message_send', params: { client_id: '123456', data: {} }, as: :json
  59. expect(response).to have_http_status(:ok)
  60. expect(json_response).to be_a_kind_of(Hash)
  61. expect(json_response).to eq({})
  62. end
  63. it 'send event spool and receive data' do
  64. # here we use a token for the authentication because the basic auth way with username and password
  65. # will update the user by every request and return a different result for the test
  66. authenticated_as(agent, token: create(:token, action: 'api', user_id: agent.id) )
  67. get '/api/v1/message_send', params: { data: { event: 'login' } }, as: :json
  68. expect(response).to have_http_status(:ok)
  69. expect(json_response['client_id'].to_i).to be_between(1, 9_999_999_999)
  70. client_id = json_response['client_id']
  71. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  72. expect(response).to have_http_status(:ok)
  73. expect(json_response).to be_a_kind_of(Array)
  74. expect(json_response).to eq([{ 'data' => { 'success' => true }, 'event' => 'ws:login' }])
  75. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
  76. expect(response).to have_http_status(:ok)
  77. expect(json_response).to be_a_kind_of(Hash)
  78. expect(json_response).to eq({})
  79. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  80. expect(response).to have_http_status(:ok)
  81. expect(json_response).to be_a_kind_of(Array)
  82. expect(json_response[0]['event']).to eq('spool:sent')
  83. expect(json_response[0]['event']).to eq('spool:sent')
  84. expect(json_response.count).to eq(1)
  85. spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
  86. expect(spool_list).to eq([])
  87. 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
  88. expect(response).to have_http_status(:ok)
  89. expect(json_response).to be_a_kind_of(Hash)
  90. expect(json_response).to eq({})
  91. get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
  92. expect(response).to have_http_status(:ok)
  93. expect(json_response).to be_a_kind_of(Hash)
  94. expect(json_response).to eq({ 'event' => 'pong' })
  95. travel 2.seconds
  96. spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
  97. expect(spool_list).to eq([])
  98. spool_list = Sessions.spool_list(nil, agent.id)
  99. expect(spool_list).to eq([{ message: { 'taskbar_id' => 9_391_633 }, type: 'direct' }])
  100. end
  101. end
  102. end