2017-12-14 03:58:10 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe Files::MultiService do
|
|
|
|
subject { described_class.new(project, user, commit_params) }
|
|
|
|
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:branch_name) { project.default_branch }
|
2017-12-20 15:57:27 +00:00
|
|
|
let(:original_file_path) { 'files/ruby/popen.rb' }
|
|
|
|
let(:new_file_path) { 'files/ruby/popen.rb' }
|
2017-12-14 03:58:10 +00:00
|
|
|
let(:action) { 'update' }
|
|
|
|
|
2017-12-16 04:24:12 +00:00
|
|
|
let!(:original_commit_id) do
|
2017-12-20 15:57:27 +00:00
|
|
|
Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha
|
2017-12-16 04:24:12 +00:00
|
|
|
end
|
|
|
|
|
2017-12-14 03:58:10 +00:00
|
|
|
let(:actions) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
action: action,
|
2017-12-20 15:57:27 +00:00
|
|
|
file_path: new_file_path,
|
|
|
|
previous_path: original_file_path,
|
2017-12-16 04:24:12 +00:00
|
|
|
content: 'New content',
|
|
|
|
last_commit_id: original_commit_id
|
2017-12-14 03:58:10 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:commit_params) do
|
|
|
|
{
|
|
|
|
commit_message: "Update File",
|
|
|
|
branch_name: branch_name,
|
|
|
|
start_branch: branch_name,
|
|
|
|
actions: actions
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_master(user)
|
2017-12-14 03:58:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'with a valid action' do
|
2018-01-04 01:47:25 +00:00
|
|
|
it 'returns a hash with the :success status' do
|
2017-12-14 03:58:10 +00:00
|
|
|
results = subject.execute
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid action' do
|
|
|
|
let(:action) { 'rename' }
|
|
|
|
|
2018-01-04 01:47:25 +00:00
|
|
|
it 'returns a hash with the :error status' do
|
2017-12-14 03:58:10 +00:00
|
|
|
results = subject.execute
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:error)
|
|
|
|
expect(results[:message]).to match(/Unknown action/)
|
|
|
|
end
|
|
|
|
end
|
2017-12-16 04:24:12 +00:00
|
|
|
|
|
|
|
describe 'Updating files' do
|
|
|
|
context 'when the file has been previously updated' do
|
|
|
|
before do
|
2017-12-20 15:57:27 +00:00
|
|
|
update_file(original_file_path)
|
2017-12-16 04:24:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects the commit' do
|
|
|
|
results = subject.execute
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:error)
|
2017-12-20 15:57:27 +00:00
|
|
|
expect(results[:message]).to match(new_file_path)
|
2017-12-16 04:24:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file have not been modified' do
|
|
|
|
it 'accepts the commit' do
|
|
|
|
results = subject.execute
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-20 15:57:27 +00:00
|
|
|
|
|
|
|
context 'when moving a file' do
|
|
|
|
let(:action) { 'move' }
|
|
|
|
let(:new_file_path) { 'files/ruby/new_popen.rb' }
|
|
|
|
|
|
|
|
context 'when original file has been updated' do
|
|
|
|
before do
|
|
|
|
update_file(original_file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects the commit' do
|
|
|
|
results = subject.execute
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:error)
|
|
|
|
expect(results[:message]).to match(original_file_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when original file have not been updated' do
|
|
|
|
it 'moves the file' do
|
|
|
|
results = subject.execute
|
|
|
|
blob = project.repository.blob_at_branch(branch_name, new_file_path)
|
|
|
|
|
|
|
|
expect(results[:status]).to eq(:success)
|
|
|
|
expect(blob).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file status validation is skipped' do
|
|
|
|
let(:action) { 'create' }
|
|
|
|
let(:new_file_path) { 'files/ruby/new_file.rb' }
|
|
|
|
|
|
|
|
it 'does not check the last commit' do
|
|
|
|
expect(Gitlab::Git::Commit).not_to receive(:last_for_path)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the file' do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
blob = project.repository.blob_at_branch(branch_name, new_file_path)
|
|
|
|
|
|
|
|
expect(blob).to be_present
|
|
|
|
end
|
|
|
|
end
|
2017-12-16 04:24:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_file(path)
|
|
|
|
params = {
|
|
|
|
file_path: path,
|
|
|
|
start_branch: branch_name,
|
|
|
|
branch_name: branch_name,
|
|
|
|
commit_message: 'Update file',
|
|
|
|
file_content: 'New content'
|
|
|
|
}
|
|
|
|
|
|
|
|
Files::UpdateService.new(project, user, params).execute
|
2017-12-14 03:58:10 +00:00
|
|
|
end
|
|
|
|
end
|