devices_updates_spec.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::User::Current::DevicesUpdates, type: :graphql do
  4. let(:subscription) do
  5. <<~QUERY
  6. subscription userCurrentDevicesUpdates($userId: ID!) {
  7. userCurrentDevicesUpdates(userId: $userId) {
  8. devices {
  9. name
  10. }
  11. }
  12. }
  13. QUERY
  14. end
  15. let(:mock_channel) { build_mock_channel }
  16. let(:target) { create(:agent) }
  17. let(:variables) { { userId: gql.id(target) } }
  18. context 'with authenticated user', authenticated_as: :target do
  19. it 'subscribes' do
  20. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  21. expect(gql.result.data).to eq({ 'devices' => nil })
  22. end
  23. it 'receives user device updates for target user' do
  24. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  25. create(:user_device, user_id: target.id)
  26. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['userCurrentDevicesUpdates']['devices'].count).to eq(1)
  27. end
  28. it 'does not receive user device updates for other users' do
  29. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  30. create(:user_device, user_id: create(:agent).id)
  31. expect(mock_channel.mock_broadcasted_messages).to be_empty
  32. end
  33. end
  34. context 'when subscribing for unauthenticated users' do
  35. let(:agent_without_device) { create(:agent, roles: []) }
  36. let(:variables) { { userId: gql.id(create(:agent)) } }
  37. it 'does not subscribe but returns an authorization error' do
  38. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  39. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  40. end
  41. end
  42. end