@@ -5,6 +5,10 @@ def set_content_type_header!
55end
66
77class PostsControllerTest < ActionController ::TestCase
8+ def setup
9+ JSONAPI . configuration . raise_if_parameters_not_allowed = true
10+ end
11+
812 def test_index
913 get :index
1014 assert_response :success
@@ -401,6 +405,38 @@ def test_create_extra_param
401405 assert_match /asdfg is not allowed/ , response . body
402406 end
403407
408+ def test_create_extra_param_allow_extra_params
409+ JSONAPI . configuration . raise_if_parameters_not_allowed = false
410+
411+ set_content_type_header!
412+ post :create ,
413+ {
414+ data : {
415+ type : 'posts' ,
416+ attributes : {
417+ asdfg : 'aaaa' ,
418+ title : 'JR is Great' ,
419+ body : 'JSONAPIResources is the greatest thing since unsliced bread.'
420+ } ,
421+ relationships : {
422+ author : { data : { type : 'people' , id : '3' } }
423+ }
424+ } ,
425+ include : 'author'
426+ }
427+
428+ assert_response :created
429+ assert json_response [ 'data' ] . is_a? ( Hash )
430+ assert_equal '3' , json_response [ 'data' ] [ 'relationships' ] [ 'author' ] [ 'data' ] [ 'id' ]
431+ assert_equal 'JR is Great' , json_response [ 'data' ] [ 'attributes' ] [ 'title' ]
432+ assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.' , json_response [ 'data' ] [ 'attributes' ] [ 'body' ]
433+
434+ assert_equal 1 , json_response [ 'meta' ] [ "warnings" ] . count
435+ assert_equal "Param not allowed" , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "title" ]
436+ assert_equal "asdfg is not allowed." , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "detail" ]
437+ assert_equal 105 , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "code" ]
438+ end
439+
404440 def test_create_with_invalid_data
405441 set_content_type_header!
406442 post :create ,
@@ -575,6 +611,40 @@ def test_create_simple_unpermitted_attributes
575611 assert_match /subject/ , json_response [ 'errors' ] [ 0 ] [ 'detail' ]
576612 end
577613
614+ def test_create_simple_unpermitted_attributes_allow_extra_params
615+ JSONAPI . configuration . raise_if_parameters_not_allowed = false
616+
617+ set_content_type_header!
618+ post :create ,
619+ {
620+ data : {
621+ type : 'posts' ,
622+ attributes : {
623+ title : 'JR is Great' ,
624+ subject : 'JR is SUPER Great' ,
625+ body : 'JSONAPIResources is the greatest thing since unsliced bread.'
626+ } ,
627+ relationships : {
628+ author : { data : { type : 'people' , id : '3' } }
629+ }
630+ } ,
631+ include : 'author'
632+ }
633+
634+ assert_response :created
635+ assert json_response [ 'data' ] . is_a? ( Hash )
636+ assert_equal '3' , json_response [ 'data' ] [ 'relationships' ] [ 'author' ] [ 'data' ] [ 'id' ]
637+ assert_equal 'JR is Great' , json_response [ 'data' ] [ 'attributes' ] [ 'title' ]
638+ assert_equal 'JR is Great' , json_response [ 'data' ] [ 'attributes' ] [ 'subject' ]
639+ assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.' , json_response [ 'data' ] [ 'attributes' ] [ 'body' ]
640+
641+
642+ assert_equal 1 , json_response [ 'meta' ] [ "warnings" ] . count
643+ assert_equal "Param not allowed" , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "title" ]
644+ assert_equal "subject is not allowed." , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "detail" ]
645+ assert_equal 105 , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "code" ]
646+ end
647+
578648 def test_create_with_links_to_many_type_ids
579649 set_content_type_header!
580650 post :create ,
@@ -704,6 +774,46 @@ def test_update_with_internal_server_error
704774 assert_equal title , post_object . title
705775 end
706776
777+ def test_update_with_links_allow_extra_params
778+ JSONAPI . configuration . raise_if_parameters_not_allowed = false
779+
780+ set_content_type_header!
781+ javascript = Section . find_by ( name : 'javascript' )
782+
783+ put :update ,
784+ {
785+ id : 3 ,
786+ data : {
787+ id : '3' ,
788+ type : 'posts' ,
789+ attributes : {
790+ title : 'A great new Post' ,
791+ subject : 'A great new Post' ,
792+ } ,
793+ relationships : {
794+ section : { data : { type : 'sections' , id : "#{ javascript . id } " } } ,
795+ tags : { data : [ { type : 'tags' , id : 3 } , { type : 'tags' , id : 4 } ] }
796+ }
797+ } ,
798+ include : 'tags,author,section'
799+ }
800+
801+ assert_response :success
802+ assert json_response [ 'data' ] . is_a? ( Hash )
803+ assert_equal '3' , json_response [ 'data' ] [ 'relationships' ] [ 'author' ] [ 'data' ] [ 'id' ]
804+ assert_equal javascript . id . to_s , json_response [ 'data' ] [ 'relationships' ] [ 'section' ] [ 'data' ] [ 'id' ]
805+ assert_equal 'A great new Post' , json_response [ 'data' ] [ 'attributes' ] [ 'title' ]
806+ assert_equal 'AAAA' , json_response [ 'data' ] [ 'attributes' ] [ 'body' ]
807+ assert matches_array? ( [ { 'type' => 'tags' , 'id' => '3' } , { 'type' => 'tags' , 'id' => '4' } ] ,
808+ json_response [ 'data' ] [ 'relationships' ] [ 'tags' ] [ 'data' ] )
809+
810+
811+ assert_equal 1 , json_response [ 'meta' ] [ "warnings" ] . count
812+ assert_equal "Param not allowed" , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "title" ]
813+ assert_equal "subject is not allowed." , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "detail" ]
814+ assert_equal 105 , json_response [ 'meta' ] [ "warnings" ] [ 0 ] [ "code" ]
815+ end
816+
707817 def test_update_remove_links
708818 set_content_type_header!
709819 put :update ,
@@ -1150,6 +1260,33 @@ def test_update_extra_param_in_links
11501260 assert_match /asdfg is not allowed/ , response . body
11511261 end
11521262
1263+ def test_update_extra_param_in_links_allow_extra_params
1264+ JSONAPI . configuration . raise_if_parameters_not_allowed = false
1265+
1266+ set_content_type_header!
1267+ javascript = Section . find_by ( name : 'javascript' )
1268+
1269+ put :update ,
1270+ {
1271+ id : 3 ,
1272+ data : {
1273+ type : 'posts' ,
1274+ id : '3' ,
1275+ attributes : {
1276+ title : 'A great new Post'
1277+ } ,
1278+ relationships : {
1279+ asdfg : 'aaaa'
1280+ }
1281+ }
1282+ }
1283+
1284+ assert_response :success
1285+ assert_equal "A great new Post" , json_response [ "data" ] [ "attributes" ] [ "title" ]
1286+ assert_equal "Param not allowed" , json_response [ "meta" ] [ "warnings" ] [ 0 ] [ "title" ]
1287+ assert_equal "asdfg is not allowed." , json_response [ "meta" ] [ "warnings" ] [ 0 ] [ "detail" ]
1288+ end
1289+
11531290 def test_update_missing_param
11541291 set_content_type_header!
11551292 javascript = Section . find_by ( name : 'javascript' )
0 commit comments