2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-11-02 17:42:57 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'LongPolling', type: :request do
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:agent) do
|
|
|
|
create(:agent)
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-11-02 17:42:57 +00:00
|
|
|
Sessions.sessions.each do |client_id|
|
|
|
|
Sessions.destroy(client_id)
|
|
|
|
end
|
|
|
|
Sessions.spool_delete
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'receive without client_id - no user login' do
|
|
|
|
get '/api/v1/message_receive', params: { data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-12-16 13:42:09 +00:00
|
|
|
expect(json_response['error']).to eq('Invalid client_id received!')
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'send without client_id - no user login' do
|
|
|
|
get '/api/v1/message_send', params: { data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-09-20 10:47:05 +00:00
|
|
|
expect(json_response['client_id']).to be_a_uuid
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
client_id = json_response['client_id']
|
|
|
|
get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-09-20 10:47:05 +00:00
|
|
|
expect(json_response['client_id']).to be_a_uuid
|
2018-11-02 17:42:57 +00:00
|
|
|
|
|
|
|
get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-12-16 13:42:09 +00:00
|
|
|
expect(json_response['error']).to eq('Invalid client_id received!')
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'receive without client_id' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-11-02 17:42:57 +00:00
|
|
|
get '/api/v1/message_receive', params: { data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-12-16 13:42:09 +00:00
|
|
|
expect(json_response['error']).to eq('Invalid client_id received!')
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'receive without wrong client_id' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-11-02 17:42:57 +00:00
|
|
|
get '/api/v1/message_receive', params: { client_id: 'not existing', data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-12-16 13:42:09 +00:00
|
|
|
expect(json_response['error']).to eq('Invalid client_id received!')
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'send without client_id' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-11-02 17:42:57 +00:00
|
|
|
get '/api/v1/message_send', params: { data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
2021-09-20 10:47:05 +00:00
|
|
|
expect(json_response['client_id']).to be_a_uuid
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'send with client_id' do
|
|
|
|
Sessions.create('123456', {}, { type: 'ajax' })
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(agent)
|
2018-11-02 17:42:57 +00:00
|
|
|
get '/api/v1/message_send', params: { client_id: '123456', data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response).to eq({})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'send event spool and receive data' do
|
|
|
|
|
2018-12-05 10:43:53 +00:00
|
|
|
# here we use a token for the authentication because the basic auth way with username and password
|
|
|
|
# will update the user by every request and return a different result for the test
|
2021-07-16 13:38:01 +00:00
|
|
|
authenticated_as(agent, token: create(:token, action: 'api', user_id: agent.id))
|
2018-11-02 17:42:57 +00:00
|
|
|
get '/api/v1/message_send', params: { data: { event: 'login' } }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2021-09-20 10:47:05 +00:00
|
|
|
expect(json_response['client_id']).to be_a_uuid
|
2018-11-02 17:42:57 +00:00
|
|
|
client_id = json_response['client_id']
|
|
|
|
|
|
|
|
get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Array)
|
|
|
|
expect(json_response).to eq([{ 'data' => { 'success' => true }, 'event' => 'ws:login' }])
|
|
|
|
|
|
|
|
get '/api/v1/message_send', params: { client_id: client_id, data: { event: 'spool' } }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response).to eq({})
|
|
|
|
|
|
|
|
get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Array)
|
|
|
|
expect(json_response[0]['event']).to eq('spool:sent')
|
|
|
|
expect(json_response[0]['event']).to eq('spool:sent')
|
|
|
|
expect(json_response.count).to eq(1)
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(spool_list).to eq([])
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
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
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response).to eq({})
|
|
|
|
|
|
|
|
get '/api/v1/message_receive', params: { client_id: client_id, data: {} }, as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
expect(json_response).to eq({ 'event' => 'pong' })
|
|
|
|
|
|
|
|
travel 2.seconds
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
spool_list = Sessions.spool_list(Time.now.utc.to_i, agent.id)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(spool_list).to eq([])
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
spool_list = Sessions.spool_list(nil, agent.id)
|
2018-11-02 17:42:57 +00:00
|
|
|
expect(spool_list).to eq([{ message: { 'taskbar_id' => 9_391_633 }, type: 'direct' }])
|
2021-07-16 09:16:04 +00:00
|
|
|
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
|
2021-07-16 09:16:04 +00:00
|
|
|
it 'automatically cleans-up old spool entries' do
|
|
|
|
authenticated_as(agent)
|
|
|
|
Sessions.spool_create({ data: 'my message', event: 'broadcast' })
|
|
|
|
|
|
|
|
# Message found
|
|
|
|
travel 2.seconds
|
|
|
|
expect(Sessions.spool_list(nil, agent.id)).to eq([{ message: 'my message', type: 'broadcast' }])
|
|
|
|
|
|
|
|
# Message expired. In this case spool_list needs to also delete it.
|
|
|
|
travel 4.days
|
|
|
|
expect(Sessions.spool_list(nil, agent.id)).to eq([])
|
|
|
|
|
|
|
|
# Verify that the message was correctly deleted
|
|
|
|
travel(-4.days)
|
|
|
|
expect(Sessions.spool_list(nil, agent.id)).to eq([])
|
|
|
|
end
|
2018-11-02 17:42:57 +00:00
|
|
|
end
|
|
|
|
end
|