Browse Source

Maintenance: Improve XOAUTH2 and EmailHelper::Probe tests.

Florian Liebe 2 years ago
parent
commit
5a6e1da194

+ 7 - 0
app/models/channel/driver/smtp.rb

@@ -2,6 +2,11 @@
 
 class Channel::Driver::Smtp
 
+  # we're using the same timeouts like in Net::SMTP gem
+  #   but we would like to have the possibility to mock it for tests
+  DEFAULT_OPEN_TIMEOUT = 30.seconds
+  DEFAULT_READ_TIMEOUT = 60.seconds
+
 =begin
 
   instance = Channel::Driver::Smtp.new
@@ -67,6 +72,8 @@ class Channel::Driver::Smtp
       port:                 options[:port],
       domain:               options[:domain],
       enable_starttls_auto: options[:enable_starttls_auto],
+      open_timeout:         DEFAULT_OPEN_TIMEOUT,
+      read_timeout:         DEFAULT_READ_TIMEOUT,
     }
 
     # set ssl if needed

+ 7 - 0
spec/integration/gmail_spec.rb

@@ -7,6 +7,13 @@ RSpec.describe 'Gmail XOAUTH2', integration: true, required_envs: %w[GMAIL_REFRE
   end
 
   context 'when probing inbound' do
+    before do
+      options = channel.options[:inbound][:options]
+      options[:port] = 993
+
+      imap_delete_old_mails(options)
+    end
+
     it 'succeeds' do
       result = EmailHelper::Probe.inbound(channel.options[:inbound])
       expect(result[:result]).to eq('ok')

+ 7 - 0
spec/integration/microsoft365_spec.rb

@@ -7,6 +7,13 @@ RSpec.describe 'Microsoft365 XOAUTH2', integration: true, required_envs: %w[MICR
   end
 
   context 'when probing inbound' do
+    before do
+      options = channel.options[:inbound][:options]
+      options[:port] = 993
+
+      imap_delete_old_mails(options)
+    end
+
     it 'succeeds' do
       result = EmailHelper::Probe.inbound(channel.options[:inbound])
       expect(result[:result]).to eq('ok')

+ 53 - 12
spec/lib/email_helper/probe_spec.rb

@@ -79,6 +79,10 @@ RSpec.describe EmailHelper::Probe, integration: true do
       let(:host)          { '192.168.254.254' }
       let(:message_human) { [ 'Host not reachable!', 'No route to host!' ] }
 
+      before do
+        stub_const('Channel::Driver::Imap::CHECK_ONLY_TIMEOUT', 1.second)
+      end
+
       include_examples 'probe tests with invalid result'
     end
 
@@ -93,6 +97,10 @@ RSpec.describe EmailHelper::Probe, integration: true do
       let(:host)          { 'mx2.zammad.com' }
       let(:message_human) { [ 'Authentication failed!', 'Host not reachable!' ] }
 
+      before do
+        stub_const('Channel::Driver::Imap::CHECK_ONLY_TIMEOUT', 1.second)
+      end
+
       include_examples 'probe tests with invalid result'
     end
 
@@ -154,6 +162,11 @@ RSpec.describe EmailHelper::Probe, integration: true do
       let(:host)          { '192.168.254.254' }
       let(:message_human) { [ 'Host not reachable!', 'No route to host!' ] }
 
+      before do
+        stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 2.seconds)
+        stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 4.seconds)
+      end
+
       include_examples 'probe tests with invalid result'
     end
 
@@ -169,6 +182,11 @@ RSpec.describe EmailHelper::Probe, integration: true do
       let(:port)          { 587 }
       let(:message_human) { 'Authentication failed!' }
 
+      before do
+        stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
+        stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
+      end
+
       include_examples 'probe tests with invalid result'
     end
 
@@ -191,6 +209,11 @@ RSpec.describe EmailHelper::Probe, integration: true do
         }
       end
 
+      before do
+        stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
+        stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
+      end
+
       it { is_expected.to include(result: 'ok') }
     end
   end
@@ -224,22 +247,25 @@ RSpec.describe EmailHelper::Probe, integration: true do
 
       shared_examples 'do real testing' do
         it 'contains all information for a successful probe' do # rubocop:disable RSpec/ExampleLength
-
-          expect(probe_result).to include(
-            result:  'ok',
-            setting: include(
-              inbound:  include(
-                options: include(
-                  host: inbound_host
+          expect(probe_result).to include(result: 'ok')
+            .and include(
+              setting: include(
+                inbound: include(
+                  options: include(
+                    host: inbound_host
+                  ),
                 ),
               ),
-              outbound: include(
-                options: include(
-                  host: outbound_host,
+            )
+            .and include(
+              setting: include(
+                outbound: include(
+                  options: include(
+                    host: outbound_host,
+                  ),
                 ),
               ),
-            ),
-          )
+            )
         end
       end
 
@@ -248,6 +274,21 @@ RSpec.describe EmailHelper::Probe, integration: true do
         let(:inbound_host)   { 'mx2.zammad.com' }
         let(:outbound_host)  { inbound_host }
 
+        before do
+          stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 5.seconds)
+          stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 10.seconds)
+
+          options = {
+            host:      inbound_host,
+            port:      993,
+            ssl:       true,
+            auth_type: 'LOGIN',
+            user:      email,
+            password:  password,
+          }
+          imap_delete_old_mails(options)
+        end
+
         include_examples 'do real testing'
       end
 

+ 22 - 0
spec/support/imap.rb

@@ -0,0 +1,22 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+module ImapHelper
+  def imap_delete_old_mails(options)
+    imap = ::Net::IMAP.new(options[:host], options[:port], options[:ssl], nil, false)
+    imap.authenticate(options[:auth_type], options[:user], options[:password])
+    imap.select('INBOX')
+
+    message_ids = imap.search(['BEFORE', 1.day.ago.to_date.strftime('%d-%b-%Y')])
+
+    Rails.logger.debug { "#{message_ids.count} messages in INBOX will be deleted!" } if message_ids.count.positive?
+
+    message_ids.each do |message_id|
+      imap.store(message_id, '+FLAGS', [:Deleted])
+    end
+    imap.expunge if message_ids.count.positive?
+  end
+end
+
+RSpec.configure do |config|
+  config.include ImapHelper, integration: true
+end