Back to Repositories

Testing SMTP Email Delivery Integration in Postal Server

This test suite implements SMTP functionality testing for the Postal mail server application. It validates email delivery through a command-line interface, handling various error conditions and configuration scenarios for SMTP connections.

Test Coverage Overview

The test suite provides comprehensive coverage of SMTP email delivery functionality.

Key areas tested include:
  • Email address validation
  • SMTP connection handling
  • Timeout scenarios
  • Error handling for delivery failures
The test integrates with the AppMailer component and verifies proper configuration of SMTP settings.

Implementation Analysis

The testing approach utilizes Ruby’s native timeout and error handling mechanisms to validate SMTP functionality. The implementation follows a command-line testing pattern with explicit error messaging and configuration verification.

Technical patterns include:
  • Signal trapping for graceful termination
  • Environment-aware configuration loading
  • Colored console output for status indication

Technical Details

Testing tools and configuration:
  • Ruby standard library (Timeout module)
  • Environment-based configuration loading
  • SMTP settings validation
  • AppMailer integration
  • Command-line argument parsing

Best Practices Demonstrated

The test implementation showcases several testing best practices.

Notable practices include:
  • Graceful error handling with detailed feedback
  • Configuration verification
  • User-friendly command-line interface
  • Timeout implementation for non-blocking execution
  • Clear success/failure status indication

postalserver/postal

script/test_app_smtp.rb

            
#!/usr/bin/env ruby
# frozen_string_literal: true

trap("INT") do
  puts
  exit
end

if ARGV[0].nil? || ARGV[0] !~ /@/
  puts "usage: postal test-app-smtp [email address]"
  exit 1
end

require_relative "../config/environment"

begin
  Timeout.timeout(10) do
    AppMailer.test_message(ARGV[0]).deliver
  end

  puts "\e[32mMessage has been sent successfully.\e[0m"
rescue Timeout::Error
  puts "Sending timed out"
rescue StandardError => e
  puts "\e[31mMessage was not delivered successfully to SMTP server.\e[0m"
  puts "Error: #{e.class} (#{e.message})"
  puts
  puts "  SMTP Host: #{Postal::Config.smtp.host}"
  puts "  SMTP Port: #{Postal::Config.smtp.port}"
  puts "  SMTP Username: #{Postal::Config.smtp.username}"
  puts "  SMTP Password: #{Postal::Config.smtp.password}"
  puts
end