Skip to content
Browse files

shoulda macro and webrat step for testing paperclip on s3

  • Loading branch information...
1 parent 4beeb7a commit 98b90bc47f012ab04aa9d0de9d475641cd5fbe83 Daniel Croak committed
Showing with 54 additions and 0 deletions.
  1. +6 −0 cucumber/paperclip_steps.rb
  2. +48 −0 shoulda_macros/paperclip.rb
View
6 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
+
View
48 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

0 comments on commit 98b90bc

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