carrierwave_direct 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ require 'carrierwave_direct/action_view_extensions/form_helper'
12
12
 
13
13
  module FormBuilderHelpers
14
14
  include ActionView::Helpers::FormHelper
15
+ include ActionView::Helpers::FormOptionsHelper
15
16
  include CarrierWaveDirect::ActionViewExtensions::FormHelper
16
17
  include ActionView::Context
17
18
 
@@ -16,16 +16,16 @@ module ModelHelpers
16
16
  if options[:accessible]
17
17
  if options[:accessible] == true
18
18
  it "should == '#{sample_data}'" do
19
- accessor_value.should == sample_data
19
+ expect(accessor_value).to eq sample_data
20
20
  end
21
21
  else
22
22
  it "##{options[:accessible].keys.first} should be #{options[:accessible].values.first}" do
23
- subject.send(options[:accessible].keys.first).should == options[:accessible].values.first
23
+ expect(subject.send(options[:accessible].keys.first)).to eq options[:accessible].values.first
24
24
  end
25
25
  end
26
26
  else
27
27
  it "should be nil" do
28
- accessor_value.should be_nil
28
+ expect(accessor_value).to be_nil
29
29
  end
30
30
  end
31
31
  end
@@ -38,14 +38,14 @@ module ModelHelpers
38
38
  describe "##{name} = '#{sample_data}'" do
39
39
  it "should set the #{delegation_object}'s #{delegation_method}" do
40
40
  subject.send("#{name}=", sample_data)
41
- subject.send(delegation_object).send(delegation_method).should == sample_data
41
+ expect(subject.send(delegation_object).send(delegation_method)).to eq sample_data
42
42
  end
43
43
  end
44
44
 
45
45
  describe "##{name}" do
46
46
  it "should return the #{delegation_method} from the #{delegation_object}" do
47
47
  subject.send(delegation_object).send("#{delegation_method}=", sample_data)
48
- subject.send(name).should == sample_data
48
+ expect(subject.send(name)).to eq sample_data
49
49
  end
50
50
 
51
51
  it_should_be_accessible(name, sample_data, options)
@@ -62,7 +62,7 @@ module ModelHelpers
62
62
  end
63
63
 
64
64
  it "should respond to ##{name}=" do
65
- subject.should respond_to("#{name}=")
65
+ expect(subject).to respond_to("#{name}=")
66
66
  end
67
67
 
68
68
  describe "##{name}" do
@@ -70,7 +70,7 @@ module ModelHelpers
70
70
  before { subject.send("#{name}=", sample_data) }
71
71
 
72
72
  it "should == '#{sample_data}'" do
73
- subject.send(name).should == sample_data
73
+ expect(subject.send(name)).to eq sample_data
74
74
  end
75
75
  end
76
76
 
@@ -36,12 +36,22 @@ module ViewHelpers
36
36
  end
37
37
 
38
38
  def have_input(resource_name, input, options = {})
39
+ count = (options.delete :count).to_i
40
+ selector_options = count > 0 ? {:count => count} : {}
41
+
39
42
  options[:type] ||= input
40
43
  options[:id] ||= "#{resource_name}_#{input}"
41
44
  options[:name] ||= "#{resource_name}[#{input}]"
42
45
  options[:required] ||= "required" unless options[:required] == false
43
46
  parent_selector << "input[#{to_xpath_attributes(options)}]"
44
- have_parent_selector
47
+ have_parent_selector selector_options
48
+ end
49
+
50
+ def have_content_type(content_type,selected=nil)
51
+ options = {:value => content_type}
52
+ options[:selected] = 'selected' if selected
53
+
54
+ have_selector :xpath, ".//select[@name=\"Content-Type\"]/option[#{to_xpath_attributes(options)}]"
45
55
  end
46
56
  end
47
57
 
@@ -8,22 +8,26 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
8
8
  end
9
9
 
10
10
  let(:subject) { ExampleSpec.new }
11
- let(:page) { mock("Page").as_null_object }
12
- let(:selector) { mock("Selector") }
11
+ let(:page) { double("Page").as_null_object }
12
+ let(:selector) { double("Selector") }
13
13
 
14
14
  def stub_page
15
- subject.stub(:page).and_return(page)
15
+ allow(subject).to receive(:page).and_return(page)
16
16
  end
17
17
 
18
- def find_element_value(css, value)
19
- page.stub(:find).with(css).and_return(selector)
20
- selector.stub(:value).and_return(value)
18
+ def find_element_value(css, value, options = nil)
19
+ if options
20
+ allow(page).to receive(:find).with(css, options).and_return(selector)
21
+ else
22
+ allow(page).to receive(:find).with(css).and_return(selector)
23
+ end
24
+ allow(selector).to receive(:value).and_return(value)
21
25
  end
22
26
 
23
27
  describe "#attach_file_for_direct_upload" do
24
28
  context "'path/to/file.ext'" do
25
29
  it "should attach a file with the locator => 'file'" do
26
- subject.should_receive(:attach_file).with("file", "path/to/file.ext")
30
+ expect(subject).to receive(:attach_file).with("file", "path/to/file.ext")
27
31
  subject.attach_file_for_direct_upload "path/to/file.ext"
28
32
  end
29
33
  end
@@ -40,19 +44,19 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
40
44
 
41
45
  def stub_common
42
46
  stub_page
43
- find_element_value("input[@name='success_action_redirect']", "http://example.com")
44
- subject.stub(:visit)
47
+ find_element_value("input[name='success_action_redirect']", "http://example.com", visible: false)
48
+ allow(subject).to receive(:visit)
45
49
  end
46
50
 
47
51
  before do
48
- subject.stub(:click_button)
52
+ allow(subject).to receive(:click_button)
49
53
  end
50
54
 
51
55
  shared_examples_for "submitting the form" do
52
56
  let(:options) { {} }
53
57
 
54
58
  it "should submit the form" do
55
- subject.should_receive(:click_button).with("Upload!")
59
+ expect(subject).to receive(:click_button).with("Upload!")
56
60
  upload_directly(options.merge(:button_locator => "Upload!"))
57
61
  end
58
62
  end
@@ -61,7 +65,7 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
61
65
  let(:options) { { :success => false } }
62
66
 
63
67
  it "should not redirect" do
64
- subject.should_not_receive(:visit)
68
+ expect(subject).to_not receive(:visit)
65
69
  upload_directly(options)
66
70
  end
67
71
  end
@@ -69,30 +73,30 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
69
73
  context "passing no options" do
70
74
  before do
71
75
  stub_common
72
- subject.stub(:find_key).and_return("upload_dir/guid/$filename")
73
- subject.stub(:find_upload_path).and_return("path/to/file.ext")
76
+ allow(subject).to receive(:find_key).and_return("upload_dir/guid/$filename")
77
+ allow(subject).to receive(:find_upload_path).and_return("path/to/file.ext")
74
78
  end
75
79
 
76
80
  it_should_behave_like "submitting the form"
77
81
 
78
82
  it "should redirect to the page's success_action_redirect url" do
79
- subject.should_receive(:visit).with(/^http:\/\/example.com/)
83
+ expect(subject).to receive(:visit).with(/^http:\/\/example.com/)
80
84
  upload_directly
81
85
  end
82
86
 
83
87
  context "the redirect url's params" do
84
88
  it "should include the bucket name" do
85
- subject.should_receive(:visit).with(/bucket=/)
89
+ expect(subject).to receive(:visit).with(/bucket=/)
86
90
  upload_directly
87
91
  end
88
92
 
89
93
  it "should include an etag" do
90
- subject.should_receive(:visit).with(/etag=/)
94
+ expect(subject).to receive(:visit).with(/etag=/)
91
95
  upload_directly
92
96
  end
93
97
 
94
98
  it "should include the key derived from the form" do
95
- subject.should_receive(:visit).with(/key=upload_dir%2Fguid%2Ffile.ext/)
99
+ expect(subject).to receive(:visit).with(/key=upload_dir%2Fguid%2Ffile.ext/)
96
100
  upload_directly
97
101
  end
98
102
  end
@@ -106,7 +110,7 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
106
110
 
107
111
  context "the redirect url's params" do
108
112
  it "should include the key from the :redirect_key option" do
109
- subject.should_receive(:visit).with(/key=some\+redirect\+key/)
113
+ expect(subject).to receive(:visit).with(/key=some\+redirect\+key/)
110
114
  upload_directly(:redirect_key => "some redirect key")
111
115
  end
112
116
  end
@@ -137,24 +141,23 @@ describe CarrierWaveDirect::Test::CapybaraHelpers do
137
141
  describe "#find_key" do
138
142
  before do
139
143
  stub_page
140
- find_element_value("input[@name='key']", "key")
144
+ find_element_value("input[name='key']", "key", visible: false)
141
145
  end
142
146
 
143
147
  it "should try to find the key on the page" do
144
- subject.find_key.should == "key"
148
+ expect(subject.find_key).to eq "key"
145
149
  end
146
150
  end
147
151
 
148
152
  describe "#find_upload_path" do
149
153
  before do
150
154
  stub_page
151
- find_element_value("input[@name='file']", "upload path")
155
+ find_element_value("input[name='file']", "upload path", visible: false)
152
156
  end
153
157
 
154
158
  it "should try to find the upload path on the page" do
155
- subject.find_upload_path.should == "upload path"
159
+ expect(subject.find_upload_path).to eq "upload path"
156
160
  end
157
161
  end
158
-
159
162
  end
160
163
 
@@ -13,23 +13,23 @@ describe CarrierWaveDirect::Test::Helpers do
13
13
 
14
14
  shared_examples_for "returning the default extension" do
15
15
  it "should return '*/guid/filename.extension'" do
16
- sample_key(direct_uploader).should =~ /#{GUID_REGEXP}\/filename\.extension$/
16
+ expect(sample_key(direct_uploader)).to match /#{GUID_REGEXP}\/filename\.extension$/
17
17
  end
18
18
  end
19
19
 
20
20
  context "['exe', 'bmp']" do
21
21
  before do
22
- direct_uploader.stub(:extension_white_list).and_return(%w{exe bmp})
22
+ allow(direct_uploader).to receive(:extension_white_list).and_return(%w{exe bmp})
23
23
  end
24
24
 
25
25
  it "should return '*/guid/filename.exe'" do
26
- sample_key(direct_uploader).should =~ /#{GUID_REGEXP}\/filename\.exe$/
26
+ expect(sample_key(direct_uploader)).to match /#{GUID_REGEXP}\/filename\.exe$/
27
27
  end
28
28
  end
29
29
 
30
30
  context "[]" do
31
31
  before do
32
- direct_uploader.stub(:extension_white_list).and_return([])
32
+ allow(direct_uploader).to receive(:extension_white_list).and_return([])
33
33
  end
34
34
 
35
35
  it_should_behave_like "returning the default extension"
@@ -37,7 +37,7 @@ describe CarrierWaveDirect::Test::Helpers do
37
37
 
38
38
  context "nil" do
39
39
  before do
40
- direct_uploader.stub(:extension_white_list).and_return(nil)
40
+ allow(direct_uploader).to receive(:extension_white_list).and_return(nil)
41
41
  end
42
42
 
43
43
  it_should_behave_like "returning the default extension"
@@ -47,9 +47,7 @@ describe CarrierWaveDirect::Test::Helpers do
47
47
 
48
48
  context "with no options" do
49
49
  it "should return '*/guid/filename.extension'" do
