2016-08-07 23:29:23 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe Files::UpdateService do
|
|
|
|
subject { described_class.new(project, user, commit_params) }
|
|
|
|
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-07 23:29:23 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:file_path) { 'files/ruby/popen.rb' }
|
2016-11-04 14:48:58 -04:00
|
|
|
let(:new_contents) { 'New Content' }
|
2017-04-19 20:37:44 -04:00
|
|
|
let(:branch_name) { project.default_branch }
|
2016-11-04 14:48:58 -04:00
|
|
|
let(:last_commit_sha) { nil }
|
|
|
|
|
2016-08-07 23:29:23 -04:00
|
|
|
let(:commit_params) do
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
commit_message: "Update File",
|
|
|
|
file_content: new_contents,
|
|
|
|
file_content_encoding: "text",
|
|
|
|
last_commit_sha: last_commit_sha,
|
2017-01-06 10:50:08 -05:00
|
|
|
start_project: project,
|
|
|
|
start_branch: project.default_branch,
|
2017-04-19 20:37:44 -04:00
|
|
|
branch_name: branch_name
|
2016-08-07 23:29:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_master(user)
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
context "when the file's last commit sha does not match the supplied last_commit_sha" do
|
|
|
|
let(:last_commit_sha) { "foo" }
|
|
|
|
|
|
|
|
it "returns a hash with the correct error message and a :error status " do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect { subject.execute }
|
|
|
|
.to raise_error(Files::UpdateService::FileChangedError,
|
2016-08-07 23:29:23 -04:00
|
|
|
"You are attempting to update a file that has changed since you started editing it.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the file's last commit sha does match the supplied last_commit_sha" do
|
|
|
|
let(:last_commit_sha) { Gitlab::Git::Commit.last_for_path(project.repository, project.default_branch, file_path).sha }
|
|
|
|
|
|
|
|
it "returns a hash with the :success status " do
|
|
|
|
results = subject.execute
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
expect(results[:status]).to match(:success)
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the file with the new contents" do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
results = project.repository.blob_at_branch(project.default_branch, file_path)
|
|
|
|
|
|
|
|
expect(results.data).to eq(new_contents)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the last_commit_sha is not supplied" do
|
|
|
|
it "returns a hash with the :success status " do
|
|
|
|
results = subject.execute
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
expect(results[:status]).to match(:success)
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the file with the new contents" do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
results = project.repository.blob_at_branch(project.default_branch, file_path)
|
|
|
|
|
|
|
|
expect(results.data).to eq(new_contents)
|
|
|
|
end
|
|
|
|
end
|
2016-11-04 14:48:58 -04:00
|
|
|
|
2018-01-08 22:51:05 -05:00
|
|
|
context 'with gitaly disabled', :skip_gitaly_mock do
|
|
|
|
context 'when target branch is different than source branch' do
|
|
|
|
let(:branch_name) { "#{project.default_branch}-new" }
|
2016-11-04 14:48:58 -04:00
|
|
|
|
2018-01-08 22:51:05 -05:00
|
|
|
it 'fires hooks only once' do
|
|
|
|
expect(Gitlab::Git::HooksService).to receive(:new).once.and_call_original
|
2016-11-04 14:48:58 -04:00
|
|
|
|
2018-01-08 22:51:05 -05:00
|
|
|
subject.execute
|
|
|
|
end
|
2016-11-04 14:48:58 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-07 23:29:23 -04:00
|
|
|
end
|
|
|
|
end
|