area17-paperclip 2.2.9.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/LICENSE +26 -0
  2. data/README.rdoc +174 -0
  3. data/Rakefile +99 -0
  4. data/generators/paperclip/USAGE +5 -0
  5. data/generators/paperclip/paperclip_generator.rb +27 -0
  6. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  7. data/init.rb +1 -0
  8. data/lib/paperclip.rb +350 -0
  9. data/lib/paperclip/attachment.rb +464 -0
  10. data/lib/paperclip/callback_compatability.rb +33 -0
  11. data/lib/paperclip/geometry.rb +150 -0
  12. data/lib/paperclip/interpolations.rb +105 -0
  13. data/lib/paperclip/iostream.rb +58 -0
  14. data/lib/paperclip/matchers.rb +4 -0
  15. data/lib/paperclip/matchers/have_attached_file_matcher.rb +49 -0
  16. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +66 -0
  17. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +48 -0
  18. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +83 -0
  19. data/lib/paperclip/processor.rb +49 -0
  20. data/lib/paperclip/storage.rb +238 -0
  21. data/lib/paperclip/thumbnail.rb +70 -0
  22. data/lib/paperclip/upfile.rb +44 -0
  23. data/shoulda_macros/paperclip.rb +68 -0
  24. data/tasks/paperclip_tasks.rake +97 -0
  25. data/test/attachment_test.rb +774 -0
  26. data/test/database.yml +4 -0
  27. data/test/fixtures/12k.png +0 -0
  28. data/test/fixtures/50x50.png +0 -0
  29. data/test/fixtures/5k.png +0 -0
  30. data/test/fixtures/bad.png +1 -0
  31. data/test/fixtures/s3.yml +4 -0
  32. data/test/fixtures/text.txt +0 -0
  33. data/test/fixtures/twopage.pdf +0 -0
  34. data/test/geometry_test.rb +177 -0
  35. data/test/helper.rb +100 -0
  36. data/test/integration_test.rb +481 -0
  37. data/test/interpolations_test.rb +120 -0
  38. data/test/iostream_test.rb +71 -0
  39. data/test/matchers/have_attached_file_matcher_test.rb +21 -0
  40. data/test/matchers/validate_attachment_content_type_matcher_test.rb +30 -0
  41. data/test/matchers/validate_attachment_presence_matcher_test.rb +21 -0
  42. data/test/matchers/validate_attachment_size_matcher_test.rb +50 -0
  43. data/test/paperclip_test.rb +291 -0
  44. data/test/processor_test.rb +10 -0
  45. data/test/storage_test.rb +278 -0
  46. data/test/thumbnail_test.rb +177 -0
  47. metadata +124 -0
