cloudfuji_paperclip 2.4.6 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +9 -6
  3. data/Appraisals +6 -6
  4. data/CONTRIBUTING.md +34 -2
  5. data/Gemfile +2 -0
  6. data/NEWS +90 -0
  7. data/README.md +62 -29
  8. data/RUNNING_TESTS.md +4 -0
  9. data/Rakefile +7 -2
  10. data/UPGRADING +14 -0
  11. data/features/basic_integration.feature +11 -7
  12. data/features/rake_tasks.feature +1 -1
  13. data/features/step_definitions/attachment_steps.rb +11 -2
  14. data/features/step_definitions/rails_steps.rb +17 -79
  15. data/features/support/env.rb +3 -0
  16. data/features/support/fakeweb.rb +7 -0
  17. data/features/support/file_helpers.rb +24 -0
  18. data/features/support/rails.rb +3 -3
  19. data/gemfiles/{rails3_1.gemfile → 3.0.gemfile} +3 -1
  20. data/gemfiles/{rails2.gemfile → 3.1.gemfile} +3 -1
  21. data/gemfiles/{rails3.gemfile → 3.2.gemfile} +3 -1
  22. data/images.rake +21 -0
  23. data/lib/cloudfuji_paperclip.rb +1 -0
  24. data/lib/generators/paperclip/paperclip_generator.rb +1 -2
  25. data/lib/paperclip.rb +54 -319
  26. data/lib/paperclip/attachment.rb +86 -107
  27. data/lib/paperclip/attachment_options.rb +9 -0
  28. data/lib/paperclip/callbacks.rb +30 -0
  29. data/lib/paperclip/errors.rb +27 -0
  30. data/lib/paperclip/geometry.rb +6 -4
  31. data/lib/paperclip/glue.rb +23 -0
  32. data/lib/paperclip/helpers.rb +71 -0
  33. data/lib/paperclip/instance_methods.rb +35 -0
  34. data/lib/paperclip/interpolations.rb +4 -4
  35. data/lib/paperclip/io_adapters/attachment_adapter.rb +69 -0
  36. data/lib/paperclip/io_adapters/file_adapter.rb +81 -0
  37. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  38. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  39. data/lib/paperclip/io_adapters/registry.rb +32 -0
  40. data/lib/paperclip/io_adapters/stringio_adapter.rb +64 -0
  41. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +63 -0
  42. data/lib/paperclip/locales/en.yml +17 -0
  43. data/lib/paperclip/logger.rb +21 -0
  44. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +5 -5
  45. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -7
  46. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -11
  47. data/lib/paperclip/missing_attachment_styles.rb +6 -9
  48. data/lib/paperclip/processor.rb +32 -17
  49. data/lib/paperclip/railtie.rb +13 -17
  50. data/lib/paperclip/storage/filesystem.rb +4 -13
  51. data/lib/paperclip/storage/fog.rb +33 -24
  52. data/lib/paperclip/storage/s3.rb +36 -28
  53. data/lib/paperclip/tempfile.rb +41 -0
  54. data/lib/paperclip/thumbnail.rb +2 -3
  55. data/lib/paperclip/validators.rb +45 -0
  56. data/lib/paperclip/validators/attachment_content_type_validator.rb +54 -0
  57. data/lib/paperclip/validators/attachment_presence_validator.rb +26 -0
  58. data/lib/paperclip/validators/attachment_size_validator.rb +102 -0
  59. data/lib/paperclip/version.rb +1 -1
  60. data/lib/tasks/paperclip.rake +4 -12
  61. data/paperclip.gemspec +15 -5
  62. data/test/adapter_registry_test.rb +32 -0
  63. data/test/attachment_adapter_test.rb +51 -0
  64. data/test/attachment_options_test.rb +27 -0
  65. data/test/attachment_test.rb +130 -46
  66. data/test/file_adapter_test.rb +88 -0
  67. data/test/generator_test.rb +78 -0
  68. data/test/geometry_test.rb +5 -5
  69. data/test/helper.rb +21 -22
  70. data/test/identity_adapter_test.rb +8 -0
  71. data/test/integration_test.rb +55 -102
  72. data/test/interpolations_test.rb +15 -5
  73. data/test/matchers/validate_attachment_content_type_matcher_test.rb +23 -0
  74. data/test/matchers/validate_attachment_presence_matcher_test.rb +21 -0
  75. data/test/matchers/validate_attachment_size_matcher_test.rb +37 -2
  76. data/test/nil_adapter_test.rb +25 -0
  77. data/test/paperclip_missing_attachment_styles_test.rb +16 -0
  78. data/test/paperclip_test.rb +34 -183
  79. data/test/storage/filesystem_test.rb +27 -27
  80. data/test/storage/fog_test.rb +68 -12
  81. data/test/storage/s3_live_test.rb +79 -38
  82. data/test/storage/s3_test.rb +204 -34
  83. data/test/stringio_adapter_test.rb +42 -0
  84. data/test/thumbnail_test.rb +29 -8
  85. data/test/uploaded_file_adapter_test.rb +98 -0
  86. data/test/url_generator_test.rb +8 -8
  87. data/test/validators/attachment_content_type_validator_test.rb +192 -0
  88. data/test/validators/attachment_presence_validator_test.rb +85 -0
  89. data/test/validators/attachment_size_validator_test.rb +207 -0
  90. data/test/validators_test.rb +25 -0
  91. metadata +166 -59
  92. data/generators/paperclip/USAGE +0 -5
  93. data/generators/paperclip/paperclip_generator.rb +0 -27
  94. data/generators/paperclip/templates/paperclip_migration.rb.erb +0 -19
  95. data/init.rb +0 -4
  96. data/lib/paperclip/callback_compatibility.rb +0 -61
  97. data/lib/paperclip/iostream.rb +0 -45
  98. data/lib/paperclip/upfile.rb +0 -62
  99. data/rails/init.rb +0 -2
  100. data/test/.gitignore +0 -1
  101. data/test/fixtures/question?mark.png +0 -0
  102. data/test/iostream_test.rb +0 -71
  103. data/test/upfile_test.rb +0 -53
