long_polling_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2024 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. 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(Hash)
  17. expect(json_response['error']).to eq('Invalid client_id received!')
  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(Hash)
  23. expect(json_response['client_id']).to be_a_uuid
  24. client_id = json_response['client_id']
  25. get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'anything' } }, as: :json
  26. expect(response).to have_http_status(:ok)
  27. expect(json_response).to be_a(Hash)
  28. expect(json_response['client_id']).to be_a_uuid
  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(Hash)
  32. expect(json_response['error']).to eq('Invalid client_id received!')
  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(Hash)
  39. expect(json_response['error']).to eq('Invalid client_id received!')
  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(Hash)
  46. expect(json_response['error']).to eq('Invalid client_id received!')
  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(Hash)
  53. expect(json_response['client_id']).to be_a_uuid
  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(Hash)
  61. expect(json_response).to eq({})
  62. end
  63. end
  64. end