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 43 if @note.save
44 44 present @note, with: Entities::Note
45 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 48 not_found!
47 49 end
48 50 end
... ...
spec/requests/api/notes_spec.rb
... ... @@ -36,6 +36,11 @@ describe Gitlab::API do
36 36 response.status.should == 200
37 37 json_response['body'].should == wall_note.note
38 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 44 end
40 45  
41 46 describe "POST /projects/:id/notes" do
... ... @@ -44,6 +49,11 @@ describe Gitlab::API do
44 49 response.status.should == 201
45 50 json_response['body'].should == 'hi!'
46 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 57 end
48 58  
49 59 describe "GET /projects/:id/noteable/:noteable_id/notes" do
... ... @@ -54,6 +64,11 @@ describe Gitlab::API do
54 64 json_response.should be_an Array
55 65 json_response.first['body'].should == issue_note.note
56 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 72 end
58 73  
59 74 context "when noteable is a Snippet" do
... ... @@ -63,6 +78,11 @@ describe Gitlab::API do
63 78 json_response.should be_an Array
64 79 json_response.first['body'].should == snippet_note.note
65 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 86 end
67 87 end
68 88  
... ... @@ -73,6 +93,11 @@ describe Gitlab::API do
73 93 response.status.should == 200
74 94 json_response['body'].should == issue_note.note
75 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 101 end
77 102  
78 103 context "when noteable is a Snippet" do
... ... @@ -81,6 +106,11 @@ describe Gitlab::API do
81 106 response.status.should == 200
82 107 json_response['body'].should == snippet_note.note
83 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 114 end
85 115 end
86 116  
... ...