File: controller_examples.rb

package info (click to toggle)
ruby-acts-as-api 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 624 kB
  • sloc: ruby: 2,366; makefile: 2
file content (401 lines) | stat: -rw-r--r-- 12,904 bytes parent folder | download
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
shared_examples_for 'a controller with ActsAsApi responses' do
  include ApiTestHelpers
  routes { SharedEngine::Engine.routes }

  describe 'xml responses' do
    describe 'get all users' do
      before(:each) do
        get :index, format: 'xml', params: { api_template: :name_only }
      end

      it 'should have a root node named users' do
        expect(response_body).to have_selector('users')
      end

      it 'should contain all users' do
        expect(response_body).to have_selector('users > user') do |users|
          expect(users.size).to eql(3)
        end
      end

      it 'should contain the specified attributes' do
        expect(response_body).to have_selector('users > user > first-name')
        expect(response_body).to have_selector('users > user > last-name')
      end
    end

    describe 'get a single user' do
      before(:each) do
        get :show, format: 'xml', params: { api_template: :name_only, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body).to have_selector('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body).to have_selector('user > first-name')
        expect(response_body).to have_selector('user > last-name')
      end
    end
  end

  describe 'json responses' do
    describe 'get all users' do
      before(:each) do
        get :index, format: 'json', params: { api_template: :name_only }
      end

      it 'should have a root node named users' do
        expect(response_body_json).to have_key('users')
      end

      it 'should contain all users' do
        expect(response_body_json['users']).to be_a(Array)
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['users'].first).to have_key('first_name')
        expect(response_body_json['users'].first).to have_key('last_name')
      end

      it 'should contain the specified values' do
        expect(response_body_json['users'].first['first_name']).to eql('Han')
        expect(response_body_json['users'].first['last_name']).to eql('Solo')
      end
    end

    describe 'get all users as a ActiveRecord::Relation (or similar chained) object, autodetecting the root node name' do
      before(:each) do
        get :index_relation, format: 'json', params: { api_template: :name_only }
      end

      it 'should have a root node named users' do
        expect(response_body_json).to have_key('users')
      end

      it 'should contain all users' do
        expect(response_body_json['users']).to be_a(Array)
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['users'].first).to have_key('first_name')
        expect(response_body_json['users'].first).to have_key('last_name')
      end

      it 'should contain the specified values' do
        expect(response_body_json['users'].first['first_name']).to eql('Han')
        expect(response_body_json['users'].first['last_name']).to eql('Solo')
      end
    end

    describe 'get a single user' do
      before(:each) do
        get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body_json).to have_key('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['user']).to have_key('first_name')
        expect(response_body_json['user']).to have_key('last_name')
      end

      it 'should contain the specified values' do
        expect(response_body_json['user']['first_name']).to eql('Luke')
        expect(response_body_json['user']['last_name']).to eql('Skywalker')
      end
    end

    describe 'get a single user with a nil profile' do
      before(:each) do
        Profile.acts_as_api
        Profile.api_accessible :include_profile do |t|
          t.add :avatar
          t.add :homepage
        end

        get :show, format: 'json', params: { api_template: :include_profile, id: @han.id }
      end

      it 'should have a root node named user' do
        expect(response_body_json).to have_key('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['user']).to have(1).keys
        expect(response_body_json['user']).to have_key('profile')
      end

      it 'should contain the specified values' do
        expect(response_body_json['user']['profile']).to be_nil
      end
    end

    describe 'get a user without specifying an api template' do
      before(:each) do
        get :show_default, format: 'json', params: { id: @luke.id }
      end

      it 'should respond with HTTP 200' do
        expect(response.code).to eq('200')
      end

      it 'should render the model with to_json' do
        expect(response.body).to eq(@luke.to_json)
      end
    end
  end

  describe 'Rails 3 default style json responses' do
    before(:each) do
      @org_include_root_in_json_collections = ActsAsApi::Config.include_root_in_json_collections
      ActsAsApi::Config.include_root_in_json_collections = true
    end

    after(:each) do
      ActsAsApi::Config.include_root_in_json_collections = @org_include_root_in_json_collections
    end

    describe 'get all users' do
      before(:each) do
        get :index, format: 'json', params: { api_template: :name_only }
      end

      it 'should have a root node named users' do
        expect(response_body_json).to have_key('users')
      end

      it 'should contain all users' do
        expect(response_body_json['users']).to be_a(Array)
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['users'].first['user']).to have_key('first_name')
        expect(response_body_json['users'].first['user']).to have_key('last_name')
      end

      it 'contains the user root nodes' do
        expect(response_body_json['users'].collect(&:keys).flatten.uniq).to eql(['user'])
      end

      it 'should contain the specified values' do
        expect(response_body_json['users'].first['user']['first_name']).to eql('Han')
        expect(response_body_json['users'].first['user']['last_name']).to eql('Solo')
      end
    end

    describe 'get a single user' do
      before(:each) do
        get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body_json).to have_key('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body_json['user']).to have_key('first_name')
        expect(response_body_json['user']).to have_key('last_name')
      end

      it 'should contain the specified values' do
        expect(response_body_json['user']['first_name']).to eql('Luke')
        expect(response_body_json['user']['last_name']).to eql('Skywalker')
      end
    end
  end

  describe 'jsonp responses with callback' do
    it 'should be disabled by default' do
      @callback = 'mycallback'
      get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
      expect(response_body_jsonp(@callback)).to be_nil
    end

    describe 'enabled jsonp callbacks' do
      before(:each) do
        @callback = 'mycallback'

        User.acts_as_api do |config|
          config.allow_jsonp_callback = true
        end
      end

      after(:each) do
        # put things back to the way they were
        User.acts_as_api do |config|
          config.allow_jsonp_callback = false
        end
      end

      describe 'get all users' do
        before(:each) do
          get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
        end

        it 'should wrap the response in the callback' do
          expect(response_body_jsonp(@callback)).not_to be_nil
        end
      end

      describe 'get a single user' do
        before(:each) do
          get :show, format: 'json', params: { api_template: :name_only, id: @luke.id, callback: @callback }
        end

        it 'should wrap the response in the callback' do
          expect(response_body_jsonp(@callback)).not_to be_nil
        end
      end

      describe 'Requesting the JSONP content as JavaScript' do
        before(:each) do
          get :index, format: :json, params: { api_template: :name_only, callback: @callback }
        end

        it 'should set the content type to JavaScript' do
          expect(response.content_type).to eq(Mime[:js])
        end
      end
    end
  end

  describe 'config.add_root_node_for is empty, so no root node is created' do
    before(:each) do
      @org_add_root_node_for_config = ActsAsApi::Config.add_root_node_for.dup
      ActsAsApi::Config.add_root_node_for = []
    end

    after(:each) do
      ActsAsApi::Config.add_root_node_for = @org_add_root_node_for_config
    end

    describe 'get all users' do
      before(:each) do
        get :index, format: 'json', params: { api_template: :name_only, callback: @callback }
      end

      it 'response has no named root node' do
        expect(response_body_json).to be_an(Array)
      end
    end

    describe 'get a single user' do
      before(:each) do
        get :show, format: 'json', params: { api_template: :name_only, id: @luke.id }
      end

      it 'response has no named root node' do
        expect(response_body_json).to be_a(Hash)
        expect(response_body_json.keys).to include('first_name')
      end
    end
  end

  describe 'pass meta information on rendering' do
    describe 'get all users' do
      before(:each) do
        get :index_meta, format: 'json', params: { api_template: :name_only }
      end

      it 'shows model response fields' do
        expect(response_body_json).to be_a(Hash)
        expect(response_body_json).to have_key('users')
        expect(response_body_json['users']).to be_an(Array)
      end

      it 'shows page field' do
        expect(response_body_json).to have_key('page')
      end

      it 'shows total field' do
        expect(response_body_json).to have_key('total')
      end
    end

    describe 'get a single user' do
      before(:each) do
        get :show_meta, format: 'json', params: { api_template: :name_only, id: @luke.id }
      end

      it 'shows model response fields' do
        expect(response_body_json).to be_a(Hash)
        expect(response_body_json).to have_key('user')
      end

      it 'shows page field' do
        expect(response_body_json).to have_key('page')
      end

      it 'shows total field' do
        expect(response_body_json).to have_key('total')
      end
    end
  end

  describe 'api prefix' do
    describe 'get single user' do
      before(:each) do
        get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_prefix: :with_prefix, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body).to have_selector('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body).to have_selector('user > prefix')
        expect(response_body).to have_selector('user > first-name')
        expect(response_body).to have_selector('user > last-name')
      end

      it 'should not contain the specified attributes' do
        expect(response_body).not_to have_selector('user > postfix')
      end
    end
  end

  describe 'api postfix' do
    describe 'get single user' do
      before(:each) do
        get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_postfix: :with_postfix, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body).to have_selector('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body).to have_selector('user > first-name')
        expect(response_body).to have_selector('user > last-name')
        expect(response_body).to have_selector('user > postfix')
      end

      it 'should not contain the specified attributes' do
        expect(response_body).not_to have_selector('user > prefix')
      end
    end
  end

  describe 'api prefix and api postfix' do
    describe 'get single user' do
      before(:each) do
        get :show_prefix_postfix, format: 'xml', params: { api_template: :name_only, api_prefix: :with_prefix, api_postfix: :with_postfix, id: @luke.id }
      end

      it 'should have a root node named user' do
        expect(response_body).to have_selector('user')
      end

      it 'should contain the specified attributes' do
        expect(response_body).to have_selector('user > prefix')
        expect(response_body).to have_selector('user > first-name')
        expect(response_body).to have_selector('user > last-name')
        expect(response_body).to have_selector('user > postfix')
      end
    end
  end
end