summaryrefslogtreecommitdiff
path: root/spec/services/commits/commit_patch_service_spec.rb
blob: 55cbd0e5d66d0f03513357fa993c81ceab835e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Commits::CommitPatchService do
  describe '#execute' do
    let(:patches) do
      patches_folder = Rails.root.join('spec/fixtures/patchfiles')
      content_1 = File.read(File.join(patches_folder, "0001-This-does-not-apply-to-the-feature-branch.patch"))
      content_2 = File.read(File.join(patches_folder, "0001-A-commit-from-a-patch.patch"))

      [content_1, content_2]
    end

    let(:user) { project.creator }
    let(:branch_name) { 'branch-with-patches' }
    let(:project) { create(:project, :repository) }
    let(:start_branch) { nil }
    let(:params) { { branch_name: branch_name, patches: patches, start_branch: start_branch } }

    subject(:service) do
      described_class.new(project, user, params)
    end

    it 'returns a successful result' do
      result = service.execute

      branch = project.repository.find_branch(branch_name)

      expect(result[:status]).to eq(:success)
      expect(result[:result]).to eq(branch.target)
    end

    it 'is based off HEAD when no start ref is passed' do
      service.execute

      merge_base = project.repository.merge_base(project.repository.root_ref, branch_name)

      expect(merge_base).to eq(project.repository.commit('HEAD').sha)
    end

    context 'when specifying a different start branch' do
      let(:start_branch) { 'with-codeowners' }

      it 'is based of the correct branch' do
        service.execute

        merge_base = project.repository.merge_base(start_branch, branch_name)

        expect(merge_base).to eq(project.repository.commit(start_branch).sha)
      end
    end

    shared_examples 'an error response' do |expected_message|
      it 'returns the correct error' do
        result = service.execute

        expect(result[:status]).to eq(:error)
        expect(result[:message]).to match(expected_message)
      end
    end

    context 'when the user does not have access' do
      let(:user) { create(:user) }

      it_behaves_like 'an error response',
                      'You are not allowed to push into this branch'
    end

    context 'when the patches are not valid' do
      let(:patches) { "a" * 2.1.megabytes }

      it_behaves_like 'an error response', 'Patches are too big'
    end

    context 'when the new branch name is invalid' do
      let(:branch_name) { 'HEAD' }

      it_behaves_like 'an error response', 'Branch name is invalid'
    end

    context 'when the patches do not apply' do
      let(:branch_name) { 'feature' }

      it_behaves_like 'an error response', 'Patch failed at'
    end

    context 'when specifying a non existent start branch' do
      let(:start_branch) { 'does-not-exist' }

      it_behaves_like 'an error response', 'Invalid reference name'
    end
  end
end