summaryrefslogtreecommitdiff
path: root/test/controllers/assets_controller_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/controllers/assets_controller_test.rb')
-rw-r--r--test/controllers/assets_controller_test.rb73
1 files changed, 54 insertions, 19 deletions
diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb
index 5f5f6e5..05fc6de 100644
--- a/test/controllers/assets_controller_test.rb
+++ b/test/controllers/assets_controller_test.rb
@@ -4,17 +4,16 @@ class AssetsControllerTest < ActionController::TestCase
4 4
5 def setup 5 def setup
6 login_as :quentin 6 login_as :quentin
7 @existing_asset_ids = Asset.pluck(:id)
7 end 8 end
8 9
9 def teardown 10 def teardown
10 # Clean up any files written to disk during tests 11 (Asset.pluck(:id) - @existing_asset_ids).each do |id|
11 Dir.glob(Rails.root.join('public', 'system', 'uploads', 'test_*')).each do |dir| 12 dir = Asset.upload_root.join(id.to_s)
13 raise "Refusing to delete #{dir} -- outside tmp/, Rails.env.test? may be false" unless
14 dir.to_s.start_with?(Rails.root.join("tmp").to_s)
12 FileUtils.rm_rf(dir) 15 FileUtils.rm_rf(dir)
13 end 16 end
14 # Remove uploads created for assets created during tests
15 Asset.where("upload_file_name IS NOT NULL").where("id > 1000000").each do |a|
16 FileUtils.rm_rf(Rails.root.join('public', 'system', 'uploads', a.id.to_s))
17 end
18 end 17 end
19 18
20 # --- index --- 19 # --- index ---
@@ -64,15 +63,14 @@ class AssetsControllerTest < ActionController::TestCase
64 63
65 # original and all four variants should exist on disk 64 # original and all four variants should exist on disk
66 %w[original medium thumb headline large].each do |style| 65 %w[original medium thumb headline large].each do |style|
67 path = Rails.root.join('public', 'system', 'uploads', 66 path = asset.send(:file_path, style)
68 asset.id.to_s, style, 'test_image.png')
69 assert File.exist?(path), "Expected #{style} variant at #{path}" 67 assert File.exist?(path), "Expected #{style} variant at #{path}"
70 end 68 end
71 end 69 end
72 70
73 # --- create with PDF --- 71 # --- create with PDF ---
74 72
75 test "create asset with PDF upload generates only original" do 73 test "create asset with PDF upload generates rasterized variants" do
76 uploaded = Rack::Test::UploadedFile.new( 74 uploaded = Rack::Test::UploadedFile.new(
77 Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'), 75 Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'),
78 'application/pdf' 76 'application/pdf'
@@ -83,19 +81,56 @@ class AssetsControllerTest < ActionController::TestCase
83 assert_response :redirect 81 assert_response :redirect
84 82
85 asset = Asset.last 83 asset = Asset.last
86 assert_equal 'test_document.pdf', asset.upload_file_name 84 original_path = asset.send(:file_path, :original)
87 assert_equal 'application/pdf', asset.upload_content_type
88
89 # only original should exist, no image variants
90 original_path = Rails.root.join('public', 'system', 'uploads',
91 asset.id.to_s, 'original', 'test_document.pdf')
92 assert File.exist?(original_path), "Expected original at #{original_path}" 85 assert File.exist?(original_path), "Expected original at #{original_path}"
86 assert_equal 'test_document.pdf', File.basename(original_path)
93 87
94 %w[medium thumb headline large].each do |style| 88 %w[medium thumb headline large].each do |style|
95 path = Rails.root.join('public', 'system', 'uploads', 89 path = asset.send(:file_path, style)
96 asset.id.to_s, style, 'test_document.pdf') 90 assert File.exist?(path), "Expected a #{style} variant at #{path}"
97 assert !File.exist?(path), "Expected no #{style} variant for PDF" 91 assert_equal '.png', File.extname(path), "Expected #{style} variant to be a PNG, not a PDF"
92 end
93 end
94
95 # --- create with attach ---
96
97 test "create with node_id attaches the asset to the node's draft" do
98 node = Node.root.children.create!(:slug => "asset_attach_target")
99
100 post :create, params: { asset: { name: 'Attach me' }, node_id: node.id }
101
102 assert_response :redirect
103 asset = Asset.last
104 assert_includes node.draft.assets.reload, asset
105 assert_match /attached/, flash[:notice]
106 end
107
108 test "create against a foreign-locked node keeps the asset but refuses the attach" do
109 node = Node.root.children.create!(:slug => "asset_attach_locked")
110 node.lock_for_editing!(users(:aaron))
111
112 assert_difference 'Asset.count', 1 do
113 post :create, params: { asset: { name: 'Orphaned for now' }, node_id: node.id }
98 end 114 end
115
116 assert_empty node.draft.assets.reload
117 assert_equal node_path(node), flash[:locked_node_path]
118 assert_equal "aaron", flash[:locked_by]
119 end
120
121 test "create with headline against a page that has one keeps the incumbent and warns" do
122 node = Node.root.children.create!(:slug => "asset_attach_headline")
123 incumbent = Asset.create!(:name => 'Incumbent', :upload_content_type => 'image/png')
124 node.draft.related_assets.create!(:asset => incumbent, :headline => true)
125
126 uploaded = Rack::Test::UploadedFile.new(
127 Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), 'image/png')
128 post :create, params: { asset: { name: 'Challenger', upload: uploaded },
129 node_id: node.id, headline: "1" }
130
131 assert_includes node.draft.assets.reload, Asset.last
132 assert_equal incumbent, node.draft.reload.headline_asset
133 assert_equal node_path(node), flash[:headline_kept_path]
99 end 134 end
100 135
101 # --- edit --- 136 # --- edit ---
@@ -137,7 +172,7 @@ class AssetsControllerTest < ActionController::TestCase
137 ) 172 )
138 post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } 173 post :create, params: { asset: { name: 'To be deleted', upload: uploaded } }
139 asset = Asset.last 174 asset = Asset.last
140 upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s) 175 upload_dir = asset.send(:upload_root).join(asset.id.to_s)
141 assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" 176 assert Dir.exist?(upload_dir), "Upload directory should exist before destroy"
142 177
143 assert_difference 'Asset.count', -1 do 178 assert_difference 'Asset.count', -1 do