debian-mirror-gitlab/spec/services/files/update_service_spec.rb

87 lines
2.7 KiB
Ruby
Raw Permalink Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require "spec_helper"
2023-05-27 22:25:52 +05:30
RSpec.describe Files::UpdateService, feature_category: :source_code_management do
2016-09-13 17:45:13 +05:30
subject { described_class.new(project, user, commit_params) }
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2018-12-05 23:21:45 +05:30
let(:user) { create(:user, :commit_email) }
2016-09-13 17:45:13 +05:30
let(:file_path) { 'files/ruby/popen.rb' }
2017-08-17 22:00:37 +05:30
let(:new_contents) { 'New Content' }
let(:branch_name) { project.default_branch }
let(:last_commit_sha) { nil }
2018-12-05 23:21:45 +05:30
let(:commit) { project.repository.commit }
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
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-08-17 22:00:37 +05:30
start_project: project,
start_branch: project.default_branch,
branch_name: branch_name
2016-09-13 17:45:13 +05:30
}
end
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2016-09-13 17:45:13 +05:30
end
describe "#execute" do
2023-05-27 22:25:52 +05:30
context "when the file's last commit sha is earlier than the latest change for that branch" do
let(:last_commit_sha) { Gitlab::Git::Commit.last_for_path(project.repository, project.default_branch, file_path).parent_id }
2016-09-13 17:45:13 +05:30
2021-02-22 17:27:13 +05:30
it "returns a hash with the correct error message and a :error status" do
2017-09-10 17:25:29 +05:30
expect { subject.execute }
.to raise_error(Files::UpdateService::FileChangedError,
2016-09-13 17:45:13 +05:30
"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 }
2021-02-22 17:27:13 +05:30
it "returns a hash with the :success status" do
2016-09-13 17:45:13 +05:30
results = subject.execute
2016-11-03 12:29:30 +05:30
expect(results[:status]).to match(:success)
2016-09-13 17:45:13 +05:30
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
2018-12-05 23:21:45 +05:30
it 'uses the commit email' do
subject.execute
expect(user.commit_email).not_to eq(user.email)
expect(commit.author_email).to eq(user.commit_email)
expect(commit.committer_email).to eq(user.commit_email)
end
2016-09-13 17:45:13 +05:30
end
context "when the last_commit_sha is not supplied" do
2021-02-22 17:27:13 +05:30
it "returns a hash with the :success status" do
2016-09-13 17:45:13 +05:30
results = subject.execute
2016-11-03 12:29:30 +05:30
expect(results[:status]).to match(:success)
2016-09-13 17:45:13 +05:30
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
end
end