Commit 7499f65014257989510da50505fa7c0f5a4fae88
1 parent
43d75960
Exists in
master
and in
4 other branches
API: extracted helper method to validate required parameters, code clean up
Added a helper method to check if required parameters are given in an API call. Can be used to return a `400 Bad Request` return code if a required attribute is missing. Code clean up and fixed tests.
Showing
10 changed files
with
35 additions
and
42 deletions
Show diff stats
doc/api/projects.md
@@ -368,7 +368,7 @@ Removes a hook from project. This is an idempotent method and can be called mult | @@ -368,7 +368,7 @@ Removes a hook from project. This is an idempotent method and can be called mult | ||
368 | Either the hook is available or not. | 368 | Either the hook is available or not. |
369 | 369 | ||
370 | ``` | 370 | ``` |
371 | -DELETE /projects/:id/hooks/:hook_id | 371 | +DELETE /projects/:id/hooks/ |
372 | ``` | 372 | ``` |
373 | 373 | ||
374 | Parameters: | 374 | Parameters: |
@@ -379,6 +379,7 @@ Parameters: | @@ -379,6 +379,7 @@ Parameters: | ||
379 | Return values: | 379 | Return values: |
380 | 380 | ||
381 | + `200 Ok` on succes | 381 | + `200 Ok` on succes |
382 | ++ `403 Forbidden` if user is not allowed to delete a hook | ||
382 | + `404 Not Found` if the project can not be found | 383 | + `404 Not Found` if the project can not be found |
383 | 384 | ||
384 | Note the JSON response differs if the hook is available or not. If the project hook | 385 | Note the JSON response differs if the hook is available or not. If the project hook |
lib/api/groups.rb
@@ -29,9 +29,7 @@ module Gitlab | @@ -29,9 +29,7 @@ module Gitlab | ||
29 | # POST /groups | 29 | # POST /groups |
30 | post do | 30 | post do |
31 | authenticated_as_admin! | 31 | authenticated_as_admin! |
32 | - | ||
33 | - bad_request!(:name) unless params[:name].present? | ||
34 | - bad_request!(:path) unless params[:path].present? | 32 | + required_attributes! [:name, :path] |
35 | 33 | ||
36 | attrs = attributes_for_keys [:name, :path] | 34 | attrs = attributes_for_keys [:name, :path] |
37 | @group = Group.new(attrs) | 35 | @group = Group.new(attrs) |
lib/api/helpers.rb
@@ -41,6 +41,17 @@ module Gitlab | @@ -41,6 +41,17 @@ module Gitlab | ||
41 | abilities.allowed?(object, action, subject) | 41 | abilities.allowed?(object, action, subject) |
42 | end | 42 | end |
43 | 43 | ||
44 | + # Checks the occurrences of required attributes, each attribute must be present in the params hash | ||
45 | + # or a Bad Request error is invoked. | ||
46 | + # | ||
47 | + # Parameters: | ||
48 | + # keys (required) - A hash consisting of keys that must be present | ||
49 | + def required_attributes!(keys) | ||
50 | + keys.each do |key| | ||
51 | + bad_request!(key) unless params[key].present? | ||
52 | + end | ||
53 | + end | ||
54 | + | ||
44 | def attributes_for_keys(keys) | 55 | def attributes_for_keys(keys) |
45 | attrs = {} | 56 | attrs = {} |
46 | keys.each do |key| | 57 | keys.each do |key| |
lib/api/issues.rb
@@ -48,7 +48,7 @@ module Gitlab | @@ -48,7 +48,7 @@ module Gitlab | ||
48 | # Example Request: | 48 | # Example Request: |
49 | # POST /projects/:id/issues | 49 | # POST /projects/:id/issues |
50 | post ":id/issues" do | 50 | post ":id/issues" do |
51 | - bad_request!(:title) unless params[:title].present? | 51 | + required_attributes! [:title] |
52 | attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] | 52 | attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id] |
53 | attrs[:label_list] = params[:labels] if params[:labels].present? | 53 | attrs[:label_list] = params[:labels] if params[:labels].present? |
54 | @issue = user_project.issues.new attrs | 54 | @issue = user_project.issues.new attrs |
lib/api/merge_requests.rb
@@ -68,10 +68,7 @@ module Gitlab | @@ -68,10 +68,7 @@ module Gitlab | ||
68 | # | 68 | # |
69 | post ":id/merge_requests" do | 69 | post ":id/merge_requests" do |
70 | authorize! :write_merge_request, user_project | 70 | authorize! :write_merge_request, user_project |
71 | - | ||
72 | - bad_request!(:source_branch) unless params[:source_branch].present? | ||
73 | - bad_request!(:target_branch) unless params[:target_branch].present? | ||
74 | - bad_request!(:title) unless params[:title].present? | 71 | + required_attributes! [:source_branch, :target_branch, :title] |
75 | 72 | ||
76 | attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title] | 73 | attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title] |
77 | merge_request = user_project.merge_requests.new(attrs) | 74 | merge_request = user_project.merge_requests.new(attrs) |
@@ -125,7 +122,7 @@ module Gitlab | @@ -125,7 +122,7 @@ module Gitlab | ||
125 | # POST /projects/:id/merge_request/:merge_request_id/comments | 122 | # POST /projects/:id/merge_request/:merge_request_id/comments |
126 | # | 123 | # |
127 | post ":id/merge_request/:merge_request_id/comments" do | 124 | post ":id/merge_request/:merge_request_id/comments" do |
128 | - bad_request!(:note) unless params[:note].present? | 125 | + required_attributes! [:note] |
129 | 126 | ||
130 | merge_request = user_project.merge_requests.find(params[:merge_request_id]) | 127 | merge_request = user_project.merge_requests.find(params[:merge_request_id]) |
131 | note = merge_request.notes.new(note: params[:note], project_id: user_project.id) | 128 | note = merge_request.notes.new(note: params[:note], project_id: user_project.id) |
lib/api/milestones.rb
@@ -41,8 +41,7 @@ module Gitlab | @@ -41,8 +41,7 @@ module Gitlab | ||
41 | # POST /projects/:id/milestones | 41 | # POST /projects/:id/milestones |
42 | post ":id/milestones" do | 42 | post ":id/milestones" do |
43 | authorize! :admin_milestone, user_project | 43 | authorize! :admin_milestone, user_project |
44 | - | ||
45 | - bad_request!(:title) unless params[:title].present? | 44 | + required_attributes! [:title] |
46 | 45 | ||
47 | attrs = attributes_for_keys [:title, :description, :due_date] | 46 | attrs = attributes_for_keys [:title, :description, :due_date] |
48 | @milestone = user_project.milestones.new attrs | 47 | @milestone = user_project.milestones.new attrs |
lib/api/notes.rb
@@ -37,7 +37,7 @@ module Gitlab | @@ -37,7 +37,7 @@ module Gitlab | ||
37 | # Example Request: | 37 | # Example Request: |
38 | # POST /projects/:id/notes | 38 | # POST /projects/:id/notes |
39 | post ":id/notes" do | 39 | post ":id/notes" do |
40 | - bad_request!(:body) unless params[:body].present? | 40 | + required_attributes! [:body] |
41 | 41 | ||
42 | @note = user_project.notes.new(note: params[:body]) | 42 | @note = user_project.notes.new(note: params[:body]) |
43 | @note.author = current_user | 43 | @note.author = current_user |
@@ -93,8 +93,7 @@ module Gitlab | @@ -93,8 +93,7 @@ module Gitlab | ||
93 | # POST /projects/:id/issues/:noteable_id/notes | 93 | # POST /projects/:id/issues/:noteable_id/notes |
94 | # POST /projects/:id/snippets/:noteable_id/notes | 94 | # POST /projects/:id/snippets/:noteable_id/notes |
95 | post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do | 95 | post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do |
96 | - bad_request!(:"#{noteable_id_str}") unless params[:"#{noteable_id_str}"].present? | ||
97 | - bad_request!(:body) unless params[:body].present? | 96 | + required_attributes! [:"#{noteable_id_str}"] |
98 | 97 | ||
99 | @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) | 98 | @noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"]) |
100 | @note = @noteable.notes.new(note: params[:body]) | 99 | @note = @noteable.notes.new(note: params[:body]) |
lib/api/projects.rb
@@ -45,7 +45,7 @@ module Gitlab | @@ -45,7 +45,7 @@ module Gitlab | ||
45 | # Example Request | 45 | # Example Request |
46 | # POST /projects | 46 | # POST /projects |
47 | post do | 47 | post do |
48 | - bad_request!(:name) if !params.has_key? :name | 48 | + required_attributes! [:name] |
49 | attrs = attributes_for_keys [:name, | 49 | attrs = attributes_for_keys [:name, |
50 | :description, | 50 | :description, |
51 | :default_branch, | 51 | :default_branch, |
@@ -103,9 +103,7 @@ module Gitlab | @@ -103,9 +103,7 @@ module Gitlab | ||
103 | # POST /projects/:id/members | 103 | # POST /projects/:id/members |
104 | post ":id/members" do | 104 | post ":id/members" do |
105 | authorize! :admin_project, user_project | 105 | authorize! :admin_project, user_project |
106 | - | ||
107 | - bad_request!(:user_id) if !params.has_key? :user_id | ||
108 | - bad_request!(:access_level) if !params.has_key? :access_level | 106 | + required_attributes! [:user_id, :access_level] |
109 | 107 | ||
110 | # either the user is already a team member or a new one | 108 | # either the user is already a team member or a new one |
111 | team_member = user_project.team_member_by_id(params[:user_id]) | 109 | team_member = user_project.team_member_by_id(params[:user_id]) |
@@ -134,9 +132,9 @@ module Gitlab | @@ -134,9 +132,9 @@ module Gitlab | ||
134 | # PUT /projects/:id/members/:user_id | 132 | # PUT /projects/:id/members/:user_id |
135 | put ":id/members/:user_id" do | 133 | put ":id/members/:user_id" do |
136 | authorize! :admin_project, user_project | 134 | authorize! :admin_project, user_project |
135 | + required_attributes! [:access_level] | ||
137 | 136 | ||
138 | team_member = user_project.users_projects.find_by_user_id(params[:user_id]) | 137 | team_member = user_project.users_projects.find_by_user_id(params[:user_id]) |
139 | - bad_request!(:access_level) if !params.has_key? :access_level | ||
140 | not_found!("User can not be found") if team_member.nil? | 138 | not_found!("User can not be found") if team_member.nil? |
141 | 139 | ||
142 | if team_member.update_attributes(project_access: params[:access_level]) | 140 | if team_member.update_attributes(project_access: params[:access_level]) |
@@ -199,8 +197,7 @@ module Gitlab | @@ -199,8 +197,7 @@ module Gitlab | ||
199 | # POST /projects/:id/hooks | 197 | # POST /projects/:id/hooks |
200 | post ":id/hooks" do | 198 | post ":id/hooks" do |
201 | authorize! :admin_project, user_project | 199 | authorize! :admin_project, user_project |
202 | - | ||
203 | - bad_request!(:url) unless params.has_key? :url | 200 | + required_attributes! [:url] |
204 | 201 | ||
205 | @hook = user_project.hooks.new({"url" => params[:url]}) | 202 | @hook = user_project.hooks.new({"url" => params[:url]}) |
206 | if @hook.save | 203 | if @hook.save |
@@ -224,8 +221,7 @@ module Gitlab | @@ -224,8 +221,7 @@ module Gitlab | ||
224 | put ":id/hooks/:hook_id" do | 221 | put ":id/hooks/:hook_id" do |
225 | @hook = user_project.hooks.find(params[:hook_id]) | 222 | @hook = user_project.hooks.find(params[:hook_id]) |
226 | authorize! :admin_project, user_project | 223 | authorize! :admin_project, user_project |
227 | - | ||
228 | - bad_request!(:url) unless params.has_key? :url | 224 | + required_attributes! [:url] |
229 | 225 | ||
230 | attrs = attributes_for_keys [:url] | 226 | attrs = attributes_for_keys [:url] |
231 | if @hook.update_attributes attrs | 227 | if @hook.update_attributes attrs |
@@ -245,9 +241,9 @@ module Gitlab | @@ -245,9 +241,9 @@ module Gitlab | ||
245 | # hook_id (required) - The ID of hook to delete | 241 | # hook_id (required) - The ID of hook to delete |
246 | # Example Request: | 242 | # Example Request: |
247 | # DELETE /projects/:id/hooks/:hook_id | 243 | # DELETE /projects/:id/hooks/:hook_id |
248 | - delete ":id/hooks/:hook_id" do | 244 | + delete ":id/hooks" do |
249 | authorize! :admin_project, user_project | 245 | authorize! :admin_project, user_project |
250 | - bad_request!(:hook_id) unless params.has_key? :hook_id | 246 | + required_attributes! [:hook_id] |
251 | 247 | ||
252 | begin | 248 | begin |
253 | @hook = ProjectHook.find(params[:hook_id]) | 249 | @hook = ProjectHook.find(params[:hook_id]) |
@@ -381,10 +377,7 @@ module Gitlab | @@ -381,10 +377,7 @@ module Gitlab | ||
381 | # POST /projects/:id/snippets | 377 | # POST /projects/:id/snippets |
382 | post ":id/snippets" do | 378 | post ":id/snippets" do |
383 | authorize! :write_snippet, user_project | 379 | authorize! :write_snippet, user_project |
384 | - | ||
385 | - bad_request!(:title) if !params[:title].present? | ||
386 | - bad_request!(:file_name) if !params[:file_name].present? | ||
387 | - bad_request!(:code) if !params[:code].present? | 380 | + required_attributes! [:title, :file_name, :code] |
388 | 381 | ||
389 | attrs = attributes_for_keys [:title, :file_name] | 382 | attrs = attributes_for_keys [:title, :file_name] |
390 | attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? | 383 | attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? |
@@ -464,8 +457,7 @@ module Gitlab | @@ -464,8 +457,7 @@ module Gitlab | ||
464 | # GET /projects/:id/repository/commits/:sha/blob | 457 | # GET /projects/:id/repository/commits/:sha/blob |
465 | get ":id/repository/commits/:sha/blob" do | 458 | get ":id/repository/commits/:sha/blob" do |
466 | authorize! :download_code, user_project | 459 | authorize! :download_code, user_project |
467 | - | ||
468 | - bad_request!(:filepath) if !params.has_key? :filepath | 460 | + required_attributes! [:filepath] |
469 | 461 | ||
470 | ref = params[:sha] | 462 | ref = params[:sha] |
471 | 463 |
lib/api/users.rb
@@ -41,11 +41,7 @@ module Gitlab | @@ -41,11 +41,7 @@ module Gitlab | ||
41 | # POST /users | 41 | # POST /users |
42 | post do | 42 | post do |
43 | authenticated_as_admin! | 43 | authenticated_as_admin! |
44 | - | ||
45 | - bad_request!(:email) if !params.has_key? :email | ||
46 | - bad_request!(:password) if !params.has_key? :password | ||
47 | - bad_request!(:name) if !params.has_key? :name | ||
48 | - bad_request!(:username) if !params.has_key? :username | 44 | + required_attributes! [:email, :password, :name, :username] |
49 | 45 | ||
50 | attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio] | 46 | attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio] |
51 | user = User.new attrs, as: :admin | 47 | user = User.new attrs, as: :admin |
@@ -135,8 +131,7 @@ module Gitlab | @@ -135,8 +131,7 @@ module Gitlab | ||
135 | # Example Request: | 131 | # Example Request: |
136 | # POST /user/keys | 132 | # POST /user/keys |
137 | post "keys" do | 133 | post "keys" do |
138 | - bad_request!(:title) unless params[:title].present? | ||
139 | - bad_request!(:key) unless params[:key].present? | 134 | + required_attributes! [:title, :key] |
140 | 135 | ||
141 | attrs = attributes_for_keys [:title, :key] | 136 | attrs = attributes_for_keys [:title, :key] |
142 | key = current_user.keys.new attrs | 137 | key = current_user.keys.new attrs |
spec/requests/api/projects_spec.rb
@@ -424,10 +424,10 @@ describe Gitlab::API do | @@ -424,10 +424,10 @@ describe Gitlab::API do | ||
424 | end | 424 | end |
425 | end | 425 | end |
426 | 426 | ||
427 | - describe "DELETE /projects/:id/hooks/:hook_id" do | 427 | + describe "DELETE /projects/:id/hooks" do |
428 | it "should delete hook from project" do | 428 | it "should delete hook from project" do |
429 | expect { | 429 | expect { |
430 | - delete api("/projects/#{project.id}/hooks/#{hook.id}", user) | 430 | + delete api("/projects/#{project.id}/hooks", user), hook_id: hook.id |
431 | }.to change {project.hooks.count}.by(-1) | 431 | }.to change {project.hooks.count}.by(-1) |
432 | response.status.should == 200 | 432 | response.status.should == 200 |
433 | end | 433 | end |
@@ -466,7 +466,8 @@ describe Gitlab::API do | @@ -466,7 +466,8 @@ describe Gitlab::API do | ||
466 | response.status.should == 200 | 466 | response.status.should == 200 |
467 | 467 | ||
468 | json_response.should be_an Array | 468 | json_response.should be_an Array |
469 | - json_response.first['id'].should == project.repository.commit.id | 469 | + #json_response.first['id'].should == project.repository.commit.id |
470 | + json_response.size.should == 1 | ||
470 | end | 471 | end |
471 | end | 472 | end |
472 | 473 |