demarches-normaliennes/spec/models/dossier_transfer_spec.rb
Paul Chavard 9d5c5447c2 fix(dossier): do not attempt to destroy transfers when a dossier is destroyed
we had a dependent option on both sides of a relationship which created race conditions
2021-10-06 17:51:09 +02:00

61 lines
1.8 KiB
Ruby

require 'rails_helper'
RSpec.describe DossierTransfer, type: :model do
let(:user) { create(:user) }
let(:other_user) { create(:user) }
let(:dossier) { create(:dossier, user: user) }
describe 'initiate' do
subject { DossierTransfer.initiate(other_user.email, [dossier]) }
it 'should send transfer request' do
expect(subject.email).to eq(other_user.email)
expect(subject.dossiers).to eq([dossier])
expect(dossier.transfer).to eq(subject)
expect(dossier.user).to eq(user)
expect(dossier.transfer_logs.count).to eq(0)
end
describe 'accept' do
let(:transfer_log) { dossier.transfer_logs.first }
before do
DossierTransfer.accept(subject.id, other_user)
dossier.reload
end
it 'should transfer dossier' do
expect(DossierTransfer.count).to eq(0)
expect(dossier.transfer).to be_nil
expect(dossier.user).to eq(other_user)
expect(dossier.transfer_logs.count).to eq(1)
expect(transfer_log.dossier).to eq(dossier)
expect(transfer_log.from).to eq(user.email)
expect(transfer_log.to).to eq(other_user.email)
end
end
describe 'with_dossiers' do
before { subject }
it { expect(DossierTransfer.with_dossiers.count).to eq(1) }
context "when dossier discarded" do
before { dossier.discard! }
it { expect(DossierTransfer.with_dossiers.count).to eq(0) }
end
end
describe 'dossier relationship' do
let(:transfer) { create(:dossier_transfer) }
let(:dossier) { create(:dossier, user: user, transfer: transfer) }
it 'nullify transfer relationship on dossier' do
expect(dossier.transfer).to eq(transfer)
transfer.destroy
expect(dossier.reload.transfer).to be_nil
end
end
end
end