File: url_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 (273 lines) | stat: -rw-r--r-- 8,094 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
require 'spec_helper'
require 'active_support/json'

describe CarrierWave::Uploader do

  let(:uploader) { MyCoolUploader.new }

  before { class MyCoolUploader < CarrierWave::Uploader::Base; end }

  after do
    FileUtils.rm_rf(public_path)
    Object.send(:remove_const, "MyCoolUploader") if defined?(::MyCoolUploader)
  end

  let(:cache_id) { '1369894322-345-1234-2255' }
  let(:test_file) { File.open(file_path(test_file_name)) }
  let(:test_file_name) { 'test.jpg' }

  before { allow(CarrierWave).to receive(:generate_cache_id).and_return(cache_id) }

  describe '#url' do
    subject(:url) { uploader.url }

    it { is_expected.to be_nil }

    it "doesn't raise exception when hash specified as argument" do
      expect { uploader.url({}) }.not_to raise_error
    end

    it "encodes the path of a file without an asset host" do
      uploader.cache!(File.open(file_path('test+.jpg')))
      is_expected.to eq("/uploads/tmp/#{cache_id}/test%2B.jpg")
    end

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

      it "gets the directory relative to public, prepending a slash" do
        is_expected.to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
      end

      describe "File#url" do
        before do
          allow(uploader.file).to receive(:url).and_return(file_url)
        end

        context "when present" do
          let(:file_url) { 'http://www.example.com/someurl.jpg' }

          it { is_expected.to eq(file_url) }
        end

        context "when blank" do
          let(:file_url) { '' }

          it "returns the relative path" do
            is_expected.to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
          end
        end
      end
    end

    context "when File#url method doesn't get params" do
      before do
        module StorageX
          class File
            def url
              true
            end
          end
        end

        allow(uploader).to receive(:file).and_return(StorageX::File.new)
      end

      it "raises ArgumentError" do
        expect { uploader.url }.not_to raise_error
      end
    end

    describe "(:thumb)" do
      subject { uploader.url(:thumb) }

      it "raises ArgumentError when version doesn't exist" do
        expect { uploader.url(:thumb) }.to raise_error(ArgumentError)
      end

      context "when version is specified" do
        before do
          MyCoolUploader.version(:thumb)
          uploader.cache!(test_file)
        end

        it "doesn't raise ArgumentError when versions version exists" do
          expect { uploader.url(:thumb) }.not_to raise_error
        end

        it "gets the directory relative to public for a specific version" do
          is_expected.to eq("/uploads/tmp/#{cache_id}/thumb_#{test_file_name}")
        end

        describe "asset_host" do
          before { uploader.class.configure { |config| config.asset_host = asset_host } }

          context "when set as a string" do
            let(:asset_host) { "http://foo.bar" }

            it "prepends the string" do
              is_expected.to eq("#{asset_host}/uploads/tmp/#{cache_id}/thumb_#{test_file_name}")
            end

            describe "encoding" do
              let(:test_file_name) { 'test+.jpg' }

              it "encodes the path of a file" do
                is_expected.to eq("#{asset_host}/uploads/tmp/#{cache_id}/thumb_test%2B.jpg")
              end

              it "double-encodes the path of an available File#url" do
                url = 'http://www.example.com/directory%2Bname/another%2Bdirectory/some%2Burl.jpg'
                allow(uploader.file).to receive(:url).and_return(url)

                expect(uploader.url).to eq(url)
              end
            end
          end

          context "when set as a proc" do
            let(:asset_host) { proc { "http://foo.bar" } }

            it "prepends the result of proc" do
              is_expected.to eq("#{asset_host.call}/uploads/tmp/#{cache_id}/thumb_#{test_file_name}")
            end

            describe "encoding" do
              let(:test_file_name) { 'test+.jpg' }

              it { is_expected.to eq("#{asset_host.call}/uploads/tmp/#{cache_id}/thumb_test%2B.jpg") }
            end
          end

          context "when set as nil" do
            let(:asset_host) { nil }

            context "when base_path is set" do
              let(:base_path) { "/base_path" }

              before do
                uploader.class.configure do |config|
                  config.base_path = base_path
                end
              end

              it "prepends the config option 'base_path'" do
                is_expected.to eq("#{base_path}/uploads/tmp/#{cache_id}/thumb_#{test_file_name}")
              end
            end
          end
        end
      end

      context "when the version is nested" do
        subject { uploader.url(:thumb, :mini) }

        before do
          MyCoolUploader.version(:thumb) { version(:mini) }
          uploader.cache!(test_file)
        end

        it "gets the directory relative to public for a nested version" do
          is_expected.to eq("/uploads/tmp/#{cache_id}/thumb_mini_#{test_file_name}")
        end
      end
    end
  end

  describe '#to_json' do
    subject(:parsed_json) { JSON.parse(to_json) }

    let(:to_json) { uploader.to_json }

    context "(:thumb)" do
      before { MyCoolUploader.version(:thumb) }

      it { expect(parsed_json.keys).to include("url") }
      it { expect(parsed_json.keys).to include("thumb") }
      it { expect(parsed_json["url"]).to be_nil }
      it { expect(parsed_json["thumb"].keys).to include("url") }
      it { expect(parsed_json["thumb"]["url"]).to be_nil }

      context "with a cached_file" do
        before { uploader.cache!(test_file) }

        it { expect(parsed_json.keys).to include("thumb") }
        it { expect(parsed_json["thumb"]).to eq({"url" => "/uploads/tmp/#{cache_id}/thumb_#{test_file_name}"}) }
      end
    end

    context "with cached file" do
      before { uploader.cache!(test_file) }

      it "returns a hash including a cached URL" do
        is_expected.to eq({"url" => "/uploads/tmp/#{cache_id}/#{test_file_name}"})
      end
    end

    it "allows an options parameter to be passed in" do
      expect { uploader.to_json({:some => 'options'}) }.not_to raise_error
    end
  end

  describe '#to_xml' do
    subject(:parsed_xml) { Hash.from_xml(to_xml) }

    let(:to_xml) { uploader.to_xml }

    it "returns a hash with a blank URL" do
      is_expected.to eq({"uploader" => {"url" => nil}})
    end

    context "with cached file" do
      before { uploader.cache!(test_file) }

      it "returns a hash including a cached URL" do
        is_expected.to eq({"uploader" => {"url" => "/uploads/tmp/#{cache_id}/#{test_file_name}"}})
      end

      context "with an array of uploaders" do
        let(:to_xml) { [uploader].to_xml }

        it "returns a hash including an array with a cached URL" do
          is_expected.to have_value([{"url"=>"/uploads/tmp/#{cache_id}/#{test_file_name}"}])
        end
      end
    end

    describe "(:thumb)" do
      before { MyCoolUploader.version(:thumb) }

      context "with cached file" do
        before { uploader.cache!(test_file) }

        it "returns a hash including a cached URL of a version" do
          expect(parsed_xml["uploader"]["thumb"]).to eq({"url" => "/uploads/tmp/#{cache_id}/thumb_#{test_file_name}"})
        end
      end
    end
  end

  describe '#to_s' do
    subject { uploader.to_s }

    it { is_expected.to eq('') }

    context "with cached file" do
      before { uploader.cache!(test_file) }

      it "gets the directory relative to public, prepending a slash" do
        is_expected.to eq("/uploads/tmp/#{cache_id}/#{test_file_name}")
      end

      describe "File#url" do
        before { allow(uploader.file).to receive(:url).and_return(url) }

        context "when present" do
          let(:url) { 'http://www.example.com/someurl.jpg' }

          it { is_expected.to eq(url) }
        end
      end
    end
  end
end