I' working on the tests and documentation of a searches endpoint that I implemented. I'm having trouble with properly adding the query params. Basically the request url should look like this
"/api/v3/workspaces/1/searches?filter[query]=b&filter[type]=ct:Tag,User,WorkingArea"
My controller looks like this
class SearchesController < ApiV3Controller
load_and_authorize_resource :workspace
load_and_authorize_resource :user, through: :workspace
load_and_authorize_resource :working_area, through: :workspace
load_and_authorize_resource :tag, through: :workspace
def index
@resources = relevant_search_results
render_json(@resources)
end
private
def ability_klasses
[WorkspaceAbility, UserWorkspaceAbility, WorkingAreaAbility, TagAbility]
end
def relevant_search_results
query = filtered_params[:query]
types = filtered_params[:type]
items = params[:items]
GlobalSearcher.new(query, types, items, @workspace).relevant_search_results
end
def render_json(resources)
render json: resources, status: :ok
end
def filtered_params
params.require(:filter).permit(:query, :type)
end
end
The functionality works as it should. The issue is with the the tests. Here's what the spec file looks like:
resource "Searches", :include_basic_variables, type: :api do
parameter :filter
parameter :type
parameter :items
let(:query) { "be" }
let(:type) { "ct:Tag,User,WorkingArea" }
let(:items) { "3" }
let_it_be(:workspace_id) { company.id }
explanation "Searches resource"
route "/api/v3/workspaces/:workspace_id/searches", "Index" do
with_options with_example: true, required: true do
parameter :workspace_id, "Workspace ID", type: :integer, example: 1
end
get "List all the relevant items" do
context "Authenticated" do
before { sign_in(admin) }
example 'Search results' do
do_request
expect(query_string).to eq("filter[query]=b&filter[type]=ct:Tag,User,WorkingArea&items=3")
expect(status).to eq 200
end
end
end
The error I get when running rspec is
expected: "filter[query]=b&filter[type]=ct:Tag,User,WorkingArea&items=3"
got: "query=be&type=ct%3ATag%2CUser%2CWorkingArea&items=3
Your controller takes two parameters, filter and items. type is not a parameter.
You never give filter a value. filter is a hash parameter with the keys query and type, but you have not made that connection. My understanding is rspec-api-documentation will infer the type from the value.
Note that you should not be testing the query string directly, that's an implementation detail. You should be testing the query has the desired effect.