PageRenderTime 67ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/imagealchemy/vendor/plugins/paperclip/test/integration_test.rb

https://github.com/retohuber/edu
Ruby | 481 lines | 402 code | 78 blank | 1 comment | 2 complexity | 94976b91cb8c4afaa3f81b68834d2366 MD5 | raw file
  1. require 'test/helper'
  2. class IntegrationTest < Test::Unit::TestCase
  3. context "Many models at once" do
  4. setup do
  5. rebuild_model
  6. @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
  7. 300.times do |i|
  8. Dummy.create! :avatar => @file
  9. end
  10. end
  11. should "not exceed the open file limit" do
  12. assert_nothing_raised do
  13. dummies = Dummy.find(:all)
  14. dummies.each { |dummy| dummy.avatar }
  15. end
  16. end
  17. end
  18. context "An attachment" do
  19. setup do
  20. rebuild_model :styles => { :thumb => "50x50#" }
  21. @dummy = Dummy.new
  22. @file = File.new(File.join(File.dirname(__FILE__),
  23. "fixtures",
  24. "5k.png"), 'rb')
  25. @dummy.avatar = @file
  26. assert @dummy.save
  27. end
  28. teardown { @file.close }
  29. should "create its thumbnails properly" do
  30. assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
  31. end
  32. context "redefining its attachment styles" do
  33. setup do
  34. Dummy.class_eval do
  35. has_attached_file :avatar, :styles => { :thumb => "150x25#" }
  36. has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } }
  37. end
  38. @d2 = Dummy.find(@dummy.id)
  39. @d2.avatar.reprocess!
  40. @d2.save
  41. end
  42. should "create its thumbnails properly" do
  43. assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
  44. assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"`
  45. end
  46. end
  47. end
  48. context "A model that modifies its original" do
  49. setup do
  50. rebuild_model :styles => { :original => "2x2#" }
  51. @dummy = Dummy.new
  52. @file = File.new(File.join(File.dirname(__FILE__),
  53. "fixtures",
  54. "5k.png"), 'rb')
  55. @dummy.avatar = @file
  56. end
  57. should "report the file size of the processed file and not the original" do
  58. assert_not_equal @file.size, @dummy.avatar.size
  59. end
  60. teardown { @file.close }
  61. end
  62. context "A model with attachments scoped under an id" do
  63. setup do
  64. rebuild_model :styles => { :large => "100x100",
  65. :medium => "50x50" },
  66. :path => ":rails_root/tmp/:id/:attachments/:style.:extension"
  67. @dummy = Dummy.new
  68. @file = File.new(File.join(File.dirname(__FILE__),
  69. "fixtures",
  70. "5k.png"), 'rb')
  71. @dummy.avatar = @file
  72. end
  73. teardown { @file.close }
  74. context "when saved" do
  75. setup do
  76. @dummy.save
  77. @saved_path = @dummy.avatar.path(:large)
  78. end
  79. should "have a large file in the right place" do
  80. assert File.exists?(@dummy.avatar.path(:large))
  81. end
  82. context "and deleted" do
  83. setup do
  84. @dummy.avatar.clear
  85. @dummy.save
  86. end
  87. should "not have a large file in the right place anymore" do
  88. assert ! File.exists?(@saved_path)
  89. end
  90. should "not have its next two parent directories" do
  91. assert ! File.exists?(File.dirname(@saved_path))
  92. assert ! File.exists?(File.dirname(File.dirname(@saved_path)))
  93. end
  94. before_should "not die if an unexpected SystemCallError happens" do
  95. FileUtils.stubs(:rmdir).raises(Errno::EPIPE)
  96. end
  97. end
  98. end
  99. end
  100. context "A model with no attachment validation" do
  101. setup do
  102. rebuild_model :styles => { :large => "300x300>",
  103. :medium => "100x100",
  104. :thumb => ["32x32#", :gif] },
  105. :default_style => :medium,
  106. :url => "/:attachment/:class/:style/:id/:basename.:extension",
  107. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  108. @dummy = Dummy.new
  109. end
  110. should "have its definition return false when asked about whiny_thumbnails" do
  111. assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
  112. end
  113. context "when validates_attachment_thumbnails is called" do
  114. setup do
  115. Dummy.validates_attachment_thumbnails :avatar
  116. end
  117. should "have its definition return true when asked about whiny_thumbnails" do
  118. assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
  119. end
  120. end
  121. context "redefined to have attachment validations" do
  122. setup do
  123. rebuild_model :styles => { :large => "300x300>",
  124. :medium => "100x100",
  125. :thumb => ["32x32#", :gif] },
  126. :whiny_thumbnails => true,
  127. :default_style => :medium,
  128. :url => "/:attachment/:class/:style/:id/:basename.:extension",
  129. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  130. end
  131. should "have its definition return true when asked about whiny_thumbnails" do
  132. assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
  133. end
  134. end
  135. end
  136. context "A model with no convert_options setting" do
  137. setup do
  138. rebuild_model :styles => { :large => "300x300>",
  139. :medium => "100x100",
  140. :thumb => ["32x32#", :gif] },
  141. :default_style => :medium,
  142. :url => "/:attachment/:class/:style/:id/:basename.:extension",
  143. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  144. @dummy = Dummy.new
  145. end
  146. should "have its definition return nil when asked about convert_options" do
  147. assert ! Dummy.attachment_definitions[:avatar][:convert_options]
  148. end
  149. context "redefined to have convert_options setting" do
  150. setup do
  151. rebuild_model :styles => { :large => "300x300>",
  152. :medium => "100x100",
  153. :thumb => ["32x32#", :gif] },
  154. :convert_options => "-strip -depth 8",
  155. :default_style => :medium,
  156. :url => "/:attachment/:class/:style/:id/:basename.:extension",
  157. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  158. end
  159. should "have its definition return convert_options value when asked about convert_options" do
  160. assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:convert_options]
  161. end
  162. end
  163. end
  164. context "A model with a filesystem attachment" do
  165. setup do
  166. rebuild_model :styles => { :large => "300x300>",
  167. :medium => "100x100",
  168. :thumb => ["32x32#", :gif] },
  169. :whiny_thumbnails => true,
  170. :default_style => :medium,
  171. :url => "/:attachment/:class/:style/:id/:basename.:extension",
  172. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  173. @dummy = Dummy.new
  174. @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
  175. @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
  176. assert @dummy.avatar = @file
  177. assert @dummy.valid?
  178. assert @dummy.save
  179. end
  180. should "write and delete its files" do
  181. [["434x66", :original],
  182. ["300x46", :large],
  183. ["100x15", :medium],
  184. ["32x32", :thumb]].each do |geo, style|
  185. cmd = %Q[identify -format "%wx%h" "#{@dummy.avatar.path(style)}"]
  186. assert_equal geo, `#{cmd}`.chomp, cmd
  187. end
  188. saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
  189. @d2 = Dummy.find(@dummy.id)
  190. assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path}"`.chomp
  191. assert_equal "434x66", `identify -format "%wx%h" "#{@d2.avatar.path(:original)}"`.chomp
  192. assert_equal "300x46", `identify -format "%wx%h" "#{@d2.avatar.path(:large)}"`.chomp
  193. assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path(:medium)}"`.chomp
  194. assert_equal "32x32", `identify -format "%wx%h" "#{@d2.avatar.path(:thumb)}"`.chomp
  195. @dummy.avatar = "not a valid file but not nil"
  196. assert_equal File.basename(@file.path), @dummy.avatar_file_name
  197. assert @dummy.valid?
  198. assert @dummy.save
  199. saved_paths.each do |p|
  200. assert File.exists?(p)
  201. end
  202. @dummy.avatar.clear
  203. assert_nil @dummy.avatar_file_name
  204. assert @dummy.valid?
  205. assert @dummy.save
  206. saved_paths.each do |p|
  207. assert ! File.exists?(p)
  208. end
  209. @d2 = Dummy.find(@dummy.id)
  210. assert_nil @d2.avatar_file_name
  211. end
  212. should "work exactly the same when new as when reloaded" do
  213. @d2 = Dummy.find(@dummy.id)
  214. assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
  215. [:thumb, :medium, :large, :original].each do |style|
  216. assert_equal @dummy.avatar.path(style), @d2.avatar.path(style)
  217. end
  218. saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) }
  219. @d2.avatar.clear
  220. assert @d2.save
  221. saved_paths.each do |p|
  222. assert ! File.exists?(p)
  223. end
  224. end
  225. should "know the difference between good files, bad files, and not files" do
  226. expected = @dummy.avatar.to_file
  227. @dummy.avatar = "not a file"
  228. assert @dummy.valid?
  229. assert_equal expected.path, @dummy.avatar.path
  230. expected.close
  231. @dummy.avatar = @bad_file
  232. assert ! @dummy.valid?
  233. end
  234. should "know the difference between good files, bad files, and not files when validating" do
  235. Dummy.validates_attachment_presence :avatar
  236. @d2 = Dummy.find(@dummy.id)
  237. @d2.avatar = @file
  238. assert @d2.valid?, @d2.errors.full_messages.inspect
  239. @d2.avatar = @bad_file
  240. assert ! @d2.valid?
  241. end
  242. should "be able to reload without saving and not have the file disappear" do
  243. @dummy.avatar = @file
  244. assert @dummy.save
  245. @dummy.avatar.clear
  246. assert_nil @dummy.avatar_file_name
  247. @dummy.reload
  248. assert_equal "5k.png", @dummy.avatar_file_name
  249. end
  250. context "that is assigned its file from another Paperclip attachment" do
  251. setup do
  252. @dummy2 = Dummy.new
  253. @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"), 'rb')
  254. assert @dummy2.avatar = @file2
  255. @dummy2.save
  256. end
  257. should "work when assigned a file" do
  258. assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
  259. `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
  260. assert @dummy.avatar = @dummy2.avatar
  261. @dummy.save
  262. assert_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
  263. `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
  264. end
  265. end
  266. end
  267. context "A model with an attachments association and a Paperclip attachment" do
  268. setup do
  269. Dummy.class_eval do
  270. has_many :attachments, :class_name => 'Dummy'
  271. end
  272. @dummy = Dummy.new
  273. @dummy.avatar = File.new(File.join(File.dirname(__FILE__),
  274. "fixtures",
  275. "5k.png"), 'rb')
  276. end
  277. should "should not error when saving" do
  278. assert_nothing_raised do
  279. @dummy.save!
  280. end
  281. end
  282. end
  283. if ENV['S3_TEST_BUCKET']
  284. def s3_files_for attachment
  285. [:thumb, :medium, :large, :original].inject({}) do |files, style|
  286. data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp
  287. t = Tempfile.new("paperclip-test")
  288. t.binmode
  289. t.write(data)
  290. t.rewind
  291. files[style] = t
  292. files
  293. end
  294. end
  295. def s3_headers_for attachment, style
  296. `curl --head "#{attachment.url(style)}" 2>/dev/null`.split("\n").inject({}) do |h,head|
  297. split_head = head.chomp.split(/\s*:\s*/, 2)
  298. h[split_head.first.downcase] = split_head.last unless split_head.empty?
  299. h
  300. end
  301. end
  302. context "A model with an S3 attachment" do
  303. setup do
  304. rebuild_model :styles => { :large => "300x300>",
  305. :medium => "100x100",
  306. :thumb => ["32x32#", :gif] },
  307. :storage => :s3,
  308. :whiny_thumbnails => true,
  309. # :s3_options => {:logger => Logger.new(StringIO.new)},
  310. :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
  311. :default_style => :medium,
  312. :bucket => ENV['S3_TEST_BUCKET'],
  313. :path => ":class/:attachment/:id/:style/:basename.:extension"
  314. @dummy = Dummy.new
  315. @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
  316. @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb')
  317. assert @dummy.avatar = @file
  318. assert @dummy.valid?
  319. assert @dummy.save
  320. @files_on_s3 = s3_files_for @dummy.avatar
  321. end
  322. should "write and delete its files" do
  323. [["434x66", :original],
  324. ["300x46", :large],
  325. ["100x15", :medium],
  326. ["32x32", :thumb]].each do |geo, style|
  327. cmd = %Q[identify -format "%wx%h" "#{@files_on_s3[style].path}"]
  328. assert_equal geo, `#{cmd}`.chomp, cmd
  329. end
  330. @d2 = Dummy.find(@dummy.id)
  331. @d2_files = s3_files_for @d2.avatar
  332. [["434x66", :original],
  333. ["300x46", :large],
  334. ["100x15", :medium],
  335. ["32x32", :thumb]].each do |geo, style|
  336. cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"]
  337. assert_equal geo, `#{cmd}`.chomp, cmd
  338. end
  339. @dummy.avatar = "not a valid file but not nil"
  340. assert_equal File.basename(@file.path), @dummy.avatar_file_name
  341. assert @dummy.valid?
  342. assert @dummy.save
  343. saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
  344. saved_keys.each do |key|
  345. assert key.exists?
  346. end
  347. @dummy.avatar.clear
  348. assert_nil @dummy.avatar_file_name
  349. assert @dummy.valid?
  350. assert @dummy.save
  351. saved_keys.each do |key|
  352. assert ! key.exists?
  353. end
  354. @d2 = Dummy.find(@dummy.id)
  355. assert_nil @d2.avatar_file_name
  356. end
  357. should "work exactly the same when new as when reloaded" do
  358. @d2 = Dummy.find(@dummy.id)
  359. assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
  360. [:thumb, :medium, :large, :original].each do |style|
  361. assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
  362. end
  363. saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
  364. @d2.avatar.clear
  365. assert @d2.save
  366. saved_keys.each do |key|
  367. assert ! key.exists?
  368. end
  369. end
  370. should "know the difference between good files, bad files, not files, and nil" do
  371. expected = @dummy.avatar.to_file
  372. @dummy.avatar = "not a file"
  373. assert @dummy.valid?
  374. assert_equal expected.full_name, @dummy.avatar.to_file.full_name
  375. @dummy.avatar = @bad_file
  376. assert ! @dummy.valid?
  377. @dummy.avatar = nil
  378. assert @dummy.valid?
  379. Dummy.validates_attachment_presence :avatar
  380. @d2 = Dummy.find(@dummy.id)
  381. @d2.avatar = @file
  382. assert @d2.valid?
  383. @d2.avatar = @bad_file
  384. assert ! @d2.valid?
  385. @d2.avatar = nil
  386. assert ! @d2.valid?
  387. end
  388. should "be able to reload without saving and not have the file disappear" do
  389. @dummy.avatar = @file
  390. assert @dummy.save
  391. @dummy.avatar = nil
  392. assert_nil @dummy.avatar_file_name
  393. @dummy.reload
  394. assert_equal "5k.png", @dummy.avatar_file_name
  395. end
  396. should "have the right content type" do
  397. headers = s3_headers_for(@dummy.avatar, :original)
  398. p headers
  399. assert_equal 'image/png', headers['content-type']
  400. end
  401. end
  402. end
  403. end