fixing specs

This commit is contained in:
tejaswini chile 2022-09-30 16:28:09 +05:30
parent ce37720b22
commit bdc99c7fc0
2 changed files with 23 additions and 2 deletions

View file

@ -43,8 +43,9 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder
return if contact.present? return if contact.present?
@contact = Contact.create!(contact_params.except(:remote_avatar_url)) @contact = Contact.create!(contact_params.except(:remote_avatar_url))
@contact_inbox = ContactInbox.find_or_create_by!(inbox: @inbox, source_id: @sender_id) @contact_inbox = ContactInbox.find_or_initialize_by(inbox: @inbox, source_id: @sender_id)
@contact_inbox.update(contact: contact) if @contact_inbox.present? && @contact.present? @contact_inbox.contact = @contact if @contact_inbox.present? && @contact.present?
@contact_inbox.save!
end end
def build_message def build_message

View file

@ -32,6 +32,26 @@ describe ::Messages::Facebook::MessageBuilder do
expect(message.content).to eq('facebook message') expect(message.content).to eq('facebook message')
end end
it 'creates contact without uniq index violation' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return(
{
first_name: 'Jane',
last_name: 'Dae',
account_id: facebook_channel.inbox.account_id,
profile_pic: 'https://chatwoot-assets.local/sample.png'
}.with_indifferent_access
)
message_builder
message_builder
contact = facebook_channel.inbox.contacts.first
expect(contact.name).to eq('Jane Dae')
expect(facebook_channel.inbox.contacts.count).to eq(1)
expect(facebook_channel.inbox.contact_inboxes.count).to eq(1)
end
it 'increments channel authorization_error_count when error is thrown' do it 'increments channel authorization_error_count when error is thrown' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::AuthenticationError.new(500, 'Error validating access token')) allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::AuthenticationError.new(500, 'Error validating access token'))