summaryrefslogtreecommitdiff
path: root/spec/graphql/types/base_field_spec.rb
blob: b85716e4d211460ce735b411c2ba7f9baa738f06 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Types::BaseField do
  describe 'authorized?' do
    let(:object) { double }
    let(:current_user) { nil }
    let(:ctx) { { current_user: current_user } }

    it 'defaults to true' do
      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true)

      expect(field).to be_authorized(object, nil, ctx)
    end

    it 'tests the field authorization, if provided' do
      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, authorize: :foo)

      expect(Ability).to receive(:allowed?).with(current_user, :foo, object).and_return(false)

      expect(field).not_to be_authorized(object, nil, ctx)
    end

    it 'tests the field authorization, if provided, when it succeeds' do
      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, authorize: :foo)

      expect(Ability).to receive(:allowed?).with(current_user, :foo, object).and_return(true)

      expect(field).to be_authorized(object, nil, ctx)
    end

    it 'only tests the resolver authorization if it authorizes_object?' do
      resolver = Class.new

      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true,
                                  resolver_class: resolver)

      expect(field).to be_authorized(object, nil, ctx)
    end

    it 'tests the resolver authorization, if provided' do
      resolver = Class.new do
        include Gitlab::Graphql::Authorize::AuthorizeResource

        authorizes_object!
      end

      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true,
                                  resolver_class: resolver)

      expect(resolver).to receive(:authorized?).with(object, ctx).and_return(false)

      expect(field).not_to be_authorized(object, nil, ctx)
    end

    it 'tests field authorization before resolver authorization, when field auth fails' do
      resolver = Class.new do
        include Gitlab::Graphql::Authorize::AuthorizeResource

        authorizes_object!
      end

      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true,
                                  authorize: :foo,
                                  resolver_class: resolver)

      expect(Ability).to receive(:allowed?).with(current_user, :foo, object).and_return(false)
      expect(resolver).not_to receive(:authorized?)

      expect(field).not_to be_authorized(object, nil, ctx)
    end

    it 'tests field authorization before resolver authorization, when field auth succeeds' do
      resolver = Class.new do
        include Gitlab::Graphql::Authorize::AuthorizeResource

        authorizes_object!
      end

      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true,
                                  authorize: :foo,
                                  resolver_class: resolver)

      expect(Ability).to receive(:allowed?).with(current_user, :foo, object).and_return(true)
      expect(resolver).to receive(:authorized?).with(object, ctx).and_return(false)

      expect(field).not_to be_authorized(object, nil, ctx)
    end
  end

  context 'when considering complexity' do
    let(:resolver) do
      Class.new(described_class) do
        def self.resolver_complexity(args, child_complexity:)
          2 if args[:foo]
        end

        def self.complexity_multiplier(args)
          0.01
        end
      end
    end

    it 'defaults to 1' do
      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true)

      expect(field.complexity).to eq 1
    end

    describe '#base_complexity' do
      context 'with no gitaly calls' do
        it 'defaults to 1' do
          field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true)

          expect(field.base_complexity).to eq 1
        end
      end

      context 'with a gitaly call' do
        it 'adds 1 to the default value' do
          field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, calls_gitaly: true)

          expect(field.base_complexity).to eq 2
        end
      end
    end

    it 'has specified value' do
      field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, complexity: 12)

      expect(field.complexity).to eq 12
    end

    context 'when field has a resolver' do
      context 'when a valid complexity is already set' do
        let(:field) { described_class.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: resolver, complexity: 2, max_page_size: 100, null: true) }

        it 'uses this complexity' do
          expect(field.complexity).to eq 2
        end
      end

      context 'and is a connection' do
        let(:field) { described_class.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: resolver, max_page_size: 100, null: true) }

        it 'sets complexity depending on arguments for resolvers' do
          expect(field.complexity.call({}, {}, 2)).to eq 4
          expect(field.complexity.call({}, { first: 50 }, 2)).to eq 3
        end

        it 'sets complexity depending on number load limits for resolvers' do
          expect(field.complexity.call({}, { first: 1 }, 2)).to eq 2
          expect(field.complexity.call({}, { first: 1, foo: true }, 2)).to eq 4
        end
      end

      context 'and is not a connection' do
        it 'sets complexity as normal' do
          field = described_class.new(name: 'test', type: GraphQL::Types::String, resolver_class: resolver, max_page_size: 100, null: true)

          expect(field.complexity.call({}, {}, 2)).to eq 2
          expect(field.complexity.call({}, { first: 50 }, 2)).to eq 2
        end
      end
    end

    context 'calls_gitaly' do
      context 'for fields with a resolver' do
        it 'adds 1 if true' do
          with_gitaly_field = described_class.new(name: 'test', type: GraphQL::Types::String, resolver_class: resolver, null: true, calls_gitaly: true)
          without_gitaly_field = described_class.new(name: 'test', type: GraphQL::Types::String, resolver_class: resolver, null: true)
          base_result = without_gitaly_field.complexity.call({}, {}, 2)

          expect(with_gitaly_field.complexity.call({}, {}, 2)).to eq base_result + 1
        end
      end

      context 'for fields without a resolver' do
        it 'adds 1 if true' do
          field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, calls_gitaly: true)

          expect(field.complexity).to eq 2
        end
      end

      it 'defaults to false' do
        field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true)

        expect(field.base_complexity).to eq Types::BaseField::DEFAULT_COMPLEXITY
      end

      context 'with declared constant complexity value' do
        it 'has complexity set to that constant' do
          field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, complexity: 12)

          expect(field.complexity).to eq 12
        end

        it 'does not raise an error even with Gitaly calls' do
          allow(Gitlab::GitalyClient).to receive(:get_request_count).and_return([0, 1])
          field = described_class.new(name: 'test', type: GraphQL::Types::String, null: true, complexity: 12)

          expect(field.complexity).to eq 12
        end
      end
    end

    describe '#visible?' do
      context 'and has a feature_flag' do
        let(:flag) { :test_feature }
        let(:field) { described_class.new(name: 'test', type: GraphQL::Types::String, _deprecated_feature_flag: flag, null: false) }
        let(:context) { {} }

        before do
          skip_feature_flags_yaml_validation
        end

        it 'checks YAML definition for default_enabled' do
          # Exception is indicative of a check for YAML definition
          expect { field.visible?(context) }.to raise_error(Feature::InvalidFeatureFlagError, /The feature flag YAML definition for '#{flag}' does not exist/)
        end

        context 'skipping YAML check' do
          before do
            skip_default_enabled_yaml_check
          end

          it 'returns false if the feature is not enabled' do
            stub_feature_flags(flag => false)

            expect(field.visible?(context)).to eq(false)
          end

          it 'returns true if the feature is enabled' do
            expect(field.visible?(context)).to eq(true)
          end
        end
      end
    end
  end

  describe '#resolve' do
    context "late_extensions is given" do
      it 'registers the late extensions after the regular extensions' do
        extension_class = Class.new(GraphQL::Schema::Field::ConnectionExtension)
        field = described_class.new(name: 'test', type: GraphQL::Types::String.connection_type, null: true, late_extensions: [extension_class])

        expect(field.extensions.last.class).to be(extension_class)
      end
    end
  end

  describe '#description' do
    context 'feature flag given' do
      let(:field) { described_class.new(name: 'test', type: GraphQL::Types::String, _deprecated_feature_flag: flag, null: false, description: 'Test description.') }
      let(:flag) { :test_flag }

      it 'prepends the description' do
        expect(field.description).to start_with 'Test description. Available only when feature flag `test_flag` is enabled.'
      end

      context 'falsey feature_flag values' do
        using RSpec::Parameterized::TableSyntax

        where(:flag, :feature_value, :default_enabled) do
          ''  | false | false
          ''  | true  | false
          nil | false | true
          nil | true  | false
        end

        with_them do
          it 'returns the correct description' do
            expect(field.description).to eq('Test description.')
          end
        end
      end

      context 'with different default_enabled values' do
        using RSpec::Parameterized::TableSyntax

        where(:feature_value, :default_enabled, :expected_description) do
          disabled_ff_description = "Test description. Available only when feature flag `test_flag` is enabled. This flag is disabled by default, because the feature is experimental and is subject to change without notice."
          enabled_ff_description = "Test description. Available only when feature flag `test_flag` is enabled. This flag is enabled by default."

          false | false | disabled_ff_description
          true  | false | disabled_ff_description
          false | true  | enabled_ff_description
          true  | true  | enabled_ff_description
        end

        with_them do
          before do
            stub_feature_flags("#{flag}": feature_value)

            allow(Feature::Definition).to receive(:has_definition?).with(flag).and_return(true)
            allow(Feature::Definition).to receive(:default_enabled?).and_return(default_enabled)
          end

          it 'returns the correct availability in the description' do
            expect(field.description).to eq expected_description
          end
        end
      end
    end
  end

  include_examples 'Gitlab-style deprecations' do
    def subject(args = {})
      base_args = { name: 'test', type: GraphQL::Types::String, null: true }

      described_class.new(**base_args.merge(args))
    end

    it 'interacts well with the `_deprecated_feature_flag` property' do
      field = subject(
        deprecated: { milestone: '1.10', reason: 'Deprecation reason' },
        description: 'Field description.',
        _deprecated_feature_flag: 'foo_flag'
      )

      expect(field.description).to start_with('Field description. Available only when feature flag `foo_flag` is enabled.')
      expect(field.description).to end_with('Deprecated in 1.10: Deprecation reason.')
    end
  end
end