summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/controllers/repository_lfs_file_load_shared_examples.rb
blob: fadf428125abf423ae1d708959e6b83f643fe4cc (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
158
159
160
161
162
# frozen_string_literal: true

# Shared examples for controllers that load and send files from the git repository
# (like Projects::RawController or Projects::AvatarsController)

# These examples requires the following variables:
# - `project`
# - `filename`: filename of the file
# - `filepath`: path of the file (contains filename)
# - `subject`: the request to be made to the controller. Example:
# subject { get :show, namespace_id: project.namespace, project_id: project }
#
# The LFS disabled scenario can be skipped by passing `skip_lfs_disabled_tests: true`
# when including the examples (Note, at time of writing this is only used by
# an EE-specific spec):
#
# it_behaves_like 'a controller that can serve LFS files', skip_lfs_disabled_tests: true do
#   ...
# end
RSpec.shared_examples 'a controller that can serve LFS files' do |options = {}|
  let(:lfs_oid) { '91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897' }
  let(:lfs_size) { '1575078' }
  let!(:lfs_object) { create(:lfs_object, oid: lfs_oid, size: lfs_size) }

  context 'when lfs is enabled' do
    before do
      allow_any_instance_of(Project).to receive(:lfs_enabled?).and_return(true)
      allow_any_instance_of(LfsObjectUploader).to receive(:exists?).and_return(true)
      allow(controller).to receive(:send_file) { controller.head :ok }
    end

    def link_project(project)
      project.lfs_objects << lfs_object
    end

    context 'when the project is linked to the LfsObject' do
      before do
        link_project(project)
      end

      it 'serves the file' do
        lfs_uploader = LfsObjectUploader.new(lfs_object)

        expect(controller).to receive(:send_file)
                          .with(
                            File.join(lfs_uploader.root, lfs_uploader.store_dir, lfs_uploader.filename),
                            filename: filename,
                            disposition: 'attachment')

        subject

        expect(response).to have_gitlab_http_status(:ok)
      end

      context 'and lfs uses object storage' do
        let(:lfs_object) { create(:lfs_object, :with_file, oid: lfs_oid, size: lfs_size) }

        before do
          stub_lfs_object_storage
          lfs_object.file.migrate!(LfsObjectUploader::Store::REMOTE)
        end

        it 'responds with redirect to file' do
          subject

          expect(response).to have_gitlab_http_status(:found)
          expect(response.location).to include(lfs_object.reload.file.path)
        end

        it 'sets content disposition' do
          subject

          file_uri = URI.parse(response.location)
          params = CGI.parse(file_uri.query)

          expect(params["response-content-disposition"].first).to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
        end
      end
    end

    context 'when project is not linked to the LfsObject' do
      it 'does not serve the file' do
        subject

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when the project is part of a fork network' do
      shared_examples 'a controller that correctly serves lfs files within a fork network' do
        it do
          expect(fork_network_member).not_to eq(fork_network.root_project)
        end

        it 'does not serve the file if no members are linked to the LfsObject' do
          subject

          expect(response).to have_gitlab_http_status(:not_found)
        end

        it 'serves the file when the fork network root is linked to the LfsObject' do
          link_project(fork_network.root_project)

          subject

          expect(response).to have_gitlab_http_status(:ok)
        end

        it 'serves the file when the fork network member is linked to the LfsObject' do
          link_project(fork_network_member)

          subject

          expect(response).to have_gitlab_http_status(:ok)
        end
      end

      context 'when the project is the root of the fork network' do
        let!(:fork_network) { create(:fork_network, root_project: project) }
        let!(:fork_network_member) { create(:fork_network_member, fork_network: fork_network).project }

        before do
          project.reload
        end

        it_behaves_like 'a controller that correctly serves lfs files within a fork network'
      end

      context 'when the project is a downstream member of the fork network' do
        let!(:fork_network) { create(:fork_network) }
        let!(:fork_network_member) do
          create(:fork_network_member, project: project, fork_network: fork_network)
          project
        end

        before do
          project.reload
        end

        it_behaves_like 'a controller that correctly serves lfs files within a fork network'
      end
    end
  end

  context 'when lfs is not enabled' do
    before do
      allow_any_instance_of(Project).to receive(:lfs_enabled?).and_return(false)
    end

    it 'delivers ASCII file' do
      skip 'Calling spec asked to skip testing LFS disabled scenario' if options[:skip_lfs_disabled_tests]

      subject

      expect(response).to have_gitlab_http_status(:ok)
      expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
      expect(response.header['Content-Disposition'])
          .to eq('inline')
      expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
    end
  end
end