-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathcontroller_test.rb
4755 lines (4144 loc) · 169 KB
/
controller_test.rb
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require File.expand_path('../../test_helper', __FILE__)
def set_content_type_header!
@request.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
end
class PostsControllerTest < ActionController::TestCase
def setup
super
JSONAPI.configuration.raise_if_parameters_not_allowed = true
JSONAPI.configuration.always_include_to_one_linkage_data = false
end
def test_links_include_relative_root
Rails.application.config.relative_url_root = '/subdir'
assert_cacheable_get :index
assert json_response['data'][0]['links']['self'].include?('/subdir')
Rails.application.config.relative_url_root = nil
end
def test_index
assert_cacheable_get :index
assert_response :success
assert json_response['data'].is_a?(Array)
end
def test_accept_header_missing
@request.headers['Accept'] = nil
assert_cacheable_get :index
assert_response :success
end
def test_accept_header_jsonapi_mixed
@request.headers['Accept'] =
"#{JSONAPI::MEDIA_TYPE},#{JSONAPI::MEDIA_TYPE};charset=test"
assert_cacheable_get :index
assert_response :success
end
def test_accept_header_jsonapi_modified
@request.headers['Accept'] = "#{JSONAPI::MEDIA_TYPE};charset=test"
assert_cacheable_get :index
assert_response 406
assert_equal 'Not acceptable', json_response['errors'][0]['title']
assert_equal "All requests must use the '#{JSONAPI::MEDIA_TYPE}' Accept without media type parameters. This request specified '#{@request.headers['Accept']}'.", json_response['errors'][0]['detail']
end
def test_accept_header_jsonapi_multiple_modified
@request.headers['Accept'] =
"#{JSONAPI::MEDIA_TYPE};charset=test,#{JSONAPI::MEDIA_TYPE};charset=test"
assert_cacheable_get :index
assert_response 406
assert_equal 'Not acceptable', json_response['errors'][0]['title']
assert_equal "All requests must use the '#{JSONAPI::MEDIA_TYPE}' Accept without media type parameters. This request specified '#{@request.headers['Accept']}'.", json_response['errors'][0]['detail']
end
def test_accept_header_all
@request.headers['Accept'] = "*/*"
assert_cacheable_get :index
assert_response :success
end
def test_accept_header_all_modified
@request.headers['Accept'] = "*/*;q=0.8"
assert_cacheable_get :index
assert_response :success
end
def test_accept_header_not_jsonapi
@request.headers['Accept'] = 'text/plain'
assert_cacheable_get :index
assert_response 406
assert_equal 'Not acceptable', json_response['errors'][0]['title']
assert_equal "All requests must use the '#{JSONAPI::MEDIA_TYPE}' Accept without media type parameters. This request specified '#{@request.headers['Accept']}'.", json_response['errors'][0]['detail']
end
def test_exception_class_whitelist
original_whitelist = JSONAPI.configuration.exception_class_whitelist.dup
$PostProcessorRaisesErrors = true
# test that the operations dispatcher rescues the error when it
# has not been added to the exception_class_whitelist
assert_cacheable_get :index
assert_response 500
# test that the operations dispatcher does not rescue the error when it
# has been added to the exception_class_whitelist
JSONAPI.configuration.exception_class_whitelist << PostsController::SpecialError
assert_cacheable_get :index
assert_response 403
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration.exception_class_whitelist = original_whitelist
end
def test_whitelist_all_exceptions
original_config = JSONAPI.configuration.whitelist_all_exceptions
$PostProcessorRaisesErrors = true
assert_cacheable_get :index
assert_response 500
JSONAPI.configuration.whitelist_all_exceptions = true
assert_cacheable_get :index
assert_response 403
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration.whitelist_all_exceptions = original_config
end
def test_exception_added_to_request_env
original_config = JSONAPI.configuration.whitelist_all_exceptions
$PostProcessorRaisesErrors = true
refute @request.env['action_dispatch.exception']
assert_cacheable_get :index
assert @request.env['action_dispatch.exception']
JSONAPI.configuration.whitelist_all_exceptions = true
assert_cacheable_get :index
assert @request.env['action_dispatch.exception']
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration.whitelist_all_exceptions = original_config
end
def test_exception_includes_backtrace_when_enabled
original_config = JSONAPI.configuration.include_backtraces_in_errors
$PostProcessorRaisesErrors = true
JSONAPI.configuration.include_backtraces_in_errors = true
assert_cacheable_get :index
assert_response 500
assert_includes @response.body, '"backtrace"', "expected backtrace in error body"
JSONAPI.configuration.include_backtraces_in_errors = false
assert_cacheable_get :index
assert_response 500
refute_includes @response.body, '"backtrace"', "expected backtrace in error body"
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration.include_backtraces_in_errors = original_config
end
def test_exception_includes_application_backtrace_when_enabled
original_config = JSONAPI.configuration.include_application_backtraces_in_errors
$PostProcessorRaisesErrors = true
JSONAPI.configuration.include_application_backtraces_in_errors = true
assert_cacheable_get :index
assert_response 500
assert_includes @response.body, '"application_backtrace"', "expected application backtrace in error body"
JSONAPI.configuration.include_application_backtraces_in_errors = false
assert_cacheable_get :index
assert_response 500
refute_includes @response.body, '"application_backtrace"', "expected application backtrace in error body"
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration.include_application_backtraces_in_errors = original_config
end
def test_on_server_error_block_callback_with_exception
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.exception_class_whitelist = []
$PostProcessorRaisesErrors = true
@controller.class.instance_variable_set(:@callback_message, "none")
BaseController.on_server_error do
@controller.class.instance_variable_set(:@callback_message, "Sent from block")
end
assert_cacheable_get :index
assert_equal @controller.class.instance_variable_get(:@callback_message), "Sent from block"
# test that it renders the default server error response
assert_equal "Internal Server Error", json_response['errors'][0]['title']
assert_equal "Internal Server Error", json_response['errors'][0]['detail']
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration = original_config
end
def test_on_server_error_method_callback_with_exception
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.exception_class_whitelist = []
$PostProcessorRaisesErrors = true
#ignores methods that don't exist
@controller.class.on_server_error :set_callback_message, :a_bogus_method
@controller.class.instance_variable_set(:@callback_message, "none")
assert_cacheable_get :index
assert_equal @controller.class.instance_variable_get(:@callback_message), "Sent from method"
# test that it renders the default server error response
assert_equal "Internal Server Error", json_response['errors'][0]['title']
ensure
$PostProcessorRaisesErrors = false
JSONAPI.configuration = original_config
end
def test_on_server_error_method_callback_with_exception_on_serialize
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.exception_class_whitelist = []
$PostSerializerRaisesErrors = true
#ignores methods that don't exist
@controller.class.on_server_error :set_callback_message, :a_bogus_method
@controller.class.instance_variable_set(:@callback_message, "none")
assert_cacheable_get :index
assert_equal "Sent from method", @controller.class.instance_variable_get(:@callback_message)
# test that it renders the default server error response
assert_equal "Internal Server Error", json_response['errors'][0]['title']
ensure
$PostSerializerRaisesErrors = false
JSONAPI.configuration = original_config
end
def test_on_server_error_callback_without_exception
callback = Proc.new { @controller.class.instance_variable_set(:@callback_message, "Sent from block") }
@controller.class.on_server_error callback
@controller.class.instance_variable_set(:@callback_message, "none")
assert_cacheable_get :index
assert_equal @controller.class.instance_variable_get(:@callback_message), "none"
# test that it does not render error
assert json_response.key?('data')
ensure
$PostProcessorRaisesErrors = false
end
def test_posts_index_include
assert_cacheable_get :index, params: {filter: {id: '10,12'}, include: 'author'}
assert_response :success
assert_equal 2, json_response['data'].size
assert_equal 2, json_response['included'].size
end
def test_index_filter_with_empty_result
assert_cacheable_get :index, params: {filter: {title: 'post that does not exist'}}
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 0, json_response['data'].size
end
def test_index_filter_by_single_id
assert_cacheable_get :index, params: {filter: {id: '1'}}
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 1, json_response['data'].size
end
def test_index_filter_by_title
assert_cacheable_get :index, params: {filter: {title: 'New post'}}
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 1, json_response['data'].size
end
def test_index_filter_with_hash_values
assert_cacheable_get :index, params: {filter: {search: {title: 'New post'}}}
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 1, json_response['data'].size
end
def test_index_filter_by_array_of_ids
assert_cacheable_get :index, params: {filter: {ids: '1,2'}}
assert_response :success
assert json_response['data'].is_a?(Array)
assert_equal 2, json_response['data'].size
end
def test_index_filter_by_ids_and_include_related
assert_cacheable_get :index, params: {filter: {id: '2'}, include: 'comments'}
assert_response :success
assert_equal 1, json_response['data'].size
assert_equal 1, json_response['included'].size
end
def test_index_filter_by_ids_and_include_related_different_type
assert_cacheable_get :index, params: {filter: {id: '1,2'}, include: 'author'}
assert_response :success
assert_equal 2, json_response['data'].size
assert_equal 1, json_response['included'].size
end
def test_index_filter_not_allowed
JSONAPI.configuration.allow_filter = false
assert_cacheable_get :index, params: {filter: {id: '1'}}
assert_response :bad_request
ensure
JSONAPI.configuration.allow_filter = true
end
def test_index_include_one_level_query_count
assert_query_count(4) do
assert_cacheable_get :index, params: {include: 'author'}
end
assert_response :success
end
def test_index_include_two_levels_query_count
assert_query_count(6) do
assert_cacheable_get :index, params: {include: 'author,author.comments'}
end
assert_response :success
end
def test_index_filter_by_ids_and_fields
assert_cacheable_get :index, params: {filter: {id: '1,2'}, fields: {posts: 'id,title,author'}}
assert_response :success
assert_equal 2, json_response['data'].size
# type, id, links, attributes, relationships
assert_equal 5, json_response['data'][0].size
assert json_response['data'][0].key?('type')
assert json_response['data'][0].key?('id')
assert json_response['data'][0]['attributes'].key?('title')
assert json_response['data'][0].key?('links')
end
def test_index_filter_by_ids_and_fields_specify_type
assert_cacheable_get :index, params: {filter: {id: '1,2'}, 'fields' => {'posts' => 'id,title,author'}}
assert_response :success
assert_equal 2, json_response['data'].size
# type, id, links, attributes, relationships
assert_equal 5, json_response['data'][0].size
assert json_response['data'][0].key?('type')
assert json_response['data'][0].key?('id')
assert json_response['data'][0]['attributes'].key?('title')
assert json_response['data'][0].key?('links')
end
def test_index_filter_by_ids_and_fields_specify_unrelated_type
assert_cacheable_get :index, params: {filter: {id: '1,2'}, 'fields' => {'currencies' => 'code'}}
assert_response :bad_request
assert_match /currencies is not a valid resource./, json_response['errors'][0]['detail']
end
def test_index_filter_by_ids_and_fields_2
assert_cacheable_get :index, params: {filter: {id: '1,2'}, fields: {posts: 'author'}}
assert_response :success
assert_equal 2, json_response['data'].size
# type, id, links, relationships
assert_equal 4, json_response['data'][0].size
assert json_response['data'][0].key?('type')
assert json_response['data'][0].key?('id')
assert json_response['data'][0]['relationships'].key?('author')
end
def test_filter_relationship_single
assert_query_count(2) do
assert_cacheable_get :index, params: {filter: {tags: '505,501'}}
end
assert_response :success
assert_equal 3, json_response['data'].size
assert_match /New post/, response.body
assert_match /JR Solves your serialization woes!/, response.body
assert_match /JR How To/, response.body
end
def test_filter_relationships_multiple
assert_query_count(2) do
assert_cacheable_get :index, params: {filter: {tags: '505,501', comments: '3'}}
end
assert_response :success
assert_equal 1, json_response['data'].size
assert_match /JR Solves your serialization woes!/, response.body
end
def test_filter_relationships_multiple_not_found
assert_cacheable_get :index, params: {filter: {tags: '501', comments: '3'}}
assert_response :success
assert_equal 0, json_response['data'].size
end
def test_bad_filter
assert_cacheable_get :index, params: {filter: {post_ids: '1,2'}}
assert_response :bad_request
assert_match /post_ids is not allowed/, response.body
end
def test_bad_filter_value_not_integer_array
assert_cacheable_get :index, params: {filter: {id: 'asdfg'}}
assert_response :bad_request
assert_match /asdfg is not a valid value for id/, response.body
end
def test_bad_filter_value_not_integer
assert_cacheable_get :index, params: {filter: {id: 'asdfg'}}
assert_response :bad_request
assert_match /asdfg is not a valid value for id/, response.body
end
def test_bad_filter_value_not_found_array
assert_cacheable_get :index, params: {filter: {id: '5412333'}}
assert_response :not_found
assert_match /5412333 could not be found/, response.body
end
def test_bad_filter_value_not_found
assert_cacheable_get :index, params: {filter: {id: '5412333'}}
assert_response :not_found
assert_match /5412333 could not be found/, json_response['errors'][0]['detail']
end
def test_field_not_supported
assert_cacheable_get :index, params: {filter: {id: '1,2'}, 'fields' => {'posts' => 'id,title,rank,author'}}
assert_response :bad_request
assert_match /rank is not a valid field for posts./, json_response['errors'][0]['detail']
end
def test_resource_not_supported
assert_cacheable_get :index, params: {filter: {id: '1,2'}, 'fields' => {'posters' => 'id,title'}}
assert_response :bad_request
assert_match /posters is not a valid resource./, json_response['errors'][0]['detail']
end
def test_index_filter_on_relationship
assert_cacheable_get :index, params: {filter: {author: '1001'}}
assert_response :success
assert_equal 3, json_response['data'].size
end
def test_sorting_blank
assert_cacheable_get :index, params: {sort: ''}
assert_response :success
end
def test_sorting_asc
assert_cacheable_get :index, params: {sort: 'title'}
assert_response :success
assert_equal "A 1ST Post", json_response['data'][0]['attributes']['title']
end
def test_sorting_desc
assert_cacheable_get :index, params: {sort: '-title'}
assert_response :success
assert_equal "Update This Later - Multiple", json_response['data'][0]['attributes']['title']
end
def test_sorting_by_multiple_fields
assert_cacheable_get :index, params: {sort: 'title,body'}
assert_response :success
assert_equal '15', json_response['data'][0]['id']
end
def create_alphabetically_first_user_and_post
author = Person.create(name: "Aardvark", date_joined: Time.now)
author.posts.create(title: "My first post", body: "Hello World")
end
def test_sorting_by_relationship_field
post = create_alphabetically_first_user_and_post
assert_cacheable_get :index, params: {sort: 'author.name'}
assert_response :success
assert json_response['data'].length > 10, 'there are enough records to show sort'
# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
if ENV['DATABASE_URL'].starts_with?('postgres')
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the start'
assert_equal post.id.to_s, json_response['data'][0]['id'], 'alphabetically first user is not first'
else
assert_equal '17', json_response['data'][0]['id'], 'nil is at the end'
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
end
end
def test_desc_sorting_by_relationship_field
post = create_alphabetically_first_user_and_post
assert_cacheable_get :index, params: {sort: '-author.name'}
assert_response :success
assert json_response['data'].length > 10, 'there are enough records to show sort'
# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
if ENV['DATABASE_URL'].starts_with?('postgres')
assert_equal '17', json_response['data'][0]['id'], 'nil is at the start'
assert_equal post.id.to_s, json_response['data'][-1]['id']
else
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the end'
assert_equal post.id.to_s, json_response['data'][-2]['id'], 'alphabetically first user is second last'
end
end
def test_sorting_by_relationship_field_include
post = create_alphabetically_first_user_and_post
assert_cacheable_get :index, params: {include: 'author', sort: 'author.name'}
assert_response :success
assert json_response['data'].length > 10, 'there are enough records to show sort'
if ENV['DATABASE_URL'].starts_with?('postgres')
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the top'
assert_equal post.id.to_s, json_response['data'][0]['id']
else
assert_equal '17', json_response['data'][0]['id'], 'nil is at the top'
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
end
end
def test_invalid_sort_param
assert_cacheable_get :index, params: {sort: 'asdfg'}
assert_response :bad_request
assert_match /asdfg is not a valid sort criteria for post/, response.body
end
def test_show_single_with_sort_disallowed
JSONAPI.configuration.allow_sort = false
assert_cacheable_get :index, params: {sort: 'title,body'}
assert_response :bad_request
ensure
JSONAPI.configuration.allow_sort = true
end
def test_excluded_sort_param
assert_cacheable_get :index, params: {sort: 'id'}
assert_response :bad_request
assert_match /id is not a valid sort criteria for post/, response.body
end
def test_show_single_no_includes
assert_cacheable_get :show, params: {id: '1'}
assert_response :success
assert json_response['data'].is_a?(Hash)
assert_equal 'New post', json_response['data']['attributes']['title']
assert_equal 'A body!!!', json_response['data']['attributes']['body']
assert_nil json_response['included']
end
def test_show_does_not_include_records_count_in_meta
JSONAPI.configuration.top_level_meta_include_record_count = true
assert_cacheable_get :show, params: { id: Post.first.id }
assert_response :success
assert_nil json_response['meta']
ensure
JSONAPI.configuration.top_level_meta_include_record_count = false
end
def test_show_does_not_include_pages_count_in_meta
JSONAPI.configuration.top_level_meta_include_page_count = true
assert_cacheable_get :show, params: { id: Post.first.id }
assert_response :success
assert_nil json_response['meta']
ensure
JSONAPI.configuration.top_level_meta_include_page_count = false
end
def test_show_single_with_has_one_include_included_exists
assert_cacheable_get :show, params: {id: '1', include: 'author'}
assert_response :success
assert_equal 1, json_response['included'].size
assert json_response['data']['relationships']['author'].has_key?('data'), 'Missing required data key'
refute_nil json_response['data']['relationships']['author']['data'], 'Data should not be nil'
refute json_response['data']['relationships']['tags'].has_key?('data'), 'Not included relationships should not have data'
end
def test_show_single_with_has_one_include_included_does_not_exist
assert_cacheable_get :show, params: {id: '1', include: 'section'}
assert_response :success
assert_nil json_response['included']
assert json_response['data']['relationships']['section'].has_key?('data'), 'Missing required data key'
assert_nil json_response['data']['relationships']['section']['data'], 'Data should be nil'
refute json_response['data']['relationships']['tags'].has_key?('data'), 'Not included relationships should not have data'
end
def test_show_single_with_has_many_include
assert_cacheable_get :show, params: {id: '1', include: 'comments'}
assert_response :success
assert json_response['data'].is_a?(Hash)
assert_equal 'New post', json_response['data']['attributes']['title']
assert_equal 'A body!!!', json_response['data']['attributes']['body']
assert_nil json_response['data']['relationships']['tags']['data']
assert matches_array?([{'type' => 'comments', 'id' => '1'}, {'type' => 'comments', 'id' => '2'}],
json_response['data']['relationships']['comments']['data'])
assert_equal 2, json_response['included'].size
end
def test_includes_for_empty_relationships_shows_but_are_empty
assert_cacheable_get :show, params: {id: '17', include: 'author,tags'}
assert_response :success
assert json_response['data']['relationships']['author'].has_key?('data'), 'data key should exist for empty has_one relationship'
assert_nil json_response['data']['relationships']['author']['data'], 'Data should be null'
assert json_response['data']['relationships']['tags'].has_key?('data'), 'data key should exist for empty has_many relationship'
assert json_response['data']['relationships']['tags']['data'].is_a?(Array), 'Data should be array'
assert json_response['data']['relationships']['tags']['data'].empty?, 'Data array should be empty'
end
def test_show_single_with_include_disallowed
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.allow_include = false
assert_cacheable_get :show, params: {id: '1', include: 'comments'}
assert_response :bad_request
ensure
JSONAPI.configuration = original_config
end
def test_show_single_include_linkage
JSONAPI.configuration.always_include_to_one_linkage_data = true
assert_cacheable_get :show, params: {id: '17'}
assert_response :success
assert json_response['data']['relationships']['author'].has_key?('data'), 'data key should exist for empty has_one relationship'
assert_nil json_response['data']['relationships']['author']['data'], 'Data should be null'
refute json_response['data']['relationships']['tags'].has_key?('data'), 'data key should not exist for empty has_many relationship if not included'
ensure
JSONAPI.configuration.always_include_to_one_linkage_data = false
end
def test_index_single_include_linkage
JSONAPI.configuration.always_include_to_one_linkage_data = true
assert_cacheable_get :index, params: { filter: { id: '17'} }
assert_response :success
assert json_response['data'][0]['relationships']['author'].has_key?('data'), 'data key should exist for empty has_one relationship'
assert_nil json_response['data'][0]['relationships']['author']['data'], 'Data should be null'
refute json_response['data'][0]['relationships']['tags'].has_key?('data'), 'data key should not exist for empty has_many relationship if not included'
ensure
JSONAPI.configuration.always_include_to_one_linkage_data = false
end
def test_show_single_with_fields
assert_cacheable_get :show, params: {id: '1', fields: {posts: 'author'}}
assert_response :success
assert json_response['data'].is_a?(Hash)
assert_nil json_response['data']['attributes']
end
def test_show_single_with_fields_string
assert_cacheable_get :show, params: {id: '1', fields: 'author'}
assert_response :bad_request
assert_match /Fields must specify a type./, json_response['errors'][0]['detail']
end
def test_show_single_invalid_id_format
assert_cacheable_get :show, params: {id: 'asdfg'}
assert_response :bad_request
assert_match /asdfg is not a valid value for id/, response.body
end
def test_show_single_missing_record
assert_cacheable_get :show, params: {id: '5412333'}
assert_response :not_found
assert_match /record identified by 5412333 could not be found/, response.body
end
def test_show_malformed_fields_not_list
assert_cacheable_get :show, params: {id: '1', 'fields' => ''}
assert_response :bad_request
assert_match /Fields must specify a type./, json_response['errors'][0]['detail']
end
def test_show_malformed_fields_type_not_list
assert_cacheable_get :show, params: {id: '1', 'fields' => {'posts' => ''}}
assert_response :bad_request
assert_match /nil is not a valid field for posts./, json_response['errors'][0]['detail']
end
def test_create_simple
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :created
assert json_response['data'].is_a?(Hash)
assert_equal 'JR is Great', json_response['data']['attributes']['title']
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
assert_equal json_response['data']['links']['self'], response.location
end
def test_create_simple_id_not_allowed
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
id: 'asdfg',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /id is not allowed/, response.body
assert_nil response.location
end
def test_create_link_to_missing_object
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '304567'}}
}
}
}
assert_response :unprocessable_entity
# TODO: check if this validation is working
assert_match /author - can't be blank/, response.body
assert_nil response.location
end
def test_create_bad_relationship_array
set_content_type_header!
put :create, params:
{
data: {
type: 'posts',
attributes: {
title: 'A poorly formed new Post'
},
relationships: {
author: {data: {type: 'people', id: '1003'}},
tags: []
}
}
}
assert_response :bad_request
assert_match /Data is not a valid Links Object./, response.body
end
def test_create_extra_param
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
asdfg: 'aaaa',
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /asdfg is not allowed/, response.body
assert_nil response.location
end
def test_create_extra_param_allow_extra_params
JSONAPI.configuration.raise_if_parameters_not_allowed = false
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
id: 'my_id',
attributes: {
asdfg: 'aaaa',
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
},
include: 'author'
}
assert_response :created
assert json_response['data'].is_a?(Hash)
assert_equal '1003', json_response['data']['relationships']['author']['data']['id']
assert_equal 'JR is Great', json_response['data']['attributes']['title']
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
assert_equal 2, json_response['meta']["warnings"].count
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
assert_equal "id is not allowed.", json_response['meta']["warnings"][0]["detail"]
assert_equal '105', json_response['meta']["warnings"][0]["code"]
assert_equal "Param not allowed", json_response['meta']["warnings"][1]["title"]
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][1]["detail"]
assert_equal '105', json_response['meta']["warnings"][1]["code"]
assert_equal json_response['data']['links']['self'], response.location
ensure
JSONAPI.configuration.raise_if_parameters_not_allowed = true
end
def test_create_with_invalid_data
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
title: 'JSONAPIResources is the greatest thing...',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: nil
}
}
}
assert_response :unprocessable_entity
assert_equal "/data/relationships/author", json_response['errors'][0]['source']['pointer']
assert_equal "can't be blank", json_response['errors'][0]['title']
assert_equal "author - can't be blank", json_response['errors'][0]['detail']
assert_equal "/data/attributes/title", json_response['errors'][1]['source']['pointer']
assert_equal "is too long (maximum is 35 characters)", json_response['errors'][1]['title']
assert_equal "title - is too long (maximum is 35 characters)", json_response['errors'][1]['detail']
assert_nil response.location
end
def test_create_multiple
set_content_type_header!
post :create, params:
{
data: [
{
type: 'posts',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
},
{
type: 'posts',
attributes: {
title: 'Ember is Great',
body: 'Ember is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
]
}
assert_response :bad_request
assert_match /Invalid data format/, response.body
end
def test_create_simple_missing_posts
set_content_type_header!
post :create, params:
{
data_spelled_wrong: {
type: 'posts',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /The required parameter, data, is missing./, json_response['errors'][0]['detail']
assert_nil response.location
end
def test_create_simple_wrong_type
set_content_type_header!
post :create, params:
{
data: {
type: 'posts_spelled_wrong',
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /posts_spelled_wrong is not a valid resource./, json_response['errors'][0]['detail']
assert_nil response.location
end
def test_create_simple_missing_type
set_content_type_header!
post :create, params:
{
data: {
attributes: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /The required parameter, type, is missing./, json_response['errors'][0]['detail']
assert_nil response.location
end
def test_create_simple_unpermitted_attributes
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
subject: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
}
}
assert_response :bad_request
assert_match /subject/, json_response['errors'][0]['detail']
assert_nil response.location
end
def test_create_simple_unpermitted_attributes_allow_extra_params
JSONAPI.configuration.raise_if_parameters_not_allowed = false
set_content_type_header!
post :create, params:
{
data: {
type: 'posts',
attributes: {
title: 'JR is Great',
subject: 'JR is SUPER Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
},
relationships: {
author: {data: {type: 'people', id: '1003'}}
}
},
include: 'author'
}
assert_response :created
assert json_response['data'].is_a?(Hash)
assert_equal '1003', json_response['data']['relationships']['author']['data']['id']
assert_equal 'JR is Great', json_response['data']['attributes']['title']
assert_equal 'JR is Great', json_response['data']['attributes']['subject']
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']