user_updates_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::UserUpdates, type: :graphql do
  4. let(:subscription) do
  5. read_graphql_file('shared/graphql/subscriptions/userUpdates.graphql') +
  6. read_graphql_file('shared/graphql/fragments/objectAttributeValues.graphql')
  7. end
  8. let(:mock_channel) { build_mock_channel }
  9. let(:target) { create(:user) }
  10. let(:variables) { { userId: Gql::ZammadSchema.id_from_object(target) } }
  11. before do
  12. graphql_execute(subscription, variables: variables, context: { channel: mock_channel })
  13. end
  14. context 'with authenticated user', authenticated_as: :agent do
  15. let(:agent) { create(:agent) }
  16. it 'subscribes' do
  17. expect(graphql_response['data']['userUpdates']).to eq({ 'user' => nil })
  18. end
  19. it 'receives user updates for target user' do
  20. target.save!
  21. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['userUpdates']['user']['firstname']).to eq(target.firstname)
  22. end
  23. it 'does not receive user updates for other users' do
  24. create(:agent).save!
  25. expect(mock_channel.mock_broadcasted_messages).to be_empty
  26. end
  27. end
  28. context 'with authenticated customer', authenticated_as: :customer do
  29. let(:customer) { create(:customer) }
  30. context 'when subscribing for other users' do
  31. it 'does not subscribe' do
  32. expect(graphql_response['errors'][0]).to be_present
  33. end
  34. end
  35. context 'when subscribing for itself' do
  36. let(:target) { customer }
  37. it 'subscribes' do
  38. expect(graphql_response['data']['userUpdates']).to eq({ 'user' => nil })
  39. end
  40. it 'receives user updates for target user' do
  41. target.save!
  42. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['userUpdates']['user']['firstname']).to eq(target.firstname)
  43. end
  44. it 'does not receive user updates for other users' do
  45. create(:agent).save!
  46. expect(mock_channel.mock_broadcasted_messages).to be_empty
  47. end
  48. end
  49. end
  50. it_behaves_like 'graphql responds with error if unauthenticated'
  51. end