summaryrefslogtreecommitdiff
path: root/vendor/plugins/paperclip/test/paperclip_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/plugins/paperclip/test/paperclip_test.rb')
-rw-r--r--vendor/plugins/paperclip/test/paperclip_test.rb233
1 files changed, 0 insertions, 233 deletions
diff --git a/vendor/plugins/paperclip/test/paperclip_test.rb b/vendor/plugins/paperclip/test/paperclip_test.rb
deleted file mode 100644
index 8365649..0000000
--- a/vendor/plugins/paperclip/test/paperclip_test.rb
+++ /dev/null
@@ -1,233 +0,0 @@
1require 'test/helper'
2
3class 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
233end