@@ -1113,4 +1113,56 @@ def source_filter_count(controller)
11131113 # Should show current range (21-40 for page 2)
11141114 assert_select '.pagination-container .current' , text : /21 - 40 of 800/
11151115 end
1116+
1117+ test 'results can be returned in JSON format when env is set and valid token is provided' do
1118+ secret_value = 'sooper_sekret'
1119+ ClimateControl . modify FORMAT_TOKEN : secret_value do
1120+ mock_timdex_search_with_hits ( 10 )
1121+ get "/results?q=test&format=json&format_token=#{ secret_value } "
1122+ assert_response :success
1123+ assert_equal 'application/json; charset=utf-8' , response . content_type
1124+ end
1125+ end
1126+
1127+ # We don't mock anything here because the error prevents any external lookups
1128+ test 'requests for JSON results without a token generate an unauthorized error' do
1129+ secret_value = 'sooper_sekret'
1130+ ClimateControl . modify FORMAT_TOKEN : secret_value do
1131+ get '/results?q=test&format=json'
1132+ assert_response :unauthorized
1133+ end
1134+ end
1135+
1136+ # We don't mock anything here because the error prevents any external lookups
1137+ test 'requests for JSON results when env var is not set generate an unauthorized error' do
1138+ secret_value = 'irrelevant'
1139+ ClimateControl . modify FORMAT_TOKEN : '' do
1140+ get "/results?q=test&format=json&format_token=#{ secret_value } "
1141+ assert_response :unauthorized
1142+ end
1143+ end
1144+
1145+ # We don't mock anything here because the error prevents any external lookups
1146+ test 'requests for JSON results with an incorrect token generate an unauthorized error' do
1147+ secret_value = 'sooper_sekret'
1148+ wrong_secret = 'something_else'
1149+ refute_equal secret_value , wrong_secret
1150+
1151+ ClimateControl . modify FORMAT_TOKEN : secret_value do
1152+ get "/results?q=test&format=json&format_token=#{ wrong_secret } "
1153+ assert_response :unauthorized
1154+ end
1155+ end
1156+
1157+ test 'requests with unsupported format receive a 406 not-acceptable error' do
1158+ mock_timdex_search_with_hits ( 10 )
1159+ get '/results?q=test&format=xml'
1160+ assert_response :not_acceptable
1161+ end
1162+
1163+ test 'requests with invalid format receive a 406 not-acceptable error' do
1164+ mock_timdex_search_with_hits ( 10 )
1165+ get '/results?q=test&format=foo'
1166+ assert_response :not_acceptable
1167+ end
11161168end
0 commit comments