Commit 98eff60d by Jon Yurek

AWS::S3 Passes integration tests that actually hit S3.

parent b3f26a69
......@@ -133,11 +133,15 @@ module Paperclip
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || 'public-read'
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
@s3_permissions = @options[:s3_permissions] || :public_read
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
@url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/)
AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
))
end
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
......@@ -150,13 +154,6 @@ module Paperclip
end
end
def s3
AWS::S3::S3Object.establish_connection( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
))
end
def bucket_name
@bucket
end
......@@ -171,7 +168,11 @@ module Paperclip
end
def exists?(style = default_style)
AWS::S3::S3Object.exists?(path(style), bucket_name)
if original_filename
AWS::S3::S3Object.exists?(path(style), bucket_name)
else
false
end
end
def s3_protocol
......@@ -195,7 +196,9 @@ module Paperclip
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => instance_read(:content_type)}.merge(@s3_headers))
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
}.merge(@s3_headers))
rescue AWS::S3::ResponseError => e
raise
end
......
......@@ -379,6 +379,11 @@ class IntegrationTest < Test::Unit::TestCase
@files_on_s3 = s3_files_for @dummy.avatar
end
should "have the same contents as the original" do
@file.rewind
assert_equal @file.read, @files_on_s3[:original].read
end
should "write and delete its files" do
[["434x66", :original],
["300x46", :large],
......@@ -403,10 +408,8 @@ class IntegrationTest < Test::Unit::TestCase
assert @dummy.valid?
assert @dummy.save
saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
saved_keys.each do |key|
assert key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert @dummy.avatar.exists?(style)
end
@dummy.avatar.clear
......@@ -414,8 +417,8 @@ class IntegrationTest < Test::Unit::TestCase
assert @dummy.valid?
assert @dummy.save
saved_keys.each do |key|
assert ! key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert ! @dummy.avatar.exists?(style)
end
@d2 = Dummy.find(@dummy.id)
......@@ -427,7 +430,7 @@ class IntegrationTest < Test::Unit::TestCase
assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
[:thumb, :medium, :large, :original].each do |style|
assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
assert_equal @dummy.avatar.to_file(style).read, @d2.avatar.to_file(style).read
end
saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
......@@ -435,8 +438,8 @@ class IntegrationTest < Test::Unit::TestCase
@d2.avatar.clear
assert @d2.save
saved_keys.each do |key|
assert ! key.exists?
[:thumb, :medium, :large, :original].each do |style|
assert ! @dummy.avatar.exists?(style)
end
end
......@@ -444,7 +447,7 @@ class IntegrationTest < Test::Unit::TestCase
expected = @dummy.avatar.to_file
@dummy.avatar = "not a file"
assert @dummy.valid?
assert_equal expected.full_name, @dummy.avatar.to_file.full_name
assert_equal expected.read, @dummy.avatar.to_file.read
@dummy.avatar = @bad_file
assert ! @dummy.valid?
......@@ -472,7 +475,6 @@ class IntegrationTest < Test::Unit::TestCase
should "have the right content type" do
headers = s3_headers_for(@dummy.avatar, :original)
p headers
assert_equal 'image/png', headers['content-type']
end
end
......
......@@ -3,6 +3,7 @@ require 'test/helper'
class StorageTest < Test::Unit::TestCase
context "Parsing S3 credentials" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:bucket => "testing",
:s3_credentials => {:not => :important}
......@@ -39,6 +40,7 @@ class StorageTest < Test::Unit::TestCase
context "" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {},
:bucket => "bucket",
......@@ -54,6 +56,7 @@ class StorageTest < Test::Unit::TestCase
end
context "" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {},
:bucket => "bucket",
......@@ -69,6 +72,7 @@ class StorageTest < Test::Unit::TestCase
end
context "" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {
:production => { :bucket => "prod_bucket" },
......@@ -88,6 +92,7 @@ class StorageTest < Test::Unit::TestCase
context "Parsing S3 credentials with a bucket in them" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:s3_credentials => {
:production => { :bucket => "prod_bucket" },
......@@ -146,7 +151,7 @@ class StorageTest < Test::Unit::TestCase
context "and saved" do
setup do
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png')
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
@dummy.save
end
......@@ -171,6 +176,7 @@ class StorageTest < Test::Unit::TestCase
context "An attachment with S3 storage and bucket defined as a Proc" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
:s3_credentials => {:not => :important}
......@@ -184,6 +190,7 @@ class StorageTest < Test::Unit::TestCase
context "An attachment with S3 storage and specific s3 headers set" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
rebuild_model :storage => :s3,
:bucket => "testing",
:path => ":attachment/:style/:basename.:extension",
......@@ -205,10 +212,12 @@ class StorageTest < Test::Unit::TestCase
context "and saved" do
setup do
AWS::S3::Base.stubs(:establish_connection!)
AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path,
anything,
'testing',
:content_type => 'image/png',
:access => :public_read,
'Cache-Control' => 'max-age=31557600')
@dummy.save
end
......@@ -245,8 +254,8 @@ class StorageTest < Test::Unit::TestCase
teardown { @file.close }
should "still return a Tempfile when sent #to_io" do
assert_equal Tempfile, @dummy.avatar.to_io.class
should "still return a Tempfile when sent #to_file" do
assert_equal Tempfile, @dummy.avatar.to_file.class
end
context "and saved" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment