Commit bb24275f8d0e726aec347c8be7f199346e90793d

Authored by Sebastian Ziebell
1 parent b9d40d25

Status code 400 is returned if body is missing on note creation.

If a note is created with a POST request via API (`/projects/:id/notes`) status
code 400 is returned instead of 404. The resource itself exists but the request
is incomplete. Specs added to check different status codes when accessing, creating
and updating notes.
lib/api/notes.rb
@@ -43,6 +43,8 @@ module Gitlab @@ -43,6 +43,8 @@ module Gitlab
43 if @note.save 43 if @note.save
44 present @note, with: Entities::Note 44 present @note, with: Entities::Note
45 else 45 else
  46 + # :note is exposed as :body, but :note is set on error
  47 + error!(@note.errors[:note], 400) if @note.errors[:note].any?
46 not_found! 48 not_found!
47 end 49 end
48 end 50 end
spec/requests/api/notes_spec.rb
@@ -36,6 +36,11 @@ describe Gitlab::API do @@ -36,6 +36,11 @@ describe Gitlab::API do
36 response.status.should == 200 36 response.status.should == 200
37 json_response['body'].should == wall_note.note 37 json_response['body'].should == wall_note.note
38 end 38 end
  39 +
  40 + it "should return a 404 error if note not found" do
  41 + get api("/projects/#{project.id}/notes/123", user)
  42 + response.status.should == 404
  43 + end
39 end 44 end
40 45
41 describe "POST /projects/:id/notes" do 46 describe "POST /projects/:id/notes" do
@@ -44,6 +49,11 @@ describe Gitlab::API do @@ -44,6 +49,11 @@ describe Gitlab::API do
44 response.status.should == 201 49 response.status.should == 201
45 json_response['body'].should == 'hi!' 50 json_response['body'].should == 'hi!'
46 end 51 end
  52 +
  53 + it "should return a 400 error if body is missing" do
  54 + post api("/projects/#{project.id}/notes", user)
  55 + response.status.should == 400
  56 + end
47 end 57 end
48 58
49 describe "GET /projects/:id/noteable/:noteable_id/notes" do 59 describe "GET /projects/:id/noteable/:noteable_id/notes" do
@@ -54,6 +64,11 @@ describe Gitlab::API do @@ -54,6 +64,11 @@ describe Gitlab::API do
54 json_response.should be_an Array 64 json_response.should be_an Array
55 json_response.first['body'].should == issue_note.note 65 json_response.first['body'].should == issue_note.note
56 end 66 end
  67 +
  68 + it "should return a 404 error when issue id not found" do
  69 + get api("/projects/#{project.id}/issues/123/notes", user)
  70 + response.status.should == 404
  71 + end
57 end 72 end
58 73
59 context "when noteable is a Snippet" do 74 context "when noteable is a Snippet" do
@@ -63,6 +78,11 @@ describe Gitlab::API do @@ -63,6 +78,11 @@ describe Gitlab::API do
63 json_response.should be_an Array 78 json_response.should be_an Array
64 json_response.first['body'].should == snippet_note.note 79 json_response.first['body'].should == snippet_note.note
65 end 80 end
  81 +
  82 + it "should return a 404 error when snippet id not found" do
  83 + get api("/projects/#{project.id}/snippets/42/notes", user)
  84 + response.status.should == 404
  85 + end
66 end 86 end
67 end 87 end
68 88
@@ -73,6 +93,11 @@ describe Gitlab::API do @@ -73,6 +93,11 @@ describe Gitlab::API do
73 response.status.should == 200 93 response.status.should == 200
74 json_response['body'].should == issue_note.note 94 json_response['body'].should == issue_note.note
75 end 95 end
  96 +
  97 + it "should return a 404 error if issue note not found" do
  98 + get api("/projects/#{project.id}/issues/#{issue.id}/notes/123", user)
  99 + response.status.should == 404
  100 + end
76 end 101 end
77 102
78 context "when noteable is a Snippet" do 103 context "when noteable is a Snippet" do
@@ -81,6 +106,11 @@ describe Gitlab::API do @@ -81,6 +106,11 @@ describe Gitlab::API do
81 response.status.should == 200 106 response.status.should == 200
82 json_response['body'].should == snippet_note.note 107 json_response['body'].should == snippet_note.note
83 end 108 end
  109 +
  110 + it "should return a 404 error if snippet note not found" do
  111 + get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/123", user)
  112 + response.status.should == 404
  113 + end
84 end 114 end
85 end 115 end
86 116