2014-02-18 05:41:21 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'mime/types'
|
|
|
|
|
2014-04-11 15:45:56 -04:00
|
|
|
describe API::API, api: true do
|
2014-02-18 05:41:21 -05:00
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
|
|
|
let!(:project) { create(:project, creator_id: user.id) }
|
2016-03-06 16:53:22 -05:00
|
|
|
let!(:master) { create(:project_member, :master, user: user, project: project) }
|
|
|
|
let!(:guest) { create(:project_member, :guest, user: user2, project: project) }
|
2014-06-27 10:48:30 -04:00
|
|
|
let!(:note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'a comment on a commit') }
|
2015-05-08 08:34:10 -04:00
|
|
|
let!(:another_note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'another comment on a commit') }
|
2014-02-18 05:41:21 -05:00
|
|
|
|
|
|
|
before { project.team << [user, :reporter] }
|
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/commits" do
|
|
|
|
context "authorized user" do
|
|
|
|
before { project.team << [user2, :reporter] }
|
|
|
|
|
|
|
|
it "should return project commits" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-02-18 05:41:21 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.first['id']).to eq(project.repository.commit.id)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not return project commits" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(401)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects:id/repository/commits/:sha" do
|
|
|
|
context "authorized user" do
|
|
|
|
it "should return a commit by sha" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['id']).to eq(project.repository.commit.id)
|
|
|
|
expect(json_response['title']).to eq(project.repository.commit.title)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 404 error if not found" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
|
|
|
it "should return not_found for CI status" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['status']).to eq('not_found')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return status for CI" do
|
|
|
|
ci_commit = project.ensure_ci_commit(project.repository.commit.sha)
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['status']).to eq(ci_commit.status)
|
|
|
|
end
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not return the selected commit" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(401)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects:id/repository/commits/:sha/diff" do
|
|
|
|
context "authorized user" do
|
|
|
|
before { project.team << [user2, :reporter] }
|
|
|
|
|
|
|
|
it "should return the diff of the selected commit" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-02-18 05:41:21 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.length).to be >= 1
|
|
|
|
expect(json_response.first.keys).to include "diff"
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 404 error if invalid commit" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not return the diff of the selected commit" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(401)
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-27 10:48:30 -04:00
|
|
|
|
|
|
|
describe 'GET /projects:id/repository/commits/:sha/comments' do
|
|
|
|
context 'authorized user' do
|
|
|
|
it 'should return merge_request comments' do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
2015-05-08 08:34:10 -04:00
|
|
|
expect(json_response.length).to eq(2)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response.first['note']).to eq('a comment on a commit')
|
|
|
|
expect(json_response.first['author']['id']).to eq(user.id)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a 404 error if merge_request_id not found' do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/1234ab/comments", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'should not return the diff of the selected commit' do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/1234ab/comments")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(401)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects:id/repository/commits/:sha/comments' do
|
|
|
|
context 'authorized user' do
|
|
|
|
it 'should return comment' do
|
|
|
|
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(json_response['note']).to eq('My comment')
|
|
|
|
expect(json_response['path']).to be_nil
|
|
|
|
expect(json_response['line']).to be_nil
|
|
|
|
expect(json_response['line_type']).to be_nil
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the inline comment' do
|
|
|
|
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment', path: project.repository.commit.diffs.first.new_path, line: 7, line_type: 'new'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(json_response['note']).to eq('My comment')
|
|
|
|
expect(json_response['path']).to eq(project.repository.commit.diffs.first.new_path)
|
|
|
|
expect(json_response['line']).to eq(7)
|
|
|
|
expect(json_response['line_type']).to eq('new')
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 400 if note is missing' do
|
|
|
|
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return 404 if note is attached to non existent commit' do
|
|
|
|
post api("/projects/#{project.id}/repository/commits/1234ab/comments", user), note: 'My comment'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'should not return the diff of the selected commit' do
|
|
|
|
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments")
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(401)
|
2014-06-27 10:48:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-02-18 05:41:21 -05:00
|
|
|
end
|