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.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb
index f834541..4be2e8a 100644
--- a/test/controllers/assets_controller_test.rb
+++ b/test/controllers/assets_controller_test.rb
@@ -92,6 +92,57 @@ class AssetsControllerTest < ActionController::TestCase
92 end 92 end
93 end 93 end
94 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 }
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]
134 end
135
136 test "create with node_id writes an asset_create and an asset_attach entry" do
137 node = Node.root.children.create!(:slug => "asset_log_pair")
138 assert_difference 'NodeAction.where(:action => "asset_create").count' do
139 assert_difference 'NodeAction.where(:action => "asset_attach").count' do
140 post :create, params: { asset: { name: 'Logged twice' }, node_id: node.id }
141 end
142 end
143 assert_equal users(:quentin), NodeAction.last.user
144 end
145
95 # --- edit --- 146 # --- edit ---
96 147
97 test "get edit" do 148 test "get edit" do
@@ -141,6 +192,16 @@ class AssetsControllerTest < ActionController::TestCase
141 assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy" 192 assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy"
142 end 193 end
143 194
195 test "destroy is witnessed in the action log with the current user" do
196 asset = Asset.create!(:name => 'Witness me',
197 :upload_file_name => 'w.png',
198 :upload_content_type => 'image/png')
199 assert_difference 'NodeAction.where(:action => "asset_destroy").count' do
200 delete :destroy, params: { id: asset.id }
201 end
202 assert_equal users(:quentin), NodeAction.last.user
203 end
204
144 # --- URL helpers --- 205 # --- URL helpers ---
145 206
146 test "upload url returns correct path for original" do 207 test "upload url returns correct path for original" do