diff --git a/app/services/create_mail_method.rb b/app/services/create_mail_method.rb deleted file mode 100644 index 7eec387603..0000000000 --- a/app/services/create_mail_method.rb +++ /dev/null @@ -1,34 +0,0 @@ -# Configures Rails to use the specified mail settings. It does so creating -# a Spree::MailMethod and applying its configuration. -class CreateMailMethod - # Constructor - # - # @param attributes [Hash] MailMethod attributes - def initialize(attributes) - @attributes = attributes - end - - def call - persist_attributes - initialize_mail_settings - end - - private - - attr_reader :attributes - - # Updates the created mail method's attributes with the ones specified - def persist_attributes - mail_method.update_attributes(attributes) - end - - # Creates a new Spree::MailMethod for the current environment - def mail_method - Spree::MailMethod.create(environment: attributes[:environment]) - end - - # Makes Spree apply the specified mail settings - def initialize_mail_settings - Spree::Core::MailSettings.init - end -end diff --git a/app/services/mail_configuration.rb b/app/services/mail_configuration.rb new file mode 100644 index 0000000000..483e02eb90 --- /dev/null +++ b/app/services/mail_configuration.rb @@ -0,0 +1,18 @@ +# Configures Rails to use the specified mail configuration +# 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 | + Spree::Config[name] = value + end + apply_mail_settings + end + + private + + def self.apply_mail_settings + Spree::Core::MailSettings.init + end +end diff --git a/db/seeds.rb b/db/seeds.rb index d285cbda01..9561e036e2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -30,26 +30,23 @@ states.each do |state| end end -def create_mail_method - Spree::MailMethod.destroy_all - - CreateMailMethod.new( - environment: Rails.env, - preferred_enable_mail_delivery: true, - preferred_mail_host: ENV.fetch('MAIL_HOST'), - preferred_mail_domain: ENV.fetch('MAIL_DOMAIN'), - preferred_mail_port: ENV.fetch('MAIL_PORT'), - preferred_mail_auth_type: 'login', - preferred_smtp_username: ENV.fetch('SMTP_USERNAME'), - preferred_smtp_password: ENV.fetch('SMTP_PASSWORD'), - preferred_secure_connection_type: ENV.fetch('MAIL_SECURE_CONNECTION', 'None'), - preferred_mails_from: ENV.fetch('MAILS_FROM', "no-reply@#{ENV.fetch('MAIL_DOMAIN')}"), - preferred_mail_bcc: ENV.fetch('MAIL_BCC', ''), - preferred_intercept_email: '' - ).call +def set_mail_configuration + MailConfiguration.entries= { + enable_mail_delivery: true, + mail_host: ENV.fetch('MAIL_HOST'), + mail_domain: ENV.fetch('MAIL_DOMAIN'), + mail_port: ENV.fetch('MAIL_PORT'), + 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 -create_mail_method +set_mail_configuration spree_user = Spree::User.first spree_user && spree_user.confirm! diff --git a/spec/services/create_mail_method_spec.rb b/spec/services/create_mail_method_spec.rb deleted file mode 100644 index d57d5882a4..0000000000 --- a/spec/services/create_mail_method_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'spec_helper' - -describe CreateMailMethod do - describe '#call' do - let(:mail_method) { Spree::MailMethod.create(environment: 'test') } - let(:mail_settings) { instance_double(Spree::Core::MailSettings) } - let(:attributes) do - { preferred_smtp_username: "smtp_username", environment: "test" } - end - - before do - allow(Spree::MailMethod) - .to receive(:create).with(environment: 'test').and_return(mail_method) - allow(Spree::Core::MailSettings).to receive(:init) { mail_settings } - end - - context 'unit' do - before do - allow(mail_method).to receive(:update_attributes).with(attributes) - end - - it 'creates a new MailMethod' do - described_class.new(attributes).call - - expect(Spree::MailMethod) - .to have_received(:create).with(environment: 'test') { mail_method } - end - - it 'updates the MailMethod' do - described_class.new(attributes).call - - expect(mail_method) - .to have_received(:update_attributes).with(attributes) { mail_method } - end - - it 'initializes the mail settings' do - described_class.new(attributes).call - expect(Spree::Core::MailSettings).to have_received(:init) - end - end - - context 'integration' do - it 'updates the mail method attributes' do - described_class.new(attributes).call - expect(mail_method.preferred_smtp_username).to eq('smtp_username') - end - end - end -end diff --git a/spec/services/mail_configuration_spec.rb b/spec/services/mail_configuration_spec.rb new file mode 100644 index 0000000000..b93bff9b28 --- /dev/null +++ b/spec/services/mail_configuration_spec.rb @@ -0,0 +1,34 @@ +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 + + 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 + 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") + end + + it 'initializes the mail settings' do + described_class.entries= entries + expect(Spree::Core::MailSettings).to have_received(:init) + end + end +end