2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-07 08:00:53 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe 'Git LFS File Locking API' do
|
2020-11-10 19:08:58 -05:00
|
|
|
include LfsHttpHelpers
|
2018-02-07 08:00:53 -05:00
|
|
|
include WorkhorseHelpers
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:maintainer) { create(:user) }
|
|
|
|
let_it_be(:developer) { create(:user) }
|
|
|
|
let_it_be(:reporter) { create(:user) }
|
|
|
|
let_it_be(:guest) { create(:user) }
|
|
|
|
let_it_be(:path) { 'README.md' }
|
|
|
|
|
|
|
|
let(:user) { developer }
|
2018-02-07 08:00:53 -05:00
|
|
|
let(:headers) do
|
|
|
|
{
|
2020-11-10 19:08:58 -05:00
|
|
|
'Authorization' => authorize_user
|
2018-02-07 08:00:53 -05:00
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'unauthorized request' do
|
2020-11-10 19:08:58 -05:00
|
|
|
context 'when user does not have download permission' do
|
|
|
|
let(:user) { guest }
|
2018-02-07 08:00:53 -05:00
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
it 'returns a 404 response' do
|
|
|
|
post_lfs_json url, body, headers
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have upload permission' do
|
|
|
|
let(:user) { reporter }
|
|
|
|
|
|
|
|
it 'returns a 403 response' do
|
2018-02-07 08:00:53 -05:00
|
|
|
post_lfs_json url, body, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
project.add_maintainer(maintainer)
|
2018-02-07 08:00:53 -05:00
|
|
|
project.add_developer(developer)
|
2020-11-10 19:08:58 -05:00
|
|
|
project.add_reporter(reporter)
|
2018-02-07 08:00:53 -05:00
|
|
|
project.add_guest(guest)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Create File Lock endpoint' do
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:url) { "#{project.http_url_to_repo}/info/lfs/locks" }
|
|
|
|
let(:body) { { path: path } }
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
include_examples 'unauthorized request'
|
|
|
|
|
|
|
|
context 'with an existent lock' do
|
|
|
|
before do
|
|
|
|
lock_file('README.md', developer)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'return an error message' do
|
|
|
|
post_lfs_json url, body, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:conflict)
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
expect(json_response.keys).to match_array(%w(lock message documentation_url))
|
|
|
|
expect(json_response['message']).to match(/already locked/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the existen lock' do
|
|
|
|
post_lfs_json url, body, headers
|
|
|
|
|
|
|
|
expect(json_response['lock']['path']).to eq('README.md')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an existent lock' do
|
|
|
|
it 'creates the lock' do
|
|
|
|
post_lfs_json url, body, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
expect(json_response['lock'].keys).to match_array(%w(id path locked_at owner))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Listing File Locks endpoint' do
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:url) { "#{project.http_url_to_repo}/info/lfs/locks" }
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
include_examples 'unauthorized request'
|
|
|
|
|
|
|
|
it 'returns the list of locked files' do
|
|
|
|
lock_file('README.md', developer)
|
|
|
|
lock_file('README', developer)
|
|
|
|
|
|
|
|
do_get url, nil, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
expect(json_response['locks'].size).to eq(2)
|
|
|
|
expect(json_response['locks'].first.keys).to match_array(%w(id path locked_at owner))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'List File Locks for verification endpoint' do
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:url) { "#{project.http_url_to_repo}/info/lfs/locks/verify" }
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
include_examples 'unauthorized request'
|
|
|
|
|
|
|
|
it 'returns the list of locked files grouped by owner' do
|
2018-07-11 10:36:08 -04:00
|
|
|
lock_file('README.md', maintainer)
|
2018-02-07 08:00:53 -05:00
|
|
|
lock_file('README', developer)
|
|
|
|
|
|
|
|
post_lfs_json url, nil, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
expect(json_response['ours'].size).to eq(1)
|
|
|
|
expect(json_response['ours'].first['path']).to eq('README')
|
|
|
|
expect(json_response['theirs'].size).to eq(1)
|
|
|
|
expect(json_response['theirs'].first['path']).to eq('README.md')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Delete File Lock endpoint' do
|
2020-11-10 19:08:58 -05:00
|
|
|
let!(:lock) { lock_file('README.md', developer) }
|
|
|
|
let(:url) { "#{project.http_url_to_repo}/info/lfs/locks/#{lock[:id]}/unlock" }
|
2018-02-07 08:00:53 -05:00
|
|
|
|
|
|
|
include_examples 'unauthorized request'
|
|
|
|
|
|
|
|
context 'with an existent lock' do
|
|
|
|
it 'deletes the lock' do
|
|
|
|
post_lfs_json url, nil, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the deleted lock' do
|
|
|
|
post_lfs_json url, nil, headers
|
|
|
|
|
|
|
|
expect(json_response['lock'].keys).to match_array(%w(id path locked_at owner))
|
|
|
|
end
|
2019-01-15 16:05:36 -05:00
|
|
|
|
|
|
|
context 'when a maintainer uses force' do
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:user) { maintainer }
|
2019-01-15 16:05:36 -05:00
|
|
|
|
|
|
|
it 'deletes the lock' do
|
|
|
|
project.add_maintainer(maintainer)
|
|
|
|
post_lfs_json url, { force: true }, headers
|
|
|
|
|
2020-01-27 10:08:51 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-01-15 16:05:36 -05:00
|
|
|
end
|
|
|
|
end
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lock_file(path, author)
|
|
|
|
result = Lfs::LockFileService.new(project, author, { path: path }).execute
|
|
|
|
|
|
|
|
result[:lock]
|
|
|
|
end
|
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
def do_get(url, params = nil, headers = nil)
|
2018-12-17 17:52:17 -05:00
|
|
|
get(url, params: (params || {}), headers: (headers || {}).merge('Content-Type' => LfsRequest::CONTENT_TYPE))
|
2018-02-07 08:00:53 -05:00
|
|
|
end
|
|
|
|
end
|