2018-09-04 18:19:29 +02:00
|
|
|
describe CommentaireService do
|
2019-06-25 17:12:44 +02:00
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
2018-09-04 18:19:29 +02:00
|
|
|
describe '.create' do
|
2019-06-20 09:58:53 +02:00
|
|
|
let(:dossier) { create :dossier, :en_construction }
|
2018-09-04 18:19:29 +02:00
|
|
|
let(:sender) { dossier.user }
|
|
|
|
let(:body) { 'Contenu du message.' }
|
|
|
|
let(:file) { nil }
|
|
|
|
|
2019-06-25 17:12:44 +02:00
|
|
|
subject(:commentaire) { CommentaireService.build(sender, dossier, { body: body, piece_jointe: file }) }
|
2018-09-04 18:19:29 +02:00
|
|
|
|
|
|
|
it 'creates a new valid commentaire' do
|
|
|
|
expect(commentaire.email).to eq sender.email
|
|
|
|
expect(commentaire.dossier).to eq dossier
|
2018-11-29 15:00:26 +01:00
|
|
|
expect(commentaire.body).to eq 'Contenu du message.'
|
2019-06-25 17:12:44 +02:00
|
|
|
expect(commentaire.piece_jointe.attached?).to be_falsey
|
2018-09-04 18:19:29 +02:00
|
|
|
expect(commentaire).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the body is empty' do
|
|
|
|
let(:body) { nil }
|
|
|
|
|
|
|
|
it 'creates an invalid comment' do
|
|
|
|
expect(commentaire.body).to be nil
|
|
|
|
expect(commentaire.valid?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it has a file' do
|
2020-06-30 17:53:54 +02:00
|
|
|
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
|
2018-09-04 18:19:29 +02:00
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
it 'attaches the file' do
|
|
|
|
expect(commentaire.piece_jointe.attached?).to be_truthy
|
2018-09-04 18:19:29 +02:00
|
|
|
end
|
|
|
|
end
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.soft_delete' do
|
|
|
|
subject { CommentaireService.soft_delete(user, params) }
|
|
|
|
|
|
|
|
context 'when dossier not found' do
|
|
|
|
let(:user) { create(:instructeur) }
|
|
|
|
let(:params) { {} }
|
|
|
|
it 'returns error struct' do
|
|
|
|
expect(subject.status).to eq(false)
|
|
|
|
end
|
|
|
|
it 'returns error message' do
|
|
|
|
expect(subject.error_message).to eq("Dossier introuvable")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when commentaire not found' do
|
|
|
|
let(:user) { create(:instructeur) }
|
|
|
|
let(:params) { { dossier_id: create(:dossier).id } }
|
|
|
|
it 'returns error struct' do
|
|
|
|
expect(subject.status).to eq(false)
|
|
|
|
end
|
|
|
|
it 'returns error message' do
|
|
|
|
expect(subject.error_message).to eq("Commentaire introuvable")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when commentaire does not belongs to instructeur' do
|
|
|
|
let(:user) { create(:instructeur) }
|
|
|
|
let(:dossier) { create(:dossier) }
|
2021-11-15 14:17:57 +01:00
|
|
|
let(:params) {
|
|
|
|
{
|
|
|
|
dossier_id: dossier.id,
|
|
|
|
id: create(:commentaire, dossier: dossier, instructeur: create(:instructeur)).id
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 12:43:53 +01:00
|
|
|
it 'returns error struct' do
|
|
|
|
expect(subject.status).to eq(false)
|
|
|
|
end
|
|
|
|
it 'returns error message' do
|
2021-11-15 13:53:32 +01:00
|
|
|
expect(subject.error_message).to eq("Impossible de supprimer le message, celui ci ne vous appartient pas")
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when commentaire belongs to instructeur' do
|
|
|
|
let(:user) { create(:instructeur) }
|
|
|
|
let(:dossier) { create(:dossier) }
|
2021-11-15 12:59:38 +01:00
|
|
|
let(:commentaire) do
|
|
|
|
create(:commentaire,
|
|
|
|
dossier: dossier,
|
|
|
|
instructeur: user,
|
|
|
|
piece_jointe: fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf'))
|
|
|
|
end
|
2021-11-15 14:17:57 +01:00
|
|
|
let(:params) {
|
|
|
|
{
|
|
|
|
dossier_id: dossier.id,
|
|
|
|
id: commentaire.id
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 12:43:53 +01:00
|
|
|
it 'returns error struct' do
|
|
|
|
expect(subject.status).to eq(true)
|
|
|
|
end
|
2021-11-15 12:59:38 +01:00
|
|
|
it 'sets commentaire.body to deleted message' do
|
|
|
|
allow(commentaire.piece_jointe).to receive(:purge_later)
|
2021-11-15 14:17:57 +01:00
|
|
|
expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("Message supprimé")
|
2021-11-15 12:59:38 +01:00
|
|
|
end
|
2021-11-15 12:43:53 +01:00
|
|
|
it 'sets commentaire.body to deleted message' do
|
2021-11-15 14:17:57 +01:00
|
|
|
expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("Message supprimé")
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
2021-11-15 15:23:57 +01:00
|
|
|
it 'sets deleted_at' do
|
|
|
|
subject
|
|
|
|
expect(commentaire.reload.deleted_at).not_to be_nil
|
2021-11-15 12:43:53 +01:00
|
|
|
end
|
|
|
|
end
|
2018-09-04 18:19:29 +02:00
|
|
|
end
|
|
|
|
end
|