jsonapi-resources 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ book_author_1_1:
2
+ book_id: 1
3
+ person_id: 1
@@ -12,36 +12,41 @@ class RequestTest < ActionDispatch::IntegrationTest
12
12
  JSONAPI.configuration.route_format = :underscored_route
13
13
  end
14
14
 
15
+ def assert_jsonapi_response(expected_status)
16
+ assert_equal JSONAPI::MEDIA_TYPE, response.content_type
17
+ assert_equal expected_status, status
18
+ end
19
+
15
20
  def test_get
16
21
  get '/posts'
17
- assert_equal 200, status
22
+ assert_jsonapi_response 200
18
23
  end
19
24
 
20
25
  def test_get_inflected_resource
21
26
  get '/api/v8/numeros_telefone'
22
- assert_equal 200, status
27
+ assert_jsonapi_response 200
23
28
  end
24
29
 
25
30
  def test_get_nested_to_one
26
31
  get '/posts/1/author'
27
- assert_equal 200, status
32
+ assert_jsonapi_response 200
28
33
  end
29
34
 
30
35
  def test_get_nested_to_many
31
36
  get '/posts/1/comments'
32
- assert_equal 200, status
37
+ assert_jsonapi_response 200
33
38
  end
34
39
 
35
40
  def test_get_nested_to_many_bad_param
36
41
  get '/posts/1/comments?relationship=books'
37
- assert_equal 200, status
42
+ assert_jsonapi_response 200
38
43
  end
39
44
 
40
45
  def test_get_underscored_key
41
46
  original_config = JSONAPI.configuration.dup
42
47
  JSONAPI.configuration.json_key_format = :underscored_key
43
48
  get '/iso_currencies'
44
- assert_equal 200, status
49
+ assert_jsonapi_response 200
45
50
  assert_equal 3, json_response['data'].size
46
51
  ensure
47
52
  JSONAPI.configuration = original_config
@@ -51,7 +56,7 @@ class RequestTest < ActionDispatch::IntegrationTest
51
56
  original_config = JSONAPI.configuration.dup
52
57
  JSONAPI.configuration.json_key_format = :underscored_key
53
58
  get '/iso_currencies?filter[country_name]=Canada'
54
- assert_equal 200, status
59
+ assert_jsonapi_response 200
55
60
  assert_equal 1, json_response['data'].size
56
61
  assert_equal 'Canada', json_response['data'][0]['attributes']['country_name']
57
62
  ensure
@@ -62,7 +67,7 @@ class RequestTest < ActionDispatch::IntegrationTest
62
67
  original_config = JSONAPI.configuration.dup
63
68
  JSONAPI.configuration.json_key_format = :camelized_key
64
69
  get '/iso_currencies?filter[countryName]=Canada'
65
- assert_equal 200, status
70
+ assert_jsonapi_response 200
66
71
  assert_equal 1, json_response['data'].size
67
72
  assert_equal 'Canada', json_response['data'][0]['attributes']['countryName']
68
73
  ensure
@@ -73,7 +78,7 @@ class RequestTest < ActionDispatch::IntegrationTest
73
78
  original_config = JSONAPI.configuration.dup
74
79
  JSONAPI.configuration.json_key_format = :camelized_key
75
80
  get '/api/v4/isoCurrencies?filter[countryName]=Canada'
76
- assert_equal 200, status
81
+ assert_jsonapi_response 200
77
82
  assert_equal 1, json_response['data'].size
78
83
  assert_equal 'Canada', json_response['data'][0]['attributes']['countryName']
79
84
  ensure
@@ -85,7 +90,7 @@ class RequestTest < ActionDispatch::IntegrationTest
85
90
  JSONAPI.configuration.json_key_format = :camelized_key
86
91
  JSONAPI.configuration.route_format = :camelized_route
87
92
  get '/api/v4/expenseEntries/1/relationships/isoCurrency'
88
- assert_equal 200, status
93
+ assert_jsonapi_response 200
89
94
  assert_hash_equals({'links' => {
90
95
  'self' => 'http://www.example.com/api/v4/expenseEntries/1/relationships/isoCurrency',
91
96
  'related' => 'http://www.example.com/api/v4/expenseEntries/1/isoCurrency'
@@ -142,7 +147,7 @@ class RequestTest < ActionDispatch::IntegrationTest
142
147
  }
143
148
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
144
149
 
145
- assert_equal 200, status
150
+ assert_jsonapi_response 200
146
151
  end
147
152
 
148
153
  def test_post_single_without_content_type
@@ -181,7 +186,7 @@ class RequestTest < ActionDispatch::IntegrationTest
181
186
  }
182
187
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
183
188
 
184
- assert_equal 201, status
189
+ assert_jsonapi_response 201
185
190
  end
186
191
 
187
192
  def test_post_single_missing_data_contents
@@ -191,7 +196,7 @@ class RequestTest < ActionDispatch::IntegrationTest
191
196
  }
