diff --git a/app/controllers/api/v1/snapshots/search_controller.rb b/app/controllers/api/v1/snapshots/search_controller.rb index d24e0d66b..95fe3179c 100644 --- a/app/controllers/api/v1/snapshots/search_controller.rb +++ b/app/controllers/api/v1/snapshots/search_controller.rb @@ -13,8 +13,17 @@ class SearchController < SnapshotsController # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/PerceivedComplexity + # rubocop:disable Layout/LineLength + api :GET, '/api/cases/:case_id/snapshots/:snapshot_id/search?q=:q', + 'Mimic a Solr query by looking up query/doc data from a specific snapshot, using the q parameter as the query' + param :case_id, :number, + desc: 'The ID of the requested case.', required: true + param :snapshot_id, :number, + desc: 'The ID of the snapshot for the case.', required: true + param :q, String, + desc: 'The query that you are looking up', required: true def index - @q = params[:q] + @q = search_params[:q] query = if '*:*' == @q @snapshot.snapshot_queries.first.query @@ -57,9 +66,17 @@ def index # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/PerceivedComplexity + # rubocop:enable Layout/LineLength private + def search_params + # Check if the 'q' parameter exists + raise ActionController::ParameterMissing, "Missing 'q' parameter" unless params.key?(:q) + + params + end + def set_snapshot @snapshot = @case.snapshots .where(id: params[:snapshot_id]) diff --git a/test/controllers/api/v1/snapshots/search_controller_test.rb b/test/controllers/api/v1/snapshots/search_controller_test.rb index 86f53ed9e..3f2f44e6a 100644 --- a/test/controllers/api/v1/snapshots/search_controller_test.rb +++ b/test/controllers/api/v1/snapshots/search_controller_test.rb @@ -124,6 +124,17 @@ class SearchControllerTest < ActionController::TestCase assert_equal data['responseHeader']['params'], solr_params.stringify_keys.transform_values(&:to_s) end end + + describe 'handles edge cases' do + let(:acase) { cases(:snapshot_case) } + let(:snapshot) { snapshots(:a_snapshot) } + + test 'replies with message when no parameters' do + assert_raises(ActionController::ParameterMissing) do + get :index, params: { case_id: acase.id, snapshot_id: snapshot.id } + end + end + end end end end