# Pull Request Template ## Description This PR adds IMAP authentication mechanism selection to Chatwoot's email inbox configuration. Users can now choose between 'plain', 'login', and 'cram-md5' authentication methods when configuring IMAP settings, providing flexibility for different email providers that require specific authentication types. https://github.com/chatwoot/chatwoot/issues/8867 The implementation includes: - Frontend dropdown with numeric keys (1, 2, 3) matching SMTP auth style - Backend API validation for allowed authentication mechanisms - Consistent 'cram-md5' format throughout the codebase - Updated IMAP service to handle different auth types properly This feature maintains consistency with existing SMTP authentication options and follows the established UI/UX patterns in the application. ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? ### Manual Testing: - Tested in Docker environment - Verified IMAP auth dropdown appears in inbox settings - Confirmed all three auth mechanisms (plain, login, cram-md5) can be selected and saved - Tested API validation by attempting to save invalid auth mechanisms ### Automated Testing: - Updated existing IMAP service tests to use consistent lowercase values - Updated API controller tests for authentication parameter handling - All tests pass locally with the new changes ### Test Configuration: - Tested with both new and existing inbox configurations ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules ## Additional Notes - This feature is backward compatible and doesn't break existing IMAP configurations - The 'cram-md5' format is used consistently throughout (UI, API, storage, services) - Net::IMAP compatibility is maintained by converting to 'CRAM-MD5' internally - Follows the same pattern established by SMTP authentication configuration --------- Co-authored-by: João Santos <joao.santos@madigital.eu> Co-authored-by: Sony Mathew <sony@chatwoot.com>
152 lines
7.3 KiB
Ruby
152 lines
7.3 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Imap::FetchEmailService do
|
|
include ActionMailbox::TestHelper
|
|
let(:logger) { instance_double(ActiveSupport::Logger, info: true, error: true) }
|
|
let(:account) { create(:account) }
|
|
let(:imap_email_channel) { create(:channel_email, :imap_email, account: account) }
|
|
let(:imap) { instance_double(Net::IMAP) }
|
|
let(:eml_content_with_message_id) { Rails.root.join('spec/fixtures/files/only_text.eml').read }
|
|
let(:eml_content_without_message_id) { eml_content_with_message_id.sub(/^Message-ID:.*\n/, '') }
|
|
|
|
describe '#perform' do
|
|
before do
|
|
allow(Rails).to receive(:logger).and_return(logger)
|
|
allow(Net::IMAP).to receive(:new).with(
|
|
imap_email_channel.imap_address, port: imap_email_channel.imap_port, ssl: imap_email_channel.imap_enable_ssl
|
|
).and_return(imap)
|
|
allow(imap).to receive(:authenticate).with(
|
|
'plain', imap_email_channel.imap_login, imap_email_channel.imap_password
|
|
)
|
|
allow(imap).to receive(:select).with('INBOX')
|
|
end
|
|
|
|
context 'when using CRAM-MD5 authentication' do
|
|
let(:cram_md5_channel) { create(:channel_email, :imap_email, account: account, imap_authentication: 'cram-md5') }
|
|
|
|
before do
|
|
allow(Net::IMAP).to receive(:new).with(
|
|
cram_md5_channel.imap_address, port: cram_md5_channel.imap_port, ssl: cram_md5_channel.imap_enable_ssl
|
|
).and_return(imap)
|
|
allow(imap).to receive(:authenticate).with(
|
|
'CRAM-MD5', cram_md5_channel.imap_login, cram_md5_channel.imap_password
|
|
)
|
|
allow(imap).to receive(:select).with('INBOX')
|
|
end
|
|
|
|
it 'uses CRAM-MD5 authentication' do
|
|
travel_to '26.10.2020 10:00'.to_datetime do
|
|
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([])
|
|
allow(imap).to receive(:logout)
|
|
|
|
described_class.new(channel: cram_md5_channel).perform
|
|
|
|
expect(imap).to have_received(:authenticate).with(
|
|
'CRAM-MD5', cram_md5_channel.imap_login, cram_md5_channel.imap_password
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when using LOGIN authentication' do
|
|
let(:login_channel) { create(:channel_email, :imap_email, account: account, imap_authentication: 'login') }
|
|
|
|
before do
|
|
allow(Net::IMAP).to receive(:new).with(
|
|
login_channel.imap_address, port: login_channel.imap_port, ssl: login_channel.imap_enable_ssl
|
|
).and_return(imap)
|
|
allow(imap).to receive(:login).with(
|
|
login_channel.imap_login, login_channel.imap_password
|
|
)
|
|
allow(imap).to receive(:select).with('INBOX')
|
|
end
|
|
|
|
it 'uses LOGIN authentication' do
|
|
travel_to '26.10.2020 10:00'.to_datetime do
|
|
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([])
|
|
allow(imap).to receive(:logout)
|
|
|
|
described_class.new(channel: login_channel).perform
|
|
|
|
expect(imap).to have_received(:login).with(
|
|
login_channel.imap_login, login_channel.imap_password
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when new emails are available in the mailbox' do
|
|
it 'fetches the emails and returns the emails that are not present in the db' do
|
|
travel_to '26.10.2020 10:00'.to_datetime do
|
|
email_object = create_inbound_email_from_fixture('only_text.eml')
|
|
email_header = Net::IMAP::FetchData.new(1, 'BODY[HEADER]' => eml_content_with_message_id)
|
|
imap_fetch_mail = Net::IMAP::FetchData.new(1, 'RFC822' => eml_content_with_message_id)
|
|
|
|
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1])
|
|
allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header])
|
|
allow(imap).to receive(:fetch).with(1, 'RFC822').and_return([imap_fetch_mail])
|
|
allow(imap).to receive(:logout)
|
|
|
|
result = described_class.new(channel: imap_email_channel).perform
|
|
|
|
expect(result.length).to eq 1
|
|
expect(result[0].message_id).to eq email_object.message_id
|
|
expect(imap).to have_received(:search).with(%w[SINCE 25-Oct-2020])
|
|
expect(imap).to have_received(:fetch).with([1], 'BODY.PEEK[HEADER]')
|
|
expect(imap).to have_received(:fetch).with(1, 'RFC822')
|
|
expect(logger).to have_received(:info).with("[IMAP::FETCH_EMAIL_SERVICE] Fetching mails from #{imap_email_channel.email}, found 1.")
|
|
expect(imap).to have_received(:logout)
|
|
end
|
|
end
|
|
|
|
it 'fetches the emails and returns the mail objects that are not present in the db' do
|
|
travel_to '26.10.2020 10:00'.to_datetime do
|
|
email_object = create_inbound_email_from_fixture('only_text.eml')
|
|
create(:message, source_id: email_object.message_id, account: account, inbox: imap_email_channel.inbox)
|
|
|
|
email_header = Net::IMAP::FetchData.new(1, 'BODY[HEADER]' => eml_content_with_message_id)
|
|
|
|
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1])
|
|
allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header])
|
|
allow(imap).to receive(:logout)
|
|
|
|
result = described_class.new(channel: imap_email_channel).perform
|
|
|
|
expect(result.length).to eq 0
|
|
expect(imap).to have_received(:search).with(%w[SINCE 25-Oct-2020])
|
|
expect(imap).to have_received(:fetch).with([1], 'BODY.PEEK[HEADER]')
|
|
expect(imap).not_to have_received(:fetch).with(1, 'RFC822')
|
|
end
|
|
end
|
|
|
|
it 'does not count emails without message ids toward the sync limit' do
|
|
travel_to '26.10.2020 10:00'.to_datetime do
|
|
email_object = create_inbound_email_from_fixture('only_text.eml')
|
|
max_messages_per_sync = Imap::BaseFetchEmailService::MAX_MESSAGES_PER_SYNC
|
|
empty_message_id_seq_nums = (1..max_messages_per_sync).to_a
|
|
valid_message_seq_num = max_messages_per_sync + 1
|
|
empty_message_id_headers = empty_message_id_seq_nums.map do |seq_num|
|
|
Net::IMAP::FetchData.new(seq_num, 'BODY[HEADER]' => eml_content_without_message_id)
|
|
end
|
|
valid_email_header = Net::IMAP::FetchData.new(valid_message_seq_num, 'BODY[HEADER]' => eml_content_with_message_id)
|
|
imap_fetch_mail = Net::IMAP::FetchData.new(valid_message_seq_num, 'RFC822' => eml_content_with_message_id)
|
|
|
|
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return(empty_message_id_seq_nums + [valid_message_seq_num])
|
|
allow(imap).to receive(:fetch).with(empty_message_id_seq_nums, 'BODY.PEEK[HEADER]').and_return(empty_message_id_headers)
|
|
allow(imap).to receive(:fetch).with([valid_message_seq_num], 'BODY.PEEK[HEADER]').and_return([valid_email_header])
|
|
allow(imap).to receive(:fetch).with(valid_message_seq_num, 'RFC822').and_return([imap_fetch_mail])
|
|
allow(imap).to receive(:logout)
|
|
|
|
result = described_class.new(channel: imap_email_channel).perform
|
|
|
|
expect(result.length).to eq 1
|
|
expect(result[0].message_id).to eq email_object.message_id
|
|
expect(imap).to have_received(:fetch).with(empty_message_id_seq_nums, 'BODY.PEEK[HEADER]')
|
|
expect(imap).to have_received(:fetch).with([valid_message_seq_num], 'BODY.PEEK[HEADER]')
|
|
expect(imap).to have_received(:fetch).with(valid_message_seq_num, 'RFC822')
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|