Skip to content
Browse files

Use FakeWeb to test the S3 asset uploading instead of directly hittin…

…g S3

This will make our test less fragile to network connection issues.
  • Loading branch information...
1 parent b18e8fd commit 9ce90bf7b1f91c6f426ab9aaf5ca3ca99b575636 @sikachu sikachu committed Sep 7, 2011
View
1 Gemfile
@@ -16,6 +16,7 @@ gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
+gem "fakeweb", :require => false
# gem "ruby-debug", :platform => :ruby_18
# gem "ruby-debug19", :platform => :ruby_19
View
13 features/basic_integration.feature
@@ -32,16 +32,15 @@ Feature: Rails integration
"""
And I write to "config/s3.yml" with:
"""
- bucket: <%= ENV['PAPERCLIP_TEST_BUCKET'] || 'paperclip' %>
- access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
- secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
+ bucket: paperclip
+ access_key_id: access_key
+ secret_access_key: secret_key
"""
And I start the rails application
- And I validate my S3 credentials
When I go to the new user page
And I fill in "Name" with "something"
- And I attach the file "test/fixtures/5k.png" to "Attachment"
+ And I attach the file "test/fixtures/5k.png" to "Attachment" on S3
And I press "Submit"
Then I should see "Name: something"
- And I should see an image with a path of "http://s3.amazonaws.com/%{BUCKET_NAME}/attachments/1/original/5k.png"
- And the file at "http://s3.amazonaws.com/%{BUCKET_NAME}/attachments/1/original/5k.png" is the same as "test/fixtures/5k.png"
+ And I should see an image with a path of "http://s3.amazonaws.com/paperclip/attachments/1/original/5k.png"
+ And the file at "http://s3.amazonaws.com/paperclip/attachments/1/original/5k.png" should be uploaded to S3
View
2 features/step_definitions/html_steps.rb
@@ -1,12 +1,10 @@
Then %r{I should see an image with a path of "([^"]*)"} do |path|
- path = path.gsub(/%\{BUCKET_NAME\}/, ENV['PAPERCLIP_TEST_BUCKET'] || 'paperclip')
page.should have_css("img[src^='#{path}']")
end
Then %r{^the file at "([^"]*)" is the same as "([^"]*)"$} do |web_file, path|
expected = IO.read(path)
actual = if web_file.match %r{^https?://}
- web_file = web_file.gsub(/%\{BUCKET_NAME\}/, ENV['PAPERCLIP_TEST_BUCKET'] || 'paperclip')
Net::HTTP.get(URI.parse(web_file))
else
visit(web_file)
View
21 features/step_definitions/s3_steps.rb
@@ -1,11 +1,14 @@
-Given /I validate my S3 credentials/ do
- key = ENV['AWS_ACCESS_KEY_ID']
- secret = ENV['AWS_SECRET_ACCESS_KEY']
- bucket = ENV['PAPERCLIP_TEST_BUCKET'] || 'paperclip'
-
- key.should_not be_nil
- secret.should_not be_nil
- bucket.should_not be_nil
+When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
+ definition = User.attachment_definitions[field.downcase.to_sym]
+ path = "http://s3.amazonaws.com/paperclip#{definition[:path]}"
+ path.gsub!(':filename', File.basename(file_path))
+ path.gsub!(/:([^\/\.]+)/) do |match|
+ "([^\/\.]+)"
+ end
+ FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
+ When "I attach the file \"#{file_path}\" to \"#{field}\""
+end
- assert_credentials(key, secret, bucket)
+Then /^the file at "([^"]*)" should be uploaded to S3$/ do |url|
+ FakeWeb.registered_uri?(:put, url)
end
View
8 features/support/env.rb
@@ -6,11 +6,3 @@
Before do
@aruba_timeout_seconds = 120
end
-
-if ENV['TRAVIS']
- puts "Using Travis CI configuration"
- puts "Current time is #{`date`} : #{Time.now}"
- ENV["AWS_ACCESS_KEY_ID"] = "AKIAI2Q4BG4ALE7GOTIA"
- ENV["AWS_SECRET_ACCESS_KEY"] = "2LHlRAravE7xQD6ypn9Snv2uc16gvCwjA1qlMxSL"
- ENV["PAPERCLIP_TEST_BUCKET"] = "paperclip-travis"
-end
View
3 features/support/fakeweb.rb
@@ -0,0 +1,3 @@
+require 'fake_web'
+
+FakeWeb.allow_net_connect = false
View
25 features/support/s3.rb
@@ -1,25 +0,0 @@
-module AWSS3Methods
- def load_s3
- begin
- require 'aws/s3'
- rescue LoadError => e
- fail "You do not have aws-s3 installed."
- end
- end
-
- def assert_credentials(key, secret, bucket)
- load_s3
- begin
- AWS::S3::Base.establish_connection!(
- :access_key_id => key,
- :secret_access_key => secret
- )
- AWS::S3::Bucket.objects(bucket, :max_keys => 0)
- rescue AWS::S3::ResponseError => e
- fail "Could not connect using AWS credentials in AWS_ACCESS_KEY_ID=#{key} and AWS_SECRET_ACCESS_KEY=#{secret} to the bucket '#{bucket}'. " +
- "Please make sure these are set in your environment." + e.message
- end
- end
-end
-
-World(AWSS3Methods)
View
1 gemfiles/rails2.gemfile
@@ -18,6 +18,7 @@ gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
+gem "fakeweb", :require=>false
gem "rails", "~> 2.3.14"
gem "paperclip", :path=>"../"
View
1 gemfiles/rails3.gemfile
@@ -18,6 +18,7 @@ gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
+gem "fakeweb", :require=>false
gem "rails", "~> 3.0.10"
gem "paperclip", :path=>"../"
View
1 gemfiles/rails3_1.gemfile
@@ -18,6 +18,7 @@ gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
+gem "fakeweb", :require=>false
gem "rails", "~> 3.1.0"
gem "paperclip", :path=>"../"

0 comments on commit 9ce90bf

Please sign in to comment.
Something went wrong with that request. Please try again.