@@ -0,0 +1,10 @@
1
+ require 'test/helper'
2
+
3
+ class ProcessorTest < Test::Unit::TestCase
4
+ should "instantiate and call #make when sent #make to the class" do
5
+ processor = mock
6
+ processor.expects(:make).with()
7
+ Paperclip::Processor.expects(:new).with(:one, :two, :three).returns(processor)
8
+ Paperclip::Processor.make(:one, :two, :three)
9
+ end
10
+ end
@@ -0,0 +1,278 @@
1
+ require 'test/helper'
2
+
3
+ class StorageTest < Test::Unit::TestCase
4
+ context "Parsing S3 credentials" do
5
+ setup do
6
+ rebuild_model :storage => :s3,
7
+ :bucket => "testing",
8
+ :s3_credentials => {:not => :important}
9
+
10
+ @dummy = Dummy.new
11
+ @avatar = @dummy.avatar
12
+ end
13
+
14
+ should "get the correct credentials when RAILS_ENV is production" do
15
+ temporary_rails_env('production') do
16
+ assert_equal({:key => "12345"},
17
+ @avatar.parse_credentials('production' => {:key => '12345'},
18
+ :development => {:key => "54321"}))
19
+ end
20
+ end
21
+
22
+ should "get the correct credentials when RAILS_ENV is development" do
23
+ temporary_rails_env('development') do
24
+ assert_equal({:key => "54321"},
25
+ @avatar.parse_credentials('production' => {:key => '12345'},
26
+ :development => {:key => "54321"}))
27
+ end
28
+ end
29
+
30
+ should "return the argument if the key does not exist" do
31
+ temporary_rails_env("not really an env") do
32
+ assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
33
+ end
34
+ end
35
+ end
36
+
37
+ context "" do
38
+ setup do
39
+ rebuild_model :storage => :s3,
40
+ :s3_credentials => {},
41
+ :bucket => "bucket",
42
+ :path => ":attachment/:basename.:extension",
43
+ :url => ":s3_path_url"
44
+ @dummy = Dummy.new
45
+ @dummy.avatar = StringIO.new(".")
46
+ end
47
+
48
+ should "return a url based on an S3 path" do
49
+ assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url
50
+ end
51
+ end
52
+ context "" do
53
+ setup do
54
+ rebuild_model :storage => :s3,
55
+ :s3_credentials => {},
56
+ :bucket => "bucket",
57
+ :path => ":attachment/:basename.:extension",
58
+ :url => ":s3_domain_url"
59
+ @dummy = Dummy.new
60
+ @dummy.avatar = StringIO.new(".")
61
+ end
62
+
63
+ should "return a url based on an S3 subdomain" do
64
+ assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url
65
+ end
66
+ end
67
+ context "" do
68
+ setup do
69
+ rebuild_model :storage => :s3,
70
+ :s3_credentials => {
71
+ :production => { :bucket => "prod_bucket" },
72
+ :development => { :bucket => "dev_bucket" }
73
+ },
74
+ :s3_host_alias => "something.something.com",
75
+ :path => ":attachment/:basename.:extension",
76
+ :url => ":s3_alias_url"
77
+ @dummy = Dummy.new
78
+ @dummy.avatar = StringIO.new(".")
79
+ end
80
+
81
+ should "return a url based on the host_alias" do
82
+ assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url
83
+ end
84
+ end
85
+
86
+ context "Parsing S3 credentials with a bucket in them" do
87
+ setup do
88
+ rebuild_model :storage => :s3,
89
+ :s3_credentials => {
90
+ :production => { :bucket => "prod_bucket" },
91
+ :development => { :bucket => "dev_bucket" }
92
+ }
93
+ @dummy = Dummy.new
94
+ end
95
+
96
+ should "get the right bucket in production" do
97
+ temporary_rails_env("production") do
98
+ assert_equal "prod_bucket", @dummy.avatar.bucket_name
99
+ end
100
+ end
101
+
102
+ should "get the right bucket in development" do
103
+ temporary_rails_env('development') do
104
+ assert_equal "dev_bucket", @dummy.avatar.bucket_name
105
+ end
106
+ end
107
+ end
108
+
109
+ context "An attachment with S3 storage" do
110
+ setup do
111
+ rebuild_model :storage => :s3,
112
+ :bucket => "testing",
113
+ :path => ":attachment/:style/:basename.:extension",
114
+ :s3_credentials => {
115
+ 'access_key_id' => "12345",
116
+ 'secret_access_key' => "54321"
117
+ }
118
+ end
119
+
120
+ should "be extended by the S3 module" do
121
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
122
+ end
123
+
124
+ should "not be extended by the Filesystem module" do
125
+ assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
126
+ end
127
+
128
+ context "when assigned" do
129
+ setup do
130
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
131
+ @dummy = Dummy.new
132
+ @dummy.avatar = @file
133
+ end
134
+
135
+ teardown { @file.close }
136
+
137
+ should "not get a bucket to get a URL" do
138
+ @dummy.avatar.expects(:s3).never
139
+ @dummy.avatar.expects(:s3_bucket).never
140
+ assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
141
+ end
142
+
143
+ context "and saved" do
144
+ setup do
145
+ @s3_mock = stub
146
+ @bucket_mock = stub
147
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
148
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
149
+ @key_mock = stub
150
+ @bucket_mock.expects(:key).returns(@key_mock)
151
+ @key_mock.expects(:data=)
152
+ @key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png')
153
+ @dummy.save
154
+ end
155
+
156
+ should "succeed" do
157
+ assert true
158
+ end
159
+ end
160
+
161
+ context "and remove" do
162
+ setup do
163
+ @s3_mock = stub
164
+ @bucket_mock = stub
165
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
166
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
167
+ @key_mock = stub
168
+ @bucket_mock.expects(:key).at_least(2).returns(@key_mock)
169
+ @key_mock.expects(:delete)
170
+ @dummy.destroy_attached_files
171
+ end
172
+
173
+ should "succeed" do
174
+ assert true
175
+ end
176
+ end
177
+ end
178
+ end
179
+
180
+ context "An attachment with S3 storage and bucket defined as a Proc" do
181
+ setup do
182
+ rebuild_model :storage => :s3,
183
+ :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" },
184
+ :s3_credentials => {:not => :important}
185
+ end
186
+
187
+ should "get the right bucket name" do
188
+ assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name
189
+ assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name
190
+ end
191
+ end
192
+
193
+ context "An attachment with S3 storage and specific s3 headers set" do
194
+ setup do
195
+ rebuild_model :storage => :s3,
196
+ :bucket => "testing",
197
+ :path => ":attachment/:style/:basename.:extension",
198
+ :s3_credentials => {
199
+ 'access_key_id' => "12345",
200
+ 'secret_access_key' => "54321"
201
+ },
202
+ :s3_headers => {'Cache-Control' => 'max-age=31557600'}
203
+ end
204
+
205
+ context "when assigned" do
206
+ setup do
207
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
208
+ @dummy = Dummy.new
209
+ @dummy.avatar = @file
210
+ end
211
+
212
+ teardown { @file.close }
213
+
214
+ context "and saved" do
215
+ setup do
216
+ @s3_mock = stub
217
+ @bucket_mock = stub
218
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
219
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
220
+ @key_mock = stub
221
+ @bucket_mock.expects(:key).returns(@key_mock)
222
+ @key_mock.expects(:data=)
223
+ @key_mock.expects(:put).with(nil,
224
+ 'public-read',
225
+ 'Content-type' => 'image/png',
226
+ 'Cache-Control' => 'max-age=31557600')
227
+ @dummy.save
228
+ end
229
+
230
+ should "succeed" do
231
+ assert true
232
+ end
233
+ end
234
+ end
235
+ end
236
+
237
+ unless ENV["S3_TEST_BUCKET"].blank?
238
+ context "Using S3 for real, an attachment with S3 storage" do
239
+ setup do
240
+ rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
241
+ :storage => :s3,
242
+ :bucket => ENV["S3_TEST_BUCKET"],
243
+ :path => ":class/:attachment/:id/:style.:extension",
244
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
245
+
246
+ Dummy.delete_all
247
+ @dummy = Dummy.new
248
+ end
249
+
250
+ should "be extended by the S3 module" do
251
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
252
+ end
253
+
254
+ context "when assigned" do
255
+ setup do
256
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
257
+ @dummy.avatar = @file
258
+ end
259
+
260
+ teardown { @file.close }
261
+
262
+ should "still return a Tempfile when sent #to_io" do
263
+ assert_equal Tempfile, @dummy.avatar.to_io.class
264
+ end
265
+
266
+ context "and saved" do
267
+ setup do
268
+ @dummy.save
269
+ end
270
+
271
+ should "be on S3" do
272
+ assert true
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,177 @@
1
+ require 'test/helper'
2
+
3
+ class ThumbnailTest < Test::Unit::TestCase
4
+
5
+ context "A Paperclip Tempfile" do
6
+ setup do
7
+ @tempfile = Paperclip::Tempfile.new("file.jpg")
8
+ end
9
+
10
+ should "have its path contain a real extension" do
11
+ assert_equal ".jpg", File.extname(@tempfile.path)
12
+ end
13
+
14
+ should "be a real Tempfile" do
15
+ assert @tempfile.is_a?(::Tempfile)
16
+ end
17
+ end
18
+
19
+ context "Another Paperclip Tempfile" do
20
+ setup do
21
+ @tempfile = Paperclip::Tempfile.new("file")
22
+ end
23
+
24
+ should "not have an extension if not given one" do
25
+ assert_equal "", File.extname(@tempfile.path)
26
+ end
27
+
28
+ should "still be a real Tempfile" do
29
+ assert @tempfile.is_a?(::Tempfile)
30
+ end
31
+ end
32
+
33
+ context "An image" do
34
+ setup do
35
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
36
+ end
37
+
38
+ teardown { @file.close }
39
+
40
+ [["600x600>", "434x66"],
41
+ ["400x400>", "400x61"],
42
+ ["32x32<", "434x66"]
43
+ ].each do |args|
44
+ context "being thumbnailed with a geometry of #{args[0]}" do
45
+ setup do
46
+ @thumb = Paperclip::Thumbnail.new(@file, :geometry => args[0])
47
+ end
48
+
49
+ should "start with dimensions of 434x66" do
50
+ cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
51
+ assert_equal "434x66", `#{cmd}`.chomp
52
+ end
53
+
54
+ should "report the correct target geometry" do
55
+ assert_equal args[0], @thumb.target_geometry.to_s
56
+ end
57
+
58
+ context "when made" do
59
+ setup do
60
+ @thumb_result = @thumb.make
61
+ end
62
+
63
+ should "be the size we expect it to be" do
64
+ cmd = %Q[identify -format "%wx%h" "#{@thumb_result.path}"]
65
+ assert_equal args[1], `#{cmd}`.chomp
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ context "being thumbnailed at 100x50 with cropping" do
72
+ setup do
73
+ @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
74
+ end
75
+
76
+ should "report its correct current and target geometries" do
77
+ assert_equal "100x50#", @thumb.target_geometry.to_s
78
+ assert_equal "434x66", @thumb.current_geometry.to_s
79
+ end
80
+
81
+ should "report its correct format" do
82
+ assert_nil @thumb.format
83
+ end
84
+
85
+ should "have whiny turned on by default" do
86
+ assert @thumb.whiny
87
+ end
88
+
89
+ should "have convert_options set to nil by default" do
90
+ assert_equal nil, @thumb.convert_options
91
+ end
92
+
93
+ should "send the right command to convert when sent #make" do
94
+ Paperclip.expects(:"`").with do |arg|
95
+ arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-resize\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+".*?"}
96
+ end
97
+ @thumb.make
98
+ end
99
+
100
+ should "create the thumbnail when sent #make" do
101
+ dst = @thumb.make
102
+ assert_match /100x50/, `identify "#{dst.path}"`
103
+ end
104
+ end
105
+
106
+ context "being thumbnailed with convert options set" do
107
+ setup do
108
+ @thumb = Paperclip::Thumbnail.new(@file,
109
+ :geometry => "100x50#",
110
+ :convert_options => "-strip -depth 8")
111
+ end
112
+
113
+ should "have convert_options value set" do
114
+ assert_equal "-strip -depth 8", @thumb.convert_options
115
+ end
116
+
117
+ should "send the right command to convert when sent #make" do
118
+ Paperclip.expects(:"`").with do |arg|
119
+ arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-resize\s+"x50"\s+-crop\s+"100x50\+114\+0"\s+\+repage\s+-strip\s+-depth\s+8\s+".*?"}
120
+ end
121
+ @thumb.make
122
+ end
123
+
124
+ should "create the thumbnail when sent #make" do
125
+ dst = @thumb.make
126
+ assert_match /100x50/, `identify "#{dst.path}"`
127
+ end
128
+
129
+ context "redefined to have bad convert_options setting" do
130
+ setup do
131
+ @thumb = Paperclip::Thumbnail.new(@file,
132
+ :geometry => "100x50#",
133
+ :convert_options => "-this-aint-no-option")
134
+ end
135
+
136
+ should "error when trying to create the thumbnail" do
137
+ assert_raises(Paperclip::PaperclipError) do
138
+ @thumb.make
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ context "A multipage PDF" do
146
+ setup do
147
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "twopage.pdf"), 'rb')
148
+ end
149
+
150
+ teardown { @file.close }
151
+
152
+ should "start with two pages with dimensions 612x792" do
153
+ cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
154
+ assert_equal "612x792"*2, `#{cmd}`.chomp
155
+ end
156
+
157
+ context "being thumbnailed at 100x100 with cropping" do
158
+ setup do
159
+ @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x100#", :format => :png)
160
+ end
161
+
162
+ should "report its correct current and target geometries" do
163
+ assert_equal "100x100#", @thumb.target_geometry.to_s
164
+ assert_equal "612x792", @thumb.current_geometry.to_s
165
+ end
166
+
167
+ should "report its correct format" do
168
+ assert_equal :png, @thumb.format
169
+ end
170
+
171
+ should "create the thumbnail when sent #make" do
172
+ dst = @thumb.make
173
+ assert_match /100x100/, `identify "#{dst.path}"`
174
+ end
175
+ end
176
+ end
177
+ end