diff --git a/cucumber/paperclip_steps.rb b/cucumber/paperclip_steps.rb new file mode 100644 index 000000000..fac0c129f --- /dev/null +++ b/cucumber/paperclip_steps.rb @@ -0,0 +1,6 @@ +When /^I attach an? "([^\"]*)" "([^\"]*)" file to an? "([^\"]*)" on S3$/ do |attachment, extension, model| + stub_paperclip_s3(model, attachment, extension) + attach_file attachment, + "features/support/paperclip/#{model.gsub(" ", "_").underscore}/#{attachment}.#{extension}" +end + diff --git a/shoulda_macros/paperclip.rb b/shoulda_macros/paperclip.rb index d69035781..178119189 100644 --- a/shoulda_macros/paperclip.rb +++ b/shoulda_macros/paperclip.rb @@ -60,9 +60,57 @@ def should_validate_attachment_size name, options = {} assert_accepts(matcher, klass) end end + + # Stubs the HTTP PUT for an attachment using S3 storage. + # + # @example + # stub_paperclip_s3('user', 'avatar', 'png') + def stub_paperclip_s3(model, attachment, extension) + definition = model.gsub(" ", "_").classify.constantize. + attachment_definitions[attachment.to_sym] + + path = "http://s3.amazonaws.com/:id/#{definition[:path]}" + path.gsub!(/:([^\/\.]+)/) do |match| + "([^\/\.]+)" + end + + begin + FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK") + rescue NameError + raise NameError, "the stub_paperclip_s3 shoulda macro requires the fakeweb gem." + end + end + + # Stub S3 and return a file for attachment. Best with Factory Girl. + # Uses a strict directory convention: + # + # features/support/paperclip + # + # This method is used by the Paperclip-provided Cucumber step: + # + # When I attach a "demo_tape" "mp3" file to a "band" on S3 + # + # @example + # Factory.define :band_with_demo_tape, :parent => :band do |band| + # band.demo_tape { band.paperclip_fixture("band", "demo_tape", "png") } + # end + def paperclip_fixture(model, attachment, extension) + stub_paperclip_s3(model, attachment, extension) + base_path = File.join(File.dirname(__FILE__), "..", "..", + "features", "support", "paperclip") + File.new(File.join(base_path, model, "#{attachment}.#{extension}")) + end end end +class ActionController::Integration::Session #:nodoc: + include Paperclip::Shoulda +end + +class Factory + include Paperclip::Shoulda #:nodoc: +end + class Test::Unit::TestCase #:nodoc: extend Paperclip::Shoulda end