192
197
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
193
198
 
194
- assert_equal 400, status
199
+ assert_jsonapi_response 400
195
200
  end
196
201
 
197
202
  def test_post_single_minimal_valid
@@ -202,7 +207,7 @@ class RequestTest < ActionDispatch::IntegrationTest
202
207
  }
203
208
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
204
209
 
205
- assert_equal 201, status
210
+ assert_jsonapi_response 201
206
211
  assert_nil json_response['data']['attributes']['body']
207
212
  assert_nil json_response['data']['relationships']['post']['data']
208
213
  assert_nil json_response['data']['relationships']['author']['data']
@@ -216,7 +221,7 @@ class RequestTest < ActionDispatch::IntegrationTest
216
221
  }
217
222
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
218
223
 
219
- assert_equal 422, status
224
+ assert_jsonapi_response 422
220
225
  end
221
226
 
222
227
  def test_update_relationship_without_content_type
@@ -246,7 +251,7 @@ class RequestTest < ActionDispatch::IntegrationTest
246
251
  rogue = Comment.find_by(body: 'Rogue Comment Here')
247
252
  patch '/posts/5/relationships/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
248
253
 
249
- assert_equal 403, status
254
+ assert_jsonapi_response 403
250
255
  end
251
256
 
252
257
  def test_post_update_relationship_to_many
@@ -262,7 +267,7 @@ class RequestTest < ActionDispatch::IntegrationTest
262
267
  rogue = Comment.find_by(body: 'Rogue Comment Here')
263
268
  put '/posts/5/relationships/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
264
269
 
265
- assert_equal 403, status
270
+ assert_jsonapi_response 403
266
271
  end
267
272
 
268
273
  def test_index_content_type
@@ -352,14 +357,14 @@ class RequestTest < ActionDispatch::IntegrationTest
352
357
  def test_pagination_none
353
358
  Api::V2::BookResource.paginator :none
354
359
  get '/api/v2/books'
355
- assert_equal 200, status
360
+ assert_jsonapi_response 200
356
361
  assert_equal 901, json_response['data'].size
357
362
  end
358
363
 
359
364
  def test_pagination_offset_style
360
365
  Api::V2::BookResource.paginator :offset
361
366
  get '/api/v2/books'
362
- assert_equal 200, status
367
+ assert_jsonapi_response 200
363
368
  assert_equal JSONAPI.configuration.default_page_size, json_response['data'].size
364
369
  assert_equal 'Book 0', json_response['data'][0]['attributes']['title']
365
370
  end
@@ -367,7 +372,7 @@ class RequestTest < ActionDispatch::IntegrationTest
367
372
  def test_pagination_offset_style_offset
368
373
  Api::V2::BookResource.paginator :offset
369
374
  get '/api/v2/books?page[offset]=50'
370
- assert_equal 200, status
375
+ assert_jsonapi_response 200
371
376
  assert_equal JSONAPI.configuration.default_page_size, json_response['data'].size
372
377
  assert_equal 'Book 50', json_response['data'][0]['attributes']['title']
373
378
  end
@@ -375,7 +380,7 @@ class RequestTest < ActionDispatch::IntegrationTest
375
380
  def test_pagination_offset_style_offset_limit
376
381
  Api::V2::BookResource.paginator :offset
377
382
  get '/api/v2/books?page[offset]=50&page[limit]=20'
378
- assert_equal 200, status
383
+ assert_jsonapi_response 200
379
384
  assert_equal 20, json_response['data'].size
380
385
  assert_equal 'Book 50', json_response['data'][0]['attributes']['title']
381
386
  end
@@ -383,13 +388,13 @@ class RequestTest < ActionDispatch::IntegrationTest
383
388
  def test_pagination_offset_bad_param
384
389
  Api::V2::BookResource.paginator :offset
385
390
  get '/api/v2/books?page[irishsetter]=50&page[limit]=20'
386
- assert_equal 400, status
391
+ assert_jsonapi_response 400
387
392
  end
388
393
 
389
394
  def test_pagination_related_resources_link
390
395
  Api::V2::BookResource.paginator :offset
