diff options
| author | hukl <contact@smyck.org> | 2009-04-24 11:43:08 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-04-25 14:55:27 +0200 |
| commit | cf1b60e0cfa7d1a8f4a80d686649cc12e73a634e (patch) | |
| tree | b68bd845d290ce968892c4532bcff52083925834 /vendor/plugins/paperclip/test | |
| parent | b2b78c06074046bd73cc3408a29386a976f0469c (diff) | |
Integrated basic Asset upload functionality. You can upload files now and use their url in pages.
Diffstat (limited to 'vendor/plugins/paperclip/test')
20 files changed, 2365 insertions, 0 deletions
diff --git a/vendor/plugins/paperclip/test/.gitignore b/vendor/plugins/paperclip/test/.gitignore new file mode 100644 index 0000000..b14c548 --- /dev/null +++ b/vendor/plugins/paperclip/test/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| debug.log | |||
diff --git a/vendor/plugins/paperclip/test/attachment_test.rb b/vendor/plugins/paperclip/test/attachment_test.rb new file mode 100644 index 0000000..61ab2a2 --- /dev/null +++ b/vendor/plugins/paperclip/test/attachment_test.rb | |||
| @@ -0,0 +1,742 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class Dummy | ||
| 4 | # This is a dummy class | ||
| 5 | end | ||
| 6 | |||
| 7 | class AttachmentTest < Test::Unit::TestCase | ||
| 8 | context "Attachment default_options" do | ||
| 9 | setup do | ||
| 10 | rebuild_model | ||
| 11 | @old_default_options = Paperclip::Attachment.default_options.dup | ||
| 12 | @new_default_options = @old_default_options.merge({ | ||
| 13 | :path => "argle/bargle", | ||
| 14 | :url => "fooferon", | ||
| 15 | :default_url => "not here.png" | ||
| 16 | }) | ||
| 17 | end | ||
| 18 | |||
| 19 | teardown do | ||
| 20 | Paperclip::Attachment.default_options.merge! @old_default_options | ||
| 21 | end | ||
| 22 | |||
| 23 | should "be overrideable" do | ||
| 24 | Paperclip::Attachment.default_options.merge!(@new_default_options) | ||
| 25 | @new_default_options.keys.each do |key| | ||
| 26 | assert_equal @new_default_options[key], | ||
| 27 | Paperclip::Attachment.default_options[key] | ||
| 28 | end | ||
| 29 | end | ||
| 30 | |||
| 31 | context "without an Attachment" do | ||
| 32 | setup do | ||
| 33 | @dummy = Dummy.new | ||
| 34 | end | ||
| 35 | |||
| 36 | should "return false when asked exists?" do | ||
| 37 | assert !@dummy.avatar.exists? | ||
| 38 | end | ||
| 39 | end | ||
| 40 | |||
| 41 | context "on an Attachment" do | ||
| 42 | setup do | ||
| 43 | @dummy = Dummy.new | ||
| 44 | @attachment = @dummy.avatar | ||
| 45 | end | ||
| 46 | |||
| 47 | Paperclip::Attachment.default_options.keys.each do |key| | ||
| 48 | should "be the default_options for #{key}" do | ||
| 49 | assert_equal @old_default_options[key], | ||
| 50 | @attachment.instance_variable_get("@#{key}"), | ||
| 51 | key | ||
| 52 | end | ||
| 53 | end | ||
| 54 | |||
| 55 | context "when redefined" do | ||
| 56 | setup do | ||
| 57 | Paperclip::Attachment.default_options.merge!(@new_default_options) | ||
| 58 | @dummy = Dummy.new | ||
| 59 | @attachment = @dummy.avatar | ||
| 60 | end | ||
| 61 | |||
| 62 | Paperclip::Attachment.default_options.keys.each do |key| | ||
| 63 | should "be the new default_options for #{key}" do | ||
| 64 | assert_equal @new_default_options[key], | ||
| 65 | @attachment.instance_variable_get("@#{key}"), | ||
| 66 | key | ||
| 67 | end | ||
| 68 | end | ||
| 69 | end | ||
| 70 | end | ||
| 71 | end | ||
| 72 | |||
| 73 | context "An attachment with similarly named interpolations" do | ||
| 74 | setup do | ||
| 75 | rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf" | ||
| 76 | @dummy = Dummy.new | ||
| 77 | @dummy.stubs(:id).returns(1024) | ||
| 78 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 79 | "fixtures", | ||
| 80 | "5k.png"), 'rb') | ||
| 81 | @dummy.avatar = @file | ||
| 82 | end | ||
| 83 | |||
| 84 | teardown { @file.close } | ||
| 85 | |||
| 86 | should "make sure that they are interpolated correctly" do | ||
| 87 | assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path | ||
| 88 | end | ||
| 89 | end | ||
| 90 | |||
| 91 | context "An attachment with a :rails_env interpolation" do | ||
| 92 | setup do | ||
| 93 | @rails_env = "blah" | ||
| 94 | @id = 1024 | ||
| 95 | rebuild_model :path => ":rails_env/:id.png" | ||
| 96 | @dummy = Dummy.new | ||
| 97 | @dummy.stubs(:id).returns(@id) | ||
| 98 | @file = StringIO.new(".") | ||
| 99 | @dummy.avatar = @file | ||
| 100 | end | ||
| 101 | |||
| 102 | should "return the proper path" do | ||
| 103 | temporary_rails_env(@rails_env) { | ||
| 104 | assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path | ||
| 105 | } | ||
| 106 | end | ||
| 107 | end | ||
| 108 | |||
| 109 | context "An attachment with :convert_options" do | ||
| 110 | setup do | ||
| 111 | rebuild_model :styles => { | ||
| 112 | :thumb => "100x100", | ||
| 113 | :large => "400x400" | ||
| 114 | }, | ||
| 115 | :convert_options => { | ||
| 116 | :all => "-do_stuff", | ||
| 117 | :thumb => "-thumbnailize" | ||
| 118 | } | ||
| 119 | @dummy = Dummy.new | ||
| 120 | @dummy.avatar | ||
| 121 | end | ||
| 122 | |||
| 123 | should "report the correct options when sent #extra_options_for(:thumb)" do | ||
| 124 | assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect | ||
| 125 | end | ||
| 126 | |||
| 127 | should "report the correct options when sent #extra_options_for(:large)" do | ||
| 128 | assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large) | ||
| 129 | end | ||
| 130 | |||
| 131 | before_should "call extra_options_for(:thumb/:large)" do | ||
| 132 | Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb) | ||
| 133 | Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large) | ||
| 134 | end | ||
| 135 | end | ||
| 136 | |||
| 137 | context "An attachment with :convert_options that is a proc" do | ||
| 138 | setup do | ||
| 139 | rebuild_model :styles => { | ||
| 140 | :thumb => "100x100", | ||
| 141 | :large => "400x400" | ||
| 142 | }, | ||
| 143 | :convert_options => { | ||
| 144 | :all => lambda{|i| i.all }, | ||
| 145 | :thumb => lambda{|i| i.thumb } | ||
| 146 | } | ||
| 147 | Dummy.class_eval do | ||
| 148 | def all; "-all"; end | ||
| 149 | def thumb; "-thumb"; end | ||
| 150 | end | ||
| 151 | @dummy = Dummy.new | ||
| 152 | @dummy.avatar | ||
| 153 | end | ||
| 154 | |||
| 155 | should "report the correct options when sent #extra_options_for(:thumb)" do | ||
| 156 | assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect | ||
| 157 | end | ||
| 158 | |||
| 159 | should "report the correct options when sent #extra_options_for(:large)" do | ||
| 160 | assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large) | ||
| 161 | end | ||
| 162 | |||
| 163 | before_should "call extra_options_for(:thumb/:large)" do | ||
| 164 | Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb) | ||
| 165 | Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large) | ||
| 166 | end | ||
| 167 | end | ||
| 168 | |||
| 169 | context "An attachment with :path that is a proc" do | ||
| 170 | setup do | ||
| 171 | rebuild_model :path => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" } | ||
| 172 | |||
| 173 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 174 | "fixtures", | ||
| 175 | "5k.png"), 'rb') | ||
| 176 | @dummyA = Dummy.new(:other => 'a') | ||
| 177 | @dummyA.avatar = @file | ||
| 178 | @dummyB = Dummy.new(:other => 'b') | ||
| 179 | @dummyB.avatar = @file | ||
| 180 | end | ||
| 181 | |||
| 182 | teardown { @file.close } | ||
| 183 | |||
| 184 | should "return correct path" do | ||
| 185 | assert_equal "path/a.png", @dummyA.avatar.path | ||
| 186 | assert_equal "path/b.png", @dummyB.avatar.path | ||
| 187 | end | ||
| 188 | end | ||
| 189 | |||
| 190 | context "An attachment with :styles that is a proc" do | ||
| 191 | setup do | ||
| 192 | rebuild_model :styles => lambda{ |attachment| {:thumb => "50x50#", :large => "400x400"} } | ||
| 193 | |||
| 194 | @attachment = Dummy.new.avatar | ||
| 195 | end | ||
| 196 | |||
| 197 | should "have the correct geometry" do | ||
| 198 | assert_equal "50x50#", @attachment.styles[:thumb][:geometry] | ||
| 199 | end | ||
| 200 | end | ||
| 201 | |||
| 202 | context "An attachment with :url that is a proc" do | ||
| 203 | setup do | ||
| 204 | rebuild_model :url => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" } | ||
| 205 | |||
| 206 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 207 | "fixtures", | ||
| 208 | "5k.png"), 'rb') | ||
| 209 | @dummyA = Dummy.new(:other => 'a') | ||
| 210 | @dummyA.avatar = @file | ||
| 211 | @dummyB = Dummy.new(:other => 'b') | ||
| 212 | @dummyB.avatar = @file | ||
| 213 | end | ||
| 214 | |||
| 215 | teardown { @file.close } | ||
| 216 | |||
| 217 | should "return correct url" do | ||
| 218 | assert_equal "path/a.png", @dummyA.avatar.url(:original, false) | ||
| 219 | assert_equal "path/b.png", @dummyB.avatar.url(:original, false) | ||
| 220 | end | ||
| 221 | end | ||
| 222 | |||
| 223 | geometry_specs = [ | ||
| 224 | [ lambda{|z| "50x50#" }, :png ], | ||
| 225 | lambda{|z| "50x50#" }, | ||
| 226 | { :geometry => lambda{|z| "50x50#" } } | ||
| 227 | ] | ||
| 228 | geometry_specs.each do |geometry_spec| | ||
| 229 | context "An attachment geometry like #{geometry_spec}" do | ||
| 230 | setup do | ||
| 231 | rebuild_model :styles => { :normal => geometry_spec } | ||
| 232 | @attachment = Dummy.new.avatar | ||
| 233 | end | ||
| 234 | |||
| 235 | should "not run the procs immediately" do | ||
| 236 | assert_kind_of Proc, @attachment.styles[:normal][:geometry] | ||
| 237 | end | ||
| 238 | |||
| 239 | context "when assigned" do | ||
| 240 | setup do | ||
| 241 | @file = StringIO.new(".") | ||
| 242 | @attachment.assign(@file) | ||
| 243 | end | ||
| 244 | |||
| 245 | should "have the correct geometry" do | ||
| 246 | assert_equal "50x50#", @attachment.styles[:normal][:geometry] | ||
| 247 | end | ||
| 248 | end | ||
| 249 | end | ||
| 250 | end | ||
| 251 | |||
| 252 | context "An attachment with both 'normal' and hash-style styles" do | ||
| 253 | setup do | ||
| 254 | rebuild_model :styles => { | ||
| 255 | :normal => ["50x50#", :png], | ||
| 256 | :hash => { :geometry => "50x50#", :format => :png } | ||
| 257 | } | ||
| 258 | @dummy = Dummy.new | ||
| 259 | @attachment = @dummy.avatar | ||
| 260 | end | ||
| 261 | |||
| 262 | [:processors, :whiny, :convert_options, :geometry, :format].each do |field| | ||
| 263 | should "have the same #{field} field" do | ||
| 264 | assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field] | ||
| 265 | end | ||
| 266 | end | ||
| 267 | end | ||
| 268 | |||
| 269 | context "An attachment with :processors that is a proc" do | ||
| 270 | setup do | ||
| 271 | rebuild_model :styles => { :normal => '' }, :processors => lambda { |a| [ :test ] } | ||
| 272 | @attachment = Dummy.new.avatar | ||
| 273 | end | ||
| 274 | |||
| 275 | should "not run the proc immediately" do | ||
| 276 | assert_kind_of Proc, @attachment.styles[:normal][:processors] | ||
| 277 | end | ||
| 278 | |||
| 279 | context "when assigned" do | ||
| 280 | setup do | ||
| 281 | @attachment.assign(StringIO.new(".")) | ||
| 282 | end | ||
| 283 | |||
| 284 | should "have the correct processors" do | ||
| 285 | assert_equal [ :test ], @attachment.styles[:normal][:processors] | ||
| 286 | end | ||
| 287 | end | ||
| 288 | end | ||
| 289 | |||
| 290 | context "An attachment with erroring processor" do | ||
| 291 | setup do | ||
| 292 | rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true | ||
| 293 | @dummy = Dummy.new | ||
| 294 | Paperclip::Thumbnail.expects(:make).raises(Paperclip::PaperclipError, "cannot be processed.") | ||
| 295 | @file = StringIO.new("...") | ||
| 296 | @file.stubs(:to_tempfile).returns(@file) | ||
| 297 | @dummy.avatar = @file | ||
| 298 | end | ||
| 299 | |||
| 300 | should "correctly forward processing error message to the instance" do | ||
| 301 | @dummy.valid? | ||
| 302 | assert_contains @dummy.errors.full_messages, "Avatar cannot be processed." | ||
| 303 | end | ||
| 304 | end | ||
| 305 | |||
| 306 | context "An attachment with multiple processors" do | ||
| 307 | setup do | ||
| 308 | class Paperclip::Test < Paperclip::Processor; end | ||
| 309 | @style_params = { :once => {:one => 1, :two => 2} } | ||
| 310 | rebuild_model :processors => [:thumbnail, :test], :styles => @style_params | ||
| 311 | @dummy = Dummy.new | ||
| 312 | @file = StringIO.new("...") | ||
| 313 | @file.stubs(:to_tempfile).returns(@file) | ||
| 314 | Paperclip::Test.stubs(:make).returns(@file) | ||
| 315 | Paperclip::Thumbnail.stubs(:make).returns(@file) | ||
| 316 | end | ||
| 317 | |||
| 318 | context "when assigned" do | ||
| 319 | setup { @dummy.avatar = @file } | ||
| 320 | |||
| 321 | before_should "call #make on all specified processors" do | ||
| 322 | expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => nil, :convert_options => ""}) | ||
| 323 | Paperclip::Thumbnail.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) | ||
| 324 | Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) | ||
| 325 | end | ||
| 326 | |||
| 327 | before_should "call #make with attachment passed as third argument" do | ||
| 328 | expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => nil, :convert_options => ""}) | ||
| 329 | Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) | ||
| 330 | end | ||
| 331 | end | ||
| 332 | end | ||
| 333 | |||
| 334 | context "An attachment with no processors defined" do | ||
| 335 | setup do | ||
| 336 | rebuild_model :processors => [], :styles => {:something => 1} | ||
| 337 | @dummy = Dummy.new | ||
| 338 | @file = StringIO.new("...") | ||
| 339 | end | ||
| 340 | should "raise when assigned to" do | ||
| 341 | assert_raises(RuntimeError){ @dummy.avatar = @file } | ||
| 342 | end | ||
| 343 | end | ||
| 344 | |||
| 345 | context "Assigning an attachment with post_process hooks" do | ||
| 346 | setup do | ||
| 347 | rebuild_model :styles => { :something => "100x100#" } | ||
| 348 | Dummy.class_eval do | ||
| 349 | before_avatar_post_process :do_before_avatar | ||
| 350 | after_avatar_post_process :do_after_avatar | ||
| 351 | before_post_process :do_before_all | ||
| 352 | after_post_process :do_after_all | ||
| 353 | def do_before_avatar; end | ||
| 354 | def do_after_avatar; end | ||
| 355 | def do_before_all; end | ||
| 356 | def do_after_all; end | ||
| 357 | end | ||
| 358 | @file = StringIO.new(".") | ||
| 359 | @file.stubs(:to_tempfile).returns(@file) | ||
| 360 | @dummy = Dummy.new | ||
| 361 | Paperclip::Thumbnail.stubs(:make).returns(@file) | ||
| 362 | @attachment = @dummy.avatar | ||
| 363 | end | ||
| 364 | |||
| 365 | should "call the defined callbacks when assigned" do | ||
| 366 | @dummy.expects(:do_before_avatar).with() | ||
| 367 | @dummy.expects(:do_after_avatar).with() | ||
| 368 | @dummy.expects(:do_before_all).with() | ||
| 369 | @dummy.expects(:do_after_all).with() | ||
| 370 | Paperclip::Thumbnail.expects(:make).returns(@file) | ||
| 371 | @dummy.avatar = @file | ||
| 372 | end | ||
| 373 | |||
| 374 | should "not cancel the processing if a before_post_process returns nil" do | ||
| 375 | @dummy.expects(:do_before_avatar).with().returns(nil) | ||
| 376 | @dummy.expects(:do_after_avatar).with() | ||
| 377 | @dummy.expects(:do_before_all).with().returns(nil) | ||
| 378 | @dummy.expects(:do_after_all).with() | ||
| 379 | Paperclip::Thumbnail.expects(:make).returns(@file) | ||
| 380 | @dummy.avatar = @file | ||
| 381 | end | ||
| 382 | |||
| 383 | should "cancel the processing if a before_post_process returns false" do | ||
| 384 | @dummy.expects(:do_before_avatar).never | ||
| 385 | @dummy.expects(:do_after_avatar).never | ||
| 386 | @dummy.expects(:do_before_all).with().returns(false) | ||
| 387 | @dummy.expects(:do_after_all).never | ||
| 388 | Paperclip::Thumbnail.expects(:make).never | ||
| 389 | @dummy.avatar = @file | ||
| 390 | end | ||
| 391 | |||
| 392 | should "cancel the processing if a before_avatar_post_process returns false" do | ||
| 393 | @dummy.expects(:do_before_avatar).with().returns(false) | ||
| 394 | @dummy.expects(:do_after_avatar).never | ||
| 395 | @dummy.expects(:do_before_all).with().returns(true) | ||
| 396 | @dummy.expects(:do_after_all).never | ||
| 397 | Paperclip::Thumbnail.expects(:make).never | ||
| 398 | @dummy.avatar = @file | ||
| 399 | end | ||
| 400 | end | ||
| 401 | |||
| 402 | context "Assigning an attachment" do | ||
| 403 | setup do | ||
| 404 | rebuild_model :styles => { :something => "100x100#" } | ||
| 405 | @file = StringIO.new(".") | ||
| 406 | @file.expects(:original_filename).returns("5k.png\n\n") | ||
| 407 | @file.expects(:content_type).returns("image/png\n\n") | ||
| 408 | @file.stubs(:to_tempfile).returns(@file) | ||
| 409 | @dummy = Dummy.new | ||
| 410 | Paperclip::Thumbnail.expects(:make).returns(@file) | ||
| 411 | @dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file}) | ||
| 412 | @dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file}) | ||
| 413 | @dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file}) | ||
| 414 | @dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file}) | ||
| 415 | @attachment = @dummy.avatar | ||
| 416 | @dummy.avatar = @file | ||
| 417 | end | ||
| 418 | |||
| 419 | should "strip whitespace from original_filename field" do | ||
| 420 | assert_equal "5k.png", @dummy.avatar.original_filename | ||
| 421 | end | ||
| 422 | |||
| 423 | should "strip whitespace from content_type field" do | ||
| 424 | assert_equal "image/png", @dummy.avatar.instance.avatar_content_type | ||
| 425 | end | ||
| 426 | end | ||
| 427 | |||
| 428 | context "Attachment with strange letters" do | ||
| 429 | setup do | ||
| 430 | rebuild_model | ||
| 431 | |||
| 432 | @not_file = mock | ||
| 433 | @tempfile = mock | ||
| 434 | @not_file.stubs(:nil?).returns(false) | ||
| 435 | @not_file.expects(:size).returns(10) | ||
| 436 | @tempfile.expects(:size).returns(10) | ||
| 437 | @not_file.expects(:to_tempfile).returns(@tempfile) | ||
| 438 | @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n") | ||
| 439 | @not_file.expects(:content_type).returns("image/png\r\n") | ||
| 440 | |||
| 441 | @dummy = Dummy.new | ||
| 442 | @attachment = @dummy.avatar | ||
| 443 | @attachment.expects(:valid_assignment?).with(@not_file).returns(true) | ||
| 444 | @attachment.expects(:queue_existing_for_delete) | ||
| 445 | @attachment.expects(:post_process) | ||
| 446 | @attachment.expects(:valid?).returns(true) | ||
| 447 | @attachment.expects(:validate) | ||
| 448 | @dummy.avatar = @not_file | ||
| 449 | end | ||
| 450 | |||
| 451 | should "remove strange letters and replace with underscore (_)" do | ||
| 452 | assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename | ||
| 453 | end | ||
| 454 | |||
| 455 | end | ||
| 456 | |||
| 457 | context "An attachment" do | ||
| 458 | setup do | ||
| 459 | Paperclip::Attachment.default_options.merge!({ | ||
| 460 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 461 | }) | ||
| 462 | FileUtils.rm_rf("tmp") | ||
| 463 | rebuild_model | ||
| 464 | @instance = Dummy.new | ||
| 465 | @attachment = Paperclip::Attachment.new(:avatar, @instance) | ||
| 466 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 467 | "fixtures", | ||
| 468 | "5k.png"), 'rb') | ||
| 469 | end | ||
| 470 | |||
| 471 | teardown { @file.close } | ||
| 472 | |||
| 473 | should "raise if there are not the correct columns when you try to assign" do | ||
| 474 | @other_attachment = Paperclip::Attachment.new(:not_here, @instance) | ||
| 475 | assert_raises(Paperclip::PaperclipError) do | ||
| 476 | @other_attachment.assign(@file) | ||
| 477 | end | ||
| 478 | end | ||
| 479 | |||
| 480 | should "return its default_url when no file assigned" do | ||
| 481 | assert @attachment.to_file.nil? | ||
| 482 | assert_equal "/avatars/original/missing.png", @attachment.url | ||
| 483 | assert_equal "/avatars/blah/missing.png", @attachment.url(:blah) | ||
| 484 | end | ||
| 485 | |||
| 486 | should "return nil as path when no file assigned" do | ||
| 487 | assert @attachment.to_file.nil? | ||
| 488 | assert_equal nil, @attachment.path | ||
| 489 | assert_equal nil, @attachment.path(:blah) | ||
| 490 | end | ||
| 491 | |||
| 492 | context "with a file assigned in the database" do | ||
| 493 | setup do | ||
| 494 | @attachment.stubs(:instance_read).with(:file_name).returns("5k.png") | ||
| 495 | @attachment.stubs(:instance_read).with(:content_type).returns("image/png") | ||
| 496 | @attachment.stubs(:instance_read).with(:file_size).returns(12345) | ||
| 497 | now = Time.now | ||
| 498 | Time.stubs(:now).returns(now) | ||
| 499 | @attachment.stubs(:instance_read).with(:updated_at).returns(Time.now) | ||
| 500 | end | ||
| 501 | |||
| 502 | should "return a correct url even if the file does not exist" do | ||
| 503 | assert_nil @attachment.to_file | ||
| 504 | assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah) | ||
| 505 | end | ||
| 506 | |||
| 507 | should "make sure the updated_at mtime is in the url if it is defined" do | ||
| 508 | assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah) | ||
| 509 | end | ||
| 510 | |||
| 511 | should "make sure the updated_at mtime is NOT in the url if false is passed to the url method" do | ||
| 512 | assert_no_match %r{#{Time.now.to_i}$}, @attachment.url(:blah, false) | ||
| 513 | end | ||
| 514 | |||
| 515 | context "with the updated_at field removed" do | ||
| 516 | setup do | ||
| 517 | @attachment.stubs(:instance_read).with(:updated_at).returns(nil) | ||
| 518 | end | ||
| 519 | |||
| 520 | should "only return the url without the updated_at when sent #url" do | ||
| 521 | assert_match "/avatars/#{@instance.id}/blah/5k.png", @attachment.url(:blah) | ||
| 522 | end | ||
| 523 | end | ||
| 524 | |||
| 525 | should "return the proper path when filename has a single .'s" do | ||
| 526 | assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.png", @attachment.path | ||
| 527 | end | ||
| 528 | |||
| 529 | should "return the proper path when filename has multiple .'s" do | ||
| 530 | @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png") | ||
| 531 | assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path | ||
| 532 | end | ||
| 533 | |||
| 534 | context "when expecting three styles" do | ||
| 535 | setup do | ||
| 536 | styles = {:styles => { :large => ["400x400", :png], | ||
| 537 | :medium => ["100x100", :gif], | ||
| 538 | :small => ["32x32#", :jpg]}} | ||
| 539 | @attachment = Paperclip::Attachment.new(:avatar, | ||
| 540 | @instance, | ||
| 541 | styles) | ||
| 542 | end | ||
| 543 | |||
| 544 | context "and assigned a file" do | ||
| 545 | setup do | ||
| 546 | now = Time.now | ||
| 547 | Time.stubs(:now).returns(now) | ||
| 548 | @attachment.assign(@file) | ||
| 549 | end | ||
| 550 | |||
| 551 | should "be dirty" do | ||
| 552 | assert @attachment.dirty? | ||
| 553 | end | ||
| 554 | |||
| 555 | context "and saved" do | ||
| 556 | setup do | ||
| 557 | @attachment.save | ||
| 558 | end | ||
| 559 | |||
| 560 | should "return the real url" do | ||
| 561 | file = @attachment.to_file | ||
| 562 | assert file | ||
| 563 | assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url | ||
| 564 | assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small) | ||
| 565 | file.close | ||
| 566 | end | ||
| 567 | |||
| 568 | should "commit the files to disk" do | ||
| 569 | [:large, :medium, :small].each do |style| | ||
| 570 | io = @attachment.to_io(style) | ||
| 571 | assert File.exists?(io) | ||
| 572 | assert ! io.is_a?(::Tempfile) | ||
| 573 | io.close | ||
| 574 | end | ||
| 575 | end | ||
| 576 | |||
| 577 | should "save the files as the right formats and sizes" do | ||
| 578 | [[:large, 400, 61, "PNG"], | ||
| 579 | [:medium, 100, 15, "GIF"], | ||
| 580 | [:small, 32, 32, "JPEG"]].each do |style| | ||
| 581 | cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"] | ||
| 582 | out = `#{cmd}` | ||
| 583 | width, height, size, format = out.split(" ") | ||
| 584 | assert_equal style[1].to_s, width.to_s | ||
| 585 | assert_equal style[2].to_s, height.to_s | ||
| 586 | assert_equal style[3].to_s, format.to_s | ||
| 587 | end | ||
| 588 | end | ||
| 589 | |||
| 590 | should "still have its #file attribute not be nil" do | ||
| 591 | assert ! (file = @attachment.to_file).nil? | ||
| 592 | file.close | ||
| 593 | end | ||
| 594 | |||
| 595 | context "and trying to delete" do | ||
| 596 | setup do | ||
| 597 | @existing_names = @attachment.styles.keys.collect do |style| | ||
| 598 | @attachment.path(style) | ||
| 599 | end | ||
| 600 | end | ||
| 601 | |||
| 602 | should "delete the files after assigning nil" do | ||
| 603 | @attachment.expects(:instance_write).with(:file_name, nil) | ||
| 604 | @attachment.expects(:instance_write).with(:content_type, nil) | ||
| 605 | @attachment.expects(:instance_write).with(:file_size, nil) | ||
| 606 | @attachment.expects(:instance_write).with(:updated_at, nil) | ||
| 607 | @attachment.assign nil | ||
| 608 | @attachment.save | ||
| 609 | @existing_names.each{|f| assert ! File.exists?(f) } | ||
| 610 | end | ||
| 611 | |||
| 612 | should "delete the files when you call #clear and #save" do | ||
| 613 | @attachment.expects(:instance_write).with(:file_name, nil) | ||
| 614 | @attachment.expects(:instance_write).with(:content_type, nil) | ||
| 615 | @attachment.expects(:instance_write).with(:file_size, nil) | ||
| 616 | @attachment.expects(:instance_write).with(:updated_at, nil) | ||
| 617 | @attachment.clear | ||
| 618 | @attachment.save | ||
| 619 | @existing_names.each{|f| assert ! File.exists?(f) } | ||
| 620 | end | ||
| 621 | |||
| 622 | should "delete the files when you call #delete" do | ||
| 623 | @attachment.expects(:instance_write).with(:file_name, nil) | ||
| 624 | @attachment.expects(:instance_write).with(:content_type, nil) | ||
| 625 | @attachment.expects(:instance_write).with(:file_size, nil) | ||
| 626 | @attachment.expects(:instance_write).with(:updated_at, nil) | ||
| 627 | @attachment.destroy | ||
| 628 | @existing_names.each{|f| assert ! File.exists?(f) } | ||
| 629 | end | ||
| 630 | end | ||
| 631 | end | ||
| 632 | end | ||
| 633 | end | ||
| 634 | |||
| 635 | end | ||
| 636 | |||
| 637 | context "when trying a nonexistant storage type" do | ||
| 638 | setup do | ||
| 639 | rebuild_model :storage => :not_here | ||
| 640 | end | ||
| 641 | |||
| 642 | should "not be able to find the module" do | ||
| 643 | assert_raise(NameError){ Dummy.new.avatar } | ||
| 644 | end | ||
| 645 | end | ||
| 646 | end | ||
| 647 | |||
| 648 | context "An attachment with only a avatar_file_name column" do | ||
| 649 | setup do | ||
| 650 | ActiveRecord::Base.connection.create_table :dummies, :force => true do |table| | ||
| 651 | table.column :avatar_file_name, :string | ||
| 652 | end | ||
| 653 | rebuild_class | ||
| 654 | @dummy = Dummy.new | ||
| 655 | @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') | ||
| 656 | end | ||
| 657 | |||
| 658 | teardown { @file.close } | ||
| 659 | |||
| 660 | should "not error when assigned an attachment" do | ||
| 661 | assert_nothing_raised { @dummy.avatar = @file } | ||
| 662 | end | ||
| 663 | |||
| 664 | should "return the time when sent #avatar_updated_at" do | ||
| 665 | now = Time.now | ||
| 666 | Time.stubs(:now).returns(now) | ||
| 667 | @dummy.avatar = @file | ||
| 668 | assert now, @dummy.avatar.updated_at | ||
| 669 | end | ||
| 670 | |||
| 671 | should "return nil when reloaded and sent #avatar_updated_at" do | ||
| 672 | @dummy.save | ||
| 673 | @dummy.reload | ||
| 674 | assert_nil @dummy.avatar.updated_at | ||
| 675 | end | ||
| 676 | |||
| 677 | should "return the right value when sent #avatar_file_size" do | ||
| 678 | @dummy.avatar = @file | ||
| 679 | assert_equal @file.size, @dummy.avatar.size | ||
| 680 | end | ||
| 681 | |||
| 682 | context "and avatar_updated_at column" do | ||
| 683 | setup do | ||
| 684 | ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp | ||
| 685 | rebuild_class | ||
| 686 | @dummy = Dummy.new | ||
| 687 | end | ||
| 688 | |||
| 689 | should "not error when assigned an attachment" do | ||
| 690 | assert_nothing_raised { @dummy.avatar = @file } | ||
| 691 | end | ||
| 692 | |||
| 693 | should "return the right value when sent #avatar_updated_at" do | ||
| 694 | now = Time.now | ||
| 695 | Time.stubs(:now).returns(now) | ||
| 696 | @dummy.avatar = @file | ||
| 697 | assert_equal now.to_i, @dummy.avatar.updated_at | ||
| 698 | end | ||
| 699 | end | ||
| 700 | |||
| 701 | context "and avatar_content_type column" do | ||
| 702 | setup do | ||
| 703 | ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string | ||
| 704 | rebuild_class | ||
| 705 | @dummy = Dummy.new | ||
| 706 | end | ||
| 707 | |||
| 708 | should "not error when assigned an attachment" do | ||
| 709 | assert_nothing_raised { @dummy.avatar = @file } | ||
| 710 | end | ||
| 711 | |||
| 712 | should "return the right value when sent #avatar_content_type" do | ||
| 713 | @dummy.avatar = @file | ||
| 714 | assert_equal "image/png", @dummy.avatar.content_type | ||
| 715 | end | ||
| 716 | end | ||
| 717 | |||
| 718 | context "and avatar_file_size column" do | ||
| 719 | setup do | ||
| 720 | ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer | ||
| 721 | rebuild_class | ||
| 722 | @dummy = Dummy.new | ||
| 723 | end | ||
| 724 | |||
| 725 | should "not error when assigned an attachment" do | ||
| 726 | assert_nothing_raised { @dummy.avatar = @file } | ||
| 727 | end | ||
| 728 | |||
| 729 | should "return the right value when sent #avatar_file_size" do | ||
| 730 | @dummy.avatar = @file | ||
| 731 | assert_equal @file.size, @dummy.avatar.size | ||
| 732 | end | ||
| 733 | |||
| 734 | should "return the right value when saved, reloaded, and sent #avatar_file_size" do | ||
| 735 | @dummy.avatar = @file | ||
| 736 | @dummy.save | ||
| 737 | @dummy = Dummy.find(@dummy.id) | ||
| 738 | assert_equal @file.size, @dummy.avatar.size | ||
| 739 | end | ||
| 740 | end | ||
| 741 | end | ||
| 742 | end | ||
diff --git a/vendor/plugins/paperclip/test/fixtures/12k.png b/vendor/plugins/paperclip/test/fixtures/12k.png new file mode 100644 index 0000000..f819d45 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/12k.png | |||
| Binary files differ | |||
diff --git a/vendor/plugins/paperclip/test/fixtures/50x50.png b/vendor/plugins/paperclip/test/fixtures/50x50.png new file mode 100644 index 0000000..63f5646 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/50x50.png | |||
| Binary files differ | |||
diff --git a/vendor/plugins/paperclip/test/fixtures/5k.png b/vendor/plugins/paperclip/test/fixtures/5k.png new file mode 100644 index 0000000..75d9f04 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/5k.png | |||
| Binary files differ | |||
diff --git a/vendor/plugins/paperclip/test/fixtures/bad.png b/vendor/plugins/paperclip/test/fixtures/bad.png new file mode 100644 index 0000000..7ba4f07 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/bad.png | |||
| @@ -0,0 +1 @@ | |||
| This is not an image. | |||
diff --git a/vendor/plugins/paperclip/test/fixtures/text.txt b/vendor/plugins/paperclip/test/fixtures/text.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/text.txt | |||
diff --git a/vendor/plugins/paperclip/test/fixtures/twopage.pdf b/vendor/plugins/paperclip/test/fixtures/twopage.pdf new file mode 100644 index 0000000..0c34a51 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/twopage.pdf | |||
| Binary files differ | |||
diff --git a/vendor/plugins/paperclip/test/geometry_test.rb b/vendor/plugins/paperclip/test/geometry_test.rb new file mode 100644 index 0000000..134372d --- /dev/null +++ b/vendor/plugins/paperclip/test/geometry_test.rb | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class GeometryTest < Test::Unit::TestCase | ||
| 4 | context "Paperclip::Geometry" do | ||
| 5 | should "correctly report its given dimensions" do | ||
| 6 | assert @geo = Paperclip::Geometry.new(1024, 768) | ||
| 7 | assert_equal 1024, @geo.width | ||
| 8 | assert_equal 768, @geo.height | ||
| 9 | end | ||
| 10 | |||
| 11 | should "set height to 0 if height dimension is missing" do | ||
| 12 | assert @geo = Paperclip::Geometry.new(1024) | ||
| 13 | assert_equal 1024, @geo.width | ||
| 14 | assert_equal 0, @geo.height | ||
| 15 | end | ||
| 16 | |||
| 17 | should "set width to 0 if width dimension is missing" do | ||
| 18 | assert @geo = Paperclip::Geometry.new(nil, 768) | ||
| 19 | assert_equal 0, @geo.width | ||
| 20 | assert_equal 768, @geo.height | ||
| 21 | end | ||
| 22 | |||
| 23 | should "be generated from a WxH-formatted string" do | ||
| 24 | assert @geo = Paperclip::Geometry.parse("800x600") | ||
| 25 | assert_equal 800, @geo.width | ||
| 26 | assert_equal 600, @geo.height | ||
| 27 | end | ||
| 28 | |||
| 29 | should "be generated from a xH-formatted string" do | ||
| 30 | assert @geo = Paperclip::Geometry.parse("x600") | ||
| 31 | assert_equal 0, @geo.width | ||
| 32 | assert_equal 600, @geo.height | ||
| 33 | end | ||
| 34 | |||
| 35 | should "be generated from a Wx-formatted string" do | ||
| 36 | assert @geo = Paperclip::Geometry.parse("800x") | ||
| 37 | assert_equal 800, @geo.width | ||
| 38 | assert_equal 0, @geo.height | ||
| 39 | end | ||
| 40 | |||
| 41 | should "be generated from a W-formatted string" do | ||
| 42 | assert @geo = Paperclip::Geometry.parse("800") | ||
| 43 | assert_equal 800, @geo.width | ||
| 44 | assert_equal 0, @geo.height | ||
| 45 | end | ||
| 46 | |||
| 47 | should "ensure the modifier is nil if not present" do | ||
| 48 | assert @geo = Paperclip::Geometry.parse("123x456") | ||
| 49 | assert_nil @geo.modifier | ||
| 50 | end | ||
| 51 | |||
| 52 | ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| | ||
| 53 | should "ensure the modifier #{mod.inspect} is preserved" do | ||
| 54 | assert @geo = Paperclip::Geometry.parse("123x456#{mod}") | ||
| 55 | assert_equal mod, @geo.modifier | ||
| 56 | assert_equal "123x456#{mod}", @geo.to_s | ||
| 57 | end | ||
| 58 | end | ||
| 59 | |||
| 60 | ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| | ||
| 61 | should "ensure the modifier #{mod.inspect} is preserved with no height" do | ||
| 62 | assert @geo = Paperclip::Geometry.parse("123x#{mod}") | ||
| 63 | assert_equal mod, @geo.modifier | ||
| 64 | assert_equal "123#{mod}", @geo.to_s | ||
| 65 | end | ||
| 66 | end | ||
| 67 | |||
| 68 | should "make sure the modifier gets passed during transformation_to" do | ||
| 69 | assert @src = Paperclip::Geometry.parse("123x456") | ||
| 70 | assert @dst = Paperclip::Geometry.parse("123x456>") | ||
| 71 | assert_equal "123x456>", @src.transformation_to(@dst).to_s | ||
| 72 | end | ||
| 73 | |||
| 74 | should "generate correct ImageMagick formatting string for W-formatted string" do | ||
| 75 | assert @geo = Paperclip::Geometry.parse("800") | ||
| 76 | assert_equal "800", @geo.to_s | ||
| 77 | end | ||
| 78 | |||
| 79 | should "generate correct ImageMagick formatting string for Wx-formatted string" do | ||
| 80 | assert @geo = Paperclip::Geometry.parse("800x") | ||
| 81 | assert_equal "800", @geo.to_s | ||
| 82 | end | ||
| 83 | |||
| 84 | should "generate correct ImageMagick formatting string for xH-formatted string" do | ||
| 85 | assert @geo = Paperclip::Geometry.parse("x600") | ||
| 86 | assert_equal "x600", @geo.to_s | ||
| 87 | end | ||
| 88 | |||
| 89 | should "generate correct ImageMagick formatting string for WxH-formatted string" do | ||
| 90 | assert @geo = Paperclip::Geometry.parse("800x600") | ||
| 91 | assert_equal "800x600", @geo.to_s | ||
| 92 | end | ||
| 93 | |||
| 94 | should "be generated from a file" do | ||
| 95 | file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") | ||
| 96 | file = File.new(file, 'rb') | ||
| 97 | assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } | ||
| 98 | assert @geo.height > 0 | ||
| 99 | assert @geo.width > 0 | ||
| 100 | end | ||
| 101 | |||
| 102 | should "be generated from a file path" do | ||
| 103 | file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") | ||
| 104 | assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } | ||
| 105 | assert @geo.height > 0 | ||
| 106 | assert @geo.width > 0 | ||
| 107 | end | ||
| 108 | |||
| 109 | should "not generate from a bad file" do | ||
| 110 | file = "/home/This File Does Not Exist.omg" | ||
| 111 | assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } | ||
| 112 | end | ||
| 113 | |||
| 114 | [['vertical', 900, 1440, true, false, false, 1440, 900, 0.625], | ||
| 115 | ['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333], | ||
| 116 | ['square', 100, 100, false, false, true, 100, 100, 1]].each do |args| | ||
| 117 | context "performing calculations on a #{args[0]} viewport" do | ||
| 118 | setup do | ||
| 119 | @geo = Paperclip::Geometry.new(args[1], args[2]) | ||
| 120 | end | ||
| 121 | |||
| 122 | should "#{args[3] ? "" : "not"} be vertical" do | ||
| 123 | assert_equal args[3], @geo.vertical? | ||
| 124 | end | ||
| 125 | |||
| 126 | should "#{args[4] ? "" : "not"} be horizontal" do | ||
| 127 | assert_equal args[4], @geo.horizontal? | ||
| 128 | end | ||
| 129 | |||
| 130 | should "#{args[5] ? "" : "not"} be square" do | ||
| 131 | assert_equal args[5], @geo.square? | ||
| 132 | end | ||
| 133 | |||
| 134 | should "report that #{args[6]} is the larger dimension" do | ||
| 135 | assert_equal args[6], @geo.larger | ||
| 136 | end | ||
| 137 | |||
| 138 | should "report that #{args[7]} is the smaller dimension" do | ||
| 139 | assert_equal args[7], @geo.smaller | ||
| 140 | end | ||
| 141 | |||
| 142 | should "have an aspect ratio of #{args[8]}" do | ||
| 143 | assert_in_delta args[8], @geo.aspect, 0.0001 | ||
| 144 | end | ||
| 145 | end | ||
| 146 | end | ||
| 147 | |||
| 148 | [[ [1000, 100], [64, 64], "x64", "64x64+288+0" ], | ||
| 149 | [ [100, 1000], [50, 950], "x950", "50x950+22+0" ], | ||
| 150 | [ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args| | ||
| 151 | context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do | ||
| 152 | setup do | ||
| 153 | @geo = Paperclip::Geometry.new(*args[0]) | ||
| 154 | @dst = Paperclip::Geometry.new(*args[1]) | ||
| 155 | @scale, @crop = @geo.transformation_to @dst, true | ||
| 156 | end | ||
| 157 | |||
| 158 | should "be able to return the correct scaling transformation geometry #{args[2]}" do | ||
| 159 | assert_equal args[2], @scale | ||
| 160 | end | ||
| 161 | |||
| 162 | should "be able to return the correct crop transformation geometry #{args[3]}" do | ||
| 163 | assert_equal args[3], @crop | ||
| 164 | end | ||
| 165 | end | ||
| 166 | end | ||
| 167 | end | ||
| 168 | end | ||
diff --git a/vendor/plugins/paperclip/test/helper.rb b/vendor/plugins/paperclip/test/helper.rb new file mode 100644 index 0000000..3e7e6a1 --- /dev/null +++ b/vendor/plugins/paperclip/test/helper.rb | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | require 'rubygems' | ||
| 2 | require 'test/unit' | ||
| 3 | gem 'thoughtbot-shoulda', ">= 2.9.0" | ||
| 4 | require 'shoulda' | ||
| 5 | require 'mocha' | ||
| 6 | require 'tempfile' | ||
| 7 | |||
| 8 | gem 'sqlite3-ruby' | ||
| 9 | |||
| 10 | require 'active_record' | ||
| 11 | require 'active_support' | ||
| 12 | begin | ||
| 13 | require 'ruby-debug' | ||
| 14 | rescue LoadError | ||
| 15 | puts "ruby-debug not loaded" | ||
| 16 | end | ||
| 17 | |||
| 18 | ROOT = File.join(File.dirname(__FILE__), '..') | ||
| 19 | RAILS_ROOT = ROOT | ||
| 20 | |||
| 21 | $LOAD_PATH << File.join(ROOT, 'lib') | ||
| 22 | $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip') | ||
| 23 | |||
| 24 | require File.join(ROOT, 'lib', 'paperclip.rb') | ||
| 25 | |||
| 26 | require 'shoulda_macros/paperclip' | ||
| 27 | |||
| 28 | ENV['RAILS_ENV'] ||= 'test' | ||
| 29 | |||
| 30 | FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures") | ||
| 31 | config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) | ||
| 32 | ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") | ||
| 33 | ActiveRecord::Base.establish_connection(config['test']) | ||
| 34 | |||
| 35 | def reset_class class_name | ||
| 36 | ActiveRecord::Base.send(:include, Paperclip) | ||
| 37 | Object.send(:remove_const, class_name) rescue nil | ||
| 38 | klass = Object.const_set(class_name, Class.new(ActiveRecord::Base)) | ||
| 39 | klass.class_eval{ include Paperclip } | ||
| 40 | klass | ||
| 41 | end | ||
| 42 | |||
| 43 | def reset_table table_name, &block | ||
| 44 | block ||= lambda{ true } | ||
| 45 | ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block | ||
| 46 | end | ||
| 47 | |||
| 48 | def modify_table table_name, &block | ||
| 49 | ActiveRecord::Base.connection.change_table :dummies, &block | ||
| 50 | end | ||
| 51 | |||
| 52 | def rebuild_model options = {} | ||
| 53 | ActiveRecord::Base.connection.create_table :dummies, :force => true do |table| | ||
| 54 | table.column :other, :string | ||
| 55 | table.column :avatar_file_name, :string | ||
| 56 | table.column :avatar_content_type, :string | ||
| 57 | table.column :avatar_file_size, :integer | ||
| 58 | table.column :avatar_updated_at, :datetime | ||
| 59 | end | ||
| 60 | rebuild_class options | ||
| 61 | end | ||
| 62 | |||
| 63 | def rebuild_class options = {} | ||
| 64 | ActiveRecord::Base.send(:include, Paperclip) | ||
| 65 | Object.send(:remove_const, "Dummy") rescue nil | ||
| 66 | Object.const_set("Dummy", Class.new(ActiveRecord::Base)) | ||
| 67 | Dummy.class_eval do | ||
| 68 | include Paperclip | ||
| 69 | has_attached_file :avatar, options | ||
| 70 | end | ||
| 71 | end | ||
| 72 | |||
| 73 | def temporary_rails_env(new_env) | ||
| 74 | old_env = defined?(RAILS_ENV) ? RAILS_ENV : nil | ||
| 75 | silence_warnings do | ||
| 76 | Object.const_set("RAILS_ENV", new_env) | ||
| 77 | end | ||
| 78 | yield | ||
| 79 | silence_warnings do | ||
| 80 | Object.const_set("RAILS_ENV", old_env) | ||
| 81 | end | ||
| 82 | end | ||
diff --git a/vendor/plugins/paperclip/test/integration_test.rb b/vendor/plugins/paperclip/test/integration_test.rb new file mode 100644 index 0000000..f7014ac --- /dev/null +++ b/vendor/plugins/paperclip/test/integration_test.rb | |||
| @@ -0,0 +1,481 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class IntegrationTest < Test::Unit::TestCase | ||
| 4 | context "Many models at once" do | ||
| 5 | setup do | ||
| 6 | rebuild_model | ||
| 7 | @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') | ||
| 8 | 300.times do |i| | ||
| 9 | Dummy.create! :avatar => @file | ||
| 10 | end | ||
| 11 | end | ||
| 12 | |||
| 13 | should "not exceed the open file limit" do | ||
| 14 | assert_nothing_raised do | ||
| 15 | dummies = Dummy.find(:all) | ||
| 16 | dummies.each { |dummy| dummy.avatar } | ||
| 17 | end | ||
| 18 | end | ||
| 19 | end | ||
| 20 | |||
| 21 | context "An attachment" do | ||
| 22 | setup do | ||
| 23 | rebuild_model :styles => { :thumb => "50x50#" } | ||
| 24 | @dummy = Dummy.new | ||
| 25 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 26 | "fixtures", | ||
| 27 | "5k.png"), 'rb') | ||
| 28 | @dummy.avatar = @file | ||
| 29 | assert @dummy.save | ||
| 30 | end | ||
| 31 | |||
| 32 | teardown { @file.close } | ||
| 33 | |||
| 34 | should "create its thumbnails properly" do | ||
| 35 | assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"` | ||
| 36 | end | ||
| 37 | |||
| 38 | context "redefining its attachment styles" do | ||
| 39 | setup do | ||
| 40 | Dummy.class_eval do | ||
| 41 | has_attached_file :avatar, :styles => { :thumb => "150x25#" } | ||
| 42 | has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } } | ||
| 43 | end | ||
| 44 | @d2 = Dummy.find(@dummy.id) | ||
| 45 | @d2.avatar.reprocess! | ||
| 46 | @d2.save | ||
| 47 | end | ||
| 48 | |||
| 49 | should "create its thumbnails properly" do | ||
| 50 | assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"` | ||
| 51 | assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"` | ||
| 52 | end | ||
| 53 | end | ||
| 54 | end | ||
| 55 | |||
| 56 | context "A model that modifies its original" do | ||
| 57 | setup do | ||
| 58 | rebuild_model :styles => { :original => "2x2#" } | ||
| 59 | @dummy = Dummy.new | ||
| 60 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 61 | "fixtures", | ||
| 62 | "5k.png"), 'rb') | ||
| 63 | @dummy.avatar = @file | ||
| 64 | end | ||
| 65 | |||
| 66 | should "report the file size of the processed file and not the original" do | ||
| 67 | assert_not_equal @file.size, @dummy.avatar.size | ||
| 68 | end | ||
| 69 | |||
| 70 | teardown { @file.close } | ||
| 71 | end | ||
| 72 | |||
| 73 | context "A model with attachments scoped under an id" do | ||
| 74 | setup do | ||
| 75 | rebuild_model :styles => { :large => "100x100", | ||
| 76 | :medium => "50x50" }, | ||
| 77 | :path => ":rails_root/tmp/:id/:attachments/:style.:extension" | ||
| 78 | @dummy = Dummy.new | ||
| 79 | @file = File.new(File.join(File.dirname(__FILE__), | ||
| 80 | "fixtures", | ||
| 81 | "5k.png"), 'rb') | ||
| 82 | @dummy.avatar = @file | ||
| 83 | end | ||
| 84 | |||
| 85 | teardown { @file.close } | ||
| 86 | |||
| 87 | context "when saved" do | ||
| 88 | setup do | ||
| 89 | @dummy.save | ||
| 90 | @saved_path = @dummy.avatar.path(:large) | ||
| 91 | end | ||
| 92 | |||
| 93 | should "have a large file in the right place" do | ||
| 94 | assert File.exists?(@dummy.avatar.path(:large)) | ||
| 95 | end | ||
| 96 | |||
| 97 | context "and deleted" do | ||
| 98 | setup do | ||
| 99 | @dummy.avatar.clear | ||
| 100 | @dummy.save | ||
| 101 | end | ||
| 102 | |||
| 103 | should "not have a large file in the right place anymore" do | ||
| 104 | assert ! File.exists?(@saved_path) | ||
| 105 | end | ||
| 106 | |||
| 107 | should "not have its next two parent directories" do | ||
| 108 | assert ! File.exists?(File.dirname(@saved_path)) | ||
| 109 | assert ! File.exists?(File.dirname(File.dirname(@saved_path))) | ||
| 110 | end | ||
| 111 | |||
| 112 | before_should "not die if an unexpected SystemCallError happens" do | ||
| 113 | FileUtils.stubs(:rmdir).raises(Errno::EPIPE) | ||
| 114 | end | ||
| 115 | end | ||
| 116 | end | ||
| 117 | end | ||
| 118 | |||
| 119 | context "A model with no attachment validation" do | ||
| 120 | setup do | ||
| 121 | rebuild_model :styles => { :large => "300x300>", | ||
| 122 | :medium => "100x100", | ||
| 123 | :thumb => ["32x32#", :gif] }, | ||
| 124 | :default_style => :medium, | ||
| 125 | :url => "/:attachment/:class/:style/:id/:basename.:extension", | ||
| 126 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 127 | @dummy = Dummy.new | ||
| 128 | end | ||
| 129 | |||
| 130 | should "have its definition return false when asked about whiny_thumbnails" do | ||
| 131 | assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails] | ||
| 132 | end | ||
| 133 | |||
| 134 | context "when validates_attachment_thumbnails is called" do | ||
| 135 | setup do | ||
| 136 | Dummy.validates_attachment_thumbnails :avatar | ||
| 137 | end | ||
| 138 | |||
| 139 | should "have its definition return true when asked about whiny_thumbnails" do | ||
| 140 | assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails] | ||
| 141 | end | ||
| 142 | end | ||
| 143 | |||
| 144 | context "redefined to have attachment validations" do | ||
| 145 | setup do | ||
| 146 | rebuild_model :styles => { :large => "300x300>", | ||
| 147 | :medium => "100x100", | ||
| 148 | :thumb => ["32x32#", :gif] }, | ||
| 149 | :whiny_thumbnails => true, | ||
| 150 | :default_style => :medium, | ||
| 151 | :url => "/:attachment/:class/:style/:id/:basename.:extension", | ||
| 152 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 153 | end | ||
| 154 | |||
| 155 | should "have its definition return true when asked about whiny_thumbnails" do | ||
| 156 | assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails] | ||
| 157 | end | ||
| 158 | end | ||
| 159 | end | ||
| 160 | |||
| 161 | context "A model with no convert_options setting" do | ||
| 162 | setup do | ||
| 163 | rebuild_model :styles => { :large => "300x300>", | ||
| 164 | :medium => "100x100", | ||
| 165 | :thumb => ["32x32#", :gif] }, | ||
| 166 | :default_style => :medium, | ||
| 167 | :url => "/:attachment/:class/:style/:id/:basename.:extension", | ||
| 168 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 169 | @dummy = Dummy.new | ||
| 170 | end | ||
| 171 | |||
| 172 | should "have its definition return nil when asked about convert_options" do | ||
| 173 | assert ! Dummy.attachment_definitions[:avatar][:convert_options] | ||
| 174 | end | ||
| 175 | |||
| 176 | context "redefined to have convert_options setting" do | ||
| 177 | setup do | ||
| 178 | rebuild_model :styles => { :large => "300x300>", | ||
| 179 | :medium => "100x100", | ||
| 180 | :thumb => ["32x32#", :gif] }, | ||
| 181 | :convert_options => "-strip -depth 8", | ||
| 182 | :default_style => :medium, | ||
| 183 | :url => "/:attachment/:class/:style/:id/:basename.:extension", | ||
| 184 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 185 | end | ||
| 186 | |||
| 187 | should "have its definition return convert_options value when asked about convert_options" do | ||
| 188 | assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:convert_options] | ||
| 189 | end | ||
| 190 | end | ||
| 191 | end | ||
| 192 | |||
| 193 | context "A model with a filesystem attachment" do | ||
| 194 | setup do | ||
| 195 | rebuild_model :styles => { :large => "300x300>", | ||
| 196 | :medium => "100x100", | ||
| 197 | :thumb => ["32x32#", :gif] }, | ||
| 198 | :whiny_thumbnails => true, | ||
| 199 | :default_style => :medium, | ||
| 200 | :url => "/:attachment/:class/:style/:id/:basename.:extension", | ||
| 201 | :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" | ||
| 202 | @dummy = Dummy.new | ||
| 203 | @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') | ||
| 204 | @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') | ||
| 205 | |||
| 206 | assert @dummy.avatar = @file | ||
| 207 | assert @dummy.valid? | ||
| 208 | assert @dummy.save | ||
| 209 | end | ||
| 210 | |||
| 211 | should "write and delete its files" do | ||
| 212 | [["434x66", :original], | ||
| 213 | ["300x46", :large], | ||
| 214 | ["100x15", :medium], | ||
| 215 | ["32x32", :thumb]].each do |geo, style| | ||
| 216 | cmd = %Q[identify -format "%wx%h" "#{@dummy.avatar.path(style)}"] | ||
| 217 | assert_equal geo, `#{cmd}`.chomp, cmd | ||
| 218 | end | ||
| 219 | |||
| 220 | saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) } | ||
| 221 | |||
| 222 | @d2 = Dummy.find(@dummy.id) | ||
| 223 | assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path}"`.chomp | ||
| 224 | assert_equal "434x66", `identify -format "%wx%h" "#{@d2.avatar.path(:original)}"`.chomp | ||
| 225 | assert_equal "300x46", `identify -format "%wx%h" "#{@d2.avatar.path(:large)}"`.chomp | ||
| 226 | assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path(:medium)}"`.chomp | ||
| 227 | assert_equal "32x32", `identify -format "%wx%h" "#{@d2.avatar.path(:thumb)}"`.chomp | ||
| 228 | |||
| 229 | @dummy.avatar = "not a valid file but not nil" | ||
| 230 | assert_equal File.basename(@file.path), @dummy.avatar_file_name | ||
| 231 | assert @dummy.valid? | ||
| 232 | assert @dummy.save | ||
| 233 | |||
| 234 | saved_paths.each do |p| | ||
| 235 | assert File.exists?(p) | ||
| 236 | end | ||
| 237 | |||
| 238 | @dummy.avatar.clear | ||
| 239 | assert_nil @dummy.avatar_file_name | ||
| 240 | assert @dummy.valid? | ||
| 241 | assert @dummy.save | ||
| 242 | |||
| 243 | saved_paths.each do |p| | ||
| 244 | assert ! File.exists?(p) | ||
| 245 | end | ||
| 246 | |||
| 247 | @d2 = Dummy.find(@dummy.id) | ||
| 248 | assert_nil @d2.avatar_file_name | ||
| 249 | end | ||
| 250 | |||
| 251 | should "work exactly the same when new as when reloaded" do | ||
| 252 | @d2 = Dummy.find(@dummy.id) | ||
| 253 | |||
| 254 | assert_equal @dummy.avatar_file_name, @d2.avatar_file_name | ||
| 255 | [:thumb, :medium, :large, :original].each do |style| | ||
| 256 | assert_equal @dummy.avatar.path(style), @d2.avatar.path(style) | ||
| 257 | end | ||
| 258 | |||
| 259 | saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) } | ||
| 260 | |||
| 261 | @d2.avatar.clear | ||
| 262 | assert @d2.save | ||
| 263 | |||
| 264 | saved_paths.each do |p| | ||
| 265 | assert ! File.exists?(p) | ||
| 266 | end | ||
| 267 | end | ||
| 268 | |||
| 269 | should "know the difference between good files, bad files, and not files" do | ||
| 270 | expected = @dummy.avatar.to_file | ||
| 271 | @dummy.avatar = "not a file" | ||
| 272 | assert @dummy.valid? | ||
| 273 | assert_equal expected.path, @dummy.avatar.path | ||
| 274 | expected.close | ||
| 275 | |||
| 276 | @dummy.avatar = @bad_file | ||
| 277 | assert ! @dummy.valid? | ||
| 278 | end | ||
| 279 | |||
| 280 | should "know the difference between good files, bad files, and not files when validating" do | ||
| 281 | Dummy.validates_attachment_presence :avatar | ||
| 282 | @d2 = Dummy.find(@dummy.id) | ||
| 283 | @d2.avatar = @file | ||
| 284 | assert @d2.valid?, @d2.errors.full_messages.inspect | ||
| 285 | @d2.avatar = @bad_file | ||
| 286 | assert ! @d2.valid? | ||
| 287 | end | ||
| 288 | |||
| 289 | should "be able to reload without saving and not have the file disappear" do | ||
| 290 | @dummy.avatar = @file | ||
| 291 | assert @dummy.save | ||
| 292 | @dummy.avatar.clear | ||
| 293 | assert_nil @dummy.avatar_file_name | ||
| 294 | @dummy.reload | ||
| 295 | assert_equal "5k.png", @dummy.avatar_file_name | ||
| 296 | end | ||
| 297 | |||
| 298 | context "that is assigned its file from another Paperclip attachment" do | ||
| 299 | setup do | ||
| 300 | @dummy2 = Dummy.new | ||
| 301 | @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"), 'rb') | ||
| 302 | assert @dummy2.avatar = @file2 | ||
| 303 | @dummy2.save | ||
| 304 | end | ||
| 305 | |||
| 306 | should "work when assigned a file" do | ||
| 307 | assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, | ||
| 308 | `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` | ||
| 309 | |||
| 310 | assert @dummy.avatar = @dummy2.avatar | ||
| 311 | @dummy.save | ||
| 312 | assert_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, | ||
| 313 | `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` | ||
| 314 | end | ||
| 315 | end | ||
| 316 | |||
| 317 | end | ||
| 318 | |||
| 319 | context "A model with an attachments association and a Paperclip attachment" do | ||
| 320 | setup do | ||
| 321 | Dummy.class_eval do | ||
| 322 | has_many :attachments, :class_name => 'Dummy' | ||
| 323 | end | ||
| 324 | |||
| 325 | @dummy = Dummy.new | ||
| 326 | @dummy.avatar = File.new(File.join(File.dirname(__FILE__), | ||
| 327 | "fixtures", | ||
| 328 | "5k.png"), 'rb') | ||
| 329 | end | ||
| 330 | |||
| 331 | should "should not error when saving" do | ||
| 332 | assert_nothing_raised do | ||
| 333 | @dummy.save! | ||
| 334 | end | ||
| 335 | end | ||
| 336 | end | ||
| 337 | |||
| 338 | if ENV['S3_TEST_BUCKET'] | ||
| 339 | def s3_files_for attachment | ||
| 340 | [:thumb, :medium, :large, :original].inject({}) do |files, style| | ||
| 341 | data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp | ||
| 342 | t = Tempfile.new("paperclip-test") | ||
| 343 | t.binmode | ||
| 344 | t.write(data) | ||
| 345 | t.rewind | ||
| 346 | files[style] = t | ||
| 347 | files | ||
| 348 | end | ||
| 349 | end | ||
| 350 | |||
| 351 | def s3_headers_for attachment, style | ||
| 352 | `curl --head "#{attachment.url(style)}" 2>/dev/null`.split("\n").inject({}) do |h,head| | ||
| 353 | split_head = head.chomp.split(/\s*:\s*/, 2) | ||
| 354 | h[split_head.first.downcase] = split_head.last unless split_head.empty? | ||
| 355 | h | ||
| 356 | end | ||
| 357 | end | ||
| 358 | |||
| 359 | context "A model with an S3 attachment" do | ||
| 360 | setup do | ||
| 361 | rebuild_model :styles => { :large => "300x300>", | ||
| 362 | :medium => "100x100", | ||
| 363 | :thumb => ["32x32#", :gif] }, | ||
| 364 | :storage => :s3, | ||
| 365 | :whiny_thumbnails => true, | ||
| 366 | # :s3_options => {:logger => Logger.new(StringIO.new)}, | ||
| 367 | :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")), | ||
| 368 | :default_style => :medium, | ||
| 369 | :bucket => ENV['S3_TEST_BUCKET'], | ||
| 370 | :path => ":class/:attachment/:id/:style/:basename.:extension" | ||
| 371 | @dummy = Dummy.new | ||
| 372 | @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') | ||
| 373 | @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') | ||
| 374 | |||
| 375 | assert @dummy.avatar = @file | ||
| 376 | assert @dummy.valid? | ||
| 377 | assert @dummy.save | ||
| 378 | |||
| 379 | @files_on_s3 = s3_files_for @dummy.avatar | ||
| 380 | end | ||
| 381 | |||
| 382 | should "write and delete its files" do | ||
| 383 | [["434x66", :original], | ||
| 384 | ["300x46", :large], | ||
| 385 | ["100x15", :medium], | ||
| 386 | ["32x32", :thumb]].each do |geo, style| | ||
| 387 | cmd = %Q[identify -format "%wx%h" "#{@files_on_s3[style].path}"] | ||
| 388 | assert_equal geo, `#{cmd}`.chomp, cmd | ||
| 389 | end | ||
| 390 | |||
| 391 | @d2 = Dummy.find(@dummy.id) | ||
| 392 | @d2_files = s3_files_for @d2.avatar | ||
| 393 | [["434x66", :original], | ||
| 394 | ["300x46", :large], | ||
| 395 | ["100x15", :medium], | ||
| 396 | ["32x32", :thumb]].each do |geo, style| | ||
| 397 | cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"] | ||
| 398 | assert_equal geo, `#{cmd}`.chomp, cmd | ||
| 399 | end | ||
| 400 | |||
| 401 | @dummy.avatar = "not a valid file but not nil" | ||
| 402 | assert_equal File.basename(@file.path), @dummy.avatar_file_name | ||
| 403 | assert @dummy.valid? | ||
| 404 | assert @dummy.save | ||
| 405 | |||
| 406 | saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) } | ||
| 407 | |||
| 408 | saved_keys.each do |key| | ||
| 409 | assert key.exists? | ||
| 410 | end | ||
| 411 | |||
| 412 | @dummy.avatar.clear | ||
| 413 | assert_nil @dummy.avatar_file_name | ||
| 414 | assert @dummy.valid? | ||
| 415 | assert @dummy.save | ||
| 416 | |||
| 417 | saved_keys.each do |key| | ||
| 418 | assert ! key.exists? | ||
| 419 | end | ||
| 420 | |||
| 421 | @d2 = Dummy.find(@dummy.id) | ||
| 422 | assert_nil @d2.avatar_file_name | ||
| 423 | end | ||
| 424 | |||
| 425 | should "work exactly the same when new as when reloaded" do | ||
| 426 | @d2 = Dummy.find(@dummy.id) | ||
| 427 | |||
| 428 | assert_equal @dummy.avatar_file_name, @d2.avatar_file_name | ||
| 429 | [:thumb, :medium, :large, :original].each do |style| | ||
| 430 | assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s | ||
| 431 | end | ||
| 432 | |||
| 433 | saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) } | ||
| 434 | |||
| 435 | @d2.avatar.clear | ||
| 436 | assert @d2.save | ||
| 437 | |||
| 438 | saved_keys.each do |key| | ||
| 439 | assert ! key.exists? | ||
| 440 | end | ||
| 441 | end | ||
| 442 | |||
| 443 | should "know the difference between good files, bad files, not files, and nil" do | ||
| 444 | expected = @dummy.avatar.to_file | ||
| 445 | @dummy.avatar = "not a file" | ||
| 446 | assert @dummy.valid? | ||
| 447 | assert_equal expected.full_name, @dummy.avatar.to_file.full_name | ||
| 448 | |||
| 449 | @dummy.avatar = @bad_file | ||
| 450 | assert ! @dummy.valid? | ||
| 451 | @dummy.avatar = nil | ||
| 452 | assert @dummy.valid? | ||
| 453 | |||
| 454 | Dummy.validates_attachment_presence :avatar | ||
| 455 | @d2 = Dummy.find(@dummy.id) | ||
| 456 | @d2.avatar = @file | ||
| 457 | assert @d2.valid? | ||
| 458 | @d2.avatar = @bad_file | ||
| 459 | assert ! @d2.valid? | ||
| 460 | @d2.avatar = nil | ||
| 461 | assert ! @d2.valid? | ||
| 462 | end | ||
| 463 | |||
| 464 | should "be able to reload without saving and not have the file disappear" do | ||
| 465 | @dummy.avatar = @file | ||
| 466 | assert @dummy.save | ||
| 467 | @dummy.avatar = nil | ||
| 468 | assert_nil @dummy.avatar_file_name | ||
| 469 | @dummy.reload | ||
| 470 | assert_equal "5k.png", @dummy.avatar_file_name | ||
| 471 | end | ||
| 472 | |||
| 473 | should "have the right content type" do | ||
| 474 | headers = s3_headers_for(@dummy.avatar, :original) | ||
| 475 | p headers | ||
| 476 | assert_equal 'image/png', headers['content-type'] | ||
| 477 | end | ||
| 478 | end | ||
| 479 | end | ||
| 480 | end | ||
| 481 | |||
diff --git a/vendor/plugins/paperclip/test/iostream_test.rb b/vendor/plugins/paperclip/test/iostream_test.rb new file mode 100644 index 0000000..97030b5 --- /dev/null +++ b/vendor/plugins/paperclip/test/iostream_test.rb | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class IOStreamTest < Test::Unit::TestCase | ||
| 4 | context "IOStream" do | ||
| 5 | should "be included in IO, File, Tempfile, and StringIO" do | ||
| 6 | [IO, File, Tempfile, StringIO].each do |klass| | ||
| 7 | assert klass.included_modules.include?(IOStream), "Not in #{klass}" | ||
| 8 | end | ||
| 9 | end | ||
| 10 | end | ||
| 11 | |||
| 12 | context "A file" do | ||
| 13 | setup do | ||
| 14 | @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') | ||
| 15 | end | ||
| 16 | |||
| 17 | teardown { @file.close } | ||
| 18 | |||
| 19 | context "that is sent #stream_to" do | ||
| 20 | |||
| 21 | context "and given a String" do | ||
| 22 | setup do | ||
| 23 | FileUtils.mkdir_p(File.join(ROOT, 'tmp')) | ||
| 24 | assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test')) | ||
| 25 | end | ||
| 26 | |||
| 27 | should "return a File" do | ||
| 28 | assert @result.is_a?(File) | ||
| 29 | end | ||
| 30 | |||
| 31 | should "contain the same data as the original file" do | ||
| 32 | @file.rewind; @result.rewind | ||
| 33 | assert_equal @file.read, @result.read | ||
| 34 | end | ||
| 35 | end | ||
| 36 | |||
| 37 | context "and given a Tempfile" do | ||
| 38 | setup do | ||
| 39 | tempfile = Tempfile.new('iostream.test') | ||
| 40 | tempfile.binmode | ||
| 41 | assert @result = @file.stream_to(tempfile) | ||
| 42 | end | ||
| 43 | |||
| 44 | should "return a Tempfile" do | ||
| 45 | assert @result.is_a?(Tempfile) | ||
| 46 | end | ||
| 47 | |||
| 48 | should "contain the same data as the original file" do | ||
| 49 | @file.rewind; @result.rewind | ||
| 50 | assert_equal @file.read, @result.read | ||
| 51 | end | ||
| 52 | end | ||
| 53 | |||
| 54 | end | ||
| 55 | |||
| 56 | context "that is sent #to_tempfile" do | ||
| 57 | setup do | ||
| 58 | assert @tempfile = @file.to_tempfile | ||
| 59 | end | ||
| 60 | |||
| 61 | should "convert it to a Tempfile" do | ||
| 62 | assert @tempfile.is_a?(Tempfile) | ||
| 63 | end | ||
| 64 | |||
| 65 | should "have the Tempfile contain the same data as the file" do | ||
| 66 | @file.rewind; @tempfile.rewind | ||
| 67 | assert_equal @file.read, @tempfile.read | ||
| 68 | end | ||
| 69 | end | ||
| 70 | end | ||
| 71 | end | ||
diff --git a/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb new file mode 100644 index 0000000..b29ec37 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class HaveAttachedFileMatcherTest < Test::Unit::TestCase | ||
| 4 | context "have_attached_file" do | ||
| 5 | setup do | ||
| 6 | @dummy_class = reset_class "Dummy" | ||
| 7 | reset_table "dummies" | ||
| 8 | @matcher = self.class.have_attached_file(:avatar) | ||
| 9 | end | ||
| 10 | |||
| 11 | should "reject a class with no attachment" do | ||
| 12 | assert_rejects @matcher, @dummy_class | ||
| 13 | end | ||
| 14 | |||
| 15 | should "accept a class with an attachment" do | ||
| 16 | modify_table("dummies"){|d| d.string :avatar_file_name } | ||
| 17 | @dummy_class.has_attached_file :avatar | ||
| 18 | assert_accepts @matcher, @dummy_class | ||
| 19 | end | ||
| 20 | end | ||
| 21 | end | ||
diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb new file mode 100644 index 0000000..241eb5f --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase | ||
| 4 | context "validate_attachment_content_type" do | ||
| 5 | setup do | ||
| 6 | reset_table("dummies") do |d| | ||
| 7 | d.string :avatar_file_name | ||
| 8 | end | ||
| 9 | @dummy_class = reset_class "Dummy" | ||
| 10 | @dummy_class.has_attached_file :avatar | ||
| 11 | @matcher = self.class.validate_attachment_content_type(:avatar). | ||
| 12 | allowing(%w(image/png image/jpeg)). | ||
| 13 | rejecting(%w(audio/mp3 application/octet-stream)) | ||
| 14 | end | ||
| 15 | |||
| 16 | should "reject a class with no validation" do | ||
| 17 | assert_rejects @matcher, @dummy_class | ||
| 18 | end | ||
| 19 | |||
| 20 | should "reject a class with a validation that doesn't match" do | ||
| 21 | @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*} | ||
| 22 | assert_rejects @matcher, @dummy_class | ||
| 23 | end | ||
| 24 | |||
| 25 | should "accept a class with a validation" do | ||
| 26 | @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*} | ||
| 27 | assert_accepts @matcher, @dummy_class | ||
| 28 | end | ||
| 29 | end | ||
| 30 | end | ||
diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb new file mode 100644 index 0000000..860a760 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase | ||
| 4 | context "validate_attachment_presence" do | ||
| 5 | setup do | ||
| 6 | reset_table("dummies"){|d| d.string :avatar_file_name } | ||
| 7 | @dummy_class = reset_class "Dummy" | ||
| 8 | @dummy_class.has_attached_file :avatar | ||
| 9 | @matcher = self.class.validate_attachment_presence(:avatar) | ||
| 10 | end | ||
| 11 | |||
| 12 | should "reject a class with no validation" do | ||
| 13 | assert_rejects @matcher, @dummy_class | ||
| 14 | end | ||
| 15 | |||
| 16 | should "accept a class with a validation" do | ||
| 17 | @dummy_class.validates_attachment_presence :avatar | ||
| 18 | assert_accepts @matcher, @dummy_class | ||
| 19 | end | ||
| 20 | end | ||
| 21 | end | ||
diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb new file mode 100644 index 0000000..7e4c9b4 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase | ||
| 4 | context "validate_attachment_size" do | ||
| 5 | setup do | ||
| 6 | reset_table("dummies") do |d| | ||
| 7 | d.string :avatar_file_name | ||
| 8 | end | ||
| 9 | @dummy_class = reset_class "Dummy" | ||
| 10 | @dummy_class.has_attached_file :avatar | ||
| 11 | end | ||
| 12 | |||
| 13 | context "of limited size" do | ||
| 14 | setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) } | ||
| 15 | |||
| 16 | should "reject a class with no validation" do | ||
| 17 | assert_rejects @matcher, @dummy_class | ||
| 18 | end | ||
| 19 | |||
| 20 | should "reject a class with a validation that's too high" do | ||
| 21 | @dummy_class.validates_attachment_size :avatar, :in => 256..2048 | ||
| 22 | assert_rejects @matcher, @dummy_class | ||
| 23 | end | ||
| 24 | |||
| 25 | should "reject a class with a validation that's too low" do | ||
| 26 | @dummy_class.validates_attachment_size :avatar, :in => 0..1024 | ||
| 27 | assert_rejects @matcher, @dummy_class | ||
| 28 | end | ||
| 29 | |||
| 30 | should "accept a class with a validation that matches" do | ||
| 31 | @dummy_class.validates_attachment_size :avatar, :in => 256..1024 | ||
| 32 | assert_accepts @matcher, @dummy_class | ||
| 33 | end | ||
| 34 | end | ||
| 35 | |||
| 36 | context "validates_attachment_size with infinite range" do | ||
| 37 | setup{ @matcher = self.class.validate_attachment_size(:avatar) } | ||
| 38 | |||
| 39 | should "accept a class with an upper limit" do | ||
| 40 | @dummy_class.validates_attachment_size :avatar, :less_than => 1 | ||
| 41 | assert_accepts @matcher, @dummy_class | ||
| 42 | end | ||
| 43 | |||
| 44 | should "accept a class with no upper limit" do | ||
| 45 | @dummy_class.validates_attachment_size :avatar, :greater_than => 1 | ||
| 46 | assert_accepts @matcher, @dummy_class | ||
| 47 | end | ||
| 48 | end | ||
| 49 | end | ||
| 50 | end | ||
diff --git a/vendor/plugins/paperclip/test/paperclip_test.rb b/vendor/plugins/paperclip/test/paperclip_test.rb new file mode 100644 index 0000000..8365649 --- /dev/null +++ b/vendor/plugins/paperclip/test/paperclip_test.rb | |||
| @@ -0,0 +1,233 @@ | |||
| 1 | require 'test/helper' | ||
| 2 | |||
| 3 | class PaperclipTest < Test::Unit::TestCase | ||
| 4 | [:image_magick_path, :convert_path].each do |path| | ||
| 5 | context "Calling Paperclip.run with an #{path} specified" do | ||
| 6 | setup do | ||
| 7 | Paperclip.options[:image_magick_path] = nil | ||
| 8 | Paperclip.options[:convert_path] = nil | ||
| 9 | Paperclip.options[path] = "/usr/bin" | ||
| 10 | end | ||
| 11 | |||
| 12 | should "execute the right command" do | ||
| 13 | Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert") | ||
| 14 | Paperclip.expects(:bit_bucket).returns("/dev/null") | ||
| 15 | Paperclip.expects(:"`").with("/usr/bin/convert one.jpg two.jpg 2>/dev/null") | ||
| 16 | Paperclip.run("convert", "one.jpg two.jpg") | ||
| 17 | end | ||
| 18 | end | ||
| 19 | end | ||
| 20 | |||
| 21 | context "Calling Paperclip.run with no path specified" do | ||
| 22 | setup do | ||
| 23 | Paperclip.options[:image_magick_path] = nil | ||
| 24 | Paperclip.options[:convert_path] = nil | ||
| 25 | end | ||
| 26 | |||
| 27 | should "execute the right command" do | ||
| 28 | Paperclip.expects(:path_for_command).with("convert").returns("convert") | ||
| 29 | Paperclip.expects(:bit_bucket).returns("/dev/null") | ||
| 30 | Paperclip.expects(:"`").with("convert one.jpg two.jpg 2>/dev/null") | ||
| 31 | Paperclip.run("convert", "one.jpg two.jpg") | ||
| 32 | end | ||
| 33 | end | ||
| 34 | |||
| 35 | should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do | ||
| 36 | assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) } | ||
| 37 | end | ||
| 38 | |||
| 39 | should "raise when sent #processor and the name of a class that doesn't exist" do | ||
| 40 | assert_raises(NameError){ Paperclip.processor(:boogey_man) } | ||
| 41 | end | ||
| 42 | |||
| 43 | should "return a class when sent #processor and the name of a class under Paperclip" do | ||
| 44 | assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail) | ||
| 45 | end | ||
| 46 | |||
| 47 | context "Paperclip.bit_bucket" do | ||
| 48 | context "on systems without /dev/null" do | ||
| 49 | setup do | ||
| 50 | File.expects(:exists?).with("/dev/null").returns(false) | ||
| 51 | end | ||
| 52 | |||
| 53 | should "return 'NUL'" do | ||
| 54 | assert_equal "NUL", Paperclip.bit_bucket | ||
| 55 | end | ||
| 56 | end | ||
| 57 | |||
| 58 | context "on systems with /dev/null" do | ||
| 59 | setup do | ||
| 60 | File.expects(:exists?).with("/dev/null").returns(true) | ||
| 61 | end | ||
| 62 | |||
| 63 | should "return '/dev/null'" do | ||
| 64 | assert_equal "/dev/null", Paperclip.bit_bucket | ||
| 65 | end | ||
| 66 | end | ||
| 67 | end | ||
| 68 | |||
| 69 | context "An ActiveRecord model with an 'avatar' attachment" do | ||
| 70 | setup do | ||
| 71 | rebuild_model :path => "tmp/:class/omg/:style.:extension" | ||
| 72 | @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') | ||
| 73 | end | ||
| 74 | |||
| 75 | teardown { @file.close } | ||
| 76 | |||
| 77 | should "not error when trying to also create a 'blah' attachment" do | ||
| 78 | assert_nothing_raised do | ||
| 79 | Dummy.class_eval do | ||
| 80 | has_attached_file :blah | ||
| 81 | end | ||
| 82 | end | ||
| 83 | end | ||
| 84 | |||
| 85 | context "that is attr_protected" do | ||
| 86 | setup do | ||
| 87 | Dummy.class_eval do | ||
| 88 | attr_protected :avatar | ||
| 89 | end | ||
| 90 | @dummy = Dummy.new | ||
| 91 | end | ||
| 92 | |||
| 93 | should "not assign the avatar on mass-set" do | ||
| 94 | @dummy.attributes = { :other => "I'm set!", | ||
| 95 | :avatar => @file } | ||
| 96 | |||
| 97 | assert_equal "I'm set!", @dummy.other | ||
| 98 | assert ! @dummy.avatar? | ||
| 99 | end | ||
| 100 | |||
| 101 | should "still allow assigment on normal set" do | ||
| 102 | @dummy.other = "I'm set!" | ||
| 103 | @dummy.avatar = @file | ||
| 104 | |||
| 105 | assert_equal "I'm set!", @dummy.other | ||
| 106 | assert @dummy.avatar? | ||
| 107 | end | ||
| 108 | end | ||
| 109 | |||
| 110 | context "with a subclass" do | ||
| 111 | setup do | ||
| 112 | class ::SubDummy < Dummy; end | ||
| 113 | end | ||
| 114 | |||
| 115 | should "be able to use the attachment from the subclass" do | ||
| 116 | assert_nothing_raised do | ||
| 117 | @subdummy = SubDummy.create(:avatar => @file) | ||
| 118 | end | ||
| 119 | end | ||
| 120 | |||
| 121 | should "be able to see the attachment definition from the subclass's class" do | ||
| 122 | assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path] | ||
| 123 | end | ||
| 124 | |||
| 125 | teardown do | ||
| 126 | Object.send(:remove_const, "SubDummy") rescue nil | ||
| 127 | end | ||
| 128 | end | ||
| 129 | |||
| 130 | should "have an #avatar method" do | ||
| 131 | assert Dummy.new.respond_to?(:avatar) | ||
| 132 | end | ||
| 133 | |||
| 134 | should "have an #avatar= method" do | ||
| 135 | assert Dummy.new.respond_to?(:avatar=) | ||
| 136 | end | ||
| 137 | |||
| 138 | context "that is valid" do | ||
| 139 | setup do | ||
| 140 | @dummy = Dummy.new | ||
| 141 | @dummy.avatar = @file | ||
| 142 | end | ||
| 143 | |||
| 144 | should "be valid" do | ||
| 145 | assert @dummy.valid? | ||
| 146 | end | ||
| 147 | |||
| 148 | context "then has a validation added that makes it invalid" do | ||
| 149 | setup do | ||
| 150 | assert @dummy.save | ||
| 151 | Dummy.class_eval do | ||
| 152 | validates_attachment_content_type :avatar, :content_type => ["text/plain"] | ||
| 153 | end | ||
| 154 | @dummy2 = Dummy.find(@dummy.id) | ||
| 155 | end | ||
| 156 | |||
| 157 | should "be invalid when reloaded" do | ||
| 158 | assert ! @dummy2.valid?, @dummy2.errors.inspect | ||
| 159 | end | ||
| 160 | |||
| 161 | should "be able to call #valid? twice without having duplicate errors" do | ||
| 162 | @dummy2.avatar.valid? | ||
| 163 | first_errors = @dummy2.avatar.errors | ||
| 164 | @dummy2.avatar.valid? | ||
| 165 | assert_equal first_errors, @dummy2.avatar.errors | ||
| 166 | end | ||
| 167 | end | ||
| 168 | end | ||
| 169 | |||
| 170 | def self.should_validate validation, options, valid_file, invalid_file | ||
| 171 | context "with #{validation} validation and #{options.inspect} options" do | ||
| 172 | setup do | ||
| 173 | Dummy.send(:"validates_attachment_#{validation}", :avatar, options) | ||
| 174 | @dummy = Dummy.new | ||
| 175 | end | ||
| 176 | context "and assigning nil" do | ||
| 177 | setup do | ||
| 178 | @dummy.avatar = nil | ||
| 179 | @dummy.valid? | ||
| 180 | end | ||
| 181 | if validation == :presence | ||
| 182 | should "have an error on the attachment" do | ||
| 183 | assert @dummy.errors.on(:avatar) | ||
| 184 | end | ||
| 185 | else | ||
| 186 | should "not have an error on the attachment" do | ||
| 187 | assert_nil @dummy.errors.on(:avatar) | ||
| 188 | end | ||
| 189 | end | ||
| 190 | end | ||
| 191 | context "and assigned a valid file" do | ||
| 192 | setup do | ||
| 193 | @dummy.avatar = valid_file | ||
| 194 | @dummy.valid? | ||
| 195 | end | ||
| 196 | should "not have an error when assigned a valid file" do | ||
| 197 | assert ! @dummy.avatar.errors.key?(validation) | ||
| 198 | end | ||
| 199 | should "not have an error on the attachment" do | ||
| 200 | assert_nil @dummy.errors.on(:avatar) | ||
| 201 | end | ||
| 202 | end | ||
| 203 | context "and assigned an invalid file" do | ||
| 204 | setup do | ||
| 205 | @dummy.avatar = invalid_file | ||
| 206 | @dummy.valid? | ||
| 207 | end | ||
| 208 | should "have an error when assigned a valid file" do | ||
| 209 | assert_not_nil @dummy.avatar.errors[validation] | ||
| 210 | end | ||
| 211 | should "have an error on the attachment" do | ||
| 212 | assert @dummy.errors.on(:avatar) | ||
| 213 | end | ||
| 214 | end | ||
| 215 | end | ||
| 216 | end | ||
| 217 | |||
| 218 | [[:presence, {}, "5k.png", nil], | ||
| 219 | [:size, {:in => 1..10240}, nil, "12k.png"], | ||
| 220 | [:size, {:less_than => 10240}, "5k.png", "12k.png"], | ||
| 221 | [:size, {:greater_than => 8096}, "12k.png", "5k.png"], | ||
| 222 | [:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"], | ||
| 223 | [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"], | ||
| 224 | [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args| | ||
| 225 | validation, options, valid_file, invalid_file = args | ||
| 226 | valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb") | ||
| 227 | invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb") | ||
| 228 | |||
| 229 | should_validate validation, options, valid_file, invalid_file | ||
| 230 | end | ||
| 231 | |||
| 232 | end | ||
| 233 | end | ||
diff --git a/vendor/plugins/paperclip/test/processor_test.rb b/vendor/plugins/paperclip/test/processor_test.rb new file mode 100644 index 0000000..a05f0a9 --- /dev/null +++ b/vendor/plugins/paperclip/test/processor_test.rb | |||
| @@ -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 | ||
diff --git a/vendor/plugins/paperclip/test/storage_test.rb b/vendor/plugins/paperclip/test/storage_test.rb new file mode 100644 index 0000000..e2ed8a4 --- /dev/null +++ b/vendor/plugins/paperclip/test/storage_test.rb | |||
| @@ -0,0 +1,277 @@ | |||
| 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 | |||
| 13 | @current_env = ENV['RAILS_ENV'] | ||
| 14 | end | ||
| 15 | |||
| 16 | teardown do | ||
| 17 | ENV['RAILS_ENV'] = @current_env | ||
| 18 | end | ||
| 19 | |||
| 20 | should "get the correct credentials when RAILS_ENV is production" do | ||
| 21 | ENV['RAILS_ENV'] = 'production' | ||
| 22 | assert_equal({:key => "12345"}, | ||
| 23 | @avatar.parse_credentials('production' => {:key => '12345'}, | ||
| 24 | :development => {:key => "54321"})) | ||
| 25 | end | ||
| 26 | |||
| 27 | should "get the correct credentials when RAILS_ENV is development" do | ||
| 28 | ENV['RAILS_ENV'] = 'development' | ||
| 29 | assert_equal({:key => "54321"}, | ||
| 30 | @avatar.parse_credentials('production' => {:key => '12345'}, | ||
| 31 | :development => {:key => "54321"})) | ||
| 32 | end | ||
| 33 | |||
| 34 | should "return the argument if the key does not exist" do | ||
| 35 | ENV['RAILS_ENV'] = "not really an env" | ||
| 36 | assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345")) | ||
| 37 | end | ||
| 38 | end | ||
| 39 | |||
| 40 | context "" do | ||
| 41 | setup do | ||
| 42 | rebuild_model :storage => :s3, | ||
| 43 | :s3_credentials => {}, | ||
| 44 | :bucket => "bucket", | ||
| 45 | :path => ":attachment/:basename.:extension", | ||
| 46 | :url => ":s3_path_url" | ||
| 47 | @dummy = Dummy.new | ||
| 48 | @dummy.avatar = StringIO.new(".") | ||
| 49 | end | ||
| 50 | |||
| 51 | should "return a url based on an S3 path" do | ||
| 52 | assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url | ||
| 53 | end | ||
| 54 | end | ||
| 55 | context "" do | ||
| 56 | setup do | ||
| 57 | rebuild_model :storage => :s3, | ||
| 58 | :s3_credentials => {}, | ||
| 59 | :bucket => "bucket", | ||
| 60 | :path => ":attachment/:basename.:extension", | ||
| 61 | :url => ":s3_domain_url" | ||
| 62 | @dummy = Dummy.new | ||
| 63 | @dummy.avatar = StringIO.new(".") | ||
| 64 | end | ||
| 65 | |||
| 66 | should "return a url based on an S3 subdomain" do | ||
| 67 | assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url | ||
| 68 | end | ||
| 69 | end | ||
| 70 | context "" do | ||
| 71 | setup do | ||
| 72 | rebuild_model :storage => :s3, | ||
| 73 | :s3_credentials => { | ||
| 74 | :production => { :bucket => "prod_bucket" }, | ||
| 75 | :development => { :bucket => "dev_bucket" } | ||
| 76 | }, | ||
| 77 | :s3_host_alias => "something.something.com", | ||
| 78 | :path => ":attachment/:basename.:extension", | ||
| 79 | :url => ":s3_alias_url" | ||
| 80 | @dummy = Dummy.new | ||
| 81 | @dummy.avatar = StringIO.new(".") | ||
| 82 | end | ||
| 83 | |||
| 84 | should "return a url based on the host_alias" do | ||
| 85 | assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url | ||
| 86 | end | ||
| 87 | end | ||
| 88 | |||
| 89 | context "Parsing S3 credentials with a bucket in them" do | ||
| 90 | setup do | ||
| 91 | rebuild_model :storage => :s3, | ||
| 92 | :s3_credentials => { | ||
| 93 | :production => { :bucket => "prod_bucket" }, | ||
| 94 | :development => { :bucket => "dev_bucket" } | ||
| 95 | } | ||
| 96 | @dummy = Dummy.new | ||
| 97 | end | ||
| 98 | |||
| 99 | should "get the right bucket in production", :before => lambda{ ENV.expects(:[]).returns('production') } do | ||
| 100 | assert_equal "prod_bucket", @dummy.avatar.bucket_name | ||
| 101 | end | ||
| 102 | |||
| 103 | should "get the right bucket in development", :before => lambda{ ENV.expects(:[]).returns('development') } do | ||
| 104 | assert_equal "dev_bucket", @dummy.avatar.bucket_name | ||
| 105 | end | ||
| 106 | end | ||
| 107 | |||
| 108 | context "An attachment with S3 storage" do | ||
| 109 | setup do | ||
| 110 | rebuild_model :storage => :s3, | ||
| 111 | :bucket => "testing", | ||
| 112 | :path => ":attachment/:style/:basename.:extension", | ||
| 113 | :s3_credentials => { | ||
| 114 | 'access_key_id' => "12345", | ||
| 115 | 'secret_access_key' => "54321" | ||
| 116 | } | ||
| 117 | end | ||
| 118 | |||
| 119 | should "be extended by the S3 module" do | ||
| 120 | assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) | ||
| 121 | end | ||
| 122 | |||
| 123 | should "not be extended by the Filesystem module" do | ||
| 124 | assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem) | ||
| 125 | end | ||
| 126 | |||
| 127 | context "when assigned" do | ||
| 128 | setup do | ||
| 129 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 130 | @dummy = Dummy.new | ||
| 131 | @dummy.avatar = @file | ||
| 132 | end | ||
| 133 | |||
| 134 | teardown { @file.close } | ||
| 135 | |||
| 136 | should "not get a bucket to get a URL" do | ||
| 137 | @dummy.avatar.expects(:s3).never | ||
| 138 | @dummy.avatar.expects(:s3_bucket).never | ||
| 139 | assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url | ||
| 140 | end | ||
| 141 | |||
| 142 | context "and saved" do | ||
| 143 | setup do | ||
| 144 | @s3_mock = stub | ||
| 145 | @bucket_mock = stub | ||
| 146 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 147 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 148 | @key_mock = stub | ||
| 149 | @bucket_mock.expects(:key).returns(@key_mock) | ||
| 150 | @key_mock.expects(:data=) | ||
| 151 | @key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png') | ||
| 152 | @dummy.save | ||
| 153 | end | ||
| 154 | |||
| 155 | should "succeed" do | ||
| 156 | assert true | ||
| 157 | end | ||
| 158 | end | ||
| 159 | |||
| 160 | context "and remove" do | ||
| 161 | setup do | ||
| 162 | @s3_mock = stub | ||
| 163 | @bucket_mock = stub | ||
| 164 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 165 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 166 | @key_mock = stub | ||
| 167 | @bucket_mock.expects(:key).at_least(2).returns(@key_mock) | ||
| 168 | @key_mock.expects(:delete) | ||
| 169 | @dummy.destroy_attached_files | ||
| 170 | end | ||
| 171 | |||
| 172 | should "succeed" do | ||
| 173 | assert true | ||
| 174 | end | ||
| 175 | end | ||
| 176 | end | ||
| 177 | end | ||
| 178 | |||
| 179 | context "An attachment with S3 storage and bucket defined as a Proc" do | ||
| 180 | setup do | ||
| 181 | rebuild_model :storage => :s3, | ||
| 182 | :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" }, | ||
| 183 | :s3_credentials => {:not => :important} | ||
| 184 | end | ||
| 185 | |||
| 186 | should "get the right bucket name" do | ||
| 187 | assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name | ||
| 188 | assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name | ||
| 189 | end | ||
| 190 | end | ||
| 191 | |||
| 192 | context "An attachment with S3 storage and specific s3 headers set" do | ||
| 193 | setup do | ||
| 194 | rebuild_model :storage => :s3, | ||
| 195 | :bucket => "testing", | ||
| 196 | :path => ":attachment/:style/:basename.:extension", | ||
| 197 | :s3_credentials => { | ||
| 198 | 'access_key_id' => "12345", | ||
| 199 | 'secret_access_key' => "54321" | ||
| 200 | }, | ||
| 201 | :s3_headers => {'Cache-Control' => 'max-age=31557600'} | ||
| 202 | end | ||
| 203 | |||
| 204 | context "when assigned" do | ||
| 205 | setup do | ||
| 206 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 207 | @dummy = Dummy.new | ||
| 208 | @dummy.avatar = @file | ||
| 209 | end | ||
| 210 | |||
| 211 | teardown { @file.close } | ||
| 212 | |||
| 213 | context "and saved" do | ||
| 214 | setup do | ||
| 215 | @s3_mock = stub | ||
| 216 | @bucket_mock = stub | ||
| 217 | RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) | ||
| 218 | @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) | ||
| 219 | @key_mock = stub | ||
| 220 | @bucket_mock.expects(:key).returns(@key_mock) | ||
| 221 | @key_mock.expects(:data=) | ||
| 222 | @key_mock.expects(:put).with(nil, | ||
| 223 | 'public-read', | ||
| 224 | 'Content-type' => 'image/png', | ||
| 225 | 'Cache-Control' => 'max-age=31557600') | ||
| 226 | @dummy.save | ||
| 227 | end | ||
| 228 | |||
| 229 | should "succeed" do | ||
| 230 | assert true | ||
| 231 | end | ||
| 232 | end | ||
| 233 | end | ||
| 234 | end | ||
| 235 | |||
| 236 | unless ENV["S3_TEST_BUCKET"].blank? | ||
| 237 | context "Using S3 for real, an attachment with S3 storage" do | ||
| 238 | setup do | ||
| 239 | rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" }, | ||
| 240 | :storage => :s3, | ||
| 241 | :bucket => ENV["S3_TEST_BUCKET"], | ||
| 242 | :path => ":class/:attachment/:id/:style.:extension", | ||
| 243 | :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")) | ||
| 244 | |||
| 245 | Dummy.delete_all | ||
| 246 | @dummy = Dummy.new | ||
| 247 | end | ||
| 248 | |||
| 249 | should "be extended by the S3 module" do | ||
| 250 | assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) | ||
| 251 | end | ||
| 252 | |||
| 253 | context "when assigned" do | ||
| 254 | setup do | ||
| 255 | @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | ||
| 256 | @dummy.avatar = @file | ||
| 257 | end | ||
| 258 | |||
| 259 | teardown { @file.close } | ||
| 260 | |||
| 261 | should "still return a Tempfile when sent #to_io" do | ||
| 262 | assert_equal Tempfile, @dummy.avatar.to_io.class | ||
| 263 | end | ||
| 264 | |||
| 265 | context "and saved" do | ||
| 266 | setup do | ||
| 267 | @dummy.save | ||
| 268 | end | ||
| 269 | |||
| 270 | should "be on S3" do | ||
| 271 | assert true | ||
| 272 | end | ||
| 273 | end | ||
| 274 | end | ||
| 275 | end | ||
| 276 | end | ||
| 277 | end | ||
diff --git a/vendor/plugins/paperclip/test/thumbnail_test.rb b/vendor/plugins/paperclip/test/thumbnail_test.rb new file mode 100644 index 0000000..624e7fa --- /dev/null +++ b/vendor/plugins/paperclip/test/thumbnail_test.rb | |||
| @@ -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 | ||
