gitlab-org--gitlab-foss/spec/requests/api/commit_status_spec.rb

210 lines
6.6 KiB
Ruby
Raw Normal View History

2015-10-06 06:01:16 -04:00
require 'spec_helper'
describe API::CommitStatus, api: true do
2015-10-06 06:01:16 -04:00
include ApiHelpers
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) }
let(:guest) { create_user(:guest) }
let(:reporter) { create_user(:reporter) }
let(:developer) { create_user(:developer) }
2016-03-01 06:33:25 -05:00
let(:sha) { commit.id }
2015-10-06 06:01:16 -04:00
describe "GET /projects/:id/repository/commits/:sha/statuses" do
2016-03-01 06:33:25 -05:00
let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
context 'ci commit exists' do
let!(:ci_commit) { project.ensure_ci_commit(commit.id) }
2015-10-06 06:01:16 -04:00
it_behaves_like 'a paginated resources' do
2016-03-01 06:33:25 -05:00
let(:request) { get api(get_url, reporter) }
2015-10-06 06:01:16 -04:00
end
context "reporter user" do
let(:statuses_id) { json_response.map { |status| status['id'] } }
2015-10-06 06:01:16 -04:00
2016-03-01 06:33:25 -05:00
def create_status(opts = {})
create(:commit_status, { commit: ci_commit }.merge(opts))
end
2016-03-01 06:33:25 -05:00
let!(:status1) { create_status(status: 'running') }
let!(:status2) { create_status(name: 'coverage', status: 'pending') }
let!(:status3) { create_status(ref: 'develop', status: 'running', allow_failure: true) }
let!(:status4) { create_status(name: 'coverage', status: 'success') }
let!(:status5) { create_status(name: 'coverage', ref: 'develop', status: 'success') }
let!(:status6) { create_status(status: 'success') }
context 'latest commit statuses' do
before { get api(get_url, reporter) }
it 'returns latest commit statuses' do
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
end
context 'all commit statuses' do
before { get api(get_url, reporter), all: 1 }
it 'returns all commit statuses' do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
2016-03-01 06:33:25 -05:00
expect(statuses_id).to contain_exactly(status1.id, status2.id,
status3.id, status4.id,
status5.id, status6.id)
end
end
context 'latest commit statuses for specific ref' do
before { get api(get_url, reporter), ref: 'develop' }
it 'returns latest commit statuses for specific ref' do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(statuses_id).to contain_exactly(status3.id, status5.id)
end
end
context 'latest commit statues for specific name' do
before { get api(get_url, reporter), name: 'coverage' }
it 'return latest commit statuses for specific name' do
expect(response.status).to eq(200)
expect(json_response).to be_an Array
2016-03-01 06:33:25 -05:00
expect(statuses_id).to contain_exactly(status4.id, status5.id)
end
end
2015-10-06 06:01:16 -04:00
end
end
2015-10-06 06:01:16 -04:00
context 'ci commit does not exist' do
before { get api(get_url, reporter) }
2015-10-06 06:01:16 -04: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
expect(json_response).to be_empty
2015-10-06 06:01:16 -04:00
end
end
context "guest user" do
before { get api(get_url, guest) }
2015-10-06 06:01:16 -04:00
it "should not return project commits" do
expect(response.status).to eq(403)
end
end
context "unauthorized user" do
before { get api(get_url) }
2015-10-06 06:01:16 -04:00
it "should not return project commits" do
expect(response.status).to eq(401)
end
end
end
2015-10-12 06:15:35 -04:00
describe 'POST /projects/:id/statuses/:sha' do
2016-03-01 06:33:25 -05:00
let(:post_url) { "/projects/#{project.id}/statuses/#{sha}" }
2015-10-06 06:01:16 -04:00
2016-02-06 09:01:37 -05:00
context 'developer user' do
context 'only required parameters' do
before { post api(post_url, developer), state: 'success' }
it 'creates commit status' do
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
end
context 'with all optional parameters' do
before do
optional_params = { state: 'success', context: 'coverage',
ref: 'develop', target_url: 'url', description: 'test' }
post api(post_url, developer), optional_params
end
2015-10-06 06:01:16 -04:00
it 'creates commit status' do
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 'invalid status' do
before { post api(post_url, developer), state: 'invalid' }
it 'does not create commit status' do
2015-10-06 06:01:16 -04:00
expect(response.status).to eq(400)
end
end
context 'request without state' do
before { post api(post_url, developer) }
2015-10-06 06:01:16 -04:00
it 'does not create commit status' do
2015-10-06 06:01:16 -04:00
expect(response.status).to eq(400)
end
end
2015-10-06 06:01:16 -04:00
context 'invalid commit' do
2016-03-01 06:33:25 -05:00
let(:sha) { 'invalid_sha' }
before { post api(post_url, developer), state: 'running' }
it 'returns not found error' do
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
before { post api(post_url, reporter) }
2016-02-06 09:01:37 -05:00
it 'should not create commit status' do
expect(response.status).to eq(403)
end
end
2015-10-06 06:01:16 -04:00
context 'guest user' do
before { post api(post_url, guest) }
2015-10-06 06:01:16 -04:00
it 'should not create commit status' do
expect(response.status).to eq(403)
end
end
context 'unauthorized user' do
before { post api(post_url) }
2015-10-06 06:01:16 -04:00
it 'should not create commit status' do
expect(response.status).to eq(401)
end
end
end
2016-02-06 09:01:37 -05:00
def create_user(access_level_trait)
2016-02-06 09:01:37 -05:00
user = create(:user)
create(:project_member, access_level_trait, user: user, project: project)
2016-02-06 09:01:37 -05:00
user
end
2015-10-06 06:01:16 -04:00
end