@@ -33,6 +33,7 @@ module ActiveRecord
3333 class AdapterTest < ActiveRecord ::TestCase
3434 # I really dont think we can support legacy binds.
3535 coerce_tests! :test_select_all_with_legacy_binds
36+ coerce_tests! :test_insert_update_delete_with_legacy_binds
3637
3738 # As far as I can tell, SQL Server does not support null bytes in strings.
3839 coerce_tests! :test_update_prepared_statement
@@ -913,3 +914,85 @@ def schema_dump_path
913914 end
914915end
915916
917+ class UnsafeRawSqlTest < ActiveRecord ::TestCase
918+ coerce_tests! %r{always allows Arel}
919+ test 'order: always allows Arel' do
920+ ids_depr = with_unsafe_raw_sql_deprecated { Post . order ( Arel . sql ( "len(title)" ) ) . pluck ( :title ) }
921+ ids_disabled = with_unsafe_raw_sql_disabled { Post . order ( Arel . sql ( "len(title)" ) ) . pluck ( :title ) }
922+
923+ assert_equal ids_depr , ids_disabled
924+ end
925+
926+ test "pluck: always allows Arel" do
927+ values_depr = with_unsafe_raw_sql_deprecated { Post . includes ( :comments ) . pluck ( :title , Arel . sql ( "len(title)" ) ) }
928+ values_disabled = with_unsafe_raw_sql_disabled { Post . includes ( :comments ) . pluck ( :title , Arel . sql ( "len(title)" ) ) }
929+
930+ assert_equal values_depr , values_disabled
931+ end
932+
933+
934+ coerce_tests! %r{order: disallows invalid Array arguments}
935+ test "order: disallows invalid Array arguments" do
936+ with_unsafe_raw_sql_disabled do
937+ assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
938+ Post . order ( [ "author_id" , "len(title)" ] ) . pluck ( :id )
939+ end
940+ end
941+ end
942+
943+ coerce_tests! %r{order: allows valid Array arguments}
944+ test "order: allows valid Array arguments" do
945+ ids_expected = Post . order ( Arel . sql ( "author_id, len(title)" ) ) . pluck ( :id )
946+
947+ ids_depr = with_unsafe_raw_sql_deprecated { Post . order ( [ "author_id" , Arel . sql ( "len(title)" ) ] ) . pluck ( :id ) }
948+ ids_disabled = with_unsafe_raw_sql_disabled { Post . order ( [ "author_id" , Arel . sql ( "len(title)" ) ] ) . pluck ( :id ) }
949+
950+ assert_equal ids_expected , ids_depr
951+ assert_equal ids_expected , ids_disabled
952+ end
953+
954+ coerce_tests! %r{order: logs deprecation warning for unrecognized column}
955+ test "order: logs deprecation warning for unrecognized column" do
956+ with_unsafe_raw_sql_deprecated do
957+ assert_deprecated ( /Dangerous query method/ ) do
958+ Post . order ( "len(title)" )
959+ end
960+ end
961+ end
962+
963+ coerce_tests! %r{pluck: disallows invalid column name}
964+ test "pluck: disallows invalid column name" do
965+ with_unsafe_raw_sql_disabled do
966+ assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
967+ Post . pluck ( "len(title)" )
968+ end
969+ end
970+ end
971+
972+ coerce_tests! %r{pluck: disallows invalid column name amongst valid names}
973+ test "pluck: disallows invalid column name amongst valid names" do
974+ with_unsafe_raw_sql_disabled do
975+ assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
976+ Post . pluck ( :title , "len(title)" )
977+ end
978+ end
979+ end
980+
981+ coerce_tests! %r{pluck: disallows invalid column names with includes}
982+ test "pluck: disallows invalid column names with includes" do
983+ with_unsafe_raw_sql_disabled do
984+ assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
985+ Post . includes ( :comments ) . pluck ( :title , "len(title)" )
986+ end
987+ end
988+ end
989+
990+ coerce_tests! %r{pluck: logs deprecation warning}
991+ test "pluck: logs deprecation warning" do
992+ with_unsafe_raw_sql_deprecated do
993+ assert_deprecated ( /Dangerous query method/ ) do
994+ Post . includes ( :comments ) . pluck ( :title , "len(title)" )
995+ end
996+ end
997+ end
998+ end
0 commit comments