From dd40bce5ff080885e017709325fb49e9ccd55655 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Wed, 22 Jul 2026 21:00:23 +0200 Subject: Add tests for the asset<->nodes attach cycle and the flash notice survival fix --- test/controllers/assets_controller_test.rb | 41 ++++++++++++++++++++++++++++++ test/controllers/nodes_controller_test.rb | 29 +++++++++++++++++++++ 2 files changed, 70 insertions(+) (limited to 'test') diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index f834541..05fc6de 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb @@ -92,6 +92,47 @@ class AssetsControllerTest < ActionController::TestCase end end + # --- create with attach --- + + test "create with node_id attaches the asset to the node's draft" do + node = Node.root.children.create!(:slug => "asset_attach_target") + + post :create, params: { asset: { name: 'Attach me' }, node_id: node.id } + + assert_response :redirect + asset = Asset.last + assert_includes node.draft.assets.reload, asset + assert_match /attached/, flash[:notice] + end + + test "create against a foreign-locked node keeps the asset but refuses the attach" do + node = Node.root.children.create!(:slug => "asset_attach_locked") + node.lock_for_editing!(users(:aaron)) + + assert_difference 'Asset.count', 1 do + post :create, params: { asset: { name: 'Orphaned for now' }, node_id: node.id } + end + + assert_empty node.draft.assets.reload + assert_equal node_path(node), flash[:locked_node_path] + assert_equal "aaron", flash[:locked_by] + end + + test "create with headline against a page that has one keeps the incumbent and warns" do + node = Node.root.children.create!(:slug => "asset_attach_headline") + incumbent = Asset.create!(:name => 'Incumbent', :upload_content_type => 'image/png') + node.draft.related_assets.create!(:asset => incumbent, :headline => true) + + uploaded = Rack::Test::UploadedFile.new( + Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), 'image/png') + post :create, params: { asset: { name: 'Challenger', upload: uploaded }, + node_id: node.id, headline: "1" } + + assert_includes node.draft.assets.reload, Asset.last + assert_equal incumbent, node.draft.reload.headline_asset + assert_equal node_path(node), flash[:headline_kept_path] + end + # --- edit --- test "get edit" do diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb index 55da524..d777108 100644 --- a/test/controllers/nodes_controller_test.rb +++ b/test/controllers/nodes_controller_test.rb @@ -79,6 +79,35 @@ class NodesControllerTest < ActionController::TestCase end end + test "create with asset_id attaches the asset to the new draft, as headline when asked" do + login_as :quentin + asset = Asset.create!(:name => 'Birth attachment', :upload_content_type => 'image/png') + + post :create, params: { :kind => "generic", :parent_id => Node.root.id, + :title => "Born Attached", + :asset_id => asset.id, :asset_headline => "1" } + + assert_response :redirect + node = Node.last + assert_includes node.draft.assets, asset + assert_equal asset, node.draft.headline_asset + assert_match /attached/, flash[:notice] + end + + test "the attach notice survives the redirect into the editor" do + login_as :quentin + asset = Asset.create!(:name => 'Flash survivor', :upload_content_type => 'image/png') + + post :create, params: { :kind => "generic", :parent_id => Node.root.id, + :title => "Notice Carrier", :asset_id => asset.id } + assert_redirected_to edit_node_path(Node.last) + + get :edit, params: { :id => Node.last.id } + assert_response :success + assert_match /attached/, flash[:notice] + assert_no_match /ready to edit/, flash[:notice] + end + test "editing a node" do login_as :quentin -- cgit v1.3