graphql.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. module ZammadSpecSupportGraphql
  3. #
  4. # Taken from github.com/rmosolgo/graphql-ruby/blob/master/spec/graphql/subscriptions/action_cable_subscriptions_spec.rb
  5. #
  6. # A stub implementation of ActionCable.
  7. # Any methods to support the mock backend have `mock` in the name.
  8. class MockActionCable
  9. class MockChannel
  10. def initialize
  11. @mock_broadcasted_messages = []
  12. end
  13. attr_reader :mock_broadcasted_messages
  14. def stream_from(stream_name, coder: nil, &block)
  15. # Rails uses `coder`, we don't
  16. block ||= ->(msg) { @mock_broadcasted_messages << msg }
  17. MockActionCable.mock_stream_for(stream_name).add_mock_channel(self, block)
  18. end
  19. end
  20. class MockStream
  21. def initialize
  22. @mock_channels = {}
  23. end
  24. def add_mock_channel(channel, handler)
  25. @mock_channels[channel] = handler
  26. end
  27. def mock_broadcast(message)
  28. @mock_channels.each do |_channel, handler|
  29. handler&.call(message)
  30. end
  31. end
  32. end
  33. class << self
  34. def clear_mocks
  35. @mock_streams = {}
  36. end
  37. def server
  38. self
  39. end
  40. def broadcast(stream_name, message)
  41. stream = @mock_streams[stream_name]
  42. stream&.mock_broadcast(message)
  43. end
  44. def mock_stream_for(stream_name)
  45. @mock_streams[stream_name] ||= MockStream.new
  46. end
  47. def build_mock_channel
  48. MockChannel.new
  49. end
  50. def mock_stream_names
  51. @mock_streams.keys
  52. end
  53. end
  54. end
  55. def graphql_current_user=(user)
  56. @graphql_current_user = user
  57. end
  58. #
  59. # Run a graphql query.
  60. #
  61. def graphql_execute(query, variables: {}, context: {})
  62. context[:current_user] ||= @graphql_current_user
  63. if @graphql_current_user
  64. # TODO: we only fake a SID for now, create a real session?
  65. context[:sid] = SecureRandom.hex(16)
  66. end
  67. @graphql_result = Gql::ZammadSchema.execute(query, variables: variables, context: context)
  68. end
  69. #
  70. # Response of the previous graphql_execute call.
  71. #
  72. def graphql_response
  73. @graphql_result
  74. end
  75. #
  76. # Create a mock channel that can be passed to graphql_execute like this:
  77. #
  78. # let(:mock_channel) { build_mock_channel }
  79. #
  80. # graphql_execute(query, context: { channel: mock_channel })
  81. #
  82. delegate :build_mock_channel, to: MockActionCable
  83. #
  84. # Read a graphql query definition file from app/frontend/*.
  85. #
  86. def read_graphql_file(filename)
  87. File.read(Rails.root.join("app/frontend/#{filename}"))
  88. end
  89. end
  90. RSpec.configure do |config|
  91. config.include ZammadSpecSupportGraphql, type: :graphql
  92. config.prepend_before(:each, type: :graphql) do
  93. ZammadSpecSupportGraphql::MockActionCable.clear_mocks
  94. Gql::ZammadSchema.subscriptions = GraphQL::Subscriptions::ActionCableSubscriptions.new(
  95. action_cable: ZammadSpecSupportGraphql::MockActionCable, action_cable_coder: JSON, schema: Gql::ZammadSchema
  96. )
  97. end
  98. config.append_after(:each, type: :graphql) do
  99. Gql::ZammadSchema.subscriptions = GraphQL::Subscriptions::ActionCableSubscriptions.new(schema: Gql::ZammadSchema)
  100. end
  101. # This helper allows you to authenticate as a given user in request specs
  102. # via the example metadata, rather than directly:
  103. #
  104. # it 'does something', authenticated_as: :user
  105. #
  106. # In order for this to work, you must define the user in a `let` block first:
  107. #
  108. # let(:user) { create(:customer) }
  109. #
  110. config.before(:each, :authenticated_as, type: :graphql) do |example|
  111. self.graphql_current_user = authenticated_as_get_user example.metadata[:authenticated_as], return_type: :user
  112. end
  113. end