@@ -0,0 +1,27 @@
1
+ require './test/helper'
2
+
3
+ class AttachmentOptionsTest < Test::Unit::TestCase
4
+ should "be a Hash" do
5
+ assert_kind_of Hash, Paperclip::AttachmentOptions.new({})
6
+ end
7
+
8
+ should "respond to []" do
9
+ assert Paperclip::AttachmentOptions.new({}).respond_to?(:[])
10
+ end
11
+
12
+ should "deliver the specified options through []" do
13
+ intended_options = {:specific_key => "specific value"}
14
+ attachment_options = Paperclip::AttachmentOptions.new(intended_options)
15
+ assert_equal "specific value", attachment_options[:specific_key]
16
+ end
17
+
18
+ should "respond to []=" do
19
+ assert Paperclip::AttachmentOptions.new({}).respond_to?(:[]=)
20
+ end
21
+
22
+ should "remember options set with []=" do
23
+ attachment_options = Paperclip::AttachmentOptions.new({})
24
+ attachment_options[:foo] = "bar"
25
+ assert_equal "bar", attachment_options[:foo]
26
+ end
27
+ end
@@ -5,6 +5,40 @@ require 'paperclip/attachment'
5
5
  class Dummy; end
6
6
 
7
7
  class AttachmentTest < Test::Unit::TestCase
