summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/pages_transfer_spec.rb
blob: 021d9cb731853700dc21186f2168c6b7e345e05d (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
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::PagesTransfer do
  describe '#async' do
    let(:async) { subject.async }

    context 'when receiving an allowed method' do
      it 'schedules a PagesTransferWorker', :aggregate_failures do
        described_class::METHODS.each do |meth|
          expect(PagesTransferWorker)
            .to receive(:perform_async).with(meth, %w[foo bar])

          async.public_send(meth, 'foo', 'bar')
        end
      end

      it 'does nothing if legacy storage is disabled' do
        allow(Settings.pages.local_store).to receive(:enabled).and_return(false)

        described_class::METHODS.each do |meth|
          expect(PagesTransferWorker)
            .not_to receive(:perform_async)

          async.public_send(meth, 'foo', 'bar')
        end
      end
    end

    context 'when receiving a private method' do
      it 'raises NoMethodError' do
        expect { async.move('foo', 'bar') }.to raise_error(NoMethodError)
      end
    end

    context 'when receiving a non-existent method' do
      it 'raises NoMethodError' do
        expect { async.foo('bar') }.to raise_error(NoMethodError)
      end
    end
  end

  RSpec.shared_examples 'moving a pages directory' do |parameter|
    let!(:pages_path_before) { project.pages_path }
    let(:config_path_before) { File.join(pages_path_before, 'config.json') }
    let(:pages_path_after) { project.reload.pages_path }
    let(:config_path_after) { File.join(pages_path_after, 'config.json') }

    before do
      FileUtils.mkdir_p(pages_path_before)
      FileUtils.touch(config_path_before)
    end

    after do
      FileUtils.remove_entry(pages_path_before, true)
      FileUtils.remove_entry(pages_path_after, true)
    end

    it 'moves the directory' do
      subject.public_send(meth, *args)

      expect(File.exist?(config_path_before)).to be(false)
      expect(File.exist?(config_path_after)).to be(true)
    end

    it 'returns false if it fails to move the directory' do
      # Move the directory once, so it can't be moved again
      subject.public_send(meth, *args)

      expect(subject.public_send(meth, *args)).to be(false)
    end

    it 'does nothing if legacy storage is disabled' do
      allow(Settings.pages.local_store).to receive(:enabled).and_return(false)

      subject.public_send(meth, *args)

      expect(File.exist?(config_path_before)).to be(true)
      expect(File.exist?(config_path_after)).to be(false)
    end
  end

  describe '#move_namespace' do
    # Can't use let_it_be because we change the path
    let(:group_1) { create(:group) }
    let(:group_2) { create(:group) }
    let(:subgroup) { create(:group, parent: group_1) }
    let(:project) { create(:project, group: subgroup) }
    let(:new_path) { "#{group_2.path}/#{subgroup.path}" }
    let(:meth) { 'move_namespace' }

    # Store the path before we change it
    let!(:args) { [project.path, subgroup.full_path, new_path] }

    before do
      # We need to skip hooks, otherwise the directory will be moved
      # via an ActiveRecord callback
      subgroup.update_columns(parent_id: group_2.id)
      subgroup.route.update!(path: new_path)
    end

    include_examples 'moving a pages directory'
  end

  describe '#move_project' do
    # Can't use let_it_be because we change the path
    let(:group_1) { create(:group) }
    let(:group_2) { create(:group) }
    let(:project) { create(:project, group: group_1) }
    let(:new_path) { group_2.path }
    let(:meth) { 'move_project' }
    let(:args) { [project.path, group_1.full_path, group_2.full_path] }

    include_examples 'moving a pages directory' do
      before do
        project.update!(group: group_2)
      end
    end
  end

  describe '#rename_project' do
    # Can't use let_it_be because we change the path
    let(:project) { create(:project) }
    let(:new_path) { project.path.succ }
    let(:meth) { 'rename_project' }

    # Store the path before we change it
    let!(:args) { [project.path, new_path, project.namespace.full_path] }

    include_examples 'moving a pages directory' do
      before do
        project.update!(path: new_path)
      end
    end
  end

  describe '#rename_namespace' do
    # Can't use let_it_be because we change the path
    let(:group) { create(:group) }
    let(:project) { create(:project, group: group) }
    let(:new_path) { project.namespace.full_path.succ }
    let(:meth) { 'rename_namespace' }

    # Store the path before we change it
    let!(:args) { [project.namespace.full_path, new_path] }

    before do
      # We need to skip hooks, otherwise the directory will be moved
      # via an ActiveRecord callback
      group.update_columns(path: new_path)
      group.route.update!(path: new_path)
    end

    include_examples 'moving a pages directory'
  end
end