OSDN Git Service

Refactor API classes. So api classes like Gitlab::Issues become API::Issues
[wvm/gitlab.git] / spec / requests / api / notes_spec.rb
1 require 'spec_helper'
2
3 describe API::API do
4   include ApiHelpers
5
6   let(:user) { create(:user) }
7   let!(:project) { create(:project, namespace: user.namespace ) }
8   let!(:issue) { create(:issue, project: project, author: user) }
9   let!(:merge_request) { create(:merge_request, project: project, author: user) }
10   let!(:snippet) { create(:snippet, project: project, author: user) }
11   let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) }
12   let!(:merge_request_note) { create(:note, noteable: merge_request, project: project, author: user) }
13   let!(:snippet_note) { create(:note, noteable: snippet, project: project, author: user) }
14   let!(:wall_note) { create(:note, project: project, author: user) }
15   before { project.team << [user, :reporter] }
16
17   describe "GET /projects/:id/notes" do
18     context "when unauthenticated" do
19       it "should return authentication error" do
20         get api("/projects/#{project.id}/notes")
21         response.status.should == 401
22       end
23     end
24
25     context "when authenticated" do
26       it "should return project wall notes" do
27         get api("/projects/#{project.id}/notes", user)
28         response.status.should == 200
29         json_response.should be_an Array
30         json_response.first['body'].should == wall_note.note
31       end
32     end
33   end
34
35   describe "GET /projects/:id/notes/:note_id" do
36     it "should return a wall note by id" do
37       get api("/projects/#{project.id}/notes/#{wall_note.id}", user)
38       response.status.should == 200
39       json_response['body'].should == wall_note.note
40     end
41
42     it "should return a 404 error if note not found" do
43       get api("/projects/#{project.id}/notes/123", user)
44       response.status.should == 404
45     end
46   end
47
48   describe "POST /projects/:id/notes" do
49     it "should create a new wall note" do
50       post api("/projects/#{project.id}/notes", user), body: 'hi!'
51       response.status.should == 201
52       json_response['body'].should == 'hi!'
53     end
54
55     it "should return 401 unauthorized error" do
56       post api("/projects/#{project.id}/notes")
57       response.status.should == 401
58     end
59
60     it "should return a 400 bad request if body is missing" do
61       post api("/projects/#{project.id}/notes", user)
62       response.status.should == 400
63     end
64   end
65
66   describe "GET /projects/:id/noteable/:noteable_id/notes" do
67     context "when noteable is an Issue" do
68       it "should return an array of issue notes" do
69         get api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
70         response.status.should == 200
71         json_response.should be_an Array
72         json_response.first['body'].should == issue_note.note
73       end
74
75       it "should return a 404 error when issue id not found" do
76         get api("/projects/#{project.id}/issues/123/notes", user)
77         response.status.should == 404
78       end
79     end
80
81     context "when noteable is a Snippet" do
82       it "should return an array of snippet notes" do
83         get api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
84         response.status.should == 200
85         json_response.should be_an Array
86         json_response.first['body'].should == snippet_note.note
87       end
88
89       it "should return a 404 error when snippet id not found" do
90         get api("/projects/#{project.id}/snippets/42/notes", user)
91         response.status.should == 404
92       end
93     end
94
95     context "when noteable is a Merge Request" do
96       it "should return an array of merge_requests notes" do
97         get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/notes", user)
98         response.status.should == 200
99         json_response.should be_an Array
100         json_response.first['body'].should == merge_request_note.note
101       end
102
103       it "should return a 404 error if merge request id not found" do
104         get api("/projects/#{project.id}/merge_requests/4444/notes", user)
105         response.status.should == 404
106       end
107     end
108   end
109
110   describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do
111     context "when noteable is an Issue" do
112       it "should return an issue note by id" do
113         get api("/projects/#{project.id}/issues/#{issue.id}/notes/#{issue_note.id}", user)
114         response.status.should == 200
115         json_response['body'].should == issue_note.note
116       end
117
118       it "should return a 404 error if issue note not found" do
119         get api("/projects/#{project.id}/issues/#{issue.id}/notes/123", user)
120         response.status.should == 404
121       end
122     end
123
124     context "when noteable is a Snippet" do
125       it "should return a snippet note by id" do
126         get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/#{snippet_note.id}", user)
127         response.status.should == 200
128         json_response['body'].should == snippet_note.note
129       end
130
131       it "should return a 404 error if snippet note not found" do
132         get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/123", user)
133         response.status.should == 404
134       end
135     end
136   end
137
138   describe "POST /projects/:id/noteable/:noteable_id/notes" do
139     context "when noteable is an Issue" do
140       it "should create a new issue note" do
141         post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
142         response.status.should == 201
143         json_response['body'].should == 'hi!'
144         json_response['author']['email'].should == user.email
145       end
146
147       it "should return a 400 bad request error if body not given" do
148         post api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
149         response.status.should == 400
150       end
151
152       it "should return a 401 unauthorized error if user not authenticated" do
153         post api("/projects/#{project.id}/issues/#{issue.id}/notes"), body: 'hi!'
154         response.status.should == 401
155       end
156     end
157
158     context "when noteable is a Snippet" do
159       it "should create a new snippet note" do
160         post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!'
161         response.status.should == 201
162         json_response['body'].should == 'hi!'
163         json_response['author']['email'].should == user.email
164       end
165
166       it "should return a 400 bad request error if body not given" do
167         post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
168         response.status.should == 400
169       end
170
171       it "should return a 401 unauthorized error if user not authenticated" do
172         post api("/projects/#{project.id}/snippets/#{snippet.id}/notes"), body: 'hi!'
173         response.status.should == 401
174       end
175     end
176   end
177 end