1# frozen_string_literal: true 2require 'spec_helper' 3 4RSpec.describe Commits::CommitPatchService do 5 describe '#execute' do 6 let(:patches) do 7 patches_folder = Rails.root.join('spec/fixtures/patchfiles') 8 content_1 = File.read(File.join(patches_folder, "0001-This-does-not-apply-to-the-feature-branch.patch")) 9 content_2 = File.read(File.join(patches_folder, "0001-A-commit-from-a-patch.patch")) 10 11 [content_1, content_2] 12 end 13 14 let(:user) { project.creator } 15 let(:branch_name) { 'branch-with-patches' } 16 let(:project) { create(:project, :repository) } 17 let(:start_branch) { nil } 18 let(:params) { { branch_name: branch_name, patches: patches, start_branch: start_branch } } 19 20 subject(:service) do 21 described_class.new(project, user, params) 22 end 23 24 it 'returns a successful result' do 25 result = service.execute 26 27 branch = project.repository.find_branch(branch_name) 28 29 expect(result[:status]).to eq(:success) 30 expect(result[:result]).to eq(branch.target) 31 end 32 33 it 'is based off HEAD when no start ref is passed' do 34 service.execute 35 36 merge_base = project.repository.merge_base(project.repository.root_ref, branch_name) 37 38 expect(merge_base).to eq(project.repository.commit('HEAD').sha) 39 end 40 41 context 'when specifying a different start branch' do 42 let(:start_branch) { 'with-codeowners' } 43 44 it 'is based of the correct branch' do 45 service.execute 46 47 merge_base = project.repository.merge_base(start_branch, branch_name) 48 49 expect(merge_base).to eq(project.repository.commit(start_branch).sha) 50 end 51 end 52 53 shared_examples 'an error response' do |expected_message| 54 it 'returns the correct error' do 55 result = service.execute 56 57 expect(result[:status]).to eq(:error) 58 expect(result[:message]).to match(expected_message) 59 end 60 end 61 62 context 'when the user does not have access' do 63 let(:user) { create(:user) } 64 65 it_behaves_like 'an error response', 66 'You are not allowed to push into this branch' 67 end 68 69 context 'when the patches are not valid' do 70 let(:patches) { "a" * 2.1.megabytes } 71 72 it_behaves_like 'an error response', 'Patches are too big' 73 end 74 75 context 'when the new branch name is invalid' do 76 let(:branch_name) { 'HEAD' } 77 78 it_behaves_like 'an error response', 'Branch name is invalid' 79 end 80 81 context 'when the patches do not apply' do 82 let(:branch_name) { 'feature' } 83 84 it_behaves_like 'an error response', 'Patch failed at' 85 end 86 87 context 'when specifying a non existent start branch' do 88 let(:start_branch) { 'does-not-exist' } 89 90 it_behaves_like 'an error response', 'Failed to create branch' 91 end 92 end 93end 94