File: has_arguments_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (29 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Schema::Member::HasArguments do
  class DefaultArgumentAuthSchema < GraphQL::Schema
    class Query < GraphQL::Schema::Object
      field :add, Int do
        argument :left, Int
        argument :right, Int, required: false, default_value: 1 do
          def authorized?(_object, _arg_value, context)
            !!context[:is_authorized]
          end
        end
      end

      def add(left:, right:)
        left + right
      end
    end

    query(Query)
  end

  it "doesn't require authorization when arguments with default values aren't present in the query" do
    assert_equal 5, DefaultArgumentAuthSchema.execute("{ add(left: 3, right: 2) }", context: { is_authorized: true })["data"].fetch("add")
    assert_equal nil, DefaultArgumentAuthSchema.execute("{ add(left: 3, right: 2) }", context: { is_authorized: false })["data"].fetch("add")
    assert_equal 4, DefaultArgumentAuthSchema.execute("{ add(left: 3) }", context: { is_authorized: false })["data"].fetch("add")
  end
end