fix: validate instagram story only while saving the message (#6340)

This commit is contained in:
Tejaswini Chile
2023-01-30 13:03:59 +05:30
committed by GitHub
parent 6bd4e8853b
commit 6013cc9bea
8 changed files with 74 additions and 8 deletions

View File

@@ -25,7 +25,6 @@ RSpec.describe Attachment, type: :model do
it 'returns external url as data and thumb urls' do
external_url = instagram_message.attachments.first.external_url
expect(instagram_message.attachments.first.push_event_data[:data_url]).to eq external_url
expect(instagram_message.attachments.first.push_event_data[:thumb_url]).to eq external_url
end
end
end

View File

@@ -30,6 +30,42 @@ RSpec.describe Channel::FacebookPage do
channel.prompt_reauthorization!
end
end
context 'when fetch instagram story' do
let!(:account) { create(:account) }
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
let(:fb_object) { double }
let(:message) { create(:message, inbox_id: instagram_inbox.id) }
let(:instagram_message) { create(:message, :instagram_story_mention, inbox_id: instagram_inbox.id) }
it '#fetch_instagram_story_link' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_return(
{ story:
{
mention: {
link: 'https://www.example.com/test.jpeg',
id: '17920786367196703'
}
},
from: {
username: 'Sender-id-1', id: 'Sender-id-1'
},
id: 'instagram-message-id-1234' }.with_indifferent_access
)
story_link = instagram_channel.fetch_instagram_story_link(message)
expect(story_link).to eq('https://www.example.com/test.jpeg')
end
it '#delete_instagram_story' do
expect(instagram_message.attachments.count).to eq(1)
instagram_channel.delete_instagram_story(instagram_message)
expect(instagram_message.attachments.count).to eq(0)
end
end
end
it 'has a valid name' do

View File

@@ -198,21 +198,21 @@ RSpec.describe Message, type: :model do
}.to_json, headers: {})
end
it 'deletes the attachment for deleted stories' do
it 'keeps the attachment for deleted stories' do
expect(instagram_message.attachments.count).to eq 1
stub_request(:get, %r{https://graph.facebook.com/.*}).to_return(status: 404)
instagram_message.push_event_data
expect(instagram_message.reload.attachments.count).to eq 0
expect(instagram_message.reload.attachments.count).to eq 1
end
it 'deletes the attachment for expired stories' do
it 'keeps the attachment for expired stories' do
expect(instagram_message.attachments.count).to eq 1
# for expired stories, the link will be empty
stub_request(:get, %r{https://graph.facebook.com/.*}).to_return(status: 200, body: {
story: { mention: { link: '', id: '17920786367196703' } }
}.to_json, headers: {})
instagram_message.push_event_data
expect(instagram_message.reload.attachments.count).to eq 0
expect(instagram_message.reload.attachments.count).to eq 1
end
end
end