Commit 7cc4339f71be5a71e1d8a95c4524c4671e9d8a24

Authored by Sebastian Ziebell
1 parent 44938026

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.
lib/api/projects.rb
@@ -195,11 +195,14 @@ module Gitlab @@ -195,11 +195,14 @@ module Gitlab
195 # POST /projects/:id/hooks 195 # POST /projects/:id/hooks
196 post ":id/hooks" do 196 post ":id/hooks" do
197 authorize! :admin_project, user_project 197 authorize! :admin_project, user_project
  198 +
  199 + error!("Url not given", 400) unless params.has_key? :url
  200 +
198 @hook = user_project.hooks.new({"url" => params[:url]}) 201 @hook = user_project.hooks.new({"url" => params[:url]})
199 if @hook.save 202 if @hook.save
200 present @hook, with: Entities::Hook 203 present @hook, with: Entities::Hook
201 else 204 else
202 - error!({'message' => '404 Not found'}, 404) 205 + not_found!
203 end 206 end
204 end 207 end
205 208
@@ -215,7 +218,7 @@ module Gitlab @@ -215,7 +218,7 @@ module Gitlab
215 @hook = user_project.hooks.find(params[:hook_id]) 218 @hook = user_project.hooks.find(params[:hook_id])
216 authorize! :admin_project, user_project 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 attrs = attributes_for_keys [:url] 223 attrs = attributes_for_keys [:url]
221 if @hook.update_attributes attrs 224 if @hook.update_attributes attrs
@@ -234,8 +237,13 @@ module Gitlab @@ -234,8 +237,13 @@ module Gitlab
234 # DELETE /projects/:id/hooks 237 # DELETE /projects/:id/hooks
235 delete ":id/hooks" do 238 delete ":id/hooks" do
236 authorize! :admin_project, user_project 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 end 247 end
240 248
241 # Get a project repository branches 249 # Get a project repository branches
spec/requests/api/projects_spec.rb
@@ -6,8 +6,8 @@ describe Gitlab::API do @@ -6,8 +6,8 @@ describe Gitlab::API do
6 let(:user) { create(:user) } 6 let(:user) { create(:user) }
7 let(:user2) { create(:user) } 7 let(:user2) { create(:user) }
8 let(:user3) { create(:user) } 8 let(:user3) { create(:user) }
9 - let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }  
10 let!(:project) { create(:project, namespace: user.namespace ) } 9 let!(:project) { create(:project, namespace: user.namespace ) }
  10 + let!(:hook) { create(:project_hook, project: project, url: "http://example.com") }
11 let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') } 11 let!(:snippet) { create(:snippet, author: user, project: project, title: 'example') }
12 let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) } 12 let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) }
13 let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) } 13 let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) }
@@ -290,6 +290,11 @@ describe Gitlab::API do @@ -290,6 +290,11 @@ describe Gitlab::API do
290 }.to change {project.hooks.count}.by(1) 290 }.to change {project.hooks.count}.by(1)
291 response.status.should == 201 291 response.status.should == 201
292 end 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 end 298 end
294 299
295 describe "PUT /projects/:id/hooks/:hook_id" do 300 describe "PUT /projects/:id/hooks/:hook_id" do
@@ -300,7 +305,7 @@ describe Gitlab::API do @@ -300,7 +305,7 @@ describe Gitlab::API do
300 json_response['url'].should == 'http://example.org' 305 json_response['url'].should == 'http://example.org'
301 end 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 put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org' 309 put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
305 response.status.should == 404 310 response.status.should == 404
306 end 311 end
@@ -319,6 +324,21 @@ describe Gitlab::API do @@ -319,6 +324,21 @@ describe Gitlab::API do
319 }.to change {project.hooks.count}.by(-1) 324 }.to change {project.hooks.count}.by(-1)
320 response.status.should == 200 325 response.status.should == 200
321 end 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 end 342 end
323 343
324 describe "GET /projects/:id/repository/tags" do 344 describe "GET /projects/:id/repository/tags" do