391
396
  get '/api/v2/books?page[limit]=2'
392
- assert_equal 200, status
397
+ assert_jsonapi_response 200
393
398
  assert_equal 2, json_response['data'].size
394
399
  assert_equal 'http://www.example.com/api/v2/books/1/book_comments',
395
400
  json_response['data'][1]['relationships']['book_comments']['links']['related']
@@ -399,7 +404,7 @@ class RequestTest < ActionDispatch::IntegrationTest
399
404
  Api::V2::BookResource.paginator :offset
400
405
  Api::V2::BookCommentResource.paginator :offset
401
406
  get '/api/v2/books/1/book_comments?page[limit]=10'
402
- assert_equal 200, status
407
+ assert_jsonapi_response 200
403
408
  assert_equal 10, json_response['data'].size
404
409
  assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
405
410
  end
@@ -427,6 +432,7 @@ class RequestTest < ActionDispatch::IntegrationTest
427
432
  end
428
433
 
429
434
  def test_filter_related_resources
435
+ Api::V2::BookCommentResource.paginator :offset
430
436
  JSONAPI.configuration.top_level_meta_include_record_count = true
431
437
  get '/api/v2/books/1/book_comments?filter[book]=2'
432
438
  assert_equal 0, json_response['meta']['record_count']
@@ -440,7 +446,7 @@ class RequestTest < ActionDispatch::IntegrationTest
440
446
  Api::V2::BookResource.paginator :offset
441
447
  Api::V2::BookCommentResource.paginator :offset
442
448
  get '/api/v2/books/10/book_comments'
443
- assert_equal 200, status
449
+ assert_jsonapi_response 200
444
450
  assert_nil json_response['links']['next']
445
451
  assert_equal 'http://www.example.com/api/v2/books/10/book_comments?page%5Blimit%5D=10&page%5Boffset%5D=0', json_response['links']['first']
446
452
  assert_equal 'http://www.example.com/api/v2/books/10/book_comments?page%5Blimit%5D=10&page%5Boffset%5D=0', json_response['links']['last']
@@ -452,7 +458,7 @@ class RequestTest < ActionDispatch::IntegrationTest
452
458
  JSONAPI.configuration.top_level_meta_include_record_count = true
453
459
 
454
460
  get '/api/v2/books/1/aliased_comments'
455
- assert_equal 200, status
461
+ assert_jsonapi_response 200
456
462
  assert_equal 26, json_response['meta']['record_count']
457
463
  ensure
458
464
  JSONAPI.configuration = original_config
@@ -462,7 +468,7 @@ class RequestTest < ActionDispatch::IntegrationTest
462
468
  Api::V2::BookResource.paginator :offset
463
469
  Api::V2::BookCommentResource.paginator :offset
464
470
  get '/api/v2/books/1/book_comments?page[limit]=10&include=author,book'
465
- assert_equal 200, status
471
+ assert_jsonapi_response 200
466
472
  assert_equal 10, json_response['data'].size
467
473
  assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
468
474
  end
@@ -471,7 +477,7 @@ class RequestTest < ActionDispatch::IntegrationTest
471
477
  Api::V2::BookResource.paginator :offset
472
478
  Api::V2::BookCommentResource.paginator :offset
473
479
  get '/api/v2/books?filter[id]=2000&page[limit]=10'
474
- assert_equal 200, status
480
+ assert_jsonapi_response 200
475
481
  assert_equal 0, json_response['data'].size
476
482
  assert_nil json_response['links']['next']
477
483
  assert_equal 'http://www.example.com/api/v2/books?filter%5Bid%5D=2000&page%5Blimit%5D=10&page%5Boffset%5D=0', json_response['links']['first']
@@ -482,7 +488,7 @@ class RequestTest < ActionDispatch::IntegrationTest
482
488
  # Api::V2::BookResource.paginator :none
483
489
  # Api::V2::BookCommentResource.paginator :none
484
490
  # get '/api/v2/books?filter[]'
485
- # assert_equal 200, status
491
+ # assert_jsonapi_response 200
486
492
  # assert_equal 10, json_response['data'].size
487
493
  # assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
488
494
  # end
@@ -490,21 +496,21 @@ class RequestTest < ActionDispatch::IntegrationTest
490
496
 
491
497
  def test_flow_self
492
498
  get '/posts'
493
- assert_equal 200, status
499
+ assert_jsonapi_response 200
494
500
  post_1 = json_response['data'][0]
495
501
 
496
502
  get post_1['links']['self']
