mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
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).
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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']
|
||||
|
||||
16
db/seeds.rb
16
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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user