test/storage_test.rb in tristandunn-paperclip-2.3.1 vs test/storage_test.rb in tristandunn-paperclip-2.3.1.1
- old
+ new
@@ -1,8 +1,15 @@
require 'test/helper'
+require 'aws/s3'
class StorageTest < Test::Unit::TestCase
+ def rails_env(env)
+ silence_warnings do
+ Object.const_set(:RAILS_ENV, env)
+ end
+ end
+
context "Parsing S3 credentials" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:bucket => "testing",
@@ -13,29 +20,29 @@
@current_env = RAILS_ENV
end
teardown do
- Object.const_set("RAILS_ENV", @current_env)
+ rails_env(@current_env)
end
should "get the correct credentials when RAILS_ENV is production" do
- Object.const_set('RAILS_ENV', "production")
+ rails_env("production")
assert_equal({:key => "12345"},
@avatar.parse_credentials('production' => {:key => '12345'},
:development => {:key => "54321"}))
end
should "get the correct credentials when RAILS_ENV is development" do
- Object.const_set('RAILS_ENV', "development")
+ rails_env("development")
assert_equal({:key => "54321"},
@avatar.parse_credentials('production' => {:key => '12345'},
:development => {:key => "54321"}))
end
should "return the argument if the key does not exist" do
- Object.const_set('RAILS_ENV', "not really an env")
+ rails_env("not really an env")
assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
end
end
context "" do
@@ -87,10 +94,37 @@
should "return a url based on the host_alias" do
assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
end
end
+
+ context "Generating a url with an expiration" do
+ setup do
+ AWS::S3::Base.stubs(:establish_connection!)
+ rebuild_model :storage => :s3,
+ :s3_credentials => {
+ :production => { :bucket => "prod_bucket" },
+ :development => { :bucket => "dev_bucket" }
+ },
+ :s3_host_alias => "something.something.com",
+ :path => ":attachment/:basename.:extension",
+ :url => ":s3_alias_url"
+
+ rails_env("production")
+
+ @dummy = Dummy.new
+ @dummy.avatar = StringIO.new(".")
+
+ AWS::S3::S3Object.expects(:url_for).with("avatars/stringio.txt", "prod_bucket", { :expires_in => 3600 })
+
+ @dummy.avatar.expiring_url
+ end
+
+ should "should succeed" do
+ assert true
+ end
+ end
context "Parsing S3 credentials with a bucket in them" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
@@ -100,19 +134,19 @@
}
@dummy = Dummy.new
@old_env = RAILS_ENV
end
- teardown{ Object.const_set("RAILS_ENV", @old_env) }
+ teardown{ rails_env(@old_env) }
should "get the right bucket in production" do
- Object.const_set("RAILS_ENV", "production")
+ rails_env("production")
assert_equal "prod_bucket", @dummy.avatar.bucket_name
end
should "get the right bucket in development" do
- Object.const_set("RAILS_ENV", "development")
+ rails_env("development")
assert_equal "dev_bucket", @dummy.avatar.bucket_name
end
end
context "An attachment with S3 storage" do
@@ -224,9 +258,32 @@
should "succeed" do
assert true
end
end
+ end
+ end
+
+ context "with S3 credentials in a YAML file" do
+ setup do
+ ENV['S3_KEY'] = 'env_key'
+ ENV['S3_BUCKET'] = 'env_bucket'
+ ENV['S3_SECRET'] = 'env_secret'
+
+ rails_env('test')
+
+ rebuild_model :storage => :s3,
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "fixtures/s3.yml"))
+
+ Dummy.delete_all
+
+ @dummy = Dummy.new
+ end
+
+ should "run it the file through ERB" do
+ assert_equal 'env_bucket', @dummy.avatar.bucket_name
+ assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
+ assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
end
end
unless ENV["S3_TEST_BUCKET"].blank?
context "Using S3 for real, an attachment with S3 storage" do