497
- assert_equal 200, status
503
+ assert_jsonapi_response 200
498
504
  assert_hash_equals post_1, json_response['data']
499
505
  end
500
506
 
501
507
  def test_flow_link_to_one_self_link
502
508
  get '/posts'
503
- assert_equal 200, status
509
+ assert_jsonapi_response 200
504
510
  post_1 = json_response['data'][0]
505
511
 
506
512
  get post_1['relationships']['author']['links']['self']
507
- assert_equal 200, status
513
+ assert_jsonapi_response 200
508
514
  assert_hash_equals(json_response, {
509
515
  'links' => {
510
516
  'self' => 'http://www.example.com/posts/1/relationships/author',
@@ -516,11 +522,11 @@ class RequestTest < ActionDispatch::IntegrationTest
516
522
 
517
523
  def test_flow_link_to_many_self_link
518
524
  get '/posts'
519
- assert_equal 200, status
525
+ assert_jsonapi_response 200
520
526
  post_1 = json_response['data'][0]
521
527
 
522
528
  get post_1['relationships']['tags']['links']['self']
523
- assert_equal 200, status
529
+ assert_jsonapi_response 200
524
530
  assert_hash_equals(json_response,
525
531
  {
526
532
  'links' => {
@@ -537,7 +543,7 @@ class RequestTest < ActionDispatch::IntegrationTest
537
543
 
538
544
  def test_flow_link_to_many_self_link_put
539
545
  get '/posts'
540
- assert_equal 200, status
546
+ assert_jsonapi_response 200
541
547
  post_1 = json_response['data'][4]
542
548
 
543
549
  post post_1['relationships']['tags']['links']['self'],
@@ -547,7 +553,7 @@ class RequestTest < ActionDispatch::IntegrationTest
547
553
  assert_equal 204, status
548
554
 
549
555
  get post_1['relationships']['tags']['links']['self']
550
- assert_equal 200, status
556
+ assert_jsonapi_response 200
551
557
  assert_hash_equals(json_response,
552
558
  {
553
559
  'links' => {
@@ -565,12 +571,12 @@ class RequestTest < ActionDispatch::IntegrationTest
565
571
  JSONAPI.configuration.route_format = :dasherized_route
566
572
  JSONAPI.configuration.json_key_format = :dasherized_key
567
573
  get '/api/v6/purchase-orders'
568
- assert_equal 200, status
574
+ assert_jsonapi_response 200
569
575
  po_1 = json_response['data'][0]
570
576
  assert_equal 'purchase-orders', json_response['data'][0]['type']
571
577
 
572
578
  get po_1['links']['self']
573
- assert_equal 200, status
579
+ assert_jsonapi_response 200
574
580
  assert_hash_equals po_1, json_response['data']
575
581
  ensure
576
582
  JSONAPI.configuration = original_config
@@ -581,13 +587,13 @@ class RequestTest < ActionDispatch::IntegrationTest
581
587
  JSONAPI.configuration.route_format = :underscored_route
582
588
  JSONAPI.configuration.json_key_format = :dasherized_key
583
589
  get '/api/v7/purchase_orders'
584
- assert_equal 200, status
590
+ assert_jsonapi_response 200
585
591
  assert_equal 'purchase-orders', json_response['data'][0]['type']
586
592
 
587
593
  po_1 = json_response['data'][0]
588
594
 
589
595
  get po_1['links']['self']
590
- assert_equal 200, status
596
+ assert_jsonapi_response 200
591
597
  assert_hash_equals po_1, json_response['data']
592
598
  ensure
593
599
  JSONAPI.configuration = original_config
@@ -598,13 +604,13 @@ class RequestTest < ActionDispatch::IntegrationTest
598
604
  JSONAPI.configuration.route_format = :underscored_route
599
605
  JSONAPI.configuration.json_key_format = :underscored_key
600
606
  get '/api/v7/purchase_orders'
601
- assert_equal 200, status
607
+ assert_jsonapi_response 200
602
608
  assert_equal 'purchase_orders', json_response['data'][0]['type']
603
609
 
604
610
  po_1 = json_response['data'][0]
605
611
 
606
612
  get po_1['links']['self']
607
- assert_equal 200, status
613
+ assert_jsonapi_response 200
608
614
  assert_hash_equals po_1, json_response['data']
609
615
  ensure
610
616
  JSONAPI.configuration = original_config
@@ -624,7 +630,7 @@ class RequestTest < ActionDispatch::IntegrationTest
624
630
  }
625
631
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
626
632
 
627
- assert_equal 201, status
633
+ assert_jsonapi_response 201
628
634
  ensure
629
635
  JSONAPI.configuration = original_config
630
636
  end
@@ -643,7 +649,7 @@ class RequestTest < ActionDispatch::IntegrationTest
643
649
  }
644
650
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
645
651
 
646
- assert_equal 201, status
652
+ assert_jsonapi_response 201
647
653
  ensure
648
654
  JSONAPI.configuration = original_config
649
655
  end
@@ -662,7 +668,7 @@ class RequestTest < ActionDispatch::IntegrationTest
662
668
  }
663
669
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
664
670
 
665
- assert_equal 201, status
671
+ assert_jsonapi_response 201
666
672
  ensure
667
673
  JSONAPI.configuration = original_config
668
674
  end
@@ -681,7 +687,7 @@ class RequestTest < ActionDispatch::IntegrationTest
681
687
  }
682
688
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
683
689
 
684
- assert_equal 400, status
690
+ assert_jsonapi_response 400
685
691
  ensure
686
692
  JSONAPI.configuration = original_config
687
693
  end
@@ -701,7 +707,7 @@ class RequestTest < ActionDispatch::IntegrationTest
701
707
  }
702
708
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
703
709
 
704
- assert_equal 200, status
710
+ assert_jsonapi_response 200
705
711
  end
706
712
 
707
713
  def test_patch_formatted_dasherized_links
@@ -724,7 +730,7 @@ class RequestTest < ActionDispatch::IntegrationTest
724
730
  }
725
731
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
726
732
 
727
- assert_equal 200, status
733
+ assert_jsonapi_response 200
728
734
  ensure
729
735
  JSONAPI.configuration = original_config
730
736
  end
@@ -755,7 +761,7 @@ class RequestTest < ActionDispatch::IntegrationTest
755
761
  }
756
762
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
757
763
 
758
- assert_equal 200, status
764
+ assert_jsonapi_response 200
759
765
  ensure
760
766
  JSONAPI.configuration = original_config
761
767
  end
@@ -788,7 +794,7 @@ class RequestTest < ActionDispatch::IntegrationTest
788
794
  }
789
795
  }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
