File: decorated_route_spec.rb

package info (click to toggle)
ruby-grape-path-helpers 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 192 kB
  • sloc: ruby: 814; makefile: 3
file content (385 lines) | stat: -rw-r--r-- 11,772 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
require 'spec_helper'

# rubocop:disable Metrics/BlockLength
describe GrapePathHelpers::DecoratedRoute do
  let(:api) { Spec::Support::API }

  let(:routes) do
    api.routes.map do |route|
      described_class.new(route)
    end
  end

  let(:index_route) do
    routes.detect { |route| route.route_namespace == '/cats' }
  end

  let(:show_route) do
    routes.detect { |route| route.route_namespace == '/cats/:id' }
  end

  let(:catch_all_route) do
    routes.detect { |route| route.route_path =~ /\*path/ }
  end

  let(:custom_route) do
    routes.detect { |route| route.route_path =~ /custom_name/ }
  end

  let(:ping_route) do
    routes.detect { |route| route.route_path =~ /ping/ }
  end

  let(:optional_route) do
    routes.detect { |route| route.route_path =~ /optional/ }
  end

  let(:wildcard_route) do
    routes.detect { |route| route.route_path =~ /\*owner_ids/ }
  end

  describe '#sanitize_method_name' do
    it 'removes characters that are illegal in Ruby method names' do
      illegal_names = ['beta-1', 'name_with_+', 'name_with_(']
      sanitized = illegal_names.map do |name|
        described_class.sanitize_method_name(name)
      end
      expect(sanitized).to match_array(%w[beta_1 name_with__ name_with__])
    end

    it 'only replaces integers if they appear at the beginning' do
      illegal_name = '1'
      legal_name = 'v1'
      expect(described_class.sanitize_method_name(illegal_name)).to eq('_')
      expect(described_class.sanitize_method_name(legal_name)).to eq('v1')
    end
  end

  describe '#define_path_helper' do
    context 'with only static segments' do
      let(:route) { ping_route }

      subject { route.api_v1_ping_path }

      it { is_expected.to eq '/api/v1/ping.json' }
    end

    context 'with optional segments' do
      let(:route) { optional_route }

      subject { route.api_v1_cats______optional_path }

      it { is_expected.to eq '/api/v1/cats/(-/)/optional.json' }
    end

    context 'with dynamic segments' do
      let(:route) { show_route }

      subject { route.api_v1_cats_path(id: 1) }

      it { is_expected.to eq '/api/v1/cats/1.json' }
    end

    context 'with wildcard segments' do
      let(:route) { wildcard_route }

      context 'including them' do
        subject do
          route.api_v1_cats_owners_owner_ids_cats_path(
            { owner_ids: 'foobar' },
            true
          )
        end

        it { is_expected.to eq '/api/v1/cats/owners/foobar/cats.json' }
      end

      context 'excluding them' do
        subject { route.api_v1_cats_owners_owner_ids_cats_path }

        it { is_expected.to eq '/api/v1/cats/owners/owner_ids/cats.json' }
      end
    end
  end

  describe '#helper_names' do
    context 'when a route is given a custom helper name' do
      it 'uses the custom name instead of the dynamically generated one' do
        expect(custom_route.helper_names.first)
          .to eq('my_custom_route_name_path')
      end

      it 'returns the correct path' do
        expect(
          custom_route.my_custom_route_name_path
        ).to eq('/api/v1/custom_name.json')
      end
    end

    context 'when an API has multiple POST routes in a resource' do
      let(:api) { Spec::Support::MultiplePostsAPI }

      it 'it creates a helper for each POST route' do
        expect(routes.size).to eq(2)
      end
    end

    context 'when an API has multiple versions' do
      let(:api) { Spec::Support::APIWithMultipleVersions }

      it "returns the route's helper name for each version" do
        helper_names = ping_route.helper_names
        expect(helper_names.size).to eq(api.versions.size)
      end

      it 'returns an array of the routes versions' do
        expect(ping_route.route_version).to eq(%w[beta alpha v1])
      end
    end

    context 'when an API has one version' do
      it "returns the route's helper name for that version" do
        helper_name = show_route.helper_names.first
        expect(helper_name).to eq('api_v1_cats_path')
      end
    end
  end

  describe '#helper_arguments' do
    context 'when no user input is needed to generate the correct path' do
      it 'returns an empty array' do
        expect(index_route.helper_arguments).to eq([])
      end
    end

    context 'when user input is needed to generate the correct path' do
      it 'returns an array of required segments' do
        expect(show_route.helper_arguments).to eq(['id'])
      end
    end
  end

  describe '#path_segments' do
    context 'when path has optional segments' do
      it 'leaves them intact' do
        result = optional_route.path_segments
        expect(result).to eq(%w[api :version cats :id (-/) optional])
      end
    end
  end

  describe '#path_segments_with_values' do
    context 'when path has dynamic segments' do
      it 'replaces segments with corresponding values found in options' do
        opts = { id: 1 }
        result = show_route.path_segments_with_values(opts)
        expect(result).to include(1)
      end

      context 'when options contains string keys' do
        it 'replaces segments with corresponding values found in the options' do
          opts = { 'id' => 1 }
          result = show_route.path_segments_with_values(opts)
          expect(result).to include(1)
        end
      end
    end

    context 'when path has wildcard segments' do
      context 'with regex excluding them' do
        it "doesn't replaces wildcard segments" do
          opts = { version: 4, id: 34, owner_ids: 'foobar' }

          result = wildcard_route.path_segments_with_values(opts)
          expect(result).to include(4)
          expect(result).to include(34)
          expect(result).to include('owner_ids')
          expect(result).not_to include('foobar')
        end

        it "doesn't blank wildcard segments" do
          opts = { version: 4, id: 34, owner_ids: '' }

          result = wildcard_route.path_segments_with_values(opts)
          expect(result).to include(4)
          expect(result).to include(34)
          expect(result).to include('owner_ids')
        end
      end

      context 'with regex including them' do
        context 'replaces segments' do
          it 'with static text' do
            opts = { version: 4, id: 34, owner_ids: 'foobar' }

            result = wildcard_route.path_segments_with_values(opts, true)
            expect(result).to include(4)
            expect(result).to include(34)
            expect(result).to include('foobar')
            expect(result).not_to include('owner_ids')
          end

          it 'with additional segments' do
            opts = { version: 4, id: 34, owner_ids: 'foo/bar/baz' }

            result = wildcard_route.path_segments_with_values(opts, true)
            expect(result).to include(4)
            expect(result).to include(34)
            expect(result).to include('foo/bar/baz')
            expect(result).not_to include('owner_ids')
          end
        end

        it 'blanks wildcard segments' do
          opts = { version: 4, id: 34, owner_ids: '' }

          result = wildcard_route.path_segments_with_values(opts, true)
          expect(result).to include(4)
          expect(result).to include(34)
          expect(result).not_to include('owner_ids')
        end

        context 'when options contains string keys' do
          it 'replaces segments' do
            opts = { 'version' => 4, 'id' => 34, 'owner_ids' => 'foobar' }

            result = wildcard_route.path_segments_with_values(opts, true)
            expect(result).to include(4)
            expect(result).to include(34)
            expect(result).to include('foobar')
            expect(result).not_to include('owner_ids')
          end
        end
      end
    end
  end

  describe '#path_helper_name' do
    it "returns the name of a route's helper method" do
      expect(index_route.path_helper_name).to eq('api_v1_cats_path')
    end

    context 'when the path is the root path' do
      let(:api_with_root) do
        Class.new(Grape::API) do
          get '/' do
          end
        end
      end

      let(:root_route) do
        grape_route = api_with_root.routes.first
        described_class.new(grape_route)
      end

      it 'returns "root_path"' do
        result = root_route.path_helper_name
        expect(result).to eq('root_path')
      end
    end

    context 'when the path is a catch-all path' do
      it 'returns a name without the glob star' do
        result = catch_all_route.path_helper_name
        expect(result).to eq('api_v1_path_path')
      end
    end

    context 'when the path has a wildcard segment' do
      it 'returns a name without the glob star' do
        result = wildcard_route.path_helper_name
        expect(result).to eq('api_v1_cats_owners_owner_ids_cats_path')
      end
    end
  end

  describe '#segment_to_value' do
    context 'when segment is dynamic' do
      it 'returns the value the segment corresponds to' do
        result = index_route.segment_to_value(':version')
        expect(result).to eq('v1')
      end

      context 'when segment is found in options' do
        it 'returns the value found in options' do
          options = { id: 1 }
          result = show_route.segment_to_value(':id', options)
          expect(result).to eq(1)
        end
      end
    end

    context 'when segment is static' do
      it 'returns the segment' do
        result = index_route.segment_to_value('api')
        expect(result).to eq('api')
      end
    end
  end

  describe 'path helper method' do
    context 'when given a "params" key' do
      context 'when value under "params" key is a hash' do
        it 'creates a query string' do
          query = { foo: :bar, baz: :zot }
          path = index_route.api_v1_cats_path(params: query)
          expect(path).to eq('/api/v1/cats.json?' + query.to_param)
        end
      end

      context 'when value under "params" is not a hash' do
        it 'coerces the value into a string' do
          path = index_route.api_v1_cats_path(params: 1)
          expect(path).to eq('/api/v1/cats.json?1')
        end
      end
    end

    # handle different Grape::Route#route_path formats in Grape 0.12.0
    context 'when route_path contains a specific format' do
      it 'returns the correct path with the correct format' do
        path = index_route.api_v1_cats_path
        expect(path).to eq('/api/v1/cats.json')
      end
    end

    context 'when helper does not require arguments' do
      it 'returns the correct path' do
        path = index_route.api_v1_cats_path
        expect(path).to eq('/api/v1/cats.json')
      end
    end

    context 'when arguments are needed required to construct the right path' do
      context 'when not missing arguments' do
        it 'returns the correct path' do
          path = show_route.api_v1_cats_path(id: 1)
          expect(path).to eq('/api/v1/cats/1.json')
        end
      end
    end

    context "when a route's API has multiple versions" do
      let(:api) { Spec::Support::APIWithMultipleVersions }

      it 'returns a path for each version' do
        expect(ping_route.alpha_ping_path).to eq('/alpha/ping')
        expect(ping_route.beta_ping_path).to eq('/beta/ping')
        expect(ping_route.v1_ping_path).to eq('/v1/ping')
      end
    end

    context 'when a format is given' do
      it 'returns the path with a correct extension' do
        path = show_route.api_v1_cats_path(id: 1, format: '.xml')
        expect(path).to eq('/api/v1/cats/1.xml')
      end
    end
  end

  describe '#optional_segment?' do
    it { expect(index_route.optional_segment?('mandatory')).to eq(false) }
    it { expect(index_route.optional_segment?('(optional/)')).to eq(true) }
  end
end
# rubocop:enable Metrics/BlockLength