From 3dc3581e6b2df11cb14fd9d0b2729ab318546cd7 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 21 May 2021 21:28:00 +0100 Subject: [PATCH] Ensure Mail configs are applied when the app starts Previously we only set these part-way through deployment, so the values could be out of sync between our ENV vars and Spree::Config (which itself is a mix of both cached values and database-persisted values). --- app/services/mail_configuration.rb | 20 +++++++++++-- config/initializers/spree.rb | 4 +++ db/seeds.rb | 16 +---------- spec/services/mail_configuration_spec.rb | 36 +++++++++++------------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/app/services/mail_configuration.rb b/app/services/mail_configuration.rb index 32111aa205..c90b3b1321 100644 --- a/app/services/mail_configuration.rb +++ b/app/services/mail_configuration.rb @@ -2,9 +2,8 @@ # by setting entries on the Spree Config # and initializing Spree:MailSettings that uses the Spree::Config. class MailConfiguration - # @param entries [Hash] Spree Config entries - def self.entries=(entries) - entries.each do |name, value| + def self.apply! + configuration.each do |name, value| Spree::Config[name] = value end apply_mail_settings @@ -12,6 +11,21 @@ class MailConfiguration private + def self.configuration + { + mail_host: ENV.fetch('MAIL_HOST'), + mail_domain: ENV.fetch('MAIL_DOMAIN'), + mail_port: ENV.fetch('MAIL_PORT'), + mail_auth_type: ENV.fetch('MAIL_AUTH_TYPE', 'login'), + smtp_username: ENV.fetch('SMTP_USERNAME'), + smtp_password: ENV.fetch('SMTP_PASSWORD'), + secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'), + mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"), + mail_bcc: ENV.fetch('MAIL_BCC', ''), + intercept_email: '' + } + end + def self.apply_mail_settings Spree::Core::MailSettings.init end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 2d8a5bbe6a..e2c78e5f78 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -31,6 +31,10 @@ Spree.config do |config| config.s3_protocol = ENV.fetch('S3_PROTOCOL', 'https') end +# Read mail configuration from ENV vars at boot time and ensure the values are +# applied correctly in Spree::Config. +MailConfiguration.apply! + # Attachments settings Spree::Image.set_attachment_attribute(:path, ENV['ATTACHMENT_PATH']) if ENV['ATTACHMENT_PATH'] Spree::Image.set_attachment_attribute(:url, ENV['ATTACHMENT_URL']) if ENV['ATTACHMENT_URL'] diff --git a/db/seeds.rb b/db/seeds.rb index 95629e77ef..88d3e96270 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,22 +2,8 @@ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). require 'yaml' -def set_mail_configuration - MailConfiguration.entries= { - mail_host: ENV.fetch('MAIL_HOST'), - mail_domain: ENV.fetch('MAIL_DOMAIN'), - mail_port: ENV.fetch('MAIL_PORT'), - mail_auth_type: ENV.fetch('MAIL_AUTH_TYPE', 'login'), - smtp_username: ENV.fetch('SMTP_USERNAME'), - smtp_password: ENV.fetch('SMTP_PASSWORD'), - secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'), - mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"), - mail_bcc: ENV.fetch('MAIL_BCC', ''), - intercept_email: '' - } -end # We need mail_configuration to create a user account, because it sends a confirmation email. -set_mail_configuration +MailConfiguration.apply! puts "[db:seed] Seeding Roles" Spree::Role.where(:name => "admin").first_or_create diff --git a/spec/services/mail_configuration_spec.rb b/spec/services/mail_configuration_spec.rb index a1751ad2fe..74873b90c4 100644 --- a/spec/services/mail_configuration_spec.rb +++ b/spec/services/mail_configuration_spec.rb @@ -3,33 +3,29 @@ require 'spec_helper' describe MailConfiguration do - describe 'entries=' do - let(:mail_settings) { instance_double(Spree::Core::MailSettings) } - let(:entries) do - { smtp_username: "smtp_username", mail_auth_type: "login" } - end - + describe 'apply!' do before do - allow(Spree::Core::MailSettings).to receive(:init) { mail_settings } - end - - # keeps spree_config unchanged - around do |example| - original_smtp_username = Spree::Config[:smtp_username] - original_mail_auth_type = Spree::Config[:mail_auth_type] - example.run - Spree::Config[:smtp_username] = original_smtp_username - Spree::Config[:mail_auth_type] = original_mail_auth_type + allow(Spree::Core::MailSettings).to receive(:init) { true } end it 'sets config entries in the Spree Config' do - described_class.entries = entries - expect(Spree::Config[:smtp_username]).to eq("smtp_username") - expect(Spree::Config[:mail_auth_type]).to eq("login") + allow(Spree::Config).to receive(:[]=) + + described_class.apply! + expect(Spree::Config).to have_received(:[]=).with(:mail_host, "example.com") + expect(Spree::Config).to have_received(:[]=).with(:mail_domain, "example.com") + expect(Spree::Config).to have_received(:[]=).with(:mail_port, "25") + expect(Spree::Config).to have_received(:[]=).with(:mail_auth_type, "login") + expect(Spree::Config).to have_received(:[]=).with(:smtp_username, "ofn") + expect(Spree::Config).to have_received(:[]=).with(:smtp_password, "f00d") + expect(Spree::Config).to have_received(:[]=).with(:secure_connection_type, "None") + expect(Spree::Config).to have_received(:[]=).with(:mails_from, "no-reply@example.com") + expect(Spree::Config).to have_received(:[]=).with(:mail_bcc, "") + expect(Spree::Config).to have_received(:[]=).with(:intercept_email, "") end it 'initializes the mail settings' do - described_class.entries = entries + described_class.apply! expect(Spree::Core::MailSettings).to have_received(:init) end end