Commit d40f9294 authored by gaga5lala's avatar gaga5lala

REFACTOR: valid params leverage on Grape DSL

parent 9412b5ee
...@@ -11,10 +11,6 @@ module API ...@@ -11,10 +11,6 @@ module API
'issues' => ->(iid) { find_project_issue(iid) } 'issues' => ->(iid) { find_project_issue(iid) }
}.freeze }.freeze
rescue_from ArgumentError do |e|
render_api_error!(e.message, 400)
end
params do params do
requires :id, type: String, desc: 'The ID of a project' requires :id, type: String, desc: 'The ID of a project'
end end
...@@ -43,8 +39,19 @@ module API ...@@ -43,8 +39,19 @@ module API
resource :todos do resource :todos do
helpers do helpers do
params :todo_filters do
optional :action_id, Integer
optional :action, Array[String], values: Todo::ACTION_NAMES.values.map(&:to_s)
optional :author_id, Integer
optional :state, String, values: Todo.state_machine.states.map(&:name).map(&:to_s)
optional :target_id, Integer
optional :type, Array[String], values: TodosFinder.todo_types
optional :project_id, Integer
optional :group_id, Integer
end
def find_todos def find_todos
TodosFinder.new(current_user, params).execute TodosFinder.new(current_user, declared_params(include_missing: false)).execute
end end
def issuable_and_awardable?(type) def issuable_and_awardable?(type)
...@@ -76,7 +83,7 @@ module API ...@@ -76,7 +83,7 @@ module API
success Entities::Todo success Entities::Todo
end end
params do params do
use :pagination use :pagination, :todo_filters
end end
get do get do
todos = paginate(find_todos.with_entity_associations) todos = paginate(find_todos.with_entity_associations)
......
...@@ -35,9 +35,25 @@ RSpec.describe API::Todos do ...@@ -35,9 +35,25 @@ RSpec.describe API::Todos do
context 'when authenticated' do context 'when authenticated' do
context 'when invalid params' do context 'when invalid params' do
it "returns argument error" do context "invalid action" do
get api('/todos', john_doe), params: { type: 'InvalidType' } it 'returns argument error' do
expect(response).to have_gitlab_http_status(:bad_request) get api('/todos', john_doe), params: { action: 'InvalidAction' }
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context "invalid state" do
it 'returns argument error' do
get api('/todos', john_doe), params: { state: 'InvalidState' }
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context "invalid type" do
it 'returns argument error' do
get api('/todos', john_doe), params: { type: 'InvalidType' }
expect(response).to have_gitlab_http_status(:bad_request)
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment