test/aws_storage_test.rb in paperclip-aws-1.4.1 vs test/aws_storage_test.rb in paperclip-aws-1.6.0
- old
+ new
@@ -6,56 +6,62 @@
Object.const_set(:Rails, stub('Rails', :env => env))
end
end
def default_model_options(options={})
- {
+ options.reverse_merge!({
:storage => :aws,
:bucket => "testing",
:s3_credentials => {
:access_key_id => "ACCESS_KEY_ID",
:secret_access_key => "SECRET_ACCESS_KEY"
},
:path => ":attachment/:basename.:extension"
- }.deep_merge!(options)
+ })
end
- context "Parsing S3 credentials" do
- setup do
- rebuild_model default_model_options
-
- @dummy = Dummy.new
- @avatar = @dummy.avatar
- end
-
+ context "Parsing S3 credentials" do
should "get the correct credentials when RAILS_ENV is production" do
rails_env("production")
- assert_equal(
- { :access_key_id => "12345" },
- @avatar.parse_credentials(
+ rebuild_model default_model_options(
+ :s3_credentials => {
:production => { :access_key_id => '12345' },
- :development => { :access_key_id => '54321' }
- )
+ :development => { :access_key_id => '54321' }
+ }
)
+
+ @dummy = Dummy.new
+ assert_equal({ :access_key_id => "12345" }, @dummy.avatar.s3_credentials)
end
should "get the correct credentials when RAILS_ENV is development" do
rails_env("development")
- assert_equal(
- { :access_key_id => "54321" },
- @avatar.parse_credentials(
+ rebuild_model default_model_options(
+ :s3_credentials => {
:production => { :access_key_id => '12345' },
- :development => { :access_key_id => '54321' }
- )
- )
+ :development => { :access_key_id => '54321' }
+ }
+ )
+
+ @dummy = Dummy.new
+ assert_equal({ :access_key_id => "54321" }, @dummy.avatar.s3_credentials)
end
should "return the argument if the key does not exist" do
rails_env("not really an env")
- assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
+
+ rebuild_model default_model_options(
+ :s3_credentials => {
+ :test => "12345"
+ }
+ )
+
+ @dummy = Dummy.new
+
+ assert_equal({:test => "12345"}, @dummy.avatar.s3_credentials)
end
end
context "Working with endpoints" do
@@ -191,11 +197,14 @@
end
end
context "An attachment that uses S3 for storage and has spaces in file name" do
setup do
- rebuild_model default_model_options(:styles => { :large => ['500x500#', :jpg] })
+ rebuild_model default_model_options(
+ :styles => { :large => ['500x500#', :jpg] },
+ :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%#]/
+ )
@dummy = Dummy.new
@dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb')
end
@@ -219,6 +228,95 @@
should "not be extended by the Filesystem module" do
assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
end
end
-end
\ No newline at end of file
+
+ context "S3 options" do
+ setup do
+ @writer = mock()
+ AWS::S3.expects(:new).returns(
+ stub(:buckets =>
+ stub(:[] =>
+ stub(:objects =>
+ stub(:[] => @writer)
+ )
+ )
+ )
+ )
+ end
+
+ should "be set to default" do
+ rebuild_model default_model_options
+
+ @writer.expects(:write).with do |value|
+ value[:sse] == nil &&
+ value[:server_side_encryption] == false &&
+ value[:storage_class] == :standard &&
+ value[:content_disposition] == nil &&
+ value[:expires] == nil
+ end
+
+ @dummy = Dummy.new
+ @dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb')
+ @dummy.save
+ end
+
+ should "be configured" do
+ rebuild_model default_model_options(:s3_options => {
+ :sse => 'AES256',
+ :storage_class => :reduced_redundancy,
+ :content_disposition => :attachment,
+ :cache_control => 'max-age=86400'
+ })
+
+ @writer.expects(:write).with do |value|
+ value[:sse] == nil &&
+ value[:server_side_encryption] == 'AES256' &&
+ value[:storage_class] == :reduced_redundancy &&
+ value[:content_disposition] == :attachment &&
+ value[:cache_control] == 'max-age=86400'
+ end
+
+ @dummy = Dummy.new
+ @dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb')
+ @dummy.save
+ end
+
+ should "work with string-keyed hash" do
+ rebuild_model default_model_options(:s3_options => {
+ 'cache_control' => 'max-age=86400'
+ })
+
+ @writer.expects(:write).with do |value|
+ value['cache_control'] == nil &&
+ value[:cache_control] == 'max-age=86400'
+ end
+
+ @dummy = Dummy.new
+ @dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb')
+ @dummy.save
+ end
+
+ should "allow to modify options in instance" do
+ rebuild_model default_model_options
+
+ @writer.expects(:write).with do |value|
+ value[:sse] == nil &&
+ value[:server_side_encryption] == false &&
+ value[:storage_class] == :standard &&
+ value[:content_disposition] == "attachment; filename=spaced_file.png" &&
+ value[:expires] == nil
+ end
+
+ Dummy.class_eval do
+ before_save do
+ self.avatar.s3_options[:content_disposition] = "attachment; filename=#{self.avatar_file_name}"
+ end
+ end
+
+ @dummy = Dummy.new
+ @dummy.avatar = File.new(fixture_file('spaced file.png'), 'rb')
+ @dummy.save
+ end
+ end
+end