thoughtbot-paperclip 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
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
+ context "that is sent #stream_to" do
18
+
19
+ context "and given a String" do
20
+ setup do
21
+ assert @result = @file.stream_to("/tmp/iostream.string.test")
22
+ end
23
+
24
+ should "return a File" do
25
+ assert @result.is_a?(File)
26
+ end
27
+
28
+ should "contain the same data as the original file" do
29
+ @file.rewind; @result.rewind
30
+ assert_equal @file.read, @result.read
31
+ end
32
+ end
33
+
34
+ context "and given a Tempfile" do
35
+ setup do
36
+ tempfile = Tempfile.new('iostream.test')
37
+ tempfile.binmode
38
+ assert @result = @file.stream_to(tempfile)
39
+ end
40
+
41
+ should "return a Tempfile" do
42
+ assert @result.is_a?(Tempfile)
43
+ end
44
+
45
+ should "contain the same data as the original file" do
46
+ @file.rewind; @result.rewind
47
+ assert_equal @file.read, @result.read
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ context "that is sent #to_tempfile" do
54
+ setup do
55
+ assert @tempfile = @file.to_tempfile
56
+ end
57
+
58
+ should "convert it to a Tempfile" do
59
+ assert @tempfile.is_a?(Tempfile)
60
+ end
61
+
62
+ should "have the Tempfile contain the same data as the file" do
63
+ @file.rewind; @tempfile.rewind
64
+ assert_equal @file.read, @tempfile.read
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,186 @@
1
+ require 'test/helper'
2
+
3
+ class PaperclipTest < Test::Unit::TestCase
4
+ context "Calling Paperclip.run" do
5
+ should "execute the right command" do
6
+ Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert")
7
+ Paperclip.expects(:bit_bucket).returns("/dev/null")
8
+ Paperclip.expects(:"`").with("/usr/bin/convert one.jpg two.jpg 2>/dev/null")
9
+ Paperclip.run("convert", "one.jpg two.jpg")
10
+ end
11
+ end
12
+
13
+ context "Paperclip.bit_bucket" do
14
+ context "on systems without /dev/null" do
15
+ setup do
16
+ File.expects(:exists?).with("/dev/null").returns(false)
17
+ end
18
+
19
+ should "return 'NUL'" do
20
+ assert_equal "NUL", Paperclip.bit_bucket
21
+ end
22
+ end
23
+
24
+ context "on systems with /dev/null" do
25
+ setup do
26
+ File.expects(:exists?).with("/dev/null").returns(true)
27
+ end
28
+
29
+ should "return '/dev/null'" do
30
+ assert_equal "/dev/null", Paperclip.bit_bucket
31
+ end
32
+ end
33
+ end
34
+
35
+ context "An ActiveRecord model with an 'avatar' attachment" do
36
+ setup do
37
+ rebuild_model :path => "tmp/:class/omg/:style.:extension"
38
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
39
+ end
40
+
41
+ should "not error when trying to also create a 'blah' attachment" do
42
+ assert_nothing_raised do
43
+ Dummy.class_eval do
44
+ has_attached_file :blah
45
+ end
46
+ end
47
+ end
48
+
49
+ context "that is attr_protected" do
50
+ setup do
51
+ Dummy.class_eval do
52
+ attr_protected :avatar
53
+ end
54
+ @dummy = Dummy.new
55
+ end
56
+
57
+ should "not assign the avatar on mass-set" do
58
+ @dummy.logger.expects(:debug)
59
+
60
+ @dummy.attributes = { :other => "I'm set!",
61
+ :avatar => @file }
62
+
63
+ assert_equal "I'm set!", @dummy.other
64
+ assert ! @dummy.avatar?
65
+ end
66
+
67
+ should "still allow assigment on normal set" do
68
+ @dummy.logger.expects(:debug).times(0)
69
+
70
+ @dummy.other = "I'm set!"
71
+ @dummy.avatar = @file
72
+
73
+ assert_equal "I'm set!", @dummy.other
74
+ assert @dummy.avatar?
75
+ end
76
+ end
77
+
78
+ context "with a subclass" do
79
+ setup do
80
+ class ::SubDummy < Dummy; end
81
+ end
82
+
83
+ should "be able to use the attachment from the subclass" do
84
+ assert_nothing_raised do
85
+ @subdummy = SubDummy.create(:avatar => @file)
86
+ end
87
+ end
88
+
89
+ should "be able to see the attachment definition from the subclass's class" do
90
+ assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
91
+ end
92
+
93
+ teardown do
94
+ Object.send(:remove_const, "SubDummy") rescue nil
95
+ end
96
+ end
97
+
98
+ should "have an #avatar method" do
99
+ assert Dummy.new.respond_to?(:avatar)
100
+ end
101
+
102
+ should "have an #avatar= method" do
103
+ assert Dummy.new.respond_to?(:avatar=)
104
+ end
105
+
106
+ context "that is valid" do
107
+ setup do
108
+ @dummy = Dummy.new
109
+ @dummy.avatar = @file
110
+ end
111
+
112
+ should "be valid" do
113
+ assert @dummy.valid?
114
+ end
115
+
116
+ context "then has a validation added that makes it invalid" do
117
+ setup do
118
+ assert @dummy.save
119
+ Dummy.class_eval do
120
+ validates_attachment_content_type :avatar, :content_type => ["text/plain"]
121
+ end
122
+ @dummy2 = Dummy.find(@dummy.id)
123
+ end
124
+
125
+ should "be invalid when reloaded" do
126
+ assert ! @dummy2.valid?, @dummy2.errors.inspect
127
+ end
128
+
129
+ should "be able to call #valid? twice without having duplicate errors" do
130
+ @dummy2.avatar.valid?
131
+ first_errors = @dummy2.avatar.errors
132
+ @dummy2.avatar.valid?
133
+ assert_equal first_errors, @dummy2.avatar.errors
134
+ end
135
+ end
136
+ end
137
+
138
+ def self.should_validate validation, options, valid_file, invalid_file
139
+ context "with #{validation} validation and #{options.inspect} options" do
140
+ setup do
141
+ Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
142
+ @dummy = Dummy.new
143
+ end
144
+ context "and assigned a valid file" do
145
+ setup do
146
+ @dummy.avatar = valid_file
147
+ @dummy.valid?
148
+ end
149
+ should "not have an error when assigned a valid file" do
150
+ assert ! @dummy.avatar.errors.key?(validation)
151
+ end
152
+ should "not have an error on the attachment" do
153
+ assert_nil @dummy.errors.on(:avatar)
154
+ end
155
+ end
156
+ context "and assigned an invalid file" do
157
+ setup do
158
+ @dummy.avatar = invalid_file
159
+ @dummy.valid?
160
+ end
161
+ should "have an error when assigned a valid file" do
162
+ assert_not_nil @dummy.avatar.errors[validation]
163
+ end
164
+ should "have an error on the attachment" do
165
+ assert @dummy.errors.on(:avatar)
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ [[:presence, {}, "5k.png", nil],
172
+ [:size, {:in => 1..10240}, nil, "12k.png"],
173
+ [:size, {:less_than => 10240}, "5k.png", "12k.png"],
174
+ [:size, {:greater_than => 8096}, "12k.png", "5k.png"],
175
+ [:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
176
+ [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"],
177
+ [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args|
178
+ validation, options, valid_file, invalid_file = args
179
+ valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb")
180
+ invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb")
181
+
182
+ should_validate validation, options, valid_file, invalid_file
183
+ end
184
+
185
+ end
186
+ end
@@ -0,0 +1,167 @@
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 "Parsing S3 credentials with a bucket in them" do
41
+ setup do
42
+ rebuild_model :storage => :s3,
43
+ :s3_credentials => {
44
+ :production => { :bucket => "prod_bucket" },
45
+ :development => { :bucket => "dev_bucket" }
46
+ }
47
+ @dummy = Dummy.new
48
+ end
49
+
50
+ should "get the right bucket in production", :before => lambda{ ENV.expects(:[]).returns('production') } do
51
+ assert_equal "prod_bucket", @dummy.avatar.bucket_name
52
+ end
53
+
54
+ should "get the right bucket in development", :before => lambda{ ENV.expects(:[]).returns('development') } do
55
+ assert_equal "dev_bucket", @dummy.avatar.bucket_name
56
+ end
57
+ end
58
+
59
+ context "An attachment with S3 storage" do
60
+ setup do
61
+ rebuild_model :storage => :s3,
62
+ :bucket => "testing",
63
+ :path => ":attachment/:style/:basename.:extension",
64
+ :s3_credentials => {
65
+ 'access_key_id' => "12345",
66
+ 'secret_access_key' => "54321"
67
+ }
68
+ end
69
+
70
+ should "be extended by the S3 module" do
71
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
72
+ end
73
+
74
+ should "not be extended by the Filesystem module" do
75
+ assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
76
+ end
77
+
78
+ context "when assigned" do
79
+ setup do
80
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
81
+ @dummy = Dummy.new
82
+ @dummy.avatar = @file
83
+ end
84
+
85
+ should "not get a bucket to get a URL" do
86
+ @dummy.avatar.expects(:s3).never
87
+ @dummy.avatar.expects(:s3_bucket).never
88
+ assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url
89
+ end
90
+
91
+ context "and saved" do
92
+ setup do
93
+ @s3_mock = stub
94
+ @bucket_mock = stub
95
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
96
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
97
+ @key_mock = stub
98
+ @bucket_mock.expects(:key).returns(@key_mock)
99
+ @key_mock.expects(:data=)
100
+ @key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png')
101
+ @dummy.save
102
+ end
103
+
104
+ should "succeed" do
105
+ assert true
106
+ end
107
+ end
108
+
109
+ context "and remove" do
110
+ setup do
111
+ @s3_mock = stub
112
+ @bucket_mock = stub
113
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
114
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
115
+ @key_mock = stub
116
+ @bucket_mock.expects(:key).at_least(2).returns(@key_mock)
117
+ @key_mock.expects(:delete)
118
+ @dummy.destroy_attached_files
119
+ end
120
+
121
+ should "succeed" do
122
+ assert true
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ unless ENV["S3_TEST_BUCKET"].blank?
129
+ context "Using S3 for real, an attachment with S3 storage" do
130
+ setup do
131
+ rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
132
+ :storage => :s3,
133
+ :bucket => ENV["S3_TEST_BUCKET"],
134
+ :path => ":class/:attachment/:id/:style.:extension",
135
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
136
+
137
+ Dummy.delete_all
138
+ @dummy = Dummy.new
139
+ end
140
+
141
+ should "be extended by the S3 module" do
142
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
143
+ end
144
+
145
+ context "when assigned" do
146
+ setup do
147
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
148
+ @dummy.avatar = @file
149
+ end
150
+
151
+ should "still return a Tempfile when sent #to_io" do
152
+ assert_equal Tempfile, @dummy.avatar.to_io.class
153
+ end
154
+
155
+ context "and saved" do
156
+ setup do
157
+ @dummy.save
158
+ end
159
+
160
+ should "be on S3" do
161
+ assert true
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,138 @@
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
+ [["600x600>", "434x66"],
39
+ ["400x400>", "400x61"],
40
+ ["32x32<", "434x66"]
41
+ ].each do |args|
42
+ context "being thumbnailed with a geometry of #{args[0]}" do
43
+ setup do
44
+ @thumb = Paperclip::Thumbnail.new(@file, args[0])
45
+ end
46
+
47
+ should "start with dimensions of 434x66" do
48
+ cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
49
+ assert_equal "434x66", `#{cmd}`.chomp
50
+ end
51
+
52
+ should "report the correct target geometry" do
53
+ assert_equal args[0], @thumb.target_geometry.to_s
54
+ end
55
+
56
+ context "when made" do
57
+ setup do
58
+ @thumb_result = @thumb.make
59
+ end
60
+
61
+ should "be the size we expect it to be" do
62
+ cmd = %Q[identify -format "%wx%h" "#{@thumb_result.path}"]
63
+ assert_equal args[1], `#{cmd}`.chomp
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ context "being thumbnailed at 100x50 with cropping" do
70
+ setup do
71
+ @thumb = Paperclip::Thumbnail.new(@file, "100x50#")
72
+ end
73
+
74
+ should "report its correct current and target geometries" do
75
+ assert_equal "100x50#", @thumb.target_geometry.to_s
76
+ assert_equal "434x66", @thumb.current_geometry.to_s
77
+ end
78
+
79
+ should "report its correct format" do
80
+ assert_nil @thumb.format
81
+ end
82
+
83
+ should "have whiny_thumbnails turned on by default" do
84
+ assert @thumb.whiny_thumbnails
85
+ end
86
+
87
+ should "have convert_options set to nil by default" do
88
+ assert_equal nil, @thumb.convert_options
89
+ end
90
+
91
+ should "send the right command to convert when sent #make" do
92
+ Paperclip.expects(:"`").with do |arg|
93
+ 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+".*?"}
94
+ end
95
+ @thumb.make
96
+ end
97
+
98
+ should "create the thumbnail when sent #make" do
99
+ dst = @thumb.make
100
+ assert_match /100x50/, `identify "#{dst.path}"`
101
+ end
102
+ end
103
+
104
+ context "being thumbnailed with convert options set" do
105
+ setup do
106
+ @thumb = Paperclip::Thumbnail.new(@file, "100x50#", format=nil, convert_options="-strip -depth 8", whiny_thumbnails=true)
107
+ end
108
+
109
+ should "have convert_options value set" do
110
+ assert_equal "-strip -depth 8", @thumb.convert_options
111
+ end
112
+
113
+ should "send the right command to convert when sent #make" do
114
+ Paperclip.expects(:"`").with do |arg|
115
+ 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+".*?"}
116
+ end
117
+ @thumb.make
118
+ end
119
+
120
+ should "create the thumbnail when sent #make" do
121
+ dst = @thumb.make
122
+ assert_match /100x50/, `identify "#{dst.path}"`
123
+ end
124
+
125
+ context "redefined to have bad convert_options setting" do
126
+ setup do
127
+ @thumb = Paperclip::Thumbnail.new(@file, "100x50#", format=nil, convert_options="-this-aint-no-option", whiny_thumbnails=true)
128
+ end
129
+
130
+ should "error when trying to create the thumbnail" do
131
+ assert_raises(Paperclip::PaperclipError) do
132
+ @thumb.make
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end