Skip to content

Commit 98b90bc

Browse files
author
Daniel Croak
committed
shoulda macro and webrat step for testing paperclip on s3
1 parent 4beeb7a commit 98b90bc

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cucumber/paperclip_steps.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
When /^I attach an? "([^\"]*)" "([^\"]*)" file to an? "([^\"]*)" on S3$/ do |attachment, extension, model|
2+
stub_paperclip_s3(model, attachment, extension)
3+
attach_file attachment,
4+
"features/support/paperclip/#{model.gsub(" ", "_").underscore}/#{attachment}.#{extension}"
5+
end
6+

shoulda_macros/paperclip.rb

+48
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,57 @@ def should_validate_attachment_size name, options = {}
6060
assert_accepts(matcher, klass)
6161
end
6262
end
63+
64+
# Stubs the HTTP PUT for an attachment using S3 storage.
65+
#
66+
# @example
67+
# stub_paperclip_s3('user', 'avatar', 'png')
68+
def stub_paperclip_s3(model, attachment, extension)
69+
definition = model.gsub(" ", "_").classify.constantize.
70+
attachment_definitions[attachment.to_sym]
71+
72+
path = "http://s3.amazonaws.com/:id/#{definition[:path]}"
73+
path.gsub!(/:([^\/\.]+)/) do |match|
74+
"([^\/\.]+)"
75+
end
76+
77+
begin
78+
FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
79+
rescue NameError
80+
raise NameError, "the stub_paperclip_s3 shoulda macro requires the fakeweb gem."
81+
end
82+
end
83+
84+
# Stub S3 and return a file for attachment. Best with Factory Girl.
85+
# Uses a strict directory convention:
86+
#
87+
# features/support/paperclip
88+
#
89+
# This method is used by the Paperclip-provided Cucumber step:
90+
#
91+
# When I attach a "demo_tape" "mp3" file to a "band" on S3
92+
#
93+
# @example
94+
# Factory.define :band_with_demo_tape, :parent => :band do |band|
95+
# band.demo_tape { band.paperclip_fixture("band", "demo_tape", "png") }
96+
# end
97+
def paperclip_fixture(model, attachment, extension)
98+
stub_paperclip_s3(model, attachment, extension)
99+
base_path = File.join(File.dirname(__FILE__), "..", "..",
100+
"features", "support", "paperclip")
101+
File.new(File.join(base_path, model, "#{attachment}.#{extension}"))
102+
end
63103
end
64104
end
65105

106+
class ActionController::Integration::Session #:nodoc:
107+
include Paperclip::Shoulda
108+
end
109+
110+
class Factory
111+
include Paperclip::Shoulda #:nodoc:
112+
end
113+
66114
class Test::Unit::TestCase #:nodoc:
67115
extend Paperclip::Shoulda
68116
end

0 commit comments

Comments
 (0)