2015-10-06 06:01:16 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-01-14 06:08:44 -05:00
|
|
|
describe API::CommitStatus, api: true do
|
2015-10-06 06:01:16 -04:00
|
|
|
include ApiHelpers
|
2016-02-29 07:54:33 -05:00
|
|
|
|
2016-02-06 09:01:37 -05:00
|
|
|
let!(:project) { create(:project) }
|
2015-10-06 06:01:16 -04:00
|
|
|
let(:commit) { project.repository.commit }
|
|
|
|
let(:commit_status) { create(:commit_status, commit: ci_commit) }
|
2016-02-06 09:01:37 -05:00
|
|
|
let(:guest) { create_user(ProjectMember::GUEST) }
|
|
|
|
let(:reporter) { create_user(ProjectMember::REPORTER) }
|
|
|
|
let(:developer) { create_user(ProjectMember::DEVELOPER) }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/commits/:sha/statuses" do
|
2016-02-29 07:54:33 -05:00
|
|
|
context 'ci commit exists' do
|
|
|
|
let!(:ci_commit) { project.ensure_ci_commit(commit.id) }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
it_behaves_like 'a paginated resources' do
|
|
|
|
let(:request) { get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter) }
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
context "reporter user" do
|
|
|
|
let(:statuses_id) { json_response.map { |status| status['id'] } }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
let!(:status1) do
|
|
|
|
create(:commit_status, commit: ci_commit, status: 'running')
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
let!(:status2) do
|
|
|
|
create(:commit_status, commit: ci_commit, name: 'coverage', status: 'pending')
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
let!(:status3) do
|
|
|
|
create(:commit_status, commit: ci_commit, name: 'coverage', ref: 'develop',
|
|
|
|
status: 'running', allow_failure: true)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
let!(:status4) do
|
|
|
|
create(:commit_status, commit: ci_commit, name: 'coverage', status: 'success')
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
let!(:status5) do
|
|
|
|
create(:commit_status, commit: ci_commit, ref: 'develop', status: 'success')
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:status6) do
|
|
|
|
create(:commit_status, commit: ci_commit, status: 'success')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return latest commit statuses" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status3.id, status4.id, status5.id, status6.id)
|
|
|
|
json_response.sort_by!{ |status| status['id'] }
|
|
|
|
expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return all commit statuses" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?all=1", reporter)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status1.id, status2.id, status3.id, status4.id, status5.id, status6.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return latest commit statuses for specific ref" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?ref=develop", reporter)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status3.id, status5.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return latest commit statuses for specific name" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?name=coverage", reporter)
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status3.id, status4.id)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
context 'ci commit does not exist' do
|
|
|
|
before do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
it 'returns empty array' do
|
|
|
|
expect(response.status).to eq 200
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(json_response).to be_an Array
|
2016-02-29 07:54:33 -05:00
|
|
|
expect(json_response).to be_empty
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "guest user" do
|
|
|
|
it "should not return project commits" do
|
2016-02-06 09:01:37 -05:00
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", guest)
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not return project commits" do
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses")
|
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-12 06:15:35 -04:00
|
|
|
describe 'POST /projects/:id/statuses/:sha' do
|
|
|
|
let(:post_url) { "/projects/#{project.id}/statuses/#{commit.id}" }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-06 09:01:37 -05:00
|
|
|
context 'developer user' do
|
2015-10-06 06:01:16 -04:00
|
|
|
context 'should create commit status' do
|
|
|
|
it 'with only required parameters' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api(post_url, developer), state: 'success'
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq('success')
|
|
|
|
expect(json_response['name']).to eq('default')
|
|
|
|
expect(json_response['ref']).to be_nil
|
|
|
|
expect(json_response['target_url']).to be_nil
|
|
|
|
expect(json_response['description']).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with all optional parameters' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api(post_url, developer), state: 'success', context: 'coverage', ref: 'develop', target_url: 'url', description: 'test'
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq('success')
|
|
|
|
expect(json_response['name']).to eq('coverage')
|
|
|
|
expect(json_response['ref']).to eq('develop')
|
|
|
|
expect(json_response['target_url']).to eq('url')
|
|
|
|
expect(json_response['description']).to eq('test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'should not create commit status' do
|
|
|
|
it 'with invalid state' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api(post_url, developer), state: 'invalid'
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'without state' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api(post_url, developer)
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'invalid commit' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api("/projects/#{project.id}/statuses/invalid_sha", developer), state: 'running'
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 09:01:37 -05:00
|
|
|
context 'reporter user' do
|
|
|
|
it 'should not create commit status' do
|
|
|
|
post api(post_url, reporter)
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
context 'guest user' do
|
|
|
|
it 'should not create commit status' do
|
2016-02-06 09:01:37 -05:00
|
|
|
post api(post_url, guest)
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'should not create commit status' do
|
|
|
|
post api(post_url)
|
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-06 09:01:37 -05:00
|
|
|
|
|
|
|
def create_user(access_level)
|
|
|
|
user = create(:user)
|
|
|
|
create(:project_member, user: user, project: project, access_level: access_level)
|
|
|
|
user
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|