ad1b10368d
* Exclude export/import blobs and attachments from account exports Export and import file blobs were being assigned the account's account_id (via the Current.account default), causing subsequent exports to include previous export/import ZIP files in the data. This roughly doubled the export size each time and made imports fail with an IntegrityError on unrecognized "Account::Export" record type. Filter out blobs attached to Export/Import records in BlobRecordSet, FileRecordSet, and a new AttachmentRecordSet. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Use STI base type for export blob exclusion filter Rails polymorphic_name returns base_class.name for STI models, so ActiveStorage stores record_type as "Export" rather than the concrete subclass names. Filter on the base type accordingly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix export filtering for mixed-use Active Storage blobs Exclude only internal-only blobs for account export metadata/file sets, scope attachment subqueries by account, and add regression coverage for blobs shared between user records and export attachments. * Simplify export blob filtering Exports always create fresh blobs, so a blob can never be legitimately shared between an Export/Import and a real account record. Replace the internal_only/external split with a single excluded_blob_ids subquery and drop the contrived shared-blob test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
111 lines
4.0 KiB
Ruby
111 lines
4.0 KiB
Ruby
require "test_helper"
|
|
|
|
class Account::ExportTest < ActiveSupport::TestCase
|
|
test "build_later enqueues DataExportJob" do
|
|
export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
|
|
assert_enqueued_with(job: DataExportJob, args: [ export ]) do
|
|
export.build_later
|
|
end
|
|
end
|
|
|
|
test "build sets status to failed on error" do
|
|
export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
ZipFile.stubs(:create_for).raises(StandardError.new("Test error"))
|
|
|
|
assert_raises(StandardError) do
|
|
export.build
|
|
end
|
|
|
|
assert export.failed?
|
|
end
|
|
|
|
test "cleanup deletes exports completed more than 24 hours ago" do
|
|
old_export = Account::Export.create!(account: Current.account, user: users(:david), status: :completed, completed_at: 25.hours.ago)
|
|
recent_export = Account::Export.create!(account: Current.account, user: users(:david), status: :completed, completed_at: 23.hours.ago)
|
|
pending_export = Account::Export.create!(account: Current.account, user: users(:david), status: :pending)
|
|
|
|
Export.cleanup
|
|
|
|
assert_not Export.exists?(old_export.id)
|
|
assert Export.exists?(recent_export.id)
|
|
assert Export.exists?(pending_export.id)
|
|
end
|
|
|
|
test "build generates zip with account data" do
|
|
export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
|
|
export.build
|
|
|
|
assert export.completed?
|
|
assert export.file.attached?
|
|
assert_equal "application/zip", export.file.content_type
|
|
end
|
|
|
|
test "build includes blob files in zip" do
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
|
io: file_fixture("moon.jpg").open,
|
|
filename: "moon.jpg",
|
|
content_type: "image/jpeg"
|
|
)
|
|
export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
|
|
export.build
|
|
|
|
assert export.completed?
|
|
export.file.open do |file|
|
|
reader = ZipKit::FileReader.read_zip_structure(io: file)
|
|
entry = reader.find { |e| e.filename == "storage/#{blob.key}" }
|
|
assert entry, "Expected blob file in zip"
|
|
end
|
|
end
|
|
|
|
test "export excludes blobs and attachments from previous exports" do
|
|
first_export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
first_export.build
|
|
assert first_export.completed?
|
|
|
|
first_export_blob = first_export.file.blob
|
|
first_export_attachment = first_export.file.attachment
|
|
|
|
second_export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
second_export.build
|
|
assert second_export.completed?
|
|
|
|
second_export.file.open do |file|
|
|
reader = ZipKit::FileReader.read_zip_structure(io: file)
|
|
filenames = reader.map(&:filename)
|
|
|
|
blob_entries = filenames.select { |f| f.start_with?("data/active_storage_blobs/") }
|
|
blob_ids = blob_entries.map { |f| File.basename(f, ".json") }
|
|
assert_not_includes blob_ids, first_export_blob.id, "Export should not include blob metadata from previous exports"
|
|
|
|
storage_entries = filenames.select { |f| f.start_with?("storage/") }
|
|
assert_not storage_entries.any? { |f| f == "storage/#{first_export_blob.key}" },
|
|
"Export should not include blob file from previous exports"
|
|
|
|
attachment_entries = filenames.select { |f| f.start_with?("data/active_storage_attachments/") }
|
|
attachment_ids = attachment_entries.map { |f| File.basename(f, ".json") }
|
|
assert_not_includes attachment_ids, first_export_attachment.id,
|
|
"Export should not include attachment records from previous exports"
|
|
end
|
|
end
|
|
|
|
test "build succeeds when rich text references missing blob" do
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
|
io: file_fixture("moon.jpg").open,
|
|
filename: "moon.jpg",
|
|
content_type: "image/jpeg"
|
|
)
|
|
card = cards(:logo)
|
|
card.update!(description: "<action-text-attachment sgid=\"#{blob.attachable_sgid}\"></action-text-attachment>")
|
|
ActiveStorage::Blob.where(id: blob.id).delete_all
|
|
|
|
export = Account::Export.create!(account: Current.account, user: users(:david))
|
|
|
|
export.build
|
|
|
|
assert export.completed?
|
|
end
|
|
end
|