Commit 7cc4339f71be5a71e1d8a95c4524c4671e9d8a24
1 parent
44938026
Exists in
master
and in
4 other branches
API: changed status codes for project hooks functions
Different status codes in the API lib are returned on hook creation, update or deletion. If a required parameter is not given (e.g. `url` in `/projects/:id/hooks/:hook_id`) status code 400 (Bad request) is returned. On hook deletion a 200 status code is returned, regardless if the hook is present or not. This makes the DELETE function an idempotent operation. Appropriate tests are added to check these status codes.
Showing
2 changed files
with
34 additions
and
6 deletions
Show diff stats
lib/api/projects.rb
... | ... | @@ -195,11 +195,14 @@ module Gitlab |
195 | 195 | # POST /projects/:id/hooks |
196 | 196 | post ":id/hooks" do |
197 | 197 | authorize! :admin_project, user_project |
198 | + | |
199 | + error!("Url not given", 400) unless params.has_key? :url | |
200 | + | |
198 | 201 | @hook = user_project.hooks.new({"url" => params[:url]}) |
199 | 202 | if @hook.save |
200 | 203 | present @hook, with: Entities::Hook |
201 | 204 | else |
202 | - error!({'message' => '404 Not found'}, 404) | |
205 | + not_found! | |
203 | 206 | end |
204 | 207 | end |
205 | 208 | |
... | ... | @@ -215,7 +218,7 @@ module Gitlab |
215 | 218 | @hook = user_project.hooks.find(params[:hook_id]) |
216 | 219 | authorize! :admin_project, user_project |
217 | 220 | |
218 | - error!("Url not given", 400) if !params.has_key? :url | |
221 | + error!("Url not given", 400) unless params.has_key? :url | |
219 | 222 | |
220 | 223 | attrs = attributes_for_keys [:url] |
221 | 224 | if @hook.update_attributes attrs |
... | ... | @@ -234,8 +237,13 @@ module Gitlab |
234 | 237 | # DELETE /projects/:id/hooks |
235 | 238 | delete ":id/hooks" do |
236 | 239 | authorize! :admin_project, user_project |
237 | - @hook = user_project.hooks.find(params[:hook_id]) | |
238 | - @hook.destroy | |
240 | + error!("Hook id not given", 400) unless params.has_key? :hook_id | |
241 | + | |
242 | + begin | |
243 | + @hook = ProjectHook.find(params[:hook_id]) | |
244 | + @hook.destroy | |
245 | + rescue | |
246 | + end | |
239 | 247 | end |
240 | 248 | |
241 | 249 | # Get a project repository branches | ... | ... |
spec/requests/api/projects_spec.rb
... | ... | @@ -6,8 +6,8 @@ describe Gitlab::API do |
6 | 6 | let(:user) { create(:user) } |
7 | 7 | let(:user2) { create(:user) } |
8 | 8 | let(:user3) { create(:user) } |
9 | - let!(:hook) { create(:project_hook, project: project, url: "http://example.com") } | |
10 | 9 | let!(:project) { create(:project, namespace: user.namespace ) } |
10 | + let!(:hook) { create(:project_hook, project: project, url: "http://example.com") } | |
11 | 11 | let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') } |
12 | 12 | let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) } |
13 | 13 | let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) } |
... | ... | @@ -290,6 +290,11 @@ describe Gitlab::API do |
290 | 290 | }.to change {project.hooks.count}.by(1) |
291 | 291 | response.status.should == 201 |
292 | 292 | end |
293 | + | |
294 | + it "should return a 400 error if url not given" do | |
295 | + post api("/projects/#{project.id}/hooks", user) | |
296 | + response.status.should == 400 | |
297 | + end | |
293 | 298 | end |
294 | 299 | |
295 | 300 | describe "PUT /projects/:id/hooks/:hook_id" do |
... | ... | @@ -300,7 +305,7 @@ describe Gitlab::API do |
300 | 305 | json_response['url'].should == 'http://example.org' |
301 | 306 | end |
302 | 307 | |
303 | - it "should return 404 error if hook id is not found" do | |
308 | + it "should return 404 error if hook id not found" do | |
304 | 309 | put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org' |
305 | 310 | response.status.should == 404 |
306 | 311 | end |
... | ... | @@ -319,6 +324,21 @@ describe Gitlab::API do |
319 | 324 | }.to change {project.hooks.count}.by(-1) |
320 | 325 | response.status.should == 200 |
321 | 326 | end |
327 | + | |
328 | + it "should return success when deleting hook" do | |
329 | + delete api("/projects/#{project.id}/hooks", user), hook_id: hook.id | |
330 | + response.status.should == 200 | |
331 | + end | |
332 | + | |
333 | + it "should return success when deleting non existent hook" do | |
334 | + delete api("/projects/#{project.id}/hooks", user), hook_id: 42 | |
335 | + response.status.should == 200 | |
336 | + end | |
337 | + | |
338 | + it "should return a 400 error if hook id not given" do | |
339 | + delete api("/projects/#{project.id}/hooks", user) | |
340 | + response.status.should == 400 | |
341 | + end | |
322 | 342 | end |
323 | 343 | |
324 | 344 | describe "GET /projects/:id/repository/tags" do | ... | ... |