50
- sample_key(
51
- direct_uploader
52
- ).should =~ /#{GUID_REGEXP}\/filename\.extension$/
50
+ expect(sample_key(direct_uploader)).to match /#{GUID_REGEXP}\/filename\.extension$/
53
51
  end
54
52
  end
55
53
 
@@ -64,7 +62,7 @@ describe CarrierWaveDirect::Test::Helpers do
64
62
 
65
63
  shared_examples_for "a custom filename" do
66
64
  it "should return '*/guid/some_file.reg'" do
67
- sample_key(direct_uploader, options).should =~ /#{GUID_REGEXP}\/some_file\.reg$/
65
+ expect(sample_key(direct_uploader, options)).to match /#{GUID_REGEXP}\/some_file\.reg$/
68
66
  end
69
67
  end
70
68
 
@@ -82,10 +80,10 @@ describe CarrierWaveDirect::Test::Helpers do
82
80
 
83
81
  context ":base => 'upload_dir/porno/movie/${filename}'" do
84
82
  it "should return 'upload_dir/porno/movie/guid/filename.extension'" do
85
- sample_key(
83
+ expect(sample_key(
86
84
  direct_uploader,
87
85
  :base => "upload_dir/porno/movie/${filename}"
88
- ).should == "upload_dir/porno/movie/filename.extension"
86
+ )).to eq "upload_dir/porno/movie/filename.extension"
89
87
  end
90
88
  end
91
89
  context ":filename => 'some_file.reg'" do
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWaveDirect::Uploader::Configuration do
4
+ include UploaderHelpers
5
+ include ModelHelpers
6
+
7
+ let(:subject) { DirectUploader }
8
+
9
+ before do
10
+ subject.reset_direct_config
11
+ end
12
+
13
+ describe "default configuration" do
14
+ it "returns false for validate_is_attached" do
15
+ expect(subject.validate_is_attached).to be_false
16
+ end
17
+
18
+ it "returns false for validate_is_uploaded" do
19
+ expect(subject.validate_is_uploaded).to be_false
20
+ end
21
+
22
+ it "return true for validate_unique_filename" do
23
+ expect(subject.validate_unique_filename).to be_true
24
+ end
25
+
26
+ it "returns true for validate_remote_net_url_format" do
27
+ expect(subject.validate_remote_net_url_format).to be_true
28
+ end
29
+
30
+ it "has upload_expiration of 10 hours" do
31
+ expect(subject.upload_expiration).to eq 36000
32
+ end
33
+
34
+ it "has min_file_size of 1 byte" do
35
+ expect(subject.min_file_size).to eq 1
36
+ end
37
+
38
+ it "has max_file_size of 5 MB" do
39
+ expect(subject.max_file_size).to eq 5242880
40
+ end
41
+
42
+ it "returns false for use_action_status" do
43
+ expect(subject.use_action_status).to be_false
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CarrierWaveDirect::Uploader::ContentType do
6
+ subject { DirectUploader.new }
7
+
8
+ describe "#content_type" do
9
+ it "defaults to binary/octet-stream when there is no default_content_type" do
10
+ allow(subject.class).to receive(:default_content_type).and_return(nil)
11
+ expect(subject.content_type).to eq 'binary/octet-stream'
12
+ end
13
+
14
+ it "returns the default_content_type if set" do
15
+ allow(subject.class).to receive(:default_content_type).and_return('video/mp4')
16
+ expect(subject.content_type).to eq 'video/mp4'
17
+ end
18
+ end
19
+
20
+ describe "#content_types" do
21
+ it "should default to common media types" do
22
+ expect(subject.content_types).to eq %w(application/atom+xml application/ecmascript application/json
23
+ application/javascript application/octet-stream application/ogg
24
+ application/pdf application/postscript application/rss+xml
25
+ application/font-woff application/xhtml+xml application/xml
26
+ application/xml-dtd application/zip application/gzip audio/basic
27
+ audio/mp4 audio/mpeg audio/ogg audio/vorbis audio/vnd.rn-realaudio
28
+ audio/vnd.wave audio/webm image/gif image/jpeg image/pjpeg image/png
29
+ image/svg+xml image/tiff text/cmd text/css text/csv text/html
30
+ text/javascript text/plain text/vcard text/xml video/mpeg video/mp4
31
+ video/ogg video/quicktime video/webm video/x-matroska video/x-ms-wmv
32
+ video/x-flv)
33
+ end
34
+
35
+ it "should be the configured value" do
36
+ allow(subject.class).to receive(:allowed_content_types).and_return(['audio/ogg'])
37
+ expect(subject.content_types).to eq ['audio/ogg']
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+ require 'data/sample_data'
5
+
6
+ describe CarrierWaveDirect::Uploader::DirectUrl do
7
+
8
+ let(:subject) { DirectUploader.new }
9
+
10
+ describe "#direct_fog_url" do
11
+ it "should return the result from CarrierWave::Storage::Fog::File#public_url" do
12
+ expect(subject.direct_fog_url).to eq CarrierWave::Storage::Fog::File.new(
13
+ subject, nil, nil
14
+ ).public_url
15
+ end
16
+
17
+ context ":with_path => true" do
18
+
19
+ context "#key is set to '#{sample(:path_with_special_chars)}'" do
20
+ before { subject.key = sample(:path_with_special_chars) }
21
+
22
+ it "should return the full url with '/#{URI.escape(sample(:path_with_special_chars))}' as the path" do
23
+ direct_fog_url = CarrierWave::Storage::Fog::File.new(
24
+ subject, nil, nil
25
+ ).public_url
26
+ expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + "#{URI.escape(sample(:path_with_special_chars))}"
27
+ end
28
+ end
29
+
30
+ context "#key is set to '#{sample(:path_with_escaped_chars)}'" do
31
+ before { subject.key = sample(:path_with_escaped_chars) }
32
+
33
+ it "should return the full url with '/#{sample(:path_with_escaped_chars)}' as the path" do
34
+ direct_fog_url = CarrierWave::Storage::Fog::File.new(
35
+ subject, nil, nil
36
+ ).public_url
37
+ expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + sample(:path_with_escaped_chars)
38
+ end
39
+ end
40
+
41
+ context "#key is set to '#{sample(:path)}'" do
42
+ before { subject.key = sample(:path) }
43
+
44
+ it "should return the full url with '/#{sample(:path)}' as the path" do
45
+ direct_fog_url = CarrierWave::Storage::Fog::File.new(
46
+ subject, nil, nil
47
+ ).public_url
48
+ expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + "#{sample(:path)}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+
@@ -1,87 +1,25 @@
1
1
  # encoding: utf-8
