avatar_updates_spec.rb 2.3 KB

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