summaryrefslogtreecommitdiff
path: root/spec/services/files/multi_service_spec.rb
blob: b9971776b33f3fb0de415b97ca93a0cdf6489e2d (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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 }
  let(:original_file_path) { 'files/ruby/popen.rb' }
  let(:new_file_path) { 'files/ruby/popen.rb' }
  let(:action) { 'update' }

  let!(:original_commit_id) do
    Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha
  end

  let(:actions) do
    [
      {
        action: action,
        file_path: new_file_path,
        previous_path: original_file_path,
        content: 'New content',
        last_commit_id: original_commit_id
      }
    ]
  end

  let(:commit_params) do
    {
      commit_message: "Update File",
      branch_name: branch_name,
      start_branch: branch_name,
      actions: actions
    }
  end

  before do
    project.add_master(user)
  end

  describe '#execute' do
    context 'with a valid action' do
      it 'returns a hash with the :success status' do
        results = subject.execute

        expect(results[:status]).to eq(:success)
      end
    end

    context 'with an invalid action' do
      let(:action) { 'rename' }

      it 'returns a hash with the :error status' do
        results = subject.execute

        expect(results[:status]).to eq(:error)
        expect(results[:message]).to match(/Unknown action/)
      end
    end

    describe 'Updating files' do
      context 'when the file has been previously 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(new_file_path)
        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

    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
  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
  end
end