2
-
3
2
  require 'spec_helper'
3
+ require 'data/sample_data'
4
4
 
5
5
  describe CarrierWaveDirect::Uploader do
6
6
  include UploaderHelpers
7
7
  include ModelHelpers
8
8
 
9
- SAMPLE_DATA = {
10
- :path => "upload_dir/bliind.exe",
11
- :path_with_special_chars => "upload_dir/some file & blah.exe",
12
- :key => "some key",
13
- :guid => "guid",
14
- :store_dir => "store_dir",
15
- :extension_regexp => "(avi)",
16
- :url => "http://example.com/some_url",
17
- :expiration => 60,
18
- :min_file_size => 1024,
19
- :max_file_size => 10485760,
20
- :file_url => "http://anyurl.com/any_path/video_dir/filename.avi",
21
- :mounted_model_name => "Porno",
22
- :mounted_as => :video,
23
- :filename => "filename",
24
- :extension => ".avi",
25
- :version => :thumb,
26
- :s3_bucket_url => "https://s3-bucket.s3.amazonaws.com"
27
- }
28
-
29
- SAMPLE_DATA.merge!(
30
- :stored_filename_base => "#{sample(:guid)}/#{sample(:filename)}"
31
- )
32
-
33
- SAMPLE_DATA.merge!(
34
- :stored_filename => "#{sample(:stored_filename_base)}#{sample(:extension)}",
35
- :stored_version_filename => "#{sample(:stored_filename_base)}_#{sample(:version)}#{sample(:extension)}"
36
- )
37
-
38
- SAMPLE_DATA.merge!(
39
- :s3_key => "#{sample(:store_dir)}/#{sample(:stored_filename)}"
40
- )
41
-
42
- SAMPLE_DATA.merge!(
43
- :s3_file_url => "#{sample(:s3_bucket_url)}/#{sample(:s3_key)}"
44
- )
45
-
46
- SAMPLE_DATA.freeze
47
-
48
9
  let(:subject) { DirectUploader.new }
49
- let(:mounted_model) { mock(sample(:mounted_model_name)) }
10
+ let(:mounted_model) { double(sample(:mounted_model_name)) }
50
11
  let(:mounted_subject) { DirectUploader.new(mounted_model, sample(:mounted_as)) }
51
12
  let(:direct_subject) { DirectUploader.new }
52
13
 
53
- describe ".upload_expiration" do
54
- it "should be 10 hours" do
55
- subject.class.upload_expiration.should == 36000
56
- end
57
- end
58
-
59
- describe ".min_file_size" do
60
- it "should be 1 byte" do
61
- subject.class.min_file_size.should == 1
62
- end
63
- end
64
-
65
- describe ".max_file_size" do
66
- it "should be 5 MB" do
67
- subject.class.max_file_size.should == 5242880
68
- end
69
- end
70
-
71
- describe ".use_action_status" do
72
- it "should be false" do
73
- subject.class.use_action_status.should be_false
74
- end
75
- end
76
14
 
77
15
  DirectUploader.fog_credentials.keys.each do |key|
78
16
  describe "##{key}" do
79
17
  it "should return the #{key.to_s.capitalize}" do
80
- subject.send(key).should == DirectUploader.fog_credentials[key]
18
+ expect(subject.send(key)).to eq subject.class.fog_credentials[key]
81
19
  end
82
20
 
83
21
  it "should not be nil" do
84
- subject.send(key).should_not be_nil
22
+ expect(subject.send(key)).to_not be_nil
85
23
  end
86
24
  end
87
25
  end
@@ -93,13 +31,13 @@ describe CarrierWaveDirect::Uploader do
93
31
  before { subject.key = sample(:key) }
94
32
 
95
33
  it "should set the key" do
96
- subject.key.should == sample(:key)
34
+ expect(subject.key).to eq sample(:key)
97
35
  end
98
36
 
99
37
  context "the versions keys" do
100
38
  it "should == this subject's key" do
101
39
  subject.versions.each do |name, version_subject|
102
- version_subject.key.should == subject.key
40
+ expect(version_subject.key).to eq subject.key
103
41
  end
104
42
  end
105
43
  end
@@ -112,39 +50,39 @@ describe CarrierWaveDirect::Uploader do
112
50
  end
113
51
 
114
52
  it "should return '*/\#\{guid\}/${filename}'" do
115
- mounted_subject.key.should =~ /#{GUID_REGEXP}\/\$\{filename\}$/
53
+ expect(mounted_subject.key).to match /#{GUID_REGEXP}\/\$\{filename\}$/
116
54
  end
117
55
 
118
56
  context "and #store_dir returns '#{sample(:store_dir)}'" do
119
57
  before do
120
- mounted_subject.stub(:store_dir).and_return(sample(:store_dir))
58
+ allow(mounted_subject).to receive(:store_dir).and_return(sample(:store_dir))
121
59
  end
122
60
 
