Commit fd01f3aacda1e7e1966489e7d9a31f89745cd509

Authored by Sebastian Ziebell
1 parent 375caeef

API: fixes a few return codes for project snippets

When using project snippets via API the functions now provide status codes for
different situations other then only returning 404 error. If required parameters are missing,
e.g. `title` when creating a project snippet a 400 (Bad request) error is returned. The snippet
delete function now is idempotent and returns a 200 (Ok) regardless if the snippet with the
given id is available or not. Changing return codes of these functions has the advantage that
the 404 error is used only for resources, which are not available.

Tests added to check these status codes when handling project snippets.
lib/api/projects.rb
@@ -368,6 +368,10 @@ module Gitlab @@ -368,6 +368,10 @@ module Gitlab
368 post ":id/snippets" do 368 post ":id/snippets" do
369 authorize! :write_snippet, user_project 369 authorize! :write_snippet, user_project
370 370
  371 + error!("Title not given", 400) if !params[:title].present?
  372 + error!("Filename not given", 400) if !params[:file_name].present?
  373 + error!("Code not given", 400) if !params[:code].present?
  374 +
371 attrs = attributes_for_keys [:title, :file_name] 375 attrs = attributes_for_keys [:title, :file_name]
372 attrs[:expires_at] = params[:lifetime] if params[:lifetime].present? 376 attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
373 attrs[:content] = params[:code] if params[:code].present? 377 attrs[:content] = params[:code] if params[:code].present?
@@ -415,10 +419,12 @@ module Gitlab @@ -415,10 +419,12 @@ module Gitlab
415 # Example Request: 419 # Example Request:
416 # DELETE /projects/:id/snippets/:snippet_id 420 # DELETE /projects/:id/snippets/:snippet_id
417 delete ":id/snippets/:snippet_id" do 421 delete ":id/snippets/:snippet_id" do
418 - @snippet = user_project.snippets.find(params[:snippet_id])  
419 - authorize! :modify_snippet, @snippet  
420 -  
421 - @snippet.destroy 422 + begin
  423 + @snippet = user_project.snippets.find(params[:snippet_id])
  424 + authorize! :modify_snippet, user_project
  425 + @snippet.destroy
  426 + rescue
  427 + end
422 end 428 end
423 429
424 # Get a raw project snippet 430 # Get a raw project snippet
spec/requests/api/projects_spec.rb
@@ -411,6 +411,18 @@ describe Gitlab::API do @@ -411,6 +411,18 @@ describe Gitlab::API do
411 file_name: 'sample.rb', code: 'test' 411 file_name: 'sample.rb', code: 'test'
412 response.status.should == 400 412 response.status.should == 400
413 end 413 end
  414 +
  415 + it "should return a 400 error if file_name not given" do
  416 + post api("/projects/#{project.id}/snippets", user),
  417 + title: 'api test', code: 'test'
  418 + response.status.should == 400
  419 + end
  420 +
  421 + it "should return a 400 error if code not given" do
  422 + post api("/projects/#{project.id}/snippets", user),
  423 + title: 'api test', file_name: 'sample.rb'
  424 + response.status.should == 400
  425 + end
414 end 426 end
415 427
416 describe "PUT /projects/:id/snippets/:shippet_id" do 428 describe "PUT /projects/:id/snippets/:shippet_id" do
@@ -421,6 +433,13 @@ describe Gitlab::API do @@ -421,6 +433,13 @@ describe Gitlab::API do
421 json_response['title'].should == 'example' 433 json_response['title'].should == 'example'
422 snippet.reload.content.should == 'updated code' 434 snippet.reload.content.should == 'updated code'
423 end 435 end
  436 +
  437 + it "should update an existing project snippet with new title" do
  438 + put api("/projects/#{project.id}/snippets/#{snippet.id}", user),
  439 + title: 'other api test'
  440 + response.status.should == 200
  441 + json_response['title'].should == 'other api test'
  442 + end
424 end 443 end
425 444
426 describe "DELETE /projects/:id/snippets/:snippet_id" do 445 describe "DELETE /projects/:id/snippets/:snippet_id" do
@@ -428,6 +447,12 @@ describe Gitlab::API do @@ -428,6 +447,12 @@ describe Gitlab::API do
428 expect { 447 expect {
429 delete api("/projects/#{project.id}/snippets/#{snippet.id}", user) 448 delete api("/projects/#{project.id}/snippets/#{snippet.id}", user)
430 }.to change { Snippet.count }.by(-1) 449 }.to change { Snippet.count }.by(-1)
  450 + response.status.should == 200
  451 + end
  452 +
  453 + it "should return success when deleting unknown snippet id" do
  454 + delete api("/projects/#{project.id}/snippets/1234", user)
  455 + response.status.should == 200
431 end 456 end
432 end 457 end
433 458
@@ -436,6 +461,11 @@ describe Gitlab::API do @@ -436,6 +461,11 @@ describe Gitlab::API do
436 get api("/projects/#{project.id}/snippets/#{snippet.id}/raw", user) 461 get api("/projects/#{project.id}/snippets/#{snippet.id}/raw", user)
437 response.status.should == 200 462 response.status.should == 200
438 end 463 end
  464 +
  465 + it "should return a 404 error if raw project snippet not found" do
  466 + get api("/projects/#{project.id}/snippets/5555/raw", user)
  467 + response.status.should == 404
  468 + end
439 end 469 end
440 470
441 describe "GET /projects/:id/:sha/blob" do 471 describe "GET /projects/:id/:sha/blob" do