8
+
9
+ should "process :original style first" do
10
+ file = File.new(File.join(File.dirname(__FILE__), "fixtures", "50x50.png"), 'rb')
11
+ rebuild_class :styles => { :small => '100x>', :original => '42x42#' }
12
+ dummy = Dummy.new
13
+ dummy.avatar = file
14
+ dummy.save
15
+
16
+ # :small avatar should be 42px wide (processed original), not 50px (preprocessed original)
17
+ assert_equal `identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip, "42"
18
+
19
+ file.close
20
+ end
21
+
22
+ should "not delete styles that don't get reprocessed" do
23
+ file = File.new(File.join(File.dirname(__FILE__), "fixtures", "50x50.png"), 'rb')
24
+ rebuild_class :styles => { :small => '100x>',
25
+ :large => '500x>',
26
+ :original => '42x42#' }
27
+ dummy = Dummy.new
28
+ dummy.avatar = file
29
+ dummy.save
30
+
31
+ assert File.exists?(dummy.avatar.path(:small))
32
+ assert File.exists?(dummy.avatar.path(:large))
33
+ assert File.exists?(dummy.avatar.path(:original))
34
+
35
+ dummy.avatar.reprocess!(:small)
36
+
37
+ assert File.exists?(dummy.avatar.path(:small))
38
+ assert File.exists?(dummy.avatar.path(:large))
39
+ assert File.exists?(dummy.avatar.path(:original))
40
+ end
41
+
8
42
  should "handle a boolean second argument to #url" do
9
43
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
10
44
  attachment = Paperclip::Attachment.new(:name, :instance, :url_generator => mock_url_generator_builder)
@@ -89,6 +123,15 @@ class AttachmentTest < Test::Unit::TestCase
89
123
  assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
90
124
  end
91
125
 
126
+ should "default to a path that scales" do
127
+ avatar_attachment = attachment
128
+ model = avatar_attachment.instance
129
+ model.id = 1234
130
+ model.avatar_file_name = "fake.jpg"
131
+ expected_path = "#{Rails.root}/public/system/fake_models/avatars/000/001/234/original/fake.jpg"
132
+ assert_equal expected_path, avatar_attachment.path
133
+ end
134
+
92
135
  context "Attachment default_options" do
93
136
  setup do
94
137
  rebuild_model
@@ -231,7 +274,7 @@ class AttachmentTest < Test::Unit::TestCase
231
274
 
232
275
  should "interpolate the hash data" do
233
276
  @attachment.expects(:interpolate).with(@attachment.options[:hash_data],anything).returns("interpolated_stuff")
234
- @attachment.hash
277
+ @attachment.hash_key
235
278
  end
236
279
 
237
280
  should "result in the correct interpolation" do
@@ -489,7 +532,7 @@ class AttachmentTest < Test::Unit::TestCase
489
532
  setup do
490
533
  rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true
491
534
  @dummy = Dummy.new
492
- Paperclip::Thumbnail.expects(:make).raises(Paperclip::PaperclipError, "cannot be processed.")
535
+ Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
493
536
  @file = StringIO.new("...")
494
537
  @file.stubs(:to_tempfile).returns(@file)
495
538
  @dummy.avatar = @file
@@ -556,7 +599,7 @@ class AttachmentTest < Test::Unit::TestCase
556
599
  should "convert underscored storage name to camelcase" do
557
600
  rebuild_model :storage => :not_here
558
601
  @dummy = Dummy.new
559
- exception = assert_raises(Paperclip::StorageMethodNotFound) do
602
+ exception = assert_raises(Paperclip::Errors::StorageMethodNotFound) do
560
603
  @dummy.avatar
561
604
  end
562
605
  assert exception.message.include?("NotHere")
@@ -565,7 +608,7 @@ class AttachmentTest < Test::Unit::TestCase
565
608
  should "raise an error if you try to include a storage module that doesn't exist" do
566
609
  rebuild_model :storage => :not_here
567
610
  @dummy = Dummy.new
568
- assert_raises(Paperclip::StorageMethodNotFound) do
611
+ assert_raises(Paperclip::Errors::StorageMethodNotFound) do
569
612
  @dummy.avatar
570
613
  end
571
614
  end
@@ -652,13 +695,10 @@ class AttachmentTest < Test::Unit::TestCase
652
695
  context "Assigning an attachment" do
653
696
  setup do
654
697
  rebuild_model :styles => { :something => "100x100#" }
655
- @file = StringIO.new(".")
698
+ @file = StringIO.new(".")
656
699
  @file.stubs(:original_filename).returns("5k.png\n\n")
657
700
  @file.stubs(:content_type).returns("image/png\n\n")
658
- @file.stubs(:to_tempfile).returns(@file)
659
701
  @dummy = Dummy.new
660
- Paperclip::Thumbnail.expects(:make).returns(@file)
661
- @attachment = @dummy.avatar
662
702
  @dummy.avatar = @file
663
703
  end
664
704
 
@@ -671,27 +711,31 @@ class AttachmentTest < Test::Unit::TestCase
671
711
  end
672
712
  end
673
713
 
714
+ context "Assigning an attachment" do
715
+ setup do
716
+ rebuild_model :styles => { :something => "100x100#" }
717
+ @file = StringIO.new(".")
718
+ @file.stubs(:original_filename).returns("5k.png\n\n")
719
+ @file.stubs(:content_type).returns(MIME::Type.new("image/png"))
720
+ @dummy = Dummy.new
721
+ @dummy.avatar = @file
722
+ end
723
+
724
+ should "make sure the content_type is a string" do
725
+ assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
726
+ end
727
+ end
728
+
674
729
  context "Attachment with strange letters" do
675
730
  setup do
676
731
  rebuild_model
677
732
 
678
- @not_file = mock("not_file")
679
- @tempfile = mock("tempfile")
680
- @not_file.stubs(:nil?).returns(false)
681
- @not_file.expects(:size).returns(10)
682
- @tempfile.expects(:size).returns(10)
683
- @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
684
- @not_file.expects(:content_type).returns("image/png\r\n")
733
+ @file = StringIO.new(".")
734
+ @file.stubs(:original_filename).returns("sheep_say_bæ.png\r\n")
735
+ @file.stubs(:content_type).returns("image/png\r\n")
685
736
 
686
737
  @dummy = Dummy.new
687
- @attachment = @dummy.avatar
688
- @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
689
- @attachment.expects(:queue_existing_for_delete)
690
- @attachment.expects(:post_process)
691
- @attachment.expects(:to_tempfile).returns(@tempfile)
692
- @attachment.expects(:generate_fingerprint).with(@tempfile).returns("12345")
693
- @attachment.expects(:generate_fingerprint).with(@not_file).returns("12345")
694
- @dummy.avatar = @not_file
738
+ @dummy.avatar = @file
695
739
  end
696
740
 
697
741
  should "not remove strange letters" do
@@ -699,6 +743,48 @@ class AttachmentTest < Test::Unit::TestCase
699
743
  end
700
744
  end
701
745
 
746
+ context "Attachment with reserved filename" do
747
+ setup do
748
+ rebuild_model
749
+ @file = Paperclip.io_adapters.for(StringIO.new("."))
750
+ end
751
+
752
+ context "with default configuration" do
753
+ "&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character|
754
+ context "with character #{character}" do
755
+ setup do
756
+ @file.original_filename = "file#{character}name.png"
757
+ @dummy = Dummy.new
758
+ @dummy.avatar = @file
759
+ end
760
+
761
+ should "convert special character into underscore" do
762
+ assert_equal "file_name.png", @dummy.avatar.original_filename
763
+ end
764
+ end
765
+ end
766
+ end
767
+
768
+ context "with specified regexp replacement" do
769
+ setup do
770
+ @old_defaults = Paperclip::Attachment.default_options.dup
771
+ Paperclip::Attachment.default_options.merge! :restricted_characters => /o/
772
+
773
+ @file.stubs(:original_filename).returns("goood.png")
774
+ @dummy = Dummy.new
775
+ @dummy.avatar = @file
776
+ end
777
+
778
+ teardown do
779
+ Paperclip::Attachment.default_options.merge! @old_defaults
780
+ end
781
+
782
+ should "match and convert that character" do
783
+ assert_equal "g___d.png", @dummy.avatar.original_filename
784
+ end
785
+ end
786
+ end
787
+
702
788
  context "Attachment with uppercase extension and a default style" do
703
789
  setup do
704
790
  @old_defaults = Paperclip::Attachment.default_options.dup
@@ -731,11 +817,8 @@ class AttachmentTest < Test::Unit::TestCase
731
817
  end
732
818
 
733
819
  should "should have matching to_s and url methods" do
734
- file = @attachment.to_file
735
- assert file
736
820
  assert_match @attachment.to_s, @attachment.url
737
821
  assert_match @attachment.to_s(:small), @attachment.url(:small)
738
- file.close
739
822
  end
740
823
  end
741
824
 
@@ -760,13 +843,12 @@ class AttachmentTest < Test::Unit::TestCase
760
843
 
761
844
  should "raise if there are not the correct columns when you try to assign" do
762
845
  @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
763
- assert_raises(Paperclip::PaperclipError) do
846
+ assert_raises(Paperclip::Error) do
764
847
  @other_attachment.assign(@file)
765
848
  end
766
849
  end
767
850
 
768
851
  should "return nil as path when no file assigned" do
769
- assert @attachment.to_file.nil?
770
852
  assert_equal nil, @attachment.path
771
853
  assert_equal nil, @attachment.path(:blah)
772
854
  end
@@ -821,10 +903,6 @@ class AttachmentTest < Test::Unit::TestCase
821
903
  assert @attachment.dirty?
822
904
  end
823
905
 
824
- should "set uploaded_file for access beyond the paperclip lifecycle" do
825
- assert_equal @file, @attachment.uploaded_file
826
- end
827
-
828
906
  context "and saved" do
829
907
  setup do
830
908
  @attachment.save
@@ -832,11 +910,7 @@ class AttachmentTest < Test::Unit::TestCase
832
910
 
833
911
  should "commit the files to disk" do
834
912
  [:large, :medium, :small].each do |style|
835
- io = @attachment.to_file(style)
836
- # p "in commit to disk test, io is #{io.inspect} and @instance.id is #{@instance.id}"
837
- assert File.exists?(io.path)
838
- assert ! io.is_a?(::Tempfile)
839
- io.close
913
+ assert File.exists?(@attachment.path(style))
840
914
  end
841
915
  end
842
916
 
@@ -853,11 +927,6 @@ class AttachmentTest < Test::Unit::TestCase
853
927
  end
854
928
  end
855
929
 
856
- should "still have its #file attribute not be nil" do
857
- assert ! (file = @attachment.to_file).nil?
858
- file.close
859
- end
860
-
861
930
  context "and trying to delete" do
862
931
  setup do
863
932
  @existing_names = @attachment.styles.keys.collect do |style|
@@ -869,6 +938,7 @@ class AttachmentTest < Test::Unit::TestCase
869
938
  @attachment.expects(:instance_write).with(:file_name, nil)
870
939
  @attachment.expects(:instance_write).with(:content_type, nil)
871
940
  @attachment.expects(:instance_write).with(:file_size, nil)
941
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
872
942
  @attachment.expects(:instance_write).with(:updated_at, nil)
873
943
  @attachment.assign nil
874
944
  @attachment.save
@@ -879,6 +949,7 @@ class AttachmentTest < Test::Unit::TestCase
879
949
  @attachment.expects(:instance_write).with(:file_name, nil)
880
950
  @attachment.expects(:instance_write).with(:content_type, nil)
881
951
  @attachment.expects(:instance_write).with(:file_size, nil)
952
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
882
953
  @attachment.expects(:instance_write).with(:updated_at, nil)
883
954
  @attachment.clear
884
955
  @attachment.save
@@ -889,6 +960,7 @@ class AttachmentTest < Test::Unit::TestCase
889
960
  @attachment.expects(:instance_write).with(:file_name, nil)
890
961
  @attachment.expects(:instance_write).with(:content_type, nil)
891
962
  @attachment.expects(:instance_write).with(:file_size, nil)
963
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
892
964
  @attachment.expects(:instance_write).with(:updated_at, nil)
893
965
  @attachment.destroy
894
966
  @existing_names.each{|f| assert ! File.exists?(f) }
@@ -903,6 +975,7 @@ class AttachmentTest < Test::Unit::TestCase
903
975
  @attachment.expects(:instance_write).with(:file_name, nil)
904
976
  @attachment.expects(:instance_write).with(:content_type, nil)
905
977
  @attachment.expects(:instance_write).with(:file_size, nil)
978
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
906
979
  @attachment.expects(:instance_write).with(:updated_at, nil)
907
980
  @attachment.assign nil
908
981
  @attachment.save
@@ -913,6 +986,7 @@ class AttachmentTest < Test::Unit::TestCase
913
986
  @attachment.expects(:instance_write).with(:file_name, nil)
914
987
  @attachment.expects(:instance_write).with(:content_type, nil)
915
988
  @attachment.expects(:instance_write).with(:file_size, nil)
989
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
916
990
  @attachment.expects(:instance_write).with(:updated_at, nil)
917
991
  @attachment.clear
918
992
  @attachment.save
@@ -923,6 +997,7 @@ class AttachmentTest < Test::Unit::TestCase
923
997
  @attachment.expects(:instance_write).with(:file_name, nil)
924
998
  @attachment.expects(:instance_write).with(:content_type, nil)
925
999
  @attachment.expects(:instance_write).with(:file_size, nil)
1000
+ @attachment.expects(:instance_write).with(:fingerprint, nil)
926
1001
  @attachment.expects(:instance_write).with(:updated_at, nil)
927
1002
  @attachment.destroy
928
1003
  @existing_names.each{|f| assert File.exists?(f) }
@@ -940,7 +1015,7 @@ class AttachmentTest < Test::Unit::TestCase
940
1015
  end
941
1016
 
942
1017
  should "not be able to find the module" do
943
- assert_raise(Paperclip::StorageMethodNotFound){ Dummy.new.avatar }
1018
+ assert_raise(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
944
1019
  end
945
1020
  end
946
1021
  end
@@ -976,7 +1051,7 @@ class AttachmentTest < Test::Unit::TestCase
976
1051
 
977
1052
  should "return the right value when sent #avatar_file_size" do
978
1053
  @dummy.avatar = @file
979
- assert_equal @file.size, @dummy.avatar.size
1054
+ assert_equal File.size(@file), @dummy.avatar.size
980
1055
  end
981
1056
 
982
1057
  context "and avatar_updated_at column" do
@@ -998,6 +1073,11 @@ class AttachmentTest < Test::Unit::TestCase
998
1073
  end
999
1074
  end
1000
1075
 
1076
+ should "not calculate fingerprint" do
1077
+ @dummy.avatar = @file
1078
+ assert_nil @dummy.avatar.fingerprint
1079
+ end
1080
+
1001
1081
  context "and avatar_content_type column" do
1002
1082
  setup do
1003
1083
  ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
@@ -1028,14 +1108,14 @@ class AttachmentTest < Test::Unit::TestCase
1028
1108
 
1029
1109
  should "return the right value when sent #avatar_file_size" do
1030
1110
  @dummy.avatar = @file
1031
- assert_equal @file.size, @dummy.avatar.size
1111
+ assert_equal File.size(@file), @dummy.avatar.size
1032
1112
  end
1033
1113
 
1034
1114
  should "return the right value when saved, reloaded, and sent #avatar_file_size" do
1035
1115
  @dummy.avatar = @file
1036
1116
  @dummy.save
1037
1117
  @dummy = Dummy.find(@dummy.id)
1038
- assert_equal @file.size, @dummy.avatar.size
1118
+ assert_equal File.size(@file), @dummy.avatar.size
1039
1119
  end
1040
1120
  end
1041
1121
 
@@ -1075,6 +1155,8 @@ class AttachmentTest < Test::Unit::TestCase
1075
1155
  @path = @attachment.path
1076
1156
  end
1077
1157
 
1158
+ teardown { @file.close }
1159
+
1078
1160
  should "not delete the files from storage when attachment is destroyed" do
1079
1161
  @attachment.destroy
1080
1162
  assert File.exists?(@path)
@@ -1097,6 +1179,8 @@ class AttachmentTest < Test::Unit::TestCase
1097
1179
  @path = @attachment.path
1098
1180
  end
1099
1181
 
1182
+ teardown { @file.close }
1183
+
1100
1184
  should "not be deleted when the model fails to destroy" do
1101
1185
  @dummy.stubs(:destroy).raises(Exception)
1102
1186
 
@@ -0,0 +1,88 @@
1
+ require './test/helper'
2
+
3
+ class FileAdapterTest < Test::Unit::TestCase
4
+ context "a new instance" do
5
+ context "with normal file" do
6
+ setup do
7
+ @file = File.new(fixture_file("5k.png"))
8
+ @file.binmode
9
+ @subject = Paperclip.io_adapters.for(@file)
10
+ end
11
+
12
+ teardown { @file.close }
13
+
14
+ should "get the right filename" do
15
+ assert_equal "5k.png", @subject.original_filename
16
+ end
17
+
18
+ should "force binmode on tempfile" do
19
+ assert @subject.instance_variable_get("@tempfile").binmode?
20
+ end
21
+
22
+ should "get the content type" do
23
+ assert_equal "image/png", @subject.content_type
24
+ end
25
+
26
+ should "return content type as a string" do
27
+ assert_kind_of String, @subject.content_type
28
+ end
29
+
30
+ should "get the file's size" do
31
+ assert_equal 4456, @subject.size
32
+ end
33
+
34
+ should "return false for a call to nil?" do
35
+ assert ! @subject.nil?
36
+ end
37
+
38
+ should "generate a MD5 hash of the contents" do
39
+ expected = Digest::MD5.file(@file.path).to_s
40
+ assert_equal expected, @subject.fingerprint
41
+ end
42
+
43
+ should "read the contents of the file" do
44
+ expected = @file.read
45
+ assert expected.length > 0
46
+ assert_equal expected, @subject.read
47
+ end
48
+
49
+ context "file with multiple possible content type" do
50
+ setup do
51
+ MIME::Types.stubs(:type_for).returns([MIME::Type.new('image/x-png'), MIME::Type.new('image/png')])
52
+ end
53
+
54
+ should "prefer officially registered mime type" do
55
+ assert_equal "image/png", @subject.content_type
56
+ end
57
+
58
+ should "return content type as a string" do
59
+ assert_kind_of String, @subject.content_type
60
+ end
61
+ end
62
+
63
+ context "file with content type derived from file command on *nix" do
64
+ setup do
65
+ MIME::Types.stubs(:type_for).returns([])
66
+ Paperclip.stubs(:run).returns("application/vnd.ms-office\n")
67
+ end
68
+
69
+ should "return content type without newline character" do
70
+ assert_equal "application/vnd.ms-office", @subject.content_type
71
+ end
72
+ end
73
+ end
74
+
75
+ context "empty file" do
76
+ setup do
77
+ @file = Tempfile.new("file_adapter_test")
78
+ @subject = Paperclip.io_adapters.for(@file)
79
+ end
80
+
81
+ teardown { @file.close }
82
+
83
+ should "provide correct mime-type" do
84
+ assert_equal "application/x-empty", @subject.content_type
85
+ end
86
+ end
87
+ end
88
+ end