2012-11-27 14:43:39 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2013-05-14 08:33:31 -04:00
|
|
|
describe API::API do
|
2012-11-27 14:43:39 -05:00
|
|
|
include ApiHelpers
|
2013-09-17 09:12:10 -04:00
|
|
|
before(:each) { ActiveRecord::Base.observers.enable(:user_observer) }
|
|
|
|
after(:each) { ActiveRecord::Base.observers.disable(:user_observer) }
|
2012-11-27 14:43:39 -05:00
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2013-01-02 12:32:34 -05:00
|
|
|
let!(:project) { create(:project, namespace: user.namespace ) }
|
2012-11-27 14:43:39 -05:00
|
|
|
let!(:issue) { create(:issue, project: project, author: user) }
|
2013-04-25 10:15:33 -04:00
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: user) }
|
2013-03-25 03:20:14 -04:00
|
|
|
let!(:snippet) { create(:project_snippet, project: project, author: user) }
|
2012-11-27 14:43:39 -05:00
|
|
|
let!(:issue_note) { create(:note, noteable: issue, project: project, author: user) }
|
2013-01-31 01:42:11 -05:00
|
|
|
let!(:merge_request_note) { create(:note, noteable: merge_request, project: project, author: user) }
|
2012-11-27 14:43:39 -05:00
|
|
|
let!(:snippet_note) { create(:note, noteable: snippet, project: project, author: user) }
|
|
|
|
let!(:wall_note) { create(:note, project: project, author: user) }
|
2013-01-04 11:50:31 -05:00
|
|
|
before { project.team << [user, :reporter] }
|
2012-11-27 14:43:39 -05:00
|
|
|
|
|
|
|
describe "GET /projects/:id/notes" do
|
|
|
|
context "when unauthenticated" do
|
|
|
|
it "should return authentication error" do
|
|
|
|
get api("/projects/#{project.id}/notes")
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when authenticated" do
|
|
|
|
it "should return project wall notes" do
|
|
|
|
get api("/projects/#{project.id}/notes", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response.should be_an Array
|
|
|
|
json_response.first['body'].should == wall_note.note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-01 05:20:45 -05:00
|
|
|
describe "GET /projects/:id/notes/:note_id" do
|
|
|
|
it "should return a wall note by id" do
|
|
|
|
get api("/projects/#{project.id}/notes/#{wall_note.id}", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response['body'].should == wall_note.note
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
|
|
|
it "should return a 404 error if note not found" do
|
|
|
|
get api("/projects/#{project.id}/notes/123", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
2012-12-01 05:20:45 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 18:52:56 -05:00
|
|
|
describe "POST /projects/:id/notes" do
|
|
|
|
it "should create a new wall note" do
|
|
|
|
post api("/projects/#{project.id}/notes", user), body: 'hi!'
|
|
|
|
response.status.should == 201
|
|
|
|
json_response['body'].should == 'hi!'
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
2013-02-20 16:17:05 -05:00
|
|
|
it "should return 401 unauthorized error" do
|
|
|
|
post api("/projects/#{project.id}/notes")
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 400 bad request if body is missing" do
|
2013-02-06 10:34:06 -05:00
|
|
|
post api("/projects/#{project.id}/notes", user)
|
|
|
|
response.status.should == 400
|
|
|
|
end
|
2012-11-29 18:52:56 -05:00
|
|
|
end
|
|
|
|
|
2012-11-27 14:43:39 -05:00
|
|
|
describe "GET /projects/:id/noteable/:noteable_id/notes" do
|
|
|
|
context "when noteable is an Issue" do
|
2012-11-29 14:33:41 -05:00
|
|
|
it "should return an array of issue notes" do
|
2012-11-27 14:43:39 -05:00
|
|
|
get api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response.should be_an Array
|
|
|
|
json_response.first['body'].should == issue_note.note
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
|
|
|
it "should return a 404 error when issue id not found" do
|
|
|
|
get api("/projects/#{project.id}/issues/123/notes", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when noteable is a Snippet" do
|
2012-11-29 14:33:41 -05:00
|
|
|
it "should return an array of snippet notes" do
|
2012-11-27 14:43:39 -05:00
|
|
|
get api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response.should be_an Array
|
|
|
|
json_response.first['body'].should == snippet_note.note
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
|
|
|
it "should return a 404 error when snippet id not found" do
|
|
|
|
get api("/projects/#{project.id}/snippets/42/notes", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
2013-01-31 01:42:11 -05:00
|
|
|
|
|
|
|
context "when noteable is a Merge Request" do
|
|
|
|
it "should return an array of merge_requests notes" do
|
|
|
|
get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/notes", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response.should be_an Array
|
|
|
|
json_response.first['body'].should == merge_request_note.note
|
|
|
|
end
|
2013-02-20 16:17:05 -05:00
|
|
|
|
|
|
|
it "should return a 404 error if merge request id not found" do
|
|
|
|
get api("/projects/#{project.id}/merge_requests/4444/notes", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
|
|
|
end
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
|
|
|
|
describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do
|
|
|
|
context "when noteable is an Issue" do
|
|
|
|
it "should return an issue note by id" do
|
|
|
|
get api("/projects/#{project.id}/issues/#{issue.id}/notes/#{issue_note.id}", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response['body'].should == issue_note.note
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
|
|
|
it "should return a 404 error if issue note not found" do
|
|
|
|
get api("/projects/#{project.id}/issues/#{issue.id}/notes/123", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when noteable is a Snippet" do
|
|
|
|
it "should return a snippet note by id" do
|
|
|
|
get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/#{snippet_note.id}", user)
|
|
|
|
response.status.should == 200
|
|
|
|
json_response['body'].should == snippet_note.note
|
|
|
|
end
|
2013-02-06 10:34:06 -05:00
|
|
|
|
|
|
|
it "should return a 404 error if snippet note not found" do
|
|
|
|
get api("/projects/#{project.id}/snippets/#{snippet.id}/notes/123", user)
|
|
|
|
response.status.should == 404
|
|
|
|
end
|
2012-11-29 14:33:41 -05:00
|
|
|
end
|
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
|
|
|
|
describe "POST /projects/:id/noteable/:noteable_id/notes" do
|
|
|
|
context "when noteable is an Issue" do
|
|
|
|
it "should create a new issue note" do
|
|
|
|
post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
|
|
|
|
response.status.should == 201
|
|
|
|
json_response['body'].should == 'hi!'
|
|
|
|
json_response['author']['email'].should == user.email
|
|
|
|
end
|
2013-02-20 16:17:05 -05:00
|
|
|
|
|
|
|
it "should return a 400 bad request error if body not given" do
|
|
|
|
post api("/projects/#{project.id}/issues/#{issue.id}/notes", user)
|
|
|
|
response.status.should == 400
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 401 unauthorized error if user not authenticated" do
|
|
|
|
post api("/projects/#{project.id}/issues/#{issue.id}/notes"), body: 'hi!'
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when noteable is a Snippet" do
|
|
|
|
it "should create a new snippet note" do
|
|
|
|
post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user), body: 'hi!'
|
|
|
|
response.status.should == 201
|
|
|
|
json_response['body'].should == 'hi!'
|
|
|
|
json_response['author']['email'].should == user.email
|
|
|
|
end
|
2013-02-20 16:17:05 -05:00
|
|
|
|
|
|
|
it "should return a 400 bad request error if body not given" do
|
|
|
|
post api("/projects/#{project.id}/snippets/#{snippet.id}/notes", user)
|
|
|
|
response.status.should == 400
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 401 unauthorized error if user not authenticated" do
|
|
|
|
post api("/projects/#{project.id}/snippets/#{snippet.id}/notes"), body: 'hi!'
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
end
|
2012-11-29 15:06:24 -05:00
|
|
|
end
|
2013-10-04 15:11:50 -04:00
|
|
|
|
|
|
|
describe "POST /projects/:id/noteable/:noteable_id/notes to test observer on create" do
|
|
|
|
before { enable_observers }
|
|
|
|
after { disable_observers }
|
|
|
|
|
|
|
|
it "should create an activity event when an issue note is created" do
|
|
|
|
Event.should_receive(:create)
|
|
|
|
|
|
|
|
post api("/projects/#{project.id}/issues/#{issue.id}/notes", user), body: 'hi!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-27 14:43:39 -05:00
|
|
|
end
|