2021-11-19 06:22:27 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Inboxes::FetchImapEmailsJob, type: :job do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:imap_email_channel) do
|
2022-04-11 14:07:20 +00:00
|
|
|
create(:channel_email, imap_enabled: true, imap_address: 'imap.gmail.com', imap_port: 993, imap_login: 'imap@gmail.com',
|
2022-07-08 11:13:24 +00:00
|
|
|
imap_password: 'password', imap_inbox_synced_at: Time.now.utc, account: account)
|
2021-11-19 06:22:27 +00:00
|
|
|
end
|
2022-07-08 11:13:24 +00:00
|
|
|
let!(:conversation) { create(:conversation, inbox: imap_email_channel.inbox, account: account) }
|
2021-11-19 06:22:27 +00:00
|
|
|
|
|
|
|
it 'enqueues the job' do
|
|
|
|
expect { described_class.perform_later }.to have_enqueued_job(described_class)
|
|
|
|
.on_queue('low')
|
|
|
|
end
|
|
|
|
|
2022-07-08 11:13:24 +00:00
|
|
|
context 'when imap fetch new emails' do
|
|
|
|
it 'process the email' do
|
|
|
|
email = Mail.new do
|
2021-11-19 06:22:27 +00:00
|
|
|
to 'test@outlook.com'
|
|
|
|
from 'test@gmail.com'
|
|
|
|
subject :test.to_s
|
|
|
|
body 'hello'
|
|
|
|
end
|
|
|
|
|
2022-07-08 11:13:24 +00:00
|
|
|
allow(Mail).to receive(:find).and_return([email])
|
2021-11-19 06:22:27 +00:00
|
|
|
imap_mailbox = double
|
|
|
|
allow(Imap::ImapMailbox).to receive(:new).and_return(imap_mailbox)
|
2022-07-08 11:13:24 +00:00
|
|
|
expect(imap_mailbox).to receive(:process).with(email, imap_email_channel).once
|
2021-11-19 06:22:27 +00:00
|
|
|
|
|
|
|
described_class.perform_now(imap_email_channel)
|
2022-07-08 11:13:24 +00:00
|
|
|
end
|
|
|
|
end
|
2021-11-19 06:22:27 +00:00
|
|
|
|
2022-07-08 11:13:24 +00:00
|
|
|
context 'when imap fetch existing emails' do
|
|
|
|
it 'does not process the email' do
|
|
|
|
email = Mail.new do
|
|
|
|
to 'test@outlook.com'
|
|
|
|
from 'test@gmail.com'
|
|
|
|
subject :test.to_s
|
|
|
|
body 'hello'
|
|
|
|
message_id '<messageId@example.com>'
|
|
|
|
end
|
|
|
|
|
|
|
|
create(:message, message_type: 'incoming', source_id: email.message_id, account: account, inbox: imap_email_channel.inbox,
|
|
|
|
conversation: conversation)
|
|
|
|
|
|
|
|
allow(Mail).to receive(:find).and_return([email])
|
|
|
|
imap_mailbox = double
|
|
|
|
allow(Imap::ImapMailbox).to receive(:new).and_return(imap_mailbox)
|
|
|
|
expect(imap_mailbox).not_to receive(:process).with(email, imap_email_channel)
|
|
|
|
|
|
|
|
described_class.perform_now(imap_email_channel)
|
2021-11-19 06:22:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|