2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
require 'mime/types'
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe API::Repositories do
|
2014-09-02 18:07:02 +05:30
|
|
|
include RepoHelpers
|
2016-06-02 11:05:42 +05:30
|
|
|
include WorkhorseHelpers
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:guest) { create(:user).tap { |u| create(:project_member, :guest, user: u, project: project) } }
|
|
|
|
let!(:project) { create(:project, :repository, creator: user) }
|
2016-06-02 11:05:42 +05:30
|
|
|
let!(:master) { create(:project_member, :master, user: user, project: project) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/tree" do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:route) { "/projects/#{project.id}/repository/tree" }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
shared_examples_for 'repository tree' do
|
|
|
|
it 'returns the repository tree' do
|
|
|
|
get api(route, current_user)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(response).to include_pagination_headers
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(json_response).to be_an Array
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
first_commit = json_response.first
|
|
|
|
expect(first_commit['name']).to eq('bar')
|
|
|
|
expect(first_commit['type']).to eq('tree')
|
|
|
|
expect(first_commit['mode']).to eq('040000')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref does not exist' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api("#{route}?ref=foo", current_user) }
|
|
|
|
let(:message) { '404 Tree Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
include_context 'disabled repository'
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, current_user) }
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'with recursive=1' do
|
|
|
|
it 'returns recursive project paths tree' do
|
|
|
|
get api("#{route}?recursive=1", current_user)
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response[4]['name']).to eq('html')
|
|
|
|
expect(json_response[4]['path']).to eq('files/html')
|
|
|
|
expect(json_response[4]['type']).to eq('tree')
|
|
|
|
expect(json_response[4]['mode']).to eq('040000')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
include_context 'disabled repository'
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, current_user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref does not exist' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api("#{route}?recursive=1&ref=foo", current_user) }
|
|
|
|
let(:message) { '404 Tree Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository tree' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository tree' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/blobs/:sha" do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:route) { "/projects/#{project.id}/repository/blobs/#{sample_blob.oid}" }
|
|
|
|
|
|
|
|
shared_examples_for 'repository blob' do
|
|
|
|
it 'returns blob attributes as json' do
|
|
|
|
get api(route, current_user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['size']).to eq(111)
|
|
|
|
expect(json_response['encoding']).to eq("base64")
|
|
|
|
expect(Base64.decode64(json_response['content']).lines.first).to eq("class Commit\n")
|
|
|
|
expect(json_response['sha']).to eq(sample_blob.oid)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sha does not exist' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route.sub(sample_blob.oid, '123456'), current_user) }
|
|
|
|
let(:message) { '404 Blob Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
include_context 'disabled repository'
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, current_user) }
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository blob' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository blob' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe "GET /projects/:id/repository/blobs/:sha/raw" do
|
|
|
|
let(:route) { "/projects/#{project.id}/repository/blobs/#{sample_blob.oid}/raw" }
|
|
|
|
|
|
|
|
shared_examples_for 'repository raw blob' do
|
|
|
|
it 'returns the repository raw blob' do
|
|
|
|
expect(Gitlab::Workhorse).to receive(:send_git_blob)
|
|
|
|
|
|
|
|
get api(route, current_user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sha does not exist' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route.sub(sample_blob.oid, '123456'), current_user) }
|
|
|
|
let(:message) { '404 Blob Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
include_context 'disabled repository'
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, current_user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository raw blob' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository raw blob' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/archive(.:format)?:sha" do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:route) { "/projects/#{project.id}/repository/archive" }
|
|
|
|
|
|
|
|
shared_examples_for 'repository archive' do
|
|
|
|
it 'returns the repository archive' do
|
|
|
|
get api(route, current_user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
repo_name = project.repository.name.gsub("\.git", "")
|
|
|
|
type, params = workhorse_send_data
|
|
|
|
|
|
|
|
expect(type).to eq('git-archive')
|
|
|
|
expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.tar.gz/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the repository archive archive.zip' do
|
|
|
|
get api("/projects/#{project.id}/repository/archive.zip", user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
repo_name = project.repository.name.gsub("\.git", "")
|
|
|
|
type, params = workhorse_send_data
|
|
|
|
|
|
|
|
expect(type).to eq('git-archive')
|
|
|
|
expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.zip/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the repository archive archive.tar.bz2' do
|
|
|
|
get api("/projects/#{project.id}/repository/archive.tar.bz2", user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
repo_name = project.repository.name.gsub("\.git", "")
|
|
|
|
type, params = workhorse_send_data
|
|
|
|
|
|
|
|
expect(type).to eq('git-archive')
|
|
|
|
expect(params['ArchivePath']).to match(/#{repo_name}\-[^\.]+\.tar.bz2/)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sha does not exist' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api("#{route}?sha=xxx", current_user) }
|
|
|
|
let(:message) { '404 File Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository archive' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository archive' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/repository/compare' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:route) { "/projects/#{project.id}/repository/compare" }
|
|
|
|
|
|
|
|
shared_examples_for 'repository compare' do
|
|
|
|
it "compares branches" do
|
|
|
|
get api(route, current_user), from: 'master', to: 'feature'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['commits']).to be_present
|
|
|
|
expect(json_response['diffs']).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "compares tags" do
|
|
|
|
get api(route, current_user), from: 'v1.0.0', to: 'v1.1.0'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['commits']).to be_present
|
|
|
|
expect(json_response['diffs']).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "compares commits" do
|
|
|
|
get api(route, current_user), from: sample_commit.id, to: sample_commit.parent_id
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['commits']).to be_empty
|
|
|
|
expect(json_response['diffs']).to be_empty
|
|
|
|
expect(json_response['compare_same_ref']).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it "compares commits in reverse order" do
|
|
|
|
get api(route, current_user), from: sample_commit.parent_id, to: sample_commit.id
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['commits']).to be_present
|
|
|
|
expect(json_response['diffs']).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "compares same refs" do
|
|
|
|
get api(route, current_user), from: 'master', to: 'master'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response['commits']).to be_empty
|
|
|
|
expect(json_response['diffs']).to be_empty
|
|
|
|
expect(json_response['compare_same_ref']).to be_truthy
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository compare' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository compare' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/repository/contributors' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:route) { "/projects/#{project.id}/repository/contributors" }
|
|
|
|
|
|
|
|
shared_examples_for 'repository contributors' do
|
|
|
|
it 'returns valid data' do
|
|
|
|
get api(route, current_user)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
|
|
|
|
first_contributor = json_response.first
|
|
|
|
expect(first_contributor['email']).to eq('tiagonbotelho@hotmail.com')
|
|
|
|
expect(first_contributor['name']).to eq('tiagonbotelho')
|
|
|
|
expect(first_contributor['commits']).to eq(1)
|
|
|
|
expect(first_contributor['additions']).to eq(0)
|
|
|
|
expect(first_contributor['deletions']).to eq(0)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'using sorting' do
|
|
|
|
context 'by commits desc' do
|
|
|
|
it 'returns the repository contribuors sorted by commits desc' do
|
|
|
|
get api(route, current_user), { order_by: 'commits', sort: 'desc' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to match_response_schema('contributors')
|
|
|
|
expect(json_response.first['commits']).to be > json_response.last['commits']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'by name desc' do
|
|
|
|
it 'returns the repository contribuors sorted by name asc case insensitive' do
|
|
|
|
get api(route, current_user), { order_by: 'name', sort: 'asc' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response).to match_response_schema('contributors')
|
|
|
|
expect(json_response.first['name'].downcase).to be < json_response.last['name'].downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is public' do
|
|
|
|
it_behaves_like 'repository contributors' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route) }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository contributors' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest) }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|