From 6ef98ad444631b1d5ba67bb16aaaa2afdfa53ae0 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Thu, 23 Jul 2026 04:12:39 +0200 Subject: Witness asset destruction, naming every node it strips --- test/controllers/assets_controller_test.rb | 10 ++++++ test/models/asset_destroy_test.rb | 51 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/models/asset_destroy_test.rb (limited to 'test') diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb index 05fc6ded..0e251aad 100644 --- a/test/controllers/assets_controller_test.rb +++ b/test/controllers/assets_controller_test.rb @@ -182,6 +182,16 @@ class AssetsControllerTest < ActionController::TestCase assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy" end + test "destroy is witnessed in the action log with the current user" do + asset = Asset.create!(:name => 'Witness me', + :upload_file_name => 'w.png', + :upload_content_type => 'image/png') + assert_difference 'NodeAction.where(:action => "asset_destroy").count' do + delete :destroy, params: { id: asset.id } + end + assert_equal users(:quentin), NodeAction.last.user + end + # --- URL helpers --- test "upload url returns correct path for original" do diff --git a/test/models/asset_destroy_test.rb b/test/models/asset_destroy_test.rb new file mode 100644 index 00000000..5583f685 --- /dev/null +++ b/test/models/asset_destroy_test.rb @@ -0,0 +1,51 @@ +require "test_helper" + +class AssetDestroyTest < ActiveSupport::TestCase + def setup + @user = users(:quentin) + @asset = Asset.create!(:name => "Doomed asset", + :upload_file_name => "doomed.png", + :upload_content_type => "image/png") + end + + test "destroying an attached asset logs nodes and asset as participants" do + node = Node.root.children.create!(:slug => "asset_destroy_attached") + node.attach_asset!(@asset, :user => @user) + + @asset.destroy_witnessed!(:user => @user) + + action = NodeAction.where(:action => "asset_destroy").last + subjects = action.action_participants.map { |p| [p.subject_type, p.subject_id] } + assert_includes subjects, ["Asset", @asset.id] + assert_includes subjects, ["Node", node.id] + assert_equal [node.unique_name], action.metadata["detached_from"] + end + + test "records which nodes lost their headline" do + node = Node.root.children.create!(:slug => "asset_destroy_headline") + node.attach_asset!(@asset, :user => @user, :headline => true) + + @asset.destroy_witnessed!(:user => @user) + + action = NodeAction.where(:action => "asset_destroy").last + assert_equal [node.unique_name], action.metadata["headline_removed_from"] + end + + test "an unattached asset is still witnessed" do + @asset.destroy_witnessed!(:user => @user) + + action = NodeAction.where(:action => "asset_destroy").last + assert_equal [["Asset", @asset.id]], + action.action_participants.map { |p| [p.subject_type, p.subject_id] } + assert_nil action.node_id + end + + test "the entry outlives the asset" do + @asset.destroy_witnessed!(:user => @user) + action = NodeAction.where(:action => "asset_destroy").last + + assert_not Asset.exists?(@asset.id) + assert_equal "Doomed asset", action.metadata["asset_name"] + assert_nil action.action_participants.first.subject + end +end -- cgit v1.3