chat_transfer_spec.rb 2.9 KB

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