graphql_channel_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe GraphqlChannel, type: :channel do
  4. # https://github.com/zammad/zammad/issues/5401
  5. describe 'setting UserInfo.current_user_id thread variable' do
  6. class TestQuery < Gql::Queries::BaseQuery # rubocop:disable RSpec/LeakyConstantDeclaration, Lint/ConstantDefinitionInBlock
  7. type Gql::Types::UserType, null: true
  8. def resolve
  9. User.find_by id: UserInfo.current_user_id
  10. end
  11. end
  12. let(:query) do
  13. <<~QUERY
  14. query testQuery {
  15. testQuery {
  16. id
  17. }
  18. }
  19. QUERY
  20. end
  21. context 'when connected with a session with a user' do
  22. let(:user) { create(:agent) }
  23. it 'sets UserInfo.current_user_id for the operation' do
  24. stub_connection sid: '123_456', current_user: user
  25. subscribe
  26. perform :execute, operationName: 'testQuery', query: query
  27. expect(transmissions.last).to include(
  28. result: include(
  29. data: include(testQuery: include(id: user.to_global_id.to_s))
  30. )
  31. )
  32. end
  33. end
  34. context 'when connected with a userless session' do
  35. it 'sets UserInfo.current_user_id for the operation' do
  36. stub_connection sid: '123_456', current_user: nil
  37. subscribe
  38. perform :execute, operationName: 'testQuery', query: query
  39. expect(transmissions.last).to include(
  40. result: include(
  41. data: include(testQuery: be_nil)
  42. )
  43. )
  44. end
  45. end
  46. end
  47. end