chat_session_start_spec.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sessions::Event::ChatSessionStart do
  4. let(:client_id) { SecureRandom.uuid }
  5. let(:chat) { Chat.first }
  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: 'waiting',
  14. )
  15. end
  16. let!(:agent) do
  17. agent = create(:agent, preferences: { chat: { active: { chat.id.to_s => 'on' } } })
  18. file = File.open('test/data/image/1000x1000.png', 'rb')
  19. contents = file.read
  20. avatar = Avatar.add(
  21. object: 'User',
  22. o_id: agent.id,
  23. default: true,
  24. resize: {
  25. content: contents,
  26. mime_type: 'image/jpg',
  27. },
  28. source: 'web',
  29. deletable: true,
  30. updated_by_id: 1,
  31. created_by_id: 1,
  32. )
  33. Avatar.set_default('User', agent.id, avatar.id)
  34. agent.image = avatar.store_hash
  35. agent.save!
  36. agent
  37. end
  38. let!(:customer) { create(:customer) }
  39. let(:subject_as_agent) do
  40. Sessions.create(client_id, { 'id' => agent.id }, {})
  41. Sessions.queue(client_id)
  42. described_class.new(
  43. payload: { 'data' => chat_session.session_id },
  44. user_id: agent.id,
  45. client_id: client_id,
  46. clients: {},
  47. session: { 'id' => agent.id },
  48. )
  49. end
  50. let(:chat_message_history) do
  51. Chat::Message.create!(
  52. chat_session_id: chat_session.id,
  53. content: 'some message',
  54. )
  55. end
  56. before do
  57. Setting.set('chat', true)
  58. end
  59. context 'when starting a chat session as customer' do
  60. let(:subject_as_customer) do
  61. Sessions.create(client_id, { 'id' => customer.id }, {})
  62. Sessions.queue(client_id)
  63. described_class.new(
  64. payload: { 'data' => chat_session.session_id },
  65. user_id: customer.id,
  66. client_id: client_id,
  67. clients: {},
  68. session: { 'id' => customer.id },
  69. )
  70. end
  71. context 'without chat.agent permissions' do
  72. it 'send out no_permission event to user' do
  73. expect(subject_as_customer.run).to be_nil
  74. messages = Sessions.queue(client_id)
  75. expect(messages.count).to eq(1)
  76. expect(messages).to eq([
  77. { 'event' => 'chat_error',
  78. 'data' => {
  79. 'state' => 'no_permission'
  80. } }
  81. ])
  82. end
  83. end
  84. end
  85. context 'when starting a chat session with invalid chat_session_id' do
  86. let(:subject_with_invalid_session_id) do
  87. described_class.new(
  88. payload: { 'data' => 'not_existing_chat_session_id' },
  89. user_id: agent.id,
  90. client_id: client_id,
  91. clients: {},
  92. session: { 'id' => agent.id },
  93. )
  94. end
  95. it 'return failed message' do
  96. expect(subject_with_invalid_session_id.run).to eq(
  97. event: 'chat_session_start',
  98. data: {
  99. state: 'failed',
  100. message: 'No session available.',
  101. },
  102. )
  103. end
  104. end
  105. context 'when starting a chat session as agent' do
  106. it 'send out chat_session_start to customer and agent' do
  107. expect(subject_as_agent.run).to be_nil
  108. messages_to_customer = Sessions.queue('customer_session_id')
  109. expect(messages_to_customer.count).to eq(1)
  110. expect(messages_to_customer[0]).to eq(
  111. 'event' => 'chat_session_start',
  112. 'data' => {
  113. 'state' => 'ok',
  114. 'agent' => {
  115. 'name' => agent.fullname,
  116. 'avatar' => 'http://zammad.example.com/api/v1/users/image/4cbd23059d5eb008f28a0f8bfbc723be',
  117. },
  118. 'chat_id' => chat.id,
  119. 'session_id' => chat_session.session_id,
  120. },
  121. )
  122. messages_to_agent = Sessions.queue(client_id)
  123. expect(messages_to_agent.count).to eq(1)
  124. expect(messages_to_agent[0]).to include(
  125. 'event' => 'chat_session_start',
  126. 'data' => hash_including(
  127. 'session' => hash_including(
  128. 'user_id' => agent.id,
  129. 'state' => 'running',
  130. 'preferences' => hash_including(
  131. 'participants' => ['customer_session_id', client_id]
  132. ),
  133. 'id' => chat_session.id,
  134. 'chat_id' => chat_session.chat_id,
  135. 'session_id' => chat_session.session_id,
  136. 'name' => nil,
  137. )
  138. )
  139. )
  140. end
  141. end
  142. context 'when starting a chat session as agent with alternative_name and no avatar_state' do
  143. it 'send out chat_session_start to customer (with prepared agent information) and agent' do
  144. agent.preferences[:chat] ||= {}
  145. agent.preferences[:chat][:alternative_name] = 'some name'
  146. agent.preferences[:chat][:avatar_state] = 'disabled'
  147. agent.save!
  148. expect(subject_as_agent.run).to be_nil
  149. messages_to_customer = Sessions.queue('customer_session_id')
  150. expect(messages_to_customer.count).to eq(1)
  151. expect(messages_to_customer[0]).to eq(
  152. 'event' => 'chat_session_start',
  153. 'data' => {
  154. 'state' => 'ok',
  155. 'agent' => {
  156. 'name' => 'some name',
  157. 'avatar' => nil,
  158. },
  159. 'chat_id' => chat.id,
  160. 'session_id' => chat_session.session_id,
  161. },
  162. )
  163. messages_to_agent = Sessions.queue(client_id)
  164. expect(messages_to_agent.count).to eq(1)
  165. expect(messages_to_agent[0]).to include(
  166. 'event' => 'chat_session_start',
  167. 'data' => hash_including(
  168. 'session' => hash_including(
  169. 'user_id' => agent.id,
  170. 'state' => 'running',
  171. 'preferences' => hash_including(
  172. 'participants' => ['customer_session_id', client_id]
  173. ),
  174. 'id' => chat_session.id,
  175. 'chat_id' => chat_session.chat_id,
  176. 'session_id' => chat_session.session_id,
  177. 'name' => nil,
  178. )
  179. )
  180. )
  181. end
  182. end
  183. context 'when starting a chat session as agent with transferred conversation' do
  184. it 'send out chat_session_start to customer and agent with already created messages' do
  185. chat_message_history
  186. expect(subject_as_agent.run).to be_nil
  187. messages_to_customer = Sessions.queue('customer_session_id')
  188. expect(messages_to_customer.count).to eq(0)
  189. messages_to_agent = Sessions.queue(client_id)
  190. expect(messages_to_agent.count).to eq(1)
  191. expect(messages_to_agent[0]).to include(
  192. 'event' => 'chat_session_start',
  193. 'data' => hash_including(
  194. 'session' => hash_including(
  195. 'user_id' => agent.id,
  196. 'state' => 'running',
  197. 'preferences' => hash_including(
  198. 'participants' => ['customer_session_id', client_id]
  199. ),
  200. 'messages' => array_including(
  201. hash_including(
  202. 'content' => 'some message',
  203. ),
  204. ),
  205. 'id' => chat_session.id,
  206. 'chat_id' => chat_session.chat_id,
  207. 'session_id' => chat_session.session_id,
  208. 'name' => nil,
  209. )
  210. )
  211. )
  212. end
  213. end
  214. end