fix(data): clean Champs::PieceJustificativeChamp for annotations that had been cloned

This commit is contained in:
mfo 2024-05-21 14:21:21 +02:00
parent 4ea601de79
commit 29ef5b313c
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,54 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe BackfillClonedChampsPrivatePieceJustificativesTask do
describe "#process" do
let(:procedure) { create(:procedure, types_de_champ_private:) }
let(:types_de_champ_private) { [{ type: :piece_justificative }, { type: :text }] }
let(:parent_dossier) { create(:dossier, procedure:) }
let(:cloned_dossier) { create(:dossier, procedure:) }
let(:parent_champ_pj) { parent_dossier.champs_private.find(&:piece_justificative?) }
let(:cloned_champ_pj) { cloned_dossier.champs_private.find(&:piece_justificative?) }
before do
cloned_dossier.update(parent_dossier:) # used on factorie, does not seed private_champs..
parent_champ_pj.piece_justificative_file.attach(
io: StringIO.new("x" * 2),
filename: "me.jpg",
content_type: "image/png",
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
subject { described_class.process(cloned_dossier) }
context 'when dossier and parent have the same pjs' do
it 'detaches sames blob between parent_dossier and dossier' do
cloned_champ_pj.piece_justificative_file.attach(parent_champ_pj.piece_justificative_file.first.blob)
subject
expect(cloned_champ_pj.reload.piece_justificative_file.attached?).to be_falsey
end
end
context 'when dossier and parent have different pjs' do
it 'keeps different blobs between parent_dossier and dossier' do
cloned_champ_pj.piece_justificative_file.attach(
io: StringIO.new("x" * 2),
filename: "me.jpg",
content_type: "image/png",
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
described_class.process(cloned_dossier)
subject
expect(cloned_champ_pj.reload.piece_justificative_file.attached?).to be_truthy
end
end
end
end
end