123
61
  it "should return '#{sample(:store_dir)}/\#\{guid\}/${filename}'" do
124
- mounted_subject.key.should =~ /^#{sample(:store_dir)}\/#{GUID_REGEXP}\/\$\{filename\}$/
62
+ expect(mounted_subject.key).to match /^#{sample(:store_dir)}\/#{GUID_REGEXP}\/\$\{filename\}$/
125
63
  end
126
64
  end
127
65
 
128
66
  context "and the uploaders url is #default_url" do
129
67
  it "should return '*/\#\{guid\}/${filename}'" do
130
- mounted_subject.stub(:url).and_return(sample(:s3_file_url))
131
- mounted_subject.stub(:present?).and_return(false)
132
- mounted_subject.key.should =~ /#{GUID_REGEXP}\/\$\{filename\}$/
68
+ allow(mounted_subject).to receive(:url).and_return(sample(:s3_file_url))
69
+ allow(mounted_subject).to receive(:present?).and_return(false)
70
+ expect(mounted_subject.key).to match /#{GUID_REGEXP}\/\$\{filename\}$/
133
71
  end
134
72
  end
135
73
 
136
74
  context "but the uploaders url is '#{sample(:s3_file_url)}'" do
137
75
  before do
138
- mounted_subject.stub(:url).and_return(sample(:s3_file_url))
139
- mounted_subject.stub(:present?).and_return(true)
76
+ allow(mounted_subject).to receive(:url).and_return(sample(:s3_file_url))
77
+ allow(mounted_subject).to receive(:present?).and_return(true)
140
78
  end
141
79
 
142
80
  it "should return '#{sample(:s3_key)}'" do
143
- mounted_subject.key.should == sample(:s3_key)
81
+ expect(mounted_subject.key).to eq sample(:s3_key)
144
82
  end
145
83
 
146
84
  it "should set the key explicitly in order to update the version keys" do
147
- mounted_subject.should_receive("key=").with(sample(:s3_key))
85
+ expect(mounted_subject).to receive("key=").with(sample(:s3_key))
148
86
  mounted_subject.key
149
87
  end
150
88
  end
@@ -154,34 +92,34 @@ describe CarrierWaveDirect::Uploader do
154
92
  before { subject.key = sample(:key) }
155
93
 
156
94
  it "should return '#{sample(:key)}'" do
157
- subject.key.should == sample(:key)
95
+ expect(subject.key).to eq sample(:key)
158
96
  end
159
97
  end
160
98
  end
161
99
 
162
100
  describe "#url_scheme_white_list" do
163
101
  it "should return nil" do
164
- subject.url_scheme_white_list.should be_nil
102
+ expect(subject.url_scheme_white_list).to be_nil
165
103
  end
166
104
  end
167
105
 
168
106
  describe "#key_regexp" do
169
107
  it "should return a regexp" do
170
- subject.key_regexp.should be_a(Regexp)
108
+ expect(subject.key_regexp).to be_a(Regexp)
171
109
  end
172
110
 
173
111
  context "where #store_dir returns '#{sample(:store_dir)}'" do
174
112
  before do
175
- subject.stub(:store_dir).and_return(sample(:store_dir))
113
+ allow(subject).to receive(:store_dir).and_return(sample(:store_dir))
176
114
  end
177
115
 
178
116
  context "and #extension_regexp returns '#{sample(:extension_regexp)}'" do
179
117
  before do
180
- subject.stub(:extension_regexp).and_return(sample(:extension_regexp))
118
+ allow(subject).to receive(:extension_regexp).and_return(sample(:extension_regexp))
181
119
  end
182
120
 
183
121
  it "should return /\\A#{sample(:store_dir)}\\/#{GUID_REGEXP}\\/.+\\.#{sample(:extension_regexp)}\\z/" do
184
- subject.key_regexp.should == /\A#{sample(:store_dir)}\/#{GUID_REGEXP}\/.+\.(?i)#{sample(:extension_regexp)}(?-i)\z/
122
+ expect(subject.key_regexp).to eq /\A#{sample(:store_dir)}\/#{GUID_REGEXP}\/.+\.(?i)#{sample(:extension_regexp)}(?-i)\z/
185
123
  end
186
124
  end
187
125
  end
@@ -190,17 +128,17 @@ describe CarrierWaveDirect::Uploader do
190
128
  describe "#extension_regexp" do
191
129
  shared_examples_for "a globally allowed file extension" do
192
130
  it "should return '\\w+'" do
193
- subject.extension_regexp.should == "\\w+"
131
+ expect(subject.extension_regexp).to eq "\\w+"
194
132
  end
195
133
  end
196
134
 
197
135
  it "should return a string" do
198
- subject.extension_regexp.should be_a(String)
136
+ expect(subject.extension_regexp).to be_a(String)
199
137
  end
200
138
 
201
139
  context "where #extension_white_list returns nil" do
202
140
  before do
203
- subject.stub(:extension_white_list).and_return(nil)
141
+ allow(subject).to receive(:extension_white_list).and_return(nil)
204
142
  end
205
143
 
206
144
  it_should_behave_like "a globally allowed file extension"
@@ -208,7 +146,7 @@ describe CarrierWaveDirect::Uploader do
208
146
 
209
147
  context "where #extension_white_list returns []" do
210
148
  before do
211
- subject.stub(:extension_white_list).and_return([])
149
+ allow(subject).to receive(:extension_white_list).and_return([])
212
150
  end
213
151
 
214
152
  it_should_behave_like "a globally allowed file extension"
@@ -217,11 +155,11 @@ describe CarrierWaveDirect::Uploader do
217
155
  context "where #extension_white_list returns ['exe', 'bmp']" do
218
156
 
219
157
  before do
