PageRenderTime 135ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/amexsbs/railsapp/vendor/plugins/paperclip/test/paperclip_test.rb

https://bitbucket.org/AcireStudios/social-app-demo
Ruby | 254 lines | 237 code | 17 blank | 0 comment | 1 complexity | 53f89aedc0f4695631469bbe6ade33f5 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, MIT
  1. require 'test/helper'
  2. class PaperclipTest < Test::Unit::TestCase
  3. context "Calling Paperclip.run" do
  4. setup do
  5. Paperclip.options[:image_magick_path] = nil
  6. Paperclip.options[:command_path] = nil
  7. Paperclip::CommandLine.stubs(:'`')
  8. end
  9. should "execute the right command with :image_magick_path" do
  10. Paperclip.options[:image_magick_path] = "/usr/bin"
  11. Paperclip.expects(:log).with(includes('[DEPRECATION]'))
  12. Paperclip.expects(:log).with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
  13. Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
  14. Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
  15. end
  16. should "execute the right command with :command_path" do
  17. Paperclip.options[:command_path] = "/usr/bin"
  18. Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
  19. Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
  20. end
  21. should "execute the right command with no path" do
  22. Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{convert ['"]one.jpg['"] ['"]two.jpg['"]}))
  23. Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
  24. end
  25. should "tell you the command isn't there if the shell returns 127" do
  26. with_exitstatus_returning(127) do
  27. assert_raises(Paperclip::CommandNotFoundError) do
  28. Paperclip.run("command")
  29. end
  30. end
  31. end
  32. should "tell you the command isn't there if an ENOENT is raised" do
  33. assert_raises(Paperclip::CommandNotFoundError) do
  34. Paperclip::CommandLine.stubs(:"`").raises(Errno::ENOENT)
  35. Paperclip.run("command")
  36. end
  37. end
  38. end
  39. should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
  40. assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
  41. end
  42. should "raise when sent #processor and the name of a class that doesn't exist" do
  43. assert_raises(NameError){ Paperclip.processor(:boogey_man) }
  44. end
  45. should "return a class when sent #processor and the name of a class under Paperclip" do
  46. assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
  47. end
  48. context "An ActiveRecord model with an 'avatar' attachment" do
  49. setup do
  50. rebuild_model :path => "tmp/:class/omg/:style.:extension"
  51. @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
  52. end
  53. teardown { @file.close }
  54. should "not error when trying to also create a 'blah' attachment" do
  55. assert_nothing_raised do
  56. Dummy.class_eval do
  57. has_attached_file :blah
  58. end
  59. end
  60. end
  61. context "that is attr_protected" do
  62. setup do
  63. Dummy.class_eval do
  64. attr_protected :avatar
  65. end
  66. @dummy = Dummy.new
  67. end
  68. should "not assign the avatar on mass-set" do
  69. @dummy.attributes = { :other => "I'm set!",
  70. :avatar => @file }
  71. assert_equal "I'm set!", @dummy.other
  72. assert ! @dummy.avatar?
  73. end
  74. should "still allow assigment on normal set" do
  75. @dummy.other = "I'm set!"
  76. @dummy.avatar = @file
  77. assert_equal "I'm set!", @dummy.other
  78. assert @dummy.avatar?
  79. end
  80. end
  81. context "with a subclass" do
  82. setup do
  83. class ::SubDummy < Dummy; end
  84. end
  85. should "be able to use the attachment from the subclass" do
  86. assert_nothing_raised do
  87. @subdummy = SubDummy.create(:avatar => @file)
  88. end
  89. end
  90. should "be able to see the attachment definition from the subclass's class" do
  91. assert_equal "tmp/:class/omg/:style.:extension",
  92. SubDummy.attachment_definitions[:avatar][:path]
  93. end
  94. teardown do
  95. Object.send(:remove_const, "SubDummy") rescue nil
  96. end
  97. end
  98. should "have an #avatar method" do
  99. assert Dummy.new.respond_to?(:avatar)
  100. end
  101. should "have an #avatar= method" do
  102. assert Dummy.new.respond_to?(:avatar=)
  103. end
  104. context "that is valid" do
  105. setup do
  106. @dummy = Dummy.new
  107. @dummy.avatar = @file
  108. end
  109. should "be valid" do
  110. assert @dummy.valid?
  111. end
  112. end
  113. context "a validation with an if guard clause" do
  114. setup do
  115. Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
  116. @dummy = Dummy.new
  117. @dummy.stubs(:avatar_file_name).returns(nil)
  118. end
  119. should "attempt validation if the guard returns true" do
  120. @dummy.expects(:foo).returns(true)
  121. assert ! @dummy.valid?
  122. end
  123. should "not attempt validation if the guard returns false" do
  124. @dummy.expects(:foo).returns(false)
  125. assert @dummy.valid?
  126. end
  127. end
  128. context "a validation with an unless guard clause" do
  129. setup do
  130. Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
  131. @dummy = Dummy.new
  132. @dummy.stubs(:avatar_file_name).returns(nil)
  133. end
  134. should "attempt validation if the guard returns true" do
  135. @dummy.expects(:foo).returns(false)
  136. assert ! @dummy.valid?
  137. end
  138. should "not attempt validation if the guard returns false" do
  139. @dummy.expects(:foo).returns(true)
  140. assert @dummy.valid?
  141. end
  142. end
  143. should "not have Attachment in the ActiveRecord::Base namespace" do
  144. assert_raises(NameError) do
  145. ActiveRecord::Base::Attachment
  146. end
  147. end
  148. def self.should_validate validation, options, valid_file, invalid_file
  149. context "with #{validation} validation and #{options.inspect} options" do
  150. setup do
  151. rebuild_class
  152. Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
  153. @dummy = Dummy.new
  154. end
  155. context "and assigning nil" do
  156. setup do
  157. @dummy.avatar = nil
  158. @dummy.valid?
  159. end
  160. if validation == :presence
  161. should "have an error on the attachment" do
  162. assert @dummy.errors[:avatar_file_name]
  163. end
  164. else
  165. should "not have an error on the attachment" do
  166. assert @dummy.errors.blank?, @dummy.errors.full_messages.join(", ")
  167. end
  168. end
  169. end
  170. context "and assigned a valid file" do
  171. setup do
  172. @dummy.avatar = valid_file
  173. @dummy.valid?
  174. end
  175. should "not have an error when assigned a valid file" do
  176. assert_equal 0, @dummy.errors.length, @dummy.errors.full_messages.join(", ")
  177. end
  178. end
  179. context "and assigned an invalid file" do
  180. setup do
  181. @dummy.avatar = invalid_file
  182. @dummy.valid?
  183. end
  184. should "have an error when assigned a valid file" do
  185. assert @dummy.errors.length > 0
  186. end
  187. end
  188. end
  189. end
  190. [[:presence, {}, "5k.png", nil],
  191. [:size, {:in => 1..10240}, "5k.png", "12k.png"],
  192. [:size, {:less_than => 10240}, "5k.png", "12k.png"],
  193. [:size, {:greater_than => 8096}, "12k.png", "5k.png"],
  194. [:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
  195. [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"],
  196. [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args|
  197. validation, options, valid_file, invalid_file = args
  198. valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb")
  199. invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb")
  200. should_validate validation, options, valid_file, invalid_file
  201. end
  202. context "with size validation and less_than 10240 option" do
  203. context "and assigned an invalid file" do
  204. setup do
  205. Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240)
  206. @dummy = Dummy.new
  207. @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
  208. @dummy.valid?
  209. end
  210. should "have a file size min/max error message" do
  211. assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/between 0 and 10240 bytes/ }
  212. end
  213. end
  214. end
  215. end
  216. end