chat_transfer_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. require 'rails_helper'
  2. RSpec.describe Sessions::Event::ChatTransfer do
  3. let(:client_id) { rand(123_456_789) }
  4. let(:chat) { Chat.first }
  5. let(:chat_transfer_into) { Chat.create!(name: 'chat 2', updated_by_id: 1, created_by_id: 1) }
  6. let(:chat_session) do
  7. Sessions.create('customer_session_id', { 'id' => customer.id }, {})
  8. Sessions.queue('customer_session_id')
  9. Chat::Session.create(
  10. chat_id: chat.id,
  11. user_id: nil,
  12. preferences: { participants: ['customer_session_id'] },
  13. state: 'running',
  14. )
  15. end
  16. let!(:agent) do
  17. create(:agent, preferences: { chat: { active: { chat.id.to_s => 'on' } } })
  18. end
  19. let!(:customer) { create(:customer) }
  20. let(:subject_as_agent) do
  21. Sessions.create(client_id, { 'id' => agent.id }, {})
  22. Sessions.queue(client_id)
  23. described_class.new(
  24. payload: { 'data' => { 'session_id' => chat_session.session_id }, 'session_id' => chat_session.id, 'chat_id' => chat_transfer_into.id },
  25. user_id: agent.id,
  26. client_id: client_id,
  27. clients: {},
  28. session: { 'id' => agent.id },
  29. )
  30. end
  31. before do
  32. Setting.set('chat', true)
  33. end
  34. context 'when transfering a chat session as customer' do
  35. let(:subject_as_customer) do
  36. Sessions.create(client_id, { 'id' => customer.id }, {})
  37. Sessions.queue(client_id)
  38. described_class.new(
  39. payload: { 'data' => { 'session_id' => chat_session.session_id }, 'chat_id' => chat_transfer_into.id },
  40. user_id: customer.id,
  41. client_id: client_id,
  42. clients: {},
  43. session: { 'id' => customer.id },
  44. )
  45. end
  46. context 'without chat.agent permissions' do
  47. it 'send out no_permission event to user' do
  48. expect(subject_as_customer.run).to eq(nil)
  49. messages = Sessions.queue(client_id)
  50. expect(messages.count).to eq(1)
  51. expect(messages).to eq([
  52. { 'event' => 'chat_error',
  53. 'data' => {
  54. 'state' => 'no_permission'
  55. } }
  56. ])
  57. end
  58. end
  59. end
  60. context 'when transfering a chat session as agent' do
  61. it 'send out chat_session_notice to customer and agent and set chat session to waiting' do
  62. expect(subject_as_agent.run).to eq(nil)
  63. messages_to_customer = Sessions.queue('customer_session_id')
  64. expect(messages_to_customer.count).to eq(1)
  65. expect(messages_to_customer[0]).to eq(
  66. 'event' => 'chat_session_notice',
  67. 'data' => {
  68. 'message' => 'Conversation transfered into other chat. Please stay tuned.',
  69. 'session_id' => chat_session.session_id,
  70. },
  71. )
  72. messages_to_agent = Sessions.queue(client_id)
  73. expect(messages_to_agent.count).to eq(0)
  74. end
  75. end
  76. end