ticket_live_user_updates_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::TicketLiveUserUpdates, :aggregate_failures, authenticated_as: :agent, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:customer) { create(:customer) }
  6. let(:ticket) { create(:ticket) }
  7. let(:live_user_entry) { create(:taskbar, key: "Ticket-#{ticket.id}", user_id: agent.id, app: 'mobile', state: { editing: true }) }
  8. let(:live_user_entry_customer) { create(:taskbar, key: "Ticket-#{ticket.id}", user_id: customer.id, app: 'mobile', state: { editing: false }) }
  9. let(:mock_channel) { build_mock_channel }
  10. let(:variables) { { userId: Gql::ZammadSchema.id_from_object(agent), key: "Ticket-#{ticket.id}", app: 'mobile' } }
  11. let(:subscription) do
  12. <<~QUERY
  13. subscription ticketLiveUserUpdates($userId: ID!, $key: String!, $app: EnumTaskbarApp!) {
  14. ticketLiveUserUpdates(userId: $userId, key: $key, app: $app) {
  15. liveUsers {
  16. user {
  17. firstname
  18. lastname
  19. }
  20. apps {
  21. name
  22. editing
  23. lastInteraction
  24. }
  25. }
  26. }
  27. }
  28. QUERY
  29. end
  30. before do
  31. live_user_entry && live_user_entry_customer
  32. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  33. end
  34. def update_taskbar_item(taskbar_item, state, agent_id)
  35. # Special case: By design, it is only allowed to update the taskbar of the current user.
  36. # We need to work around this, otherwise this test would fail.
  37. UserInfo.current_user_id = agent_id
  38. taskbar_item.update!(state: state)
  39. UserInfo.current_user_id = agent.id
  40. end
  41. context 'when subscribed' do
  42. it 'subscribes and delivers initial data' do
  43. expect(gql.result.data['liveUsers'].size).to eq(2)
  44. expect(gql.result.data['liveUsers'].first).to include('user' => {
  45. 'firstname' => agent.firstname,
  46. 'lastname' => agent.lastname,
  47. })
  48. expect(gql.result.data['liveUsers'].last).to include('user' => {
  49. 'firstname' => customer.firstname,
  50. 'lastname' => customer.lastname,
  51. })
  52. expect(gql.result.data['liveUsers'].last['apps'].first).to include('editing' => false)
  53. end
  54. it 'receives taskbar updates' do
  55. update_taskbar_item(live_user_entry_customer, { editing: true }, customer.id)
  56. result = mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'ticketLiveUserUpdates', 'liveUsers')
  57. expect(result.size).to eq(2)
  58. expect(result.first).to include('user' => {
  59. 'firstname' => agent.firstname,
  60. 'lastname' => agent.lastname,
  61. })
  62. expect(result.last).to include('user' => {
  63. 'firstname' => customer.firstname,
  64. 'lastname' => customer.lastname,
  65. })
  66. expect(result.last['apps'].first).to include('editing' => true)
  67. end
  68. context 'with multiple viewers' do
  69. let(:third_agent) { create(:agent) }
  70. let(:live_user_entry_third_agent) { create(:taskbar, key: "Ticket-#{ticket.id}", user_id: third_agent.id, app: 'mobile', state: { editing: false }) }
  71. it 'receives taskbar updates for all viewers' do
  72. update_taskbar_item(live_user_entry_customer, { editing: true }, customer.id)
  73. result = mock_channel.mock_broadcasted_messages.last.dig(:result, 'data', 'ticketLiveUserUpdates', 'liveUsers')
  74. expect(result.size).to eq(2)
  75. UserInfo.current_user_id = third_agent.id
  76. live_user_entry_third_agent
  77. UserInfo.current_user_id = agent.id
  78. update_taskbar_item(live_user_entry_third_agent, { editing: true }, third_agent.id)
  79. result = mock_channel.mock_broadcasted_messages.last.dig(:result, 'data', 'ticketLiveUserUpdates', 'liveUsers')
  80. expect(result.size).to eq(3)
  81. expect(result.first).to include('user' => {
  82. 'firstname' => agent.firstname,
  83. 'lastname' => agent.lastname,
  84. })
  85. expect(result[1]).to include('user' => {
  86. 'firstname' => customer.firstname,
  87. 'lastname' => customer.lastname,
  88. })
  89. expect(result.last).to include('user' => {
  90. 'firstname' => third_agent.firstname,
  91. 'lastname' => third_agent.lastname,
  92. })
  93. end
  94. end
  95. end
  96. context 'when a customer', authenticated_as: :customer do
  97. it 'can not use subscription wihtout agent permission' do
  98. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  99. end
  100. end
  101. end