220
- subject.stub(:extension_white_list).and_return(%w{exe bmp})
158
+ allow(subject).to receive(:extension_white_list).and_return(%w{exe bmp})
221
159
  end
222
160
 
223
161
  it "should return '(exe|bmp)'" do
224
- subject.extension_regexp.should == "(exe|bmp)"
162
+ expect(subject.extension_regexp).to eq "(exe|bmp)"
225
163
  end
226
164
  end
227
165
  end
@@ -230,7 +168,7 @@ describe CarrierWaveDirect::Uploader do
230
168
  context "a key has not been set" do
231
169
 
232
170
  it "should return false" do
233
- subject.should_not have_key
171
+ expect(subject).to_not have_key
234
172
  end
235
173
  end
236
174
 
@@ -238,7 +176,7 @@ describe CarrierWaveDirect::Uploader do
238
176
  before { subject.key }
239
177
 
240
178
  it "should return false" do
241
- subject.should_not have_key
179
+ expect(subject).to_not have_key
242
180
  end
243
181
  end
244
182
 
@@ -246,47 +184,14 @@ describe CarrierWaveDirect::Uploader do
246
184
  before { subject.key = sample_key }
247
185
 
248
186
  it "should return true" do
249
- subject.should have_key
250
- end
251
- end
252
- end
253
-
254
- describe "#direct_fog_url" do
255
- it "should return the result from CarrierWave::Storage::Fog::File#public_url" do
256
- subject.direct_fog_url.should == CarrierWave::Storage::Fog::File.new(
257
- subject, nil, nil
258
- ).public_url
259
- end
260
-
261
- context ":with_path => true" do
262
-
263
- context "#key is set to '#{sample(:path_with_special_chars)}'" do
264
- before { subject.key = sample(:path_with_special_chars) }
265
-
266
- it "should return the full url with '/#{URI.escape(sample(:path_with_special_chars))}' as the path" do
267
- direct_fog_url = CarrierWave::Storage::Fog::File.new(
268
- subject, nil, nil
269
- ).public_url
270
- subject.direct_fog_url(:with_path => true).should == direct_fog_url + "#{URI.escape(sample(:path_with_special_chars))}"
271
- end
272
- end
273
-
274
- context "#key is set to '#{sample(:path)}'" do
275
- before { subject.key = sample(:path) }
276
-
277
- it "should return the full url with '/#{sample(:path)}' as the path" do
278
- direct_fog_url = CarrierWave::Storage::Fog::File.new(
279
- subject, nil, nil
280
- ).public_url
281
- subject.direct_fog_url(:with_path => true).should == direct_fog_url + "#{sample(:path)}"
282
- end
187
+ expect(subject).to have_key
283
188
  end
284
189
  end
285
190
  end
286
191
 
287
192
  describe "#persisted?" do
288
193
  it "should return false" do
289
- subject.should_not be_persisted
194
+ expect(subject).to_not be_persisted
290
195
  end
291
196
  end
292
197
 
@@ -295,7 +200,7 @@ describe CarrierWaveDirect::Uploader do
295
200
  before { mounted_subject.key = sample(:s3_key) }
296
201
 
297
202
  it "should return '#{sample(:stored_filename)}'" do
298
- mounted_subject.filename.should == sample(:stored_filename)
203
+ expect(mounted_subject.filename).to eq sample(:stored_filename)
299
204
  end
300
205
  end
301
206
 
@@ -303,7 +208,7 @@ describe CarrierWaveDirect::Uploader do
303
208
  before { subject.key = sample(:key) }
304
209
 
305
210
  it "should return '#{sample(:key)}'" do
306
- subject.filename.should == sample(:key)
211
+ expect(subject.filename).to eq sample(:key)
307
212
  end
308
213
  end
309
214
 
@@ -311,51 +216,76 @@ describe CarrierWaveDirect::Uploader do
311
216
  context "but the model's remote #{sample(:mounted_as)} url is: '#{sample(:file_url)}'" do
312
217
 
313
218
  before do
