user_updates_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::UserUpdates, type: :graphql do
  4. let(:subscription) do
  5. <<~QUERY
  6. subscription userUpdates($userId: ID!) {
  7. userUpdates(userId: $userId) {
  8. user {
  9. id
  10. firstname
  11. lastname
  12. }
  13. }
  14. }
  15. QUERY
  16. end
  17. let(:mock_channel) { build_mock_channel }
  18. let(:target) { create(:user) }
  19. let(:variables) { { userId: gql.id(target) } }
  20. before do
  21. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  22. end
  23. context 'with authenticated user', authenticated_as: :agent do
  24. let(:agent) { create(:agent) }
  25. it 'subscribes' do
  26. expect(gql.result.data).to eq({ 'user' => nil })
  27. end
  28. it 'receives user updates for target user' do
  29. target.save!
  30. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['userUpdates']['user']['firstname']).to eq(target.firstname)
  31. end
  32. it 'does not receive user updates for other users' do
  33. create(:agent).save!
  34. expect(mock_channel.mock_broadcasted_messages).to be_empty
  35. end
  36. end
  37. context 'with authenticated customer', authenticated_as: :customer do
  38. let(:customer) { create(:customer) }
  39. context 'when subscribing for other users' do
  40. it 'does not subscribe but returns an authorization error' do
  41. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  42. end
  43. end
  44. context 'when subscribing for itself' do
  45. let(:target) { customer }
  46. it 'subscribes' do
  47. expect(gql.result.data).to eq({ 'user' => nil })
  48. end
  49. it 'receives user updates for target user' do
  50. target.save!
  51. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['userUpdates']['user']['firstname']).to eq(target.firstname)
  52. end
  53. it 'does not receive user updates for other users' do
  54. create(:agent).save!
  55. expect(mock_channel.mock_broadcasted_messages).to be_empty
  56. end
  57. end
  58. end
  59. it_behaves_like 'graphql responds with error if unauthenticated'
  60. end