summaryrefslogtreecommitdiff
path: root/spec/lib/uploaded_file_spec.rb
blob: 721b3d70febd40e3f8b8580a49b628414b3b0ece (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe UploadedFile, feature_category: :package_registry do
  let(:temp_dir) { Dir.tmpdir }
  let(:temp_file) { Tempfile.new(%w[test test], temp_dir) }

  before do
    FileUtils.touch(temp_file)
  end

  after do
    FileUtils.rm_f(temp_file)
  end

  context 'from_params functions' do
    RSpec.shared_examples 'using the file path' do |filename:, content_type:, sha256:, path_suffix:, upload_duration:, sha1:, md5:|
      it { is_expected.not_to be_nil }

      it 'sets properly the attributes' do
        expect(subject.original_filename).to eq(filename)
        expect(subject.content_type).to eq(content_type)
        expect(subject.sha256).to eq(sha256)
        expect(subject.remote_id).to be_nil
        expect(subject.path).to end_with(path_suffix)
        expect(subject.upload_duration).to eq(upload_duration)
        expect(subject.sha1).to eq(sha1)
        expect(subject.md5).to eq(md5)
      end

      it 'handles a blank path' do
        params['path'] = ''

        # Not a real file, so can't determine size itself
        params['size'] = 1.byte

        expect { described_class.from_params(params, upload_path) }
          .not_to raise_error
      end
    end

    RSpec.shared_examples 'using the remote id' do |filename:, content_type:, sha256:, size:, remote_id:, upload_duration:, sha1:, md5:|
      it { is_expected.not_to be_nil }

      it 'sets properly the attributes' do
        expect(subject.original_filename).to eq(filename)
        expect(subject.content_type).to eq(content_type)
        expect(subject.sha256).to eq(sha256)
        expect(subject.path).to be_nil
        expect(subject.size).to eq(size)
        expect(subject.remote_id).to eq(remote_id)
        expect(subject.upload_duration).to eq(upload_duration)
        expect(subject.sha1).to eq(sha1)
        expect(subject.md5).to eq(md5)
      end
    end

    describe '.from_params' do
      let(:upload_path) { nil }

      after do
        FileUtils.rm_r(upload_path) if upload_path
      end

      subject do
        described_class.from_params(params, [upload_path, Dir.tmpdir])
      end

      context 'when valid file is specified' do
        context 'only local path is specified' do
          let(:params) { { 'path' => temp_file.path } }

          it { is_expected.not_to be_nil }

          it 'generates filename from path' do
            expect(subject.original_filename).to eq(::File.basename(temp_file.path))
          end
        end

        context 'all parameters are specified' do
          context 'with a filepath' do
            let(:params) do
              { 'path' => temp_file.path,
                'name' => 'dir/my file&.txt',
                'type' => 'my/type',
                'upload_duration' => '5.05',
                'sha256' => 'sha256',
                'sha1' => 'sha1',
                'md5' => 'md5' }
            end

            it_behaves_like 'using the file path',
                            filename: 'my_file_.txt',
                            content_type: 'my/type',
                            sha256: 'sha256',
                            path_suffix: 'test',
                            upload_duration: 5.05,
                            sha1: 'sha1',
                            md5: 'md5'
          end

          context 'with a remote id' do
            let(:params) do
              {
                'name' => 'dir/my file&.txt',
                'sha256' => 'sha256',
                'remote_url' => 'http://localhost/file',
                'remote_id' => '1234567890',
                'etag' => 'etag1234567890',
                'upload_duration' => '5.05',
                'size' => '123456',
                'sha1' => 'sha1',
                'md5' => 'md5'
              }
            end

            it_behaves_like 'using the remote id',
                            filename: 'my_file_.txt',
                            content_type: 'application/octet-stream',
                            sha256: 'sha256',
                            size: 123456,
                            remote_id: '1234567890',
                            upload_duration: 5.05,
                            sha1: 'sha1',
                            md5: 'md5'
          end

          context 'with a path and a remote id' do
            let(:params) do
              {
                'path' => temp_file.path,
                'name' => 'dir/my file&.txt',
                'sha256' => 'sha256',
                'remote_url' => 'http://localhost/file',
                'remote_id' => '1234567890',
                'etag' => 'etag1234567890',
                'upload_duration' => '5.05',
                'size' => '123456',
                'sha1' => 'sha1',
                'md5' => 'md5'
              }
            end

            it_behaves_like 'using the remote id',
                            filename: 'my_file_.txt',
                            content_type: 'application/octet-stream',
                            sha256: 'sha256',
                            size: 123456,
                            remote_id: '1234567890',
                            upload_duration: 5.05,
                            sha1: 'sha1',
                            md5: 'md5'
          end
        end
      end

      context 'when no params are specified' do
        let(:params) { {} }

        it 'does not return an object' do
          is_expected.to be_nil
        end
      end

      context 'when verifying allowed paths' do
        let(:params) { { 'path' => temp_file.path } }

        context 'when file is stored in system temporary folder' do
          let(:temp_dir) { Dir.tmpdir }

          it { is_expected.not_to be_nil }
        end

        context 'when file is stored in user provided upload path' do
          let(:upload_path) { Dir.mktmpdir }
          let(:temp_dir) { upload_path }

          it { is_expected.not_to be_nil }
        end

        context 'when file is stored outside of user provided upload path' do
          let!(:generated_dir) { Dir.mktmpdir }
          let!(:temp_dir) { Dir.mktmpdir }

          before do
            # We overwrite default temporary path
            allow(Dir).to receive(:tmpdir).and_return(generated_dir)
          end

          it 'raises an error' do
            expect { subject }.to raise_error(UploadedFile::InvalidPathError, /insecure path used/)
          end
        end
      end
    end
  end

  describe '.initialize' do
    context 'when no size is provided' do
      it 'determine size from local path' do
        file = described_class.new(temp_file.path)

        expect(file.size).to eq(temp_file.size)
      end

      it 'raises an exception if is a remote file' do
        expect do
          described_class.new(nil, remote_id: 'id')
        end.to raise_error(UploadedFile::UnknownSizeError, 'Unable to determine file size')
      end
    end

    context 'when size is a number' do
      let_it_be(:size) { 1.gigabyte }

      it 'is overridden by the size of the local file' do
        file = described_class.new(temp_file.path, size: size)

        expect(file.size).to eq(temp_file.size)
      end

      it 'is respected if is a remote file' do
        file = described_class.new(nil, remote_id: 'id', size: size)

        expect(file.size).to eq(size)
      end
    end

    context 'when size is a string' do
      it 'is converted to a number' do
        file = described_class.new(nil, remote_id: 'id', size: '1')

        expect(file.size).to eq(1)
      end

      it 'raises an exception if does not represent a number' do
        expect do
          described_class.new(nil, remote_id: 'id', size: 'not a number')
        end.to raise_error(UploadedFile::UnknownSizeError, 'Unable to determine file size')
      end
    end

    context 'when upload_duration is not provided' do
      it 'sets upload_duration to zero' do
        file = described_class.new(temp_file.path)

        expect(file.upload_duration).to be_zero
      end
    end

    context 'when upload_duration is provided' do
      let(:file) { described_class.new(temp_file.path, upload_duration: duration) }

      context 'and upload_duration is a number' do
        let(:duration) { 5.505 }

        it 'sets the upload_duration' do
          expect(file.upload_duration).to eq(duration)
        end
      end

      context 'and upload_duration is a string' do
        context 'and represents a number' do
          let(:duration) { '5.505' }

          it 'converts upload_duration to a number' do
            expect(file.upload_duration).to eq(duration.to_f)
          end
        end

        context 'and does not represent a number' do
          let(:duration) { 'not a number' }

          it 'sets upload_duration to zero' do
            expect(file.upload_duration).to be_zero
          end
        end
      end
    end

    context 'when unknown keyword params are provided' do
      it 'raises an exception' do
        expect do
          described_class.new(temp_file.path, foo: 'param1', bar: 'param2')
        end.to raise_error(ArgumentError, 'unknown keyword(s): foo, bar')
      end
    end
  end

  describe '#sanitize_filename' do
    it { expect(described_class.new(temp_file.path).sanitize_filename('spaced name')).to eq('spaced_name') }
    it { expect(described_class.new(temp_file.path).sanitize_filename('#$%^&')).to eq('_____') }
    it { expect(described_class.new(temp_file.path).sanitize_filename('..')).to eq('_..') }
    it { expect(described_class.new(temp_file.path).sanitize_filename('')).to eq('unnamed') }
  end
end