2020-04-05 16:41:27 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
describe ::ContactInboxWithContactBuilder do
|
2020-04-05 16:41:27 +00:00
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
2021-09-04 12:26:46 +00:00
|
|
|
let(:contact) { create(:contact, email: 'xyc@example.com', phone_number: '+23423424123', account: account, identifier: '123') }
|
2020-04-05 16:41:27 +00:00
|
|
|
let(:existing_contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
|
|
|
|
|
|
|
|
describe '#perform' do
|
2021-04-20 17:12:14 +00:00
|
|
|
it 'doesnot create contact if it already exist with source id' do
|
2020-04-05 16:41:27 +00:00
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: existing_contact_inbox.source_id,
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
phone_number: '+1234567890',
|
|
|
|
email: 'testemail@example.com'
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).to be(contact.id)
|
|
|
|
end
|
|
|
|
|
2021-04-20 17:12:14 +00:00
|
|
|
it 'creates contact if contact doesnot exist with source id' do
|
2020-04-05 16:41:27 +00:00
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: '123456',
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
phone_number: '+1234567890',
|
2021-12-19 05:29:03 +00:00
|
|
|
email: 'testemail@example.com',
|
|
|
|
custom_attributes: { test: 'test' }
|
2020-04-05 16:41:27 +00:00
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).not_to eq(contact.id)
|
|
|
|
expect(contact_inbox.contact.name).to eq('Contact')
|
2021-12-19 05:29:03 +00:00
|
|
|
expect(contact_inbox.contact.custom_attributes).to eq({ 'test' => 'test' })
|
2020-04-05 16:41:27 +00:00
|
|
|
expect(contact_inbox.inbox_id).to eq(inbox.id)
|
|
|
|
end
|
2021-04-20 17:12:14 +00:00
|
|
|
|
|
|
|
it 'doesnot create contact if it already exist with identifier' do
|
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: '123456',
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
identifier: contact.identifier,
|
|
|
|
phone_number: contact.phone_number,
|
|
|
|
email: 'testemail@example.com'
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).to be(contact.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'doesnot create contact if it already exist with email' do
|
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: '123456',
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
phone_number: '+1234567890',
|
|
|
|
email: contact.email
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).to be(contact.id)
|
|
|
|
end
|
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
it 'doesnot create contact when an uppercase email is passed for an already existing contact email' do
|
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: '123456',
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
phone_number: '+1234567890',
|
|
|
|
email: contact.email.upcase
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).to be(contact.id)
|
|
|
|
end
|
|
|
|
|
2021-04-20 17:12:14 +00:00
|
|
|
it 'doesnot create contact if it already exist with phone number' do
|
|
|
|
contact_inbox = described_class.new(
|
|
|
|
source_id: '123456',
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: 'Contact',
|
|
|
|
phone_number: contact.phone_number,
|
|
|
|
email: 'testemail@example.com'
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
|
|
|
|
expect(contact_inbox.contact.id).to be(contact.id)
|
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
end
|