790
796
 
791
- assert_equal 200, status
797
+ assert_jsonapi_response 200
792
798
  ensure
793
799
  JSONAPI.configuration = original_config
794
800
  $test_user = $original_test_user
@@ -884,13 +890,13 @@ class RequestTest < ActionDispatch::IntegrationTest
884
890
 
885
891
  def test_include_parameter_allowed
886
892
  get '/api/v2/books/1/book_comments?include=author'
887
- assert_equal 200, status
893
+ assert_jsonapi_response 200
888
894
  end
889
895
 
890
896
  def test_include_parameter_not_allowed
891
897
  JSONAPI.configuration.allow_include = false
892
898
  get '/api/v2/books/1/book_comments?include=author'
893
- assert_equal 400, status
899
+ assert_jsonapi_response 400
894
900
  ensure
895
901
  JSONAPI.configuration.allow_include = true
896
902
  end
@@ -898,7 +904,7 @@ class RequestTest < ActionDispatch::IntegrationTest
898
904
  def test_filter_parameter_not_allowed
899
905
  JSONAPI.configuration.allow_filter = false
900
906
  get '/api/v2/books?filter[author]=1'
901
- assert_equal 400, status
907
+ assert_jsonapi_response 400
902
908
  ensure
903
909
  JSONAPI.configuration.allow_filter = true
904
910
  end
@@ -906,21 +912,21 @@ class RequestTest < ActionDispatch::IntegrationTest
906
912
  def test_sort_parameter_not_allowed
907
913
  JSONAPI.configuration.allow_sort = false
908
914
  get '/api/v2/books?sort=title'
909
- assert_equal 400, status
915
+ assert_jsonapi_response 400
910
916
  ensure
911
917
  JSONAPI.configuration.allow_sort = true
912
918
  end
913
919
 
914
920
  def test_getting_different_resources_when_sti
915
921
  get '/vehicles'
916
- assert_equal 200, status
922
+ assert_jsonapi_response 200
917
923
  types = json_response['data'].map{|r| r['type']}.sort
918
924
  assert_array_equals ['boats', 'cars'], types
919
925
  end
920
926
 
921
927
  def test_getting_resource_with_correct_type_when_sti
922
928
  get '/vehicles/1'
923
- assert_equal 200, status
929
+ assert_jsonapi_response 200
924
930
  assert_equal 'cars', json_response['data']['type']
925
931
  end
926
932
  end