From a7f1ed660bf6657ecefe53b635efade1532d25ef Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 13 Mar 2018 13:45:09 +0100 Subject: [PATCH] Add service to create a mail method This will make loading sample data into staging environments easier. --- app/services/create_mail_method.rb | 34 ++++++++++++++++++++ spec/services/create_mail_method_spec.rb | 41 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/services/create_mail_method.rb create mode 100644 spec/services/create_mail_method_spec.rb diff --git a/app/services/create_mail_method.rb b/app/services/create_mail_method.rb new file mode 100644 index 0000000000..7eec387603 --- /dev/null +++ b/app/services/create_mail_method.rb @@ -0,0 +1,34 @@ +# 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/spec/services/create_mail_method_spec.rb b/spec/services/create_mail_method_spec.rb new file mode 100644 index 0000000000..de78cf83a1 --- /dev/null +++ b/spec/services/create_mail_method_spec.rb @@ -0,0 +1,41 @@ +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 + + 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 + expect(mail_method) + .to(receive(:update_attributes)).with(attributes) { mail_method } + + described_class.new(attributes).call + end + + it 'updates the mail method attributes' do + described_class.new(attributes).call + expect(mail_method.preferred_smtp_username).to eq('smtp_username') + end + + it 'initializes the mail settings' do + described_class.new(attributes).call + expect(Spree::Core::MailSettings).to have_received(:init) + end + end +end