Skip to content

Commit

Permalink
AWS::S3 Passes integration tests that actually hit S3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Aug 4, 2009
1 parent b3f26a6 commit 98eff60
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
25 changes: 14 additions & 11 deletions lib/paperclip/storage.rb
Expand Up @@ -133,11 +133,15 @@ def self.extended base
@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{^/}, "")}"
Expand All @@ -150,13 +154,6 @@ def self.extended base
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
Expand All @@ -171,7 +168,11 @@ def parse_credentials creds
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
Expand All @@ -195,7 +196,9 @@ def flush_writes #:nodoc:
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
Expand Down
24 changes: 13 additions & 11 deletions test/integration_test.rb
Expand Up @@ -379,6 +379,11 @@ def s3_headers_for attachment, style
@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],
Expand All @@ -403,19 +408,17 @@ def s3_headers_for attachment, style
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
assert_nil @dummy.avatar_file_name
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)
Expand All @@ -427,24 +430,24 @@ def s3_headers_for attachment, style

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) }

@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

should "know the difference between good files, bad files, not files, and nil" do
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?
Expand Down Expand Up @@ -472,7 +475,6 @@ def s3_headers_for attachment, style

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
Expand Down
15 changes: 12 additions & 3 deletions test/storage_test.rb
Expand Up @@ -3,6 +3,7 @@
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}
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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" },
Expand All @@ -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" },
Expand Down Expand Up @@ -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

Expand All @@ -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}
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98eff60

Please sign in to comment.