File: cache_spec.rb

package info (click to toggle)
ruby-carrierwave 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: ruby: 9,881; sh: 26; makefile: 5
file content (339 lines) | stat: -rw-r--r-- 11,057 bytes parent folder | download | duplicates (2)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
require 'spec_helper'

describe CarrierWave::Uploader do
  let(:uploader_class) { Class.new(CarrierWave::Uploader::Base) }
  let(:uploader) { uploader_class.new }
  let(:test_file_name) { "test.jpg"}
  let(:test_file_path) { file_path(test_file_name) }
  let(:test_file) { File.open(test_file_path) }
  let(:permission) { 0777 }
  let(:cache_id) { '1369894322-345-1234-2255' }

  before { FileUtils.rm_rf(public_path) }

  after { FileUtils.rm_rf(public_path) }

  describe '#cache_dir' do
    it "defaults to the config option" do
      expect(uploader.cache_dir).to eq('uploads/tmp')
    end
  end

  describe '#sanitized_file' do
    before { uploader.store! CarrierWave::SanitizedFile.new(test_file) }

    it "returns a sanitized file" do
      expect(uploader.sanitized_file).to be_an_instance_of(CarrierWave::SanitizedFile)
    end

    it "only read the file once" do
      expect(uploader.file).to receive(:read).once.and_return('this is stuff')

      uploader.sanitized_file
    end
  end

  context "permissions" do
    it "sets permissions if options are given" do
      uploader_class.permissions = permission
      uploader.cache!(test_file)

      expect(uploader).to have_permissions(permission)
    end

    it "sets directory permissions if options are given" do
      uploader_class.directory_permissions = permission
      uploader.cache!(test_file)

      expect(uploader).to have_directory_permissions(permission)
    end

    describe "with ensuring multipart form deactivated" do
      before do
        CarrierWave.configure { |config| config.ensure_multipart_form = false }
      end

      it "doesn't raise an error when trying to cache a string" do
        expect(running {
                 uploader.cache!(file_path(test_file_name))
        }).not_to raise_error
      end

      it "doesn't raise an error when trying to cache a pathname and " do
        expect(running {
                 uploader.cache!(Pathname.new(file_path(test_file_name)))
        }).not_to raise_error
      end
    end
  end

  describe '#cache!' do
    before { allow(CarrierWave).to receive(:generate_cache_id).and_return(cache_id) }

    context "when ensure_multipart_form is true" do
      before { CarrierWave.configure { |config| config.ensure_multipart_form = true } }

      it "raises an error when trying to cache a string" do
        expect(running { uploader.cache!(test_file_path) }).to raise_error(CarrierWave::FormNotMultipart)
      end

      it "raises an error when trying to cache a pathname" do
        expect { uploader.cache!(Pathname.new(test_file)) }.to raise_error(CarrierWave::FormNotMultipart)
      end
    end

    context "when a file is cached" do
      before { uploader.cache!(test_file) }

      it "caches the file" do
        expect(uploader.file).to be_an_instance_of(CarrierWave::SanitizedFile)
      end

      it { expect(uploader).to be_cached }

      it "stores the cache name" do
        expect(uploader.cache_name).to eq("#{cache_id}/#{test_file_name}")
      end

      it "sets the filename to the file's sanitized filename" do
        expect(uploader.filename).to eq(test_file_name)
      end

      it "moves it to the tmp dir" do
        expect(uploader.file.path).to eq(public_path("uploads/tmp/#{cache_id}/#{test_file_name}"))
      end

      it { expect(uploader.file.exists?).to be_truthy }

      it "sets the url" do
        expect(uploader.url).to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
      end

      it "does nothing when trying to cache an empty file" do
        uploader.cache!(nil)
      end


      context 'negative cache id' do
        let(:cache_id) { '-1369894322-345-1234-2255' }

        before do
          allow(CarrierWave).to receive(:generate_cache_id).and_return(cache_id)
        end

        it "doesn't raise an error when caching" do
          expect(running {
                   uploader.cache!(test_file)
          }).not_to raise_error
        end
      end
    end

    describe "with the move_to_cache option" do
      let(:tmp_file_name) { "test_move.jpeg" }
      let(:tmp_file_path) { file_path(tmp_file_name) }
      let(:tmp_file) { File.open(tmp_file_path) }
      let(:cached_id) { '1369894322-345-1234-2255' }
      let(:cached_path) { public_path("uploads/tmp/#{cached_id}/#{tmp_file_name}") }
      let(:workfile_path) { tmp_path("#{cached_id}/#{tmp_file_name}") }

      before do
        FileUtils.cp(test_file, File.join(File.dirname(test_file), tmp_file_name))

        allow(CarrierWave).to receive(:generate_cache_id).and_return(cached_id)

        uploader_class.permissions = permission
        uploader_class.directory_permissions = permission
      end

      after do
        FileUtils.rm_f(tmp_file.path)
      end

      context "set to true" do
        before { uploader_class.move_to_cache = true }

        context "moving from the upload directory to the temporary directory" do
          let(:original_path) { tmp_file.path }

          before { uploader.cache!(tmp_file) }

          it { expect(uploader.file.path).to eq(cached_path) }

          it { expect(File.exist?(cached_path)).to be_truthy }

          it { expect(File.exist?(original_path)).to be_falsey }

        end

        describe "method usage" do
          after { uploader.cache!(tmp_file) }

          it "uses #move_to during #cache!" do
            moved_file = double('moved file').as_null_object

            expect_any_instance_of(CarrierWave::SanitizedFile).to receive(:move_to).with(workfile_path, permission, permission).and_return(moved_file)
            expect(moved_file).to receive(:move_to).with(cached_path, permission, permission, true)
          end

          it "doesn't use #copy_to during #cache!" do
            expect_any_instance_of(CarrierWave::SanitizedFile).not_to receive(:copy_to)
          end
        end
      end

      context "set to false" do
        before { uploader_class.move_to_cache = false }

        context "copying from the upload directory to the temporary directory" do
          let(:original_path) { tmp_file.path }

          before { uploader.cache!(tmp_file) }

          it { expect(uploader.file.path).to eq(cached_path) }
          it { expect(File.exist?(cached_path)).to be_truthy }
          it { expect(File.exist?(original_path)).to be_truthy }
        end

        describe "method usage" do
          after { uploader.cache!(tmp_file) }

          it "uses #move_to during cache!" do
            moved_file = double('moved file').as_null_object

            expect_any_instance_of(CarrierWave::SanitizedFile).to receive(:copy_to).with(workfile_path, permission, permission).and_return(moved_file)
            expect(moved_file).to receive(:move_to).with(cached_path, permission, permission, true)
          end

          it "doesn't use #move_to during #cache!" do
            expect_any_instance_of(CarrierWave::SanitizedFile).not_to receive(:move_to).with(workfile_path, permission, permission)
          end
        end
      end
    end

    it "uses different workfiles for different versions" do
      uploader_class.version(:small)
      uploader_class.version(:large)

      uploader.cache!(test_file)

      expect(uploader.small.send(:workfile_path)).not_to eq uploader.large.send(:workfile_path)
    end
  end

  describe '#retrieve_from_cache!' do
    before { uploader.retrieve_from_cache!("#{cache_id}/#{test_file_name}") }

    it "caches a file" do
      expect(uploader.file).to be_an_instance_of(CarrierWave::SanitizedFile)
    end

    it { expect(uploader).to be_cached }

    it "sets the path to the tmp dir" do
      expect(uploader.current_path).to eq(public_path("uploads/tmp/#{cache_id}/#{test_file_name}"))
    end

    it "overwrites a file that has already been cached" do
      uploader.retrieve_from_cache!("#{cache_id}/#{test_file_name}")
      uploader.retrieve_from_cache!("#{cache_id}/bork.txt")

      expect(uploader.current_path).to eq(public_path("uploads/tmp/#{cache_id}/bork.txt"))
    end

    it "stores the cache_name" do
      expect(uploader.cache_name).to eq("#{cache_id}/#{test_file_name}")
    end

    it "stores the filename" do
      expect(uploader.filename).to eq(test_file_name)
    end

    it "sets the url" do
      expect(uploader.url).to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
    end


    it "supports old format of cache_id (without counter) for backwards compartibility" do
      expect(uploader.url).to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
    end

    it "raises an error when the cache_id has an invalid format" do
      expect(running {
        uploader.retrieve_from_cache!("12345/#{test_file_name}")
      }).to raise_error(CarrierWave::InvalidParameter)
    end

    context "when the original filename has invalid characters" do
      it do
        expect(running {
          uploader.retrieve_from_cache!('1369894322-345-1234-2255/te/st.jpeg')
        }).to raise_error(CarrierWave::InvalidParameter)
      end

      it do
        expect(running {
          uploader.retrieve_from_cache!('1369894322-345-1234-2255/te??%st.jpeg')
        }).to raise_error(CarrierWave::InvalidParameter)
      end
    end
  end

  describe 'with an overridden, reversing, filename' do
    before do
      uploader_class.class_eval do
        def filename
          super.reverse unless super.blank?
        end
      end

      allow(CarrierWave).to receive(:generate_cache_id).and_return(cache_id)
      uploader.cache!(test_file)
    end

    let(:reversed_test_file_name) { test_file_name.reverse }

    describe '#cache!' do
      it "sets the filename to the file's reversed filename" do
        expect(uploader.filename).to eq(reversed_test_file_name)
      end

      it "moves it to the tmp dir with the filename unreversed" do
        expect(uploader.current_path).to eq(public_path("uploads/tmp/#{cache_id}/#{test_file_name}"))
        expect(uploader.file.exists?).to be_truthy
      end
    end

    describe '#retrieve_from_cache!' do
      it "sets the path to the tmp dir" do
        expect(uploader.current_path).to eq(public_path("uploads/tmp/#{cache_id}/#{test_file_name}"))
      end

      it "sets the filename to the reversed name of the file" do
        expect(uploader.filename).to eq(reversed_test_file_name)
      end
    end
  end

  describe '.generate_cache_id' do
    it 'generates dir name based on UTC time' do
      Timecop.freeze(Time.at(1369896000)) do
        expect(CarrierWave.generate_cache_id).to match(/\A1369896000-\d+-\d+-\d+\Z/)
      end
    end

    it 'generates dir name with a counter substring' do
      counter = CarrierWave.generate_cache_id.split('-')[2].to_i

      expect(CarrierWave.generate_cache_id.split('-')[2].to_i).to eq(counter + 1)
    end

    it 'generates dir name with constant length even when counter has big value' do
      length = CarrierWave.generate_cache_id.length
      allow(CarrierWave::CacheCounter).to receive(:increment).and_return(1234567890)

      expect(CarrierWave.generate_cache_id.length).to eq(length)
    end
  end
end