2024-06-13 13:27:32 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe HelpscoutCreateConversationJob, type: :job do
|
2024-07-24 17:18:02 +02:00
|
|
|
let(:api) { instance_double("Helpscout::API") }
|
|
|
|
let(:email) { 'help@rspec.net' }
|
|
|
|
let(:subject_text) { 'Bonjour' }
|
|
|
|
let(:text) { "J'ai un pb" }
|
|
|
|
let(:tags) { ["first tag"] }
|
2024-07-30 19:14:04 +02:00
|
|
|
let(:question_type) { "lost" }
|
2024-07-24 17:18:02 +02:00
|
|
|
let(:phone) { nil }
|
2024-08-01 16:38:39 +02:00
|
|
|
let(:user) { nil }
|
|
|
|
let(:contact_form) { create(:contact_form, email:, user:, subject: subject_text, text:, tags:, phone:, question_type:) }
|
2024-06-13 13:27:32 +02:00
|
|
|
|
|
|
|
describe '#perform' do
|
2024-07-24 17:18:02 +02:00
|
|
|
before do
|
|
|
|
allow(Helpscout::API).to receive(:new).and_return(api)
|
|
|
|
allow(api).to receive(:create_conversation)
|
|
|
|
.and_return(double(
|
|
|
|
success?: true,
|
|
|
|
headers: { 'Resource-ID' => 'new-conversation-id' }
|
|
|
|
))
|
|
|
|
allow(api).to receive(:add_tags)
|
2024-07-30 19:14:04 +02:00
|
|
|
allow(api).to receive(:add_phone_number) if phone.present?
|
2024-07-24 17:18:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
subject {
|
2024-07-30 19:14:04 +02:00
|
|
|
described_class.perform_now(contact_form)
|
2024-07-24 17:18:02 +02:00
|
|
|
}
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
context 'when no file is attached' do
|
2024-06-13 13:27:32 +02:00
|
|
|
it 'sends the form without a file' do
|
2024-07-24 17:18:02 +02:00
|
|
|
subject
|
|
|
|
expect(api).to have_received(:create_conversation).with(email, subject_text, text, nil)
|
2024-07-30 19:14:04 +02:00
|
|
|
expect(api).to have_received(:add_tags).with("new-conversation-id", match_array(tags.concat(["contact form", question_type])))
|
|
|
|
expect(contact_form).to be_destroyed
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
context 'when a file is attached' do
|
2024-06-13 13:27:32 +02:00
|
|
|
before do
|
2024-07-30 19:14:04 +02:00
|
|
|
file = fixture_file_upload('spec/fixtures/files/white.png', 'image/png')
|
|
|
|
contact_form.piece_jointe.attach(file)
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file has not been scanned yet' do
|
2024-07-30 19:14:04 +02:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(ActiveStorage::Blob).to receive(:virus_scanner).and_return(double('VirusScanner', pending?: true, safe?: false))
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
it 'reenqueues job' do
|
|
|
|
expect { subject }.to have_enqueued_job(described_class).with(contact_form)
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file is safe' do
|
2024-07-30 19:14:04 +02:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(ActiveStorage::Blob).to receive(:virus_scanner).and_return(double('VirusScanner', pending?: false, safe?: true))
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
it 'sends the form with the file' do
|
2024-07-24 17:18:02 +02:00
|
|
|
subject
|
2024-07-30 19:14:04 +02:00
|
|
|
expect(api).to have_received(:create_conversation).with(email, subject_text, text, contact_form.piece_jointe)
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file is not safe' do
|
2024-07-30 19:14:04 +02:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(ActiveStorage::Blob).to receive(:virus_scanner).and_return(double('VirusScanner', pending?: false, safe?: false))
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
it 'ignores the file' do
|
2024-07-24 17:18:02 +02:00
|
|
|
subject
|
|
|
|
expect(api).to have_received(:create_conversation).with(email, subject_text, text, nil)
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-07-24 17:18:02 +02:00
|
|
|
|
|
|
|
context 'with a phone' do
|
|
|
|
let(:phone) { "06" }
|
2024-07-30 19:14:04 +02:00
|
|
|
|
2024-07-24 17:18:02 +02:00
|
|
|
it 'associates the phone number' do
|
|
|
|
subject
|
|
|
|
expect(api).to have_received(:add_phone_number).with(email, phone)
|
|
|
|
end
|
|
|
|
end
|
2024-08-01 16:38:39 +02:00
|
|
|
|
|
|
|
context 'attached to an user' do
|
|
|
|
let(:email) { nil }
|
|
|
|
let(:user) { users(:default_user) }
|
|
|
|
|
|
|
|
it 'associates the email from user' do
|
|
|
|
subject
|
|
|
|
expect(api).to have_received(:create_conversation).with(user.email, subject_text, text, nil)
|
2024-08-01 19:33:57 +02:00
|
|
|
expect(contact_form).to be_destroyed
|
|
|
|
expect(user.reload).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'having dossiers' do
|
|
|
|
before do
|
|
|
|
create(:dossier, user:)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'associates the email from user' do
|
|
|
|
subject
|
|
|
|
expect(api).to have_received(:create_conversation).with(user.email, subject_text, text, nil)
|
|
|
|
expect(contact_form).to be_destroyed
|
|
|
|
expect(user.reload).to be_truthy
|
|
|
|
end
|
2024-08-01 16:38:39 +02:00
|
|
|
end
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
end
|