debian-mirror-gitlab/spec/requests/api/commits_spec.rb

216 lines
8.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
require 'mime/types'
describe API::API, api: true do
include ApiHelpers
let(:user) { create(:user) }
let(:user2) { create(:user) }
let!(:project) { create(:project, creator_id: user.id) }
2016-06-02 11:05:42 +05:30
let!(:master) { create(:project_member, :master, user: user, project: project) }
let!(:guest) { create(:project_member, :guest, user: user2, project: project) }
2015-04-26 12:48:37 +05:30
let!(:note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'a comment on a commit') }
2015-09-11 14:41:01 +05:30
let!(:another_note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'another comment on a commit') }
2014-09-02 18:07:02 +05:30
before { project.team << [user, :reporter] }
describe "GET /projects/:id/repository/commits" do
context "authorized user" do
before { project.team << [user2, :reporter] }
2016-09-13 17:45:13 +05:30
it "returns project commits" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
expect(json_response.first['id']).to eq(project.repository.commit.id)
2014-09-02 18:07:02 +05:30
end
end
context "unauthorized user" do
2016-09-13 17:45:13 +05:30
it "does not return project commits" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2014-09-02 18:07:02 +05:30
end
end
2016-06-02 11:05:42 +05:30
context "since optional parameter" do
2016-09-13 17:45:13 +05:30
it "returns project commits since provided parameter" do
2016-06-02 11:05:42 +05:30
commits = project.repository.commits("master")
since = commits.second.created_at
get api("/projects/#{project.id}/repository/commits?since=#{since.utc.iso8601}", user)
expect(json_response.size).to eq 2
expect(json_response.first["id"]).to eq(commits.first.id)
expect(json_response.second["id"]).to eq(commits.second.id)
end
end
context "until optional parameter" do
2016-09-13 17:45:13 +05:30
it "returns project commits until provided parameter" do
2016-06-02 11:05:42 +05:30
commits = project.repository.commits("master")
before = commits.second.created_at
get api("/projects/#{project.id}/repository/commits?until=#{before.utc.iso8601}", user)
expect(json_response.size).to eq(commits.size - 1)
expect(json_response.first["id"]).to eq(commits.second.id)
expect(json_response.second["id"]).to eq(commits.third.id)
end
end
context "invalid xmlschema date parameters" do
2016-09-13 17:45:13 +05:30
it "returns an invalid parameter error message" do
2016-06-02 11:05:42 +05:30
get api("/projects/#{project.id}/repository/commits?since=invalid-date", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2016-06-02 11:05:42 +05:30
expect(json_response['message']).to include "\"since\" must be a timestamp in ISO 8601 format"
end
end
2014-09-02 18:07:02 +05:30
end
describe "GET /projects:id/repository/commits/:sha" do
context "authorized user" do
2016-09-13 17:45:13 +05:30
it "returns a commit by sha" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
2016-09-13 17:45:13 +05:30
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response['id']).to eq(project.repository.commit.id)
expect(json_response['title']).to eq(project.repository.commit.title)
2016-09-13 17:45:13 +05:30
expect(json_response['stats']['additions']).to eq(project.repository.commit.stats.additions)
expect(json_response['stats']['deletions']).to eq(project.repository.commit.stats.deletions)
expect(json_response['stats']['total']).to eq(project.repository.commit.stats.total)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns a 404 error if not found" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2014-09-02 18:07:02 +05:30
end
2015-10-24 18:46:33 +05:30
2016-09-13 17:45:13 +05:30
it "returns nil for commit without CI" do
2015-10-24 18:46:33 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
2016-09-13 17:45:13 +05:30
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2016-06-02 11:05:42 +05:30
expect(json_response['status']).to be_nil
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns status for CI" do
pipeline = project.ensure_pipeline(project.repository.commit.sha, 'master')
2016-09-13 17:45:13 +05:30
pipeline.update(status: 'success')
2015-10-24 18:46:33 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
2016-09-13 17:45:13 +05:30
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
expect(json_response['status']).to eq(pipeline.status)
2015-10-24 18:46:33 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns status for CI when pipeline is created" do
project.ensure_pipeline(project.repository.commit.sha, 'master')
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
expect(response).to have_http_status(200)
expect(json_response['status']).to be_nil
end
2014-09-02 18:07:02 +05:30
end
context "unauthorized user" do
2016-09-13 17:45:13 +05:30
it "does not return the selected commit" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2014-09-02 18:07:02 +05:30
end
end
end
describe "GET /projects:id/repository/commits/:sha/diff" do
context "authorized user" do
before { project.team << [user2, :reporter] }
2016-09-13 17:45:13 +05:30
it "returns the diff of the selected commit" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
expect(json_response.length).to be >= 1
expect(json_response.first.keys).to include "diff"
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns a 404 error if invalid commit" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2014-09-02 18:07:02 +05:30
end
end
context "unauthorized user" do
2016-09-13 17:45:13 +05:30
it "does not return the diff of the selected commit" do
2014-09-02 18:07:02 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2015-04-26 12:48:37 +05:30
end
end
end
describe 'GET /projects:id/repository/commits/:sha/comments' do
context 'authorized user' do
2016-09-13 17:45:13 +05:30
it 'returns merge_request comments' do
2015-04-26 12:48:37 +05:30
get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
2015-09-11 14:41:01 +05:30
expect(json_response.length).to eq(2)
2015-04-26 12:48:37 +05:30
expect(json_response.first['note']).to eq('a comment on a commit')
expect(json_response.first['author']['id']).to eq(user.id)
end
2016-09-13 17:45:13 +05:30
it 'returns a 404 error if merge_request_id not found' do
2015-04-26 12:48:37 +05:30
get api("/projects/#{project.id}/repository/commits/1234ab/comments", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2015-04-26 12:48:37 +05:30
end
end
context 'unauthorized user' do
2016-09-13 17:45:13 +05:30
it 'does not return the diff of the selected commit' do
2015-04-26 12:48:37 +05:30
get api("/projects/#{project.id}/repository/commits/1234ab/comments")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2015-04-26 12:48:37 +05:30
end
end
end
describe 'POST /projects:id/repository/commits/:sha/comments' do
context 'authorized user' do
2016-09-13 17:45:13 +05:30
it 'returns comment' do
2015-04-26 12:48:37 +05:30
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2015-04-26 12:48:37 +05:30
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
end
2016-09-13 17:45:13 +05:30
it 'returns the inline comment' do
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user), note: 'My comment', path: project.repository.commit.raw_diffs.first.new_path, line: 7, line_type: 'new'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2015-04-26 12:48:37 +05:30
expect(json_response['note']).to eq('My comment')
2016-09-13 17:45:13 +05:30
expect(json_response['path']).to eq(project.repository.commit.raw_diffs.first.new_path)
2015-04-26 12:48:37 +05:30
expect(json_response['line']).to eq(7)
expect(json_response['line_type']).to eq('new')
end
2016-09-13 17:45:13 +05:30
it 'returns 400 if note is missing' do
2015-04-26 12:48:37 +05:30
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns 404 if note is attached to non existent commit' do
2015-04-26 12:48:37 +05:30
post api("/projects/#{project.id}/repository/commits/1234ab/comments", user), note: 'My comment'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2015-04-26 12:48:37 +05:30
end
end
context 'unauthorized user' do
2016-09-13 17:45:13 +05:30
it 'does not return the diff of the selected commit' do
2015-04-26 12:48:37 +05:30
post api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/comments")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
2014-09-02 18:07:02 +05:30
end
end
end
end