acts_as_api 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,635 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
2
-
3
- describe "acts_as_api", :orm => :active_record do
4
-
5
- before(:each) do
6
- @luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
7
- @han = User.create({ :first_name => 'Han', :last_name => 'Solo', :age => 35, :active => true })
8
- @leia = User.create({ :first_name => 'Princess', :last_name => 'Leia', :age => 25, :active => false })
9
-
10
- @luke.profile = Profile.create({ :avatar => 'picard.jpg', :homepage => 'lukasarts.com' })
11
-
12
- @destroy_deathstar = @luke.tasks.create({ :heading => "Destroy Deathstar", :description => "XWing, Shoot, BlowUp", :time_spent => 30, :done => true })
13
- @study_with_yoda = @luke.tasks.create({ :heading => "Study with Yoda", :description => "Jedi Stuff, ya know", :time_spent => 60, :done => true })
14
- @win_rebellion = @luke.tasks.create({ :heading => "Win Rebellion", :description => "no idea yet...", :time_spent => 180, :done => false })
15
- end
16
-
17
- after(:each) do
18
- User.delete_all
19
- Task.delete_all
20
- end
21
-
22
- describe "is disabled by default" do
23
- it "should indicate that acts_as_api is disabled" do
24
- Untouched.acts_as_api?.should be_false
25
- end
26
-
27
- it "should not respond to api_accessible" do
28
- Untouched.should_not respond_to :api_accessible
29
- end
30
- end
31
-
32
- describe "is enabled" do
33
-
34
- before(:each) do
35
- User.acts_as_api
36
- end
37
-
38
- it "should indicate that acts_as_api is enabled" do
39
- User.acts_as_api?.should be_true
40
- end
41
-
42
- it "should not respond to api_accessible" do
43
- User.should respond_to :api_accessible
44
- end
45
-
46
- describe "listing attributes in the api template" do
47
-
48
- before(:each) do
49
- @response = @luke.as_api_response(:name_only)
50
- end
51
-
52
- it "should return a hash" do
53
- @response.should be_kind_of(Hash)
54
- end
55
-
56
- it "should return the correct number of keys" do
57
- @response.should have(2).keys
58
- end
59
-
60
- it "should return all specified fields" do
61
- @response.keys.should include(:first_name, :last_name)
62
- end
63
-
64
- it "should return the correct values for the specified fields" do
65
- @response.values.should include(@luke.first_name, @luke.last_name)
66
- end
67
-
68
- end
69
-
70
- describe "trying to render an api template that is not defined" do
71
-
72
- it "should raise an descriptive error" do
73
- lambda{ @luke.as_api_response(:does_not_exist) }.should raise_error(RuntimeError)
74
- end
75
-
76
- end
77
-
78
- describe "calling a method in the api template" do
79
-
80
- before(:each) do
81
- @response = @luke.as_api_response(:only_full_name)
82
- end
83
-
84
- it "should return a hash" do
85
- @response.should be_kind_of(Hash)
86
- end
87
-
88
- it "should return the correct number of keys" do
89
- @response.should have(1).key
90
- end
91
-
92
- it "should return all specified fields" do
93
- @response.keys.should include(:full_name)
94
- end
95
-
96
- it "should return the correct values for the specified fields" do
97
- @response.values.should include(@luke.full_name)
98
- end
99
-
100
- end
101
-
102
- describe "renaming an attribute in the api template" do
103
-
104
- before(:each) do
105
- @response = @luke.as_api_response(:rename_last_name)
106
- end
107
-
108
- it "should return a hash" do
109
- @response.should be_kind_of(Hash)
110
- end
111
-
112
- it "should return the correct number of keys" do
113
- @response.should have(1).key
114
- end
115
-
116
- it "should return all specified fields" do
117
- @response.keys.should include(:family_name)
118
- end
119
-
120
- it "should return the correct values for the specified fields" do
121
- @response.values.should include(@luke.last_name)
122
- end
123
-
124
- end
125
-
126
- describe "renaming the node/key of a method in the api template" do
127
-
128
- before(:each) do
129
- @response = @luke.as_api_response(:rename_full_name)
130
- end
131
-
132
- it "should return a hash" do
133
- @response.should be_kind_of(Hash)
134
- end
135
-
136
- it "should return the correct number of keys" do
137
- @response.should have(1).key
138
- end
139
-
140
- it "should return all specified fields" do
141
- @response.keys.should include(:other_full_name)
142
- end
143
-
144
- it "should return the correct values for the specified fields" do
145
- @response.values.should include(@luke.full_name)
146
- end
147
-
148
- end
149
-
150
- describe "extending a given api template (multiple times)" do
151
-
152
- before(:each) do
153
- User.api_accessible :public do |t|
154
- t.add :first_name
155
- end
156
-
157
- User.api_accessible :for_buddies, :extend => :public do |t|
158
- t.add :age
159
- end
160
-
161
- User.api_accessible :private, :extend => :for_buddies do |t|
162
- t.add :last_name
163
- end
164
- @response = @luke.as_api_response(:private)
165
- end
166
-
167
- it "should return a hash" do
168
- @response.should be_kind_of(Hash)
169
- end
170
-
171
- it "should return the correct number of keys" do
172
- @response.should have(3).keys
173
- end
174
-
175
- it "should return all specified fields" do
176
- @response.keys.sort_by(&:to_s).should eql([:age, :first_name, :last_name])
177
- end
178
-
179
- it "should return the correct values for the specified fields" do
180
- @response.values.sort_by(&:to_s).should eql([@luke.age, @luke.first_name, @luke.last_name].sort_by(&:to_s))
181
- end
182
-
183
- end
184
-
185
- describe "extending a given api template and removing a former added value" do
186
-
187
- before(:each) do
188
- @response = @luke.as_api_response(:age_and_first_name)
189
- end
190
-
191
- it "should return a hash" do
192
- @response.should be_kind_of(Hash)
193
- end
194
-
195
- it "should return the correct number of keys" do
196
- @response.should have(2).keys
197
- end
198
-
199
- it "should return all specified fields" do
200
- @response.keys.sort_by(&:to_s).should eql([:first_name, :age].sort_by(&:to_s))
201
- end
202
-
203
- it "should return the correct values for the specified fields" do
204
- @response.values.sort_by(&:to_s).should eql([@luke.first_name, @luke.age].sort_by(&:to_s))
205
- end
206
-
207
- end
208
-
209
- describe "calling a proc in the api (the record is passed as only parameter)" do
210
-
211
- before(:each) do
212
- @response = @luke.as_api_response(:calling_a_proc)
213
- end
214
-
215
- it "should return a hash" do
216
- @response.should be_kind_of(Hash)
217
- end
218
-
219
- it "should return the correct number of keys" do
220
- @response.should have(2).keys
221
- end
222
-
223
- it "should return all specified fields" do
224
- @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
225
- end
226
-
227
- it "should return the correct values for the specified fields" do
228
- @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
229
- end
230
- end
231
-
232
- describe "calling a lambda in the api (the record is passed as only parameter)" do
233
-
234
- before(:each) do
235
- @response = @luke.as_api_response(:calling_a_lambda)
236
- end
237
-
238
- it "should return a hash" do
239
- @response.should be_kind_of(Hash)
240
- end
241
-
242
- it "should return the correct number of keys" do
243
- @response.should have(2).keys
244
- end
245
-
246
- it "should return all specified fields" do
247
- @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
248
- end
249
-
250
- it "should return the correct values for the specified fields" do
251
- @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
252
- end
253
-
254
- end
255
-
256
- describe "including an association (which doesn't acts_as_api) in the api template" do
257
-
258
- before(:each) do
259
- @response = @luke.as_api_response(:include_tasks)
260
- end
261
-
262
- it "should return a hash" do
263
- @response.should be_kind_of(Hash)
264
- end
265
-
266
- it "should return the correct number of keys" do
267
- @response.should have(1).key
268
- end
269
-
270
- it "should return all specified fields" do
271
- @response.keys.should include(:tasks)
272
- end
273
-
274
- it "should return the correct values for the specified fields" do
275
- @response[:tasks].should be_an Array
276
- @response[:tasks].should have(3).tasks
277
- end
278
-
279
- it "should contain the associated sub models" do
280
- @response[:tasks].should include(@destroy_deathstar, @study_with_yoda, @win_rebellion)
281
- end
282
-
283
- end
284
-
285
- describe "including an association (which does acts_as_api) in the api template" do
286
-
287
- context "has_many" do
288
-
289
- before(:each) do
290
- Task.acts_as_api
291
- Task.api_accessible :include_tasks do |t|
292
- t.add :heading
293
- t.add :done
294
- end
295
- @response = @luke.as_api_response(:include_tasks)
296
- end
297
-
298
- it "should return a hash" do
299
- @response.should be_kind_of(Hash)
300
- end
301
-
302
- it "should return the correct number of keys" do
303
- @response.should have(1).key
304
- end
305
-
306
- it "should return all specified fields" do
307
- @response.keys.should include(:tasks)
308
- end
309
-
310
- it "should return the correct values for the specified fields" do
311
- @response[:tasks].should be_an Array
312
- @response[:tasks].should have(3).tasks
313
- end
314
-
315
- it "should contain the associated child models with the determined api template" do
316
- @response[:tasks].each do |task|
317
- task.keys.should include(:heading, :done)
318
- task.keys.should have(2).attributes
319
- end
320
- end
321
-
322
- it "should contain the correct data of the child models" do
323
- task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
324
- @response[:tasks].should eql task_hash
325
- end
326
-
327
- end
328
-
329
- context "has_one" do
330
-
331
- before(:each) do
332
- Profile.acts_as_api
333
- Profile.api_accessible :include_profile do |t|
334
- t.add :avatar
335
- t.add :homepage
336
- end
337
- @response = @luke.as_api_response(:include_profile)
338
- end
339
-
340
- it "should return a hash" do
341
- @response.should be_kind_of(Hash)
342
- end
343
-
344
- it "should return the correct number of keys" do
345
- @response.should have(1).key
346
- end
347
-
348
- it "should return all specified fields" do
349
- @response.keys.should include(:profile)
350
- end
351
-
352
- it "should return the correct values for the specified fields" do
353
- @response[:profile].should be_a Hash
354
- @response[:profile].should have(2).attributes
355
- end
356
-
357
- it "should contain the associated child models with the determined api template" do
358
- @response[:profile].keys.should include(:avatar, :homepage)
359
- end
360
-
361
- it "should contain the correct data of the child models" do
362
- profile_hash = { :avatar => @luke.profile.avatar, :homepage => @luke.profile.homepage }
363
- @response[:profile].should eql profile_hash
364
- end
365
-
366
- end
367
-
368
- end
369
-
370
- describe "including an association (which does acts_as_api, but with using another template name) in the api template", :meow => true do
371
-
372
- before(:each) do
373
- Task.acts_as_api
374
- Task.api_accessible :other_template do |t|
375
- t.add :description
376
- t.add :time_spent
377
- end
378
- @response = @luke.as_api_response(:other_sub_template)
379
- end
380
-
381
- it "should return a hash" do
382
- @response.should be_kind_of(Hash)
383
- end
384
-
385
- it "should return the correct number of keys" do
386
- @response.should have(2).keys
387
- end
388
-
389
- it "should return all specified fields" do
390
- @response.keys.should include(:first_name)
391
- end
392
-
393
- it "should return the correct values for the specified fields" do
394
- @response.values.should include(@luke.first_name)
395
- end
396
-
397
- it "should return all specified fields" do
398
- @response.keys.should include(:tasks)
399
- end
400
-
401
- it "should return the correct values for the specified fields" do
402
- @response[:tasks].should be_an Array
403
- @response[:tasks].should have(3).tasks
404
- end
405
-
406
- it "should contain the associated child models with the determined api template" do
407
- @response[:tasks].each do |task|
408
- task.keys.should include(:description, :time_spent)
409
- task.keys.should have(2).attributes
410
- end
411
- end
412
-
413
- it "should contain the correct data of the child models" do
414
- task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
415
- @response[:tasks].should eql task_hash
416
- end
417
-
418
- end
419
-
420
- describe "handling nil values in associations" do
421
-
422
- context "has_many" do
423
-
424
- before(:each) do
425
- Task.acts_as_api
426
- Task.api_accessible :include_tasks do |t|
427
- t.add :heading
428
- t.add :done
429
- end
430
- @response = @han.as_api_response(:include_tasks)
431
- end
432
-
433
- it "should return a hash" do
434
- @response.should be_kind_of(Hash)
435
- end
436
-
437
- it "should return the correct number of keys" do
438
- @response.should have(1).key
439
- end
440
-
441
- it "should return all specified fields" do
442
- @response.keys.should include(:tasks)
443
- end
444
-
445
- it "should return the correct values for the specified fields" do
446
- @response[:tasks].should be_kind_of(Array)
447
- end
448
-
449
- it "should contain the associated child models with the determined api template" do
450
- @response[:tasks].should have(0).items
451
- end
452
-
453
- end
454
-
455
- context "has one" do
456
- before(:each) do
457
- Profile.acts_as_api
458
- Profile.api_accessible :include_profile do |t|
459
- t.add :avatar
460
- t.add :homepage
461
- end
462
- @response = @han.as_api_response(:include_profile)
463
- end
464
-
465
- it "should return a hash" do
466
- @response.should be_kind_of(Hash)
467
- end
468
-
469
- it "should return the correct number of keys" do
470
- @response.should have(1).key
471
- end
472
-
473
- it "should return all specified fields" do
474
- @response.keys.should include(:profile)
475
- end
476
-
477
- it "should return the correct values for the specified fields" do
478
- @response[:profile].should be_nil
479
- end
480
- end
481
- end
482
-
483
- describe "including a scoped association in the api template" do
484
-
485
- before(:each) do
486
- # extend task model with scope
487
- class Task < ActiveRecord::Base
488
- scope :completed, where(:done => true)
489
- end
490
- Task.acts_as_api
491
- Task.api_accessible :include_completed_tasks do |t|
492
- t.add :heading
493
- t.add :done
494
- end
495
-
496
- @response = @luke.as_api_response(:include_completed_tasks)
497
- end
498
-
499
- it "should return a hash" do
500
- @response.should be_kind_of(Hash)
501
- end
502
-
503
- it "should return the correct number of keys" do
504
- @response.should have(1).key
505
- end
506
-
507
- it "should return all specified fields" do
508
- @response.keys.should include(:completed_tasks)
509
- end
510
-
511
- it "should return the correct values for the specified fields" do
512
- @response[:completed_tasks].should be_an Array
513
- @response[:completed_tasks].should have(2).tasks
514
- end
515
-
516
- it "should contain the associated child models with the determined api template" do
517
- @response[:completed_tasks].each do |task|
518
- task.keys.should include(:heading, :done)
519
- task.keys.should have(2).attributes
520
- end
521
- end
522
-
523
- it "should contain the correct data of the child models" do
524
- task_hash = [ @destroy_deathstar, @study_with_yoda ].collect{|t| { :done => t.done, :heading => t.heading } }
525
- @response[:completed_tasks].should eql task_hash
526
- end
527
-
528
- end
529
-
530
- describe "creating a sub node in the api template and putting an attribute in it" do
531
-
532
- before(:each) do
533
- @response = @luke.as_api_response(:sub_node)
534
- end
535
-
536
- it "should return a hash" do
537
- @response.should be_kind_of(Hash)
538
- end
539
-
540
- it "should return the correct number of keys" do
541
- @response.should have(1).key
542
- end
543
-
544
- it "should return all specified fields" do
545
- @response.keys.should include(:sub_nodes)
546
- end
547
-
548
- it "should return the correct values for the specified fields" do
549
- @response[:sub_nodes].should be_a Hash
550
- end
551
-
552
- it "should provide the correct number of sub nodes" do
553
- @response[:sub_nodes].should have(1).keys
554
- end
555
-
556
- it "should provide the correct sub nodes values" do
557
- @response[:sub_nodes][:foo].should eql("something")
558
- end
559
- end
560
-
561
- describe "creating multiple sub nodes in the api template and putting an attribute in it" do
562
-
563
- before(:each) do
564
- @response = @luke.as_api_response(:nested_sub_node)
565
- end
566
-
567
- it "should return a hash" do
568
- @response.should be_kind_of(Hash)
569
- end
570
-
571
- it "should return the correct number of keys" do
572
- @response.should have(1).key
573
- end
574
-
575
- it "should return all specified fields" do
576
- @response.keys.should include(:sub_nodes)
577
- end
578
-
579
- it "should return the correct values for the specified fields" do
580
- @response[:sub_nodes].should be_a Hash
581
- end
582
-
583
- it "should provide the correct number of sub nodes" do
584
- @response[:sub_nodes].should have(1).keys
585
- end
586
-
587
- it "should provide the correct number of sub nodes in the second level" do
588
- @response[:sub_nodes][:foo].should have(1).keys
589
- end
590
-
591
- it "should provide the correct sub nodes values" do
592
- @response[:sub_nodes][:foo].tap do |foo|
593
- foo[:bar].tap do |bar|
594
- bar.should eql(@luke.last_name)
595
- end
596
- end
597
- end
598
-
599
- end
600
-
601
- end
602
-
603
- describe "creating a sub hash in the api template using a method" do
604
-
605
- before(:each) do
606
- @response = @luke.as_api_response(:nested_sub_hash)
607
- end
608
-
609
- it "should return a hash" do
610
- @response.should be_kind_of(Hash)
611
- end
612
-
613
- it "should return the correct number of keys" do
614
- @response.should have(1).key
615
- end
616
-
617
- it "should return all specified fields" do
618
- @response.keys.should include(:sub_hash)
619
- end
620
-
621
- it "should provide the correct number of sub nodes" do
622
- @response[:sub_hash].should have(2).keys
623
- end
624
-
625
- it "should provide the correct sub nodes" do
626
- @response[:sub_hash].keys.should include(:foo, :hello)
627
- end
628
-
629
- it "should provide the correct values in its sub nodes" do
630
- @response[:sub_hash].values.should include("bar", "world")
631
- end
632
-
633
- end
634
-
635
- end