PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/asset_test.rb

https://github.com/noubani/crabgrass
Ruby | 293 lines | 235 code | 52 blank | 6 comment | 5 complexity | 346975efa2e4d9bfcd8addba24107d1a MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. class AssetTest < Test::Unit::TestCase
  3. fixtures :groups, :users, :page_terms, :assets, :pages, :group_participations
  4. @@private = AssetExtension::Storage.private_storage = "#{RAILS_ROOT}/tmp/private_assets"
  5. @@public = AssetExtension::Storage.public_storage = "#{RAILS_ROOT}/tmp/public_assets"
  6. def setup
  7. FileUtils.mkdir_p(@@private)
  8. FileUtils.mkdir_p(@@public)
  9. #Media::Process::Base.log_to_stdout_when = :always
  10. Media::Process::Base.log_to_stdout_when = :on_error
  11. Conf.disable_site_testing
  12. end
  13. def teardown
  14. FileUtils.rm_rf(@@private)
  15. FileUtils.rm_rf(@@public)
  16. end
  17. def test_associations
  18. assert check_associations(Asset)
  19. assert check_associations(Asset::Version)
  20. assert check_associations(Thumbnail)
  21. end
  22. def test_simple_upload
  23. file_to_upload = upload_data('image.png')
  24. @asset = Asset.make :uploaded_data => file_to_upload
  25. assert File.exists?( @asset.private_filename ), 'the private file should exist'
  26. assert read_file('image.png') == File.read(@asset.private_filename), 'full_filename should be the uploaded_data'
  27. end
  28. def test_paths
  29. @asset = Asset.make :uploaded_data => upload_data('image.png')
  30. assert_equal "%s/0000/%04d/image.png" % [@@private,@asset.id], @asset.private_filename
  31. assert_equal "%s/0000/%04d/image_small.png" % [@@private,@asset.id], @asset.private_thumbnail_filename(:small)
  32. assert_equal "%s/%s/image.png" % [@@public,@asset.id], @asset.public_filename
  33. assert_equal "%s/%s/image_small.png" % [@@public,@asset.id], @asset.public_thumbnail_filename(:small)
  34. assert_equal "/assets/%s/image.png" % @asset.id, @asset.url
  35. assert_equal "/assets/%s/image_small.png" % @asset.id, @asset.thumbnail_url(:small)
  36. end
  37. def test_single_table_inheritance
  38. @asset = Asset.make :uploaded_data => upload_data('image.png')
  39. assert_equal 'PngAsset', @asset.type, 'initial asset should be a png'
  40. assert_equal 'image/png', @asset.content_type, 'initial asset should be a png'
  41. @asset.uploaded_data = upload_data('photo.jpg')
  42. @asset.save
  43. assert_equal 'ImageAsset', @asset.type, 'then the asset should be a jpg'
  44. assert_equal 'image/jpeg', @asset.content_type, 'then the asset should be a jpg'
  45. end
  46. def test_versions
  47. assert_equal 0, Asset::Version.count
  48. @asset = Asset.make :uploaded_data => upload_data('image.png')
  49. @id = @asset.id
  50. @filename_for_1 = @asset.private_filename
  51. assert_equal 1, @asset.version, 'should be on version 1'
  52. @asset.uploaded_data = upload_data('photo.jpg')
  53. @asset.save
  54. @filename_for_2 = @asset.private_filename
  55. assert_equal 2, @asset.version, 'should be on version 2'
  56. assert_equal 2, @asset.versions.size, 'there should be two versions'
  57. assert !File.exists?(@filename_for_1), 'first non-version file should not exist'
  58. assert File.exists?(@filename_for_2), 'second non-version file should exist'
  59. @version = @asset.versions.earliest
  60. assert_equal @version.class, Asset::Version
  61. assert_equal 'PngAsset', @version.versioned_type
  62. assert_equal 'image.png', @version.filename
  63. #puts @version.inspect
  64. #puts @version.thumbdefs.inspect
  65. #puts @version.thumbnail_filename(:small)
  66. assert_equal 'image_small.png', @version.thumbnail_filename(:small)
  67. assert_equal "/assets/#{@id}/versions/1/image.png", @version.url
  68. assert read_file('image.png') == File.read(@version.private_filename), 'version 1 data should match image.png'
  69. @version = @asset.versions.latest
  70. assert_equal 'ImageAsset', @version.versioned_type
  71. assert_equal 'photo.jpg', @version.filename
  72. assert_equal 'photo_small.jpg', @version.thumbnail_filename(:small)
  73. assert_equal "/assets/#{@id}/versions/2/photo.jpg", @version.url
  74. assert read_file('photo.jpg') == File.read(@version.private_filename), 'version 2 data should match photo.jpg'
  75. end
  76. def test_rename
  77. @asset = Asset.create :uploaded_data => upload_data('image.png')
  78. @asset.base_filename = 'newimage'
  79. @asset.save
  80. assert_equal "%s/0000/%04d/newimage.png" % [@@private,@asset.id], @asset.private_filename
  81. assert File.exists?(@asset.private_filename)
  82. assert !File.exists?("%s/0000/%04d/image.png" % [@@private,@asset.id])
  83. end
  84. def test_file_cleanup_on_destroy
  85. @asset = Asset.create :uploaded_data => upload_data('image.png')
  86. @asset.update_access
  87. @asset.destroy
  88. assert !File.exists?(@asset.private_filename), 'private file should not exist'
  89. assert !File.exists?(File.dirname(@asset.private_filename)), 'dir for private file should not exist'
  90. assert !File.exists?(@asset.public_filename), 'public file should not exist'
  91. end
  92. def test_access
  93. @asset = Asset.create :uploaded_data => upload_data('image.png')
  94. assert @asset.public?
  95. @asset.update_access
  96. assert File.exists?(@asset.public_filename), 'public file "%s" should exist' % @asset.public_filename
  97. assert File.symlink?(File.dirname(@asset.public_filename)), 'dir of public file should be a symlink'
  98. @asset.instance_eval do
  99. def public?
  100. false
  101. end
  102. end
  103. @asset.update_access
  104. assert !File.exists?(@asset.public_filename), 'public file should NOT exist'
  105. assert !File.symlink?(File.dirname(@asset.public_filename)), 'dir of public file should NOT be a symlink'
  106. end
  107. def test_thumbnails
  108. start_thumb_count = Thumbnail.count
  109. @asset = Asset.make :uploaded_data => upload_data('photo.jpg')
  110. assert @asset.thumbdefs.any?, 'asset should have thumbdefs'
  111. assert @asset.thumbnails.any?, 'asset should have thumbnail objects'
  112. @asset.generate_thumbnails
  113. @thumb1 = @asset.private_thumbnail_filename(:small)
  114. @thumb_v1 = @asset.versions.latest.private_thumbnail_filename(:small)
  115. assert File.exists?(@thumb1), '%s should exist' % @thumb1
  116. assert File.exists?(@thumb_v1), '%s should exist' % @thumb_v1
  117. @asset.uploaded_data = upload_data('image.png')
  118. @asset.save
  119. @asset = Asset.find(@asset.id)
  120. assert_equal 3, @asset.thumbnails.length, 'there should be three thumbnails'
  121. assert_equal 2, @asset.versions.length, 'there should be two versions'
  122. @asset.versions.each do |version|
  123. assert_equal 3, version.thumbnails.length, 'each version should have thumbnails'
  124. end
  125. @asset.generate_thumbnails
  126. @thumb2 = @asset.private_thumbnail_filename(:small)
  127. @thumb_v2 = @asset.versions.latest.private_thumbnail_filename(:small)
  128. assert File.exists?(@thumb2), '%s should exist (new thumb)' % @thumb2
  129. assert File.exists?(@thumb_v2), '%s should exist (new versioned thumb)' % @thumb_v2
  130. assert !File.exists?(@thumb1), '%s should NOT exist (old filename)' % @thumb1
  131. end_thumb_count = Thumbnail.count
  132. assert_equal start_thumb_count+9, end_thumb_count, 'there should be exactly 9 more thumbnail objects'
  133. end
  134. def test_type_changes
  135. @asset = Asset.make :uploaded_data => upload_data('bee.jpg')
  136. assert_equal 'ImageAsset', @asset.type
  137. assert_equal 3, @asset.thumbnails.count
  138. # change to TextAsset
  139. @asset.uploaded_data = upload_data('msword.doc')
  140. @asset.save
  141. assert_equal 'TextAsset', @asset.type
  142. assert_equal 6, @asset.thumbnails.count
  143. # change back
  144. @asset = Asset.find(@asset.id)
  145. @asset.uploaded_data = upload_data('bee.jpg')
  146. @asset.save
  147. assert_equal 'ImageAsset', @asset.type
  148. assert_equal 3, @asset.thumbnails.count
  149. end
  150. def test_dimensions
  151. @asset = Asset.make :uploaded_data => upload_data('photo.jpg')
  152. assert_equal 500, @asset.width, 'width must match file'
  153. assert_equal 321, @asset.height, 'height must match file'
  154. @asset.uploaded_data = upload_data('bee.jpg')
  155. @asset.save
  156. assert_equal 333, @asset.width, 'width must match after new upload'
  157. assert_equal 500, @asset.height, 'height must match after new upload'
  158. assert_equal 43, @asset.thumbnail(:small).width, 'guess width should match actual'
  159. assert_equal 64, @asset.thumbnail(:small).height, 'guess height should match actual'
  160. @asset.generate_thumbnails
  161. assert_equal 43, @asset.thumbnail(:small).width, 'actual width should be 43'
  162. assert_equal 64, @asset.thumbnail(:small).height, 'actual height should be 64'
  163. assert_equal 43, @asset.versions.latest.thumbnail(:small).width, 'actual width of versioned thumb should be 43'
  164. assert_equal 64, @asset.versions.latest.thumbnail(:small).height, 'actual height of versioned thumb should be 64'
  165. end
  166. def test_doc
  167. if !Media::Process::OpenOffice.new.available?
  168. puts "\nOpenOffice converter is not available. Either OpenOffice is not installed or it can not be started. Skipping AssetTest#test_doc."
  169. return
  170. end
  171. @asset = Asset.make :uploaded_data => upload_data('msword.doc')
  172. assert_equal TextAsset, @asset.class, 'asset should be a TextAsset'
  173. assert_equal 'TextAsset', @asset.versions.earliest.versioned_type, 'version should by of type TextAsset'
  174. @asset.generate_thumbnails
  175. @asset.thumbnails.each do |thumb|
  176. assert_equal false, thumb.failure?, 'generating thumbnail "%s" should have succeeded' % thumb.name
  177. assert thumb.private_filename, 'thumbnail "%s" should exist' % thumb.name
  178. end
  179. end
  180. def test_binary
  181. @asset = Asset.make :uploaded_data => upload_data('raw_file.bin')
  182. assert_equal Asset, @asset.class, 'asset should be an Asset'
  183. assert_equal 'Asset', @asset.versions.earliest.versioned_type, 'version should by of type Asset'
  184. end
  185. def test_failure_on_corrupted_file
  186. Media::Process::Base.log_to_stdout_when = :never
  187. @asset = Asset.make :uploaded_data => upload_data('corrupt.jpg')
  188. @asset.generate_thumbnails
  189. @asset.thumbnails.each do |thumb|
  190. assert_equal true, thumb.failure?, 'generating the thumbnail should have failed'
  191. end
  192. Media::Process::Base.log_to_stdout_when = :on_error
  193. end
  194. def test_failure
  195. Media::Process::Base.log_to_stdout_when = :never
  196. Media::Process::GraphicMagick.gm_override = "false" # the sh command, not the boolean value.
  197. @asset = Asset.make :uploaded_data => upload_data('photo.jpg')
  198. @asset.generate_thumbnails
  199. @asset.thumbnails.each do |thumb|
  200. assert_equal true, thumb.failure?, 'generating the thumbnail should have failed'
  201. end
  202. Media::Process::GraphicMagick.gm_override = nil
  203. Media::Process::Base.log_to_stdout_when = :on_error
  204. end
  205. def test_search
  206. user = users(:kangaroo)
  207. correct_ids = Asset.find(:all).collect do |asset|
  208. asset.page_terms = asset.page.page_terms
  209. asset.save
  210. asset.id if user.may?(:view, asset.page)
  211. end.compact.sort
  212. ids = Asset.visible_to(user).media_type(:image).find(:all).collect{|asset| asset.id}
  213. assert_equal correct_ids, ids.sort
  214. end
  215. def test_asset_page
  216. asset = Asset.build(:uploaded_data => upload_data('photo.jpg'))
  217. page = nil
  218. assert_nothing_raised do
  219. page = AssetPage.create! :title => 'hi', :data => asset, :user => users(:blue)
  220. end
  221. assert_equal asset, page.data
  222. asset.reload
  223. assert asset.page_terms
  224. assert "1", page.data.page_terms.media
  225. end
  226. def test_asset_page_access
  227. page = AssetPage.build! :title => 'hi', :user => users(:blue)
  228. asset = Asset.build(:uploaded_data => upload_data('photo.jpg'))
  229. page.data = asset
  230. page.save!
  231. assert File.exists?(asset.private_filename)
  232. assert !File.exists?(asset.public_filename), 'public file "%s" should NOT exist' % asset.public_filename
  233. end
  234. def test_content_type
  235. assert_equal 'application/octet-stream', Asset.new.content_type
  236. end
  237. protected
  238. def debug
  239. puts `find #{@@private}` if true
  240. end
  241. end