314
- mounted_subject.model.stub(
219
+ allow(mounted_subject.model).to receive(
315
220
  "remote_#{mounted_subject.mounted_as}_url"
316
221
  ).and_return(sample(:file_url))
317
222
  end
318
223
 
319
224
  it "should set the key to contain '#{File.basename(sample(:file_url))}'" do
320
225
  mounted_subject.filename
321
- mounted_subject.key.should =~ /#{Regexp.escape(File.basename(sample(:file_url)))}$/
226
+ expect(mounted_subject.key).to match /#{Regexp.escape(File.basename(sample(:file_url)))}$/
322
227
  end
323
228
 
324
229
  it "should return a filename based off the key and remote url" do
325
230
  filename = mounted_subject.filename
326
- mounted_subject.key.should =~ /#{Regexp.escape(filename)}$/
231
+ expect(mounted_subject.key).to match /#{Regexp.escape(filename)}$/
327
232
  end
328
233
 
329
234
  # this ensures that the version subject keys are updated
330
235
  # see spec for key= for more details
331
236
  it "should set the key explicitly" do
332
- mounted_subject.should_receive(:key=)
237
+ expect(mounted_subject).to receive(:key=)
333
238
  mounted_subject.filename
334
239
  end
335
240
  end
336
241
 
337
242
  context "and the model's remote #{sample(:mounted_as)} url has whitespace in it" do
338
243
  before do
339
- mounted_model.stub(
244
+ allow(mounted_model).to receive(
340
245
  "remote_#{mounted_subject.mounted_as}_url"
341
246
  ).and_return("http://anyurl.com/any_path/video_dir/filename 2.avi")
342
247
  end
343
248
 
344
249
  it "should be sanitized (whitespace replaced with _)" do
345
250
  mounted_subject.filename
346
- mounted_subject.key.should =~ /filename_2.avi$/
251
+ expect(mounted_subject.key).to match /filename_2.avi$/
347
252
  end
348
253
  end
349
254
 
255
+ context "and the model's remote url contains escape characters" do
256
+ before do
257
+ subject.key = nil
258
+ allow(subject).to receive(:present?).and_return(:true)
259
+ allow(subject).to receive(:url).and_return("http://anyurl.com/any_path/video_dir/filename ()+[]2.avi")
260
+ end
261
+
262
+ it "should be escaped and replaced with non whitespace characters" do
263
+ expect(subject.key).to match /filename%20%28%29%2B%5B%5D2.avi/
264
+ end
265
+ end
266
+
267
+ context "and the model's remote url contains already escaped characters" do
268
+ before do
269
+ subject.key = nil
270
+ allow(subject).to receive(:present?).and_return(:true)
271
+ allow(subject).to receive(:url).and_return("http://anyurl.com/any_path/video_dir/filename%20%28%29%2B%5B%5D2.avi")
272
+ end
273
+
274
+ it "should not double escape already escaped characters" do
275
+ expect(subject.key).to match /filename%20%28%29%2B%5B%5D2.avi/
276
+ end
277
+
278
+ end
279
+
350
280
  context "and the model's remote #{sample(:mounted_as)} url is blank" do
351
281
  before do
352
- mounted_model.stub(
282
+ allow(mounted_model).to receive(
353
283
  "remote_#{mounted_subject.mounted_as}_url"
354
284
  ).and_return nil
355
285
  end
356
286
 
357
287
  it "should return nil" do
358
- mounted_subject.filename.should be_nil
288
+ expect(mounted_subject.filename).to be_nil
359
289
  end
360
290
  end
361
291
  end
@@ -363,7 +293,7 @@ describe CarrierWaveDirect::Uploader do
363
293
 
364
294
  describe "#acl" do
365
295
  it "should return the correct s3 access policy" do
366
- subject.acl.should == (subject.fog_public ? 'public-read' : 'private')
296
+ expect(subject.acl).to eq (subject.fog_public ? 'public-read' : 'private')
367
297
  end
368
298
  end
369
299
 
@@ -375,11 +305,21 @@ describe CarrierWaveDirect::Uploader do
375
305
  end
376
306
 
377
307
  it "should return Base64-encoded JSON" do
378
- decoded_policy.should be_a(Hash)
308
+ expect(decoded_policy).to be_a(Hash)
379
309
  end
380
310
 
381
311
  it "should not contain any new lines" do
382
- subject.policy.should_not include("\n")
312
+ expect(subject.policy).to_not include("\n")
313
+ end
314
+
315
+ it "should be cached" do
316
+ Timecop.freeze(Time.now) do
317
+ @policy_now = subject.policy
318
+ end
319
+ Timecop.freeze(1.second.from_now) do
320
+ @policy_later = subject.policy
321
+ end
322
+ expect(@policy_later).to eql @policy_now
383
323
  end
384
324
 
385
325
  context "expiration" do
@@ -399,17 +339,17 @@ describe CarrierWaveDirect::Uploader do
399
339
 
400
340
  it "should be #{DirectUploader.upload_expiration / 3600} hours from now" do
401
341
  Timecop.freeze(Time.now) do
402
- Time.parse(expiration).should have_expiration
342
+ expect(Time.parse(expiration)).to have_expiration
403
343
  end
404
344
  end
405
345
 
406
346
  it "should be encoded as a utc time" do
407
- Time.parse(expiration).should be_utc
347
+ expect(Time.parse(expiration)).to be_utc
408
348
  end
409
349
 
410
350
  it "should be #{sample(:expiration) / 60 } minutes from now when passing {:expiration => #{sample(:expiration)}}" do
411
351
  Timecop.freeze(Time.now) do
412
- Time.parse(expiration(:expiration => sample(:expiration))).should have_expiration(sample(:expiration))
352
+ expect(Time.parse(expiration(:expiration => sample(:expiration)))).to have_expiration(sample(:expiration))
413
353
  end
414
354
  end
415
355
  end
@@ -426,38 +366,44 @@ describe CarrierWaveDirect::Uploader do
426
366
  context "should include" do
427
367
  # Rails form builder conditions
428
368
  it "'utf8'" do
429
- conditions.should have_condition(:utf8)
369
+ expect(conditions).to have_condition(:utf8)
430
370
  end
431
371
 
432
372
  # S3 conditions
433
373
  it "'key'" do
434
- mounted_subject.stub(:key).and_return(sample(:s3_key))
435
- conditions(
374
+ allow(mounted_subject).to receive(:key).and_return(sample(:s3_key))
375
+ expect(conditions(
436
376
  :subject => mounted_subject
437
- ).should have_condition(:key, sample(:s3_key))
377
+ )).to have_condition(:key, sample(:s3_key))
438
378
  end
439
379
 
440
380
  it "'key' without FILENAME_WILDCARD" do
441
- conditions(
381
+ expect(conditions(
442
382
  :subject => mounted_subject
443
- ).should have_condition(:key, mounted_subject.key.sub("${filename}", ""))
383
+ )).to have_condition(:key, mounted_subject.key.sub("${filename}", ""))
444
384
  end
445
385
 
446
386
  it "'bucket'" do
447
- conditions.should have_condition("bucket" => subject.fog_directory)
387
+ expect(conditions).to have_condition("bucket" => subject.fog_directory)
448
388
  end
449
389
 
450
390
  it "'acl'" do
451
- conditions.should have_condition("acl" => subject.acl)
391
+ expect(conditions).to have_condition("acl" => subject.acl)
452
392
  end
453
393
 
454
394
  it "'success_action_redirect'" do
455
395
  subject.success_action_redirect = "http://example.com/some_url"
456
- conditions.should have_condition("success_action_redirect" => "http://example.com/some_url")
396
+ expect(conditions).to have_condition("success_action_redirect" => "http://example.com/some_url")
457
397
  end
458
398
 
459
- it "'content-type' only if enabled" do
460
- conditions.should have_condition('Content-Type') if subject.class.will_include_content_type
399
+ it "does not have 'content-type' when will_include_content_type is false" do
400
+ allow(subject.class).to receive(:will_include_content_type).and_return(false)
401
+ expect(conditions).to_not have_condition('Content-Type')
402
+ end
403
+
404
+ it "has 'content-type' when will_include_content_type is true" do
405
+ allow(subject.class).to receive(:will_include_content_type).and_return(true)
406
+ expect(conditions).to have_condition('Content-Type')
461
407
  end
462
408
 
463
409
  context 'when use_action_status is true' do
@@ -471,12 +417,12 @@ describe CarrierWaveDirect::Uploader do
471
417
 
472
418
  it "'success_action_status'" do
473
419
  subject.success_action_status = '200'
474
- conditions.should have_condition("success_action_status" => "200")
420
+ expect(conditions).to have_condition("success_action_status" => "200")
475
421
  end
476
422
 
477
423
  it "does not have 'success_action_redirect'" do
478
424
  subject.success_action_redirect = "http://example.com/some_url"
479
- conditions.should_not have_condition("success_action_redirect" => "http://example.com/some_url")
425
+ expect(conditions).to_not have_condition("success_action_redirect" => "http://example.com/some_url")
480
426
  end
481
427
  end
482
428
 
@@ -490,53 +436,68 @@ describe CarrierWaveDirect::Uploader do
490
436
  end
491
437
 
492
438
  it "#{DirectUploader.min_file_size} bytes" do
493
- conditions.should have_content_length_range
439
+ expect(conditions).to have_content_length_range
494
440
  end
495
441
 
496
442
  it "#{DirectUploader.max_file_size} bytes" do
497
- conditions.should have_content_length_range
443
+ expect(conditions).to have_content_length_range
498
444
  end
499
445
 
500
446
  it "#{sample(:min_file_size)} bytes when passing {:min_file_size => #{sample(:min_file_size)}}" do
501
- conditions(
447
+ expect(conditions(
502
448
  :min_file_size => sample(:min_file_size)
503
- ).should have_content_length_range(:min_file_size => sample(:min_file_size))
449
+ )).to have_content_length_range(:min_file_size => sample(:min_file_size))
504
450
  end
505
451
 
506
452
  it "#{sample(:max_file_size)} bytes when passing {:max_file_size => #{sample(:max_file_size)}}" do
507
- conditions(
453
+ expect(conditions(
508
454
  :max_file_size => sample(:max_file_size)
509
- ).should have_content_length_range(:max_file_size => sample(:max_file_size))
455
+ )).to have_content_length_range(:max_file_size => sample(:max_file_size))
510
456
  end
511
457
  end
512
458
  end
513
459
  end
514
460
  end
515
461
 
462
+ describe "clear_policy!" do
463
+ it "should reset the cached policy string" do
464
+ Timecop.freeze(Time.now) do
465
+ @policy_now = subject.policy
466
+ end
467
+ subject.clear_policy!
468
+
469
+ Timecop.freeze(1.second.from_now) do
470
+ @policy_after_reset = subject.policy
471
+ end
472
+ expect(@policy_after_reset).not_to eql @policy_now
473
+ end
474
+ end
475
+
516
476
  describe "#signature" do
517
477
  it "should not contain any new lines" do
518
- subject.signature.should_not include("\n")
478
+ expect(subject.signature).to_not include("\n")
519
479
  end
520
480
 
521
481
  it "should return a base64 encoded 'sha1' hash of the secret key and policy document" do
522
- Base64.decode64(subject.signature).should == OpenSSL::HMAC.digest(
523
- OpenSSL::Digest::Digest.new('sha1'),
482
+ expect(Base64.decode64(subject.signature)).to eq OpenSSL::HMAC.digest(
483
+ OpenSSL::Digest.new('sha1'),
524
484
  subject.aws_secret_access_key, subject.policy
525
485
  )
526
486
  end
527
487
  end
528
488
 
489
+
529
490
  # note that 'video' is hardcoded into the MountedClass support file
530
491
  # so changing the sample will cause the tests to fail
531
492
  context "a class has a '#{sample(:mounted_as)}' mounted" do
532
493
  describe "#{sample(:mounted_as).to_s.capitalize}Uploader" do
533
494
  describe "##{sample(:mounted_as)}" do
534
495
  it "should be defined" do
535
- direct_subject.should be_respond_to(sample(:mounted_as))
496
+ expect(direct_subject).to be_respond_to(sample(:mounted_as))
536
497
  end
537
498
 
538
499
  it "should return itself" do
539
- direct_subject.send(sample(:mounted_as)).should == direct_subject
500
+ expect(direct_subject.send(sample(:mounted_as))).to eq direct_subject
540
501
  end
541
502
  end
542
503
 
@@ -556,11 +517,11 @@ describe CarrierWaveDirect::Uploader do
556
517
  let(:store_path) { video_subject.send(sample(:version)).store_path }
557
518
 
558
519
  it "should be like '#{sample(:stored_version_filename)}'" do
559
- store_path.should =~ /#{sample(:stored_version_filename)}$/
520
+ expect(store_path).to match /#{sample(:stored_version_filename)}$/
560
521
  end
561
522
 
562
523
  it "should not be like '#{sample(:version)}_#{sample(:stored_filename_base)}'" do
563
- store_path.should_not =~ /#{sample(:version)}_#{sample(:stored_filename_base)}/
524
+ expect(store_path).to_not match /#{sample(:version)}_#{sample(:stored_filename_base)}/
564
525
  end
565
526
  end
566
527
  end