mirror of https://github.com/mastodon/mastodon
				
				
				
			Move email env var reading to yml files (#35191)
							parent
							
								
									2e0a00ab46
								
							
						
					
					
						commit
						48451b782d
					
				@ -0,0 +1,21 @@
 | 
			
		||||
# Note that these settings only apply in `production` even when other
 | 
			
		||||
# keys are added here.
 | 
			
		||||
production:
 | 
			
		||||
  delivery_method: <%= ENV.fetch('SMTP_DELIVERY_METHOD', 'smtp') %>
 | 
			
		||||
  from_address: <%= ENV.fetch('SMTP_FROM_ADDRESS', 'notifications@localhost') %>
 | 
			
		||||
  reply_to: <%= ENV.fetch('SMTP_REPLY_TO', nil) %>
 | 
			
		||||
  return_path: <%= ENV.fetch('SMTP_RETURN_PATH', nil) %>
 | 
			
		||||
  smtp_settings:
 | 
			
		||||
    port: <%= ENV.fetch('SMTP_PORT', nil) %>
 | 
			
		||||
    address: <%= ENV.fetch('SMTP_SERVER', nil) %>
 | 
			
		||||
    user_name: <%= ENV.fetch('SMTP_LOGIN', nil) %>
 | 
			
		||||
    password: <%= ENV.fetch('SMTP_PASSWORD', nil) %>
 | 
			
		||||
    domain: <%= ENV.fetch('SMTP_DOMAIN', ENV.fetch('LOCAL_DOMAIN', nil)) %>
 | 
			
		||||
    authentication: <%= ENV.fetch('SMTP_AUTH_METHOD', 'plain') %>
 | 
			
		||||
    ca_file: <%= ENV.fetch('SMTP_CA_FILE', '/etc/ssl/certs/ca-certificates.crt') %>
 | 
			
		||||
    openssl_verify_mode: <%= ENV.fetch('SMTP_OPENSSL_VERIFY_MODE', nil) %>
 | 
			
		||||
    enable_starttls: <%= ENV.fetch('SMTP_ENABLE_STARTTLS', nil) %>
 | 
			
		||||
    enable_starttls_auto: <%= ENV.fetch('SMTP_ENABLE_STARTTLS_AUTO', true) != 'false' %>
 | 
			
		||||
    tls: <%= ENV.fetch('SMTP_TLS', false) == 'true' ? true : nil %>
 | 
			
		||||
    ssl: <%= ENV.fetch('SMTP_SSL', false) == 'true' ? true : nil %>
 | 
			
		||||
    read_timeout: 20
 | 
			
		||||
@ -0,0 +1,33 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
module Mastodon
 | 
			
		||||
  module EmailConfigurationHelper
 | 
			
		||||
    module_function
 | 
			
		||||
 | 
			
		||||
    # Convert smtp settings from environment variables (or defaults in
 | 
			
		||||
    # `config/email.yml`) into the format that `ActionMailer` understands
 | 
			
		||||
    def smtp_settings(config)
 | 
			
		||||
      enable_starttls = nil
 | 
			
		||||
      enable_starttls_auto = nil
 | 
			
		||||
 | 
			
		||||
      case config[:enable_starttls]
 | 
			
		||||
      when 'always'
 | 
			
		||||
        enable_starttls = true
 | 
			
		||||
      when 'never'
 | 
			
		||||
        enable_starttls = false
 | 
			
		||||
      when 'auto'
 | 
			
		||||
        enable_starttls_auto = true
 | 
			
		||||
      else
 | 
			
		||||
        enable_starttls_auto = config[:enable_starttls_auto] != 'false'
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      authentication = config[:authentication] == 'none' ? nil : (config[:authentication] || 'plain')
 | 
			
		||||
 | 
			
		||||
      config.merge(
 | 
			
		||||
        authentication:,
 | 
			
		||||
        enable_starttls:,
 | 
			
		||||
        enable_starttls_auto:
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -0,0 +1,98 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe Mastodon::EmailConfigurationHelper do
 | 
			
		||||
  describe '#smtp_settings' do
 | 
			
		||||
    subject { described_class }
 | 
			
		||||
 | 
			
		||||
    let(:converted_settings) { subject.smtp_settings(configuration) }
 | 
			
		||||
    let(:base_configuration) do
 | 
			
		||||
      {
 | 
			
		||||
        address: 'localhost',
 | 
			
		||||
        port: 25,
 | 
			
		||||
        user_name: 'mastodon',
 | 
			
		||||
        password: 'mastodon',
 | 
			
		||||
      }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `enable_starttls` is "always"' do
 | 
			
		||||
      let(:configuration) do
 | 
			
		||||
        base_configuration.merge({ enable_starttls: 'always' })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'converts this to `true`' do
 | 
			
		||||
        expect(converted_settings[:enable_starttls]).to be true
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `enable_starttls` is "never"' do
 | 
			
		||||
      let(:configuration) do
 | 
			
		||||
        base_configuration.merge({ enable_starttls: 'never' })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'converts this to `false`' do
 | 
			
		||||
        expect(converted_settings[:enable_starttls]).to be false
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `enable_starttls` is "auto"' do
 | 
			
		||||
      let(:configuration) do
 | 
			
		||||
        base_configuration.merge({ enable_starttls: 'auto' })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'sets `enable_starttls_auto` instead' do
 | 
			
		||||
        expect(converted_settings[:enable_starttls]).to be_nil
 | 
			
		||||
        expect(converted_settings[:enable_starttls_auto]).to be true
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `enable_starttls` is unset' do
 | 
			
		||||
      context 'when `enable_starttls_auto` is unset' do
 | 
			
		||||
        let(:configuration) { base_configuration }
 | 
			
		||||
 | 
			
		||||
        it 'sets `enable_starttls_auto` to `true`' do
 | 
			
		||||
          expect(converted_settings[:enable_starttls_auto]).to be true
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context 'when `enable_starttls_auto` is set to "false"' do
 | 
			
		||||
        let(:configuration) do
 | 
			
		||||
          base_configuration.merge({ enable_starttls_auto: 'false' })
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it 'sets `enable_starttls_auto` to `false`' do
 | 
			
		||||
          expect(converted_settings[:enable_starttls_auto]).to be false
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `authentication` is set to "none"' do
 | 
			
		||||
      let(:configuration) do
 | 
			
		||||
        base_configuration.merge({ authentication: 'none' })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'sets `authentication` to `nil`' do
 | 
			
		||||
        expect(converted_settings[:authentication]).to be_nil
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `authentication` is set to `login`' do
 | 
			
		||||
      let(:configuration) do
 | 
			
		||||
        base_configuration.merge({ authentication: 'login' })
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'is left as-is' do
 | 
			
		||||
        expect(converted_settings[:authentication]).to eq 'login'
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when `authentication` is unset' do
 | 
			
		||||
      let(:configuration) { base_configuration }
 | 
			
		||||
 | 
			
		||||
      it 'sets `authentication` to "plain"' do
 | 
			
		||||
        expect(converted_settings[:authentication]).to eq 'plain'
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue