Skip to content

Commit

Permalink
Fix send_email to handle kwargs / hash to avoid interface change
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Sep 9, 2022
1 parent dca9b97 commit 1a421c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.miq_log_workspace(obj, _inputs)
end

def self.miq_send_email(_obj, inputs)
MiqAeMethodService::MiqAeServiceMethods.send_email(inputs["to"], inputs["from"], inputs["subject"], inputs["body"], {:cc => inputs["cc"], :bcc => inputs["bcc"], :content_type => inputs["content_type"]})
MiqAeMethodService::MiqAeServiceMethods.send_email(inputs["to"], inputs["from"], inputs["subject"], inputs["body"], :cc => inputs["cc"], :bcc => inputs["bcc"], :content_type => inputs["content_type"])
end

def self.miq_snmp_trap_v1(_obj, inputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ class MiqAeServiceMethods

SYNCHRONOUS = Rails.env.test?

def self.send_email(to, from, subject, body, options = {}) # rubocop:disable Naming/MethodParameterName
def self.send_email(to, from, subject, body, *args, **kwargs)
# TODO: Remove this mess once we're on ruby 3.0+ only: go back to only kwargs.
# This is the only method that was expecting kwargs and one caller, execute_with_user,
# was changed to capture the kwargs as a hash (via **kwargs) and append them to the args
# and call MiqAeServiceMethods methods with those args. We now accept options hash as the last
# args or kwargs for any non execute_with_user callers.
options = if kwargs.present?
kwargs
else
args.last || {}
end

# we accept only 3 kwargs / options keys
options = options.slice(:cc, :bcc, :content_type)

ar_method do
meth = SYNCHRONOUS ? :deliver : :deliver_queue
options = {
options.merge!({
:to => to,
:from => from,
:cc => options[:cc],
:bcc => options[:bcc],
:subject => subject,
:content_type => options[:content_type],
:body => body
}
})
GenericMailer.send(meth, :automation_notification, options)
true
end
Expand Down
19 changes: 19 additions & 0 deletions spec/engine/miq_ae_method_service/miq_ae_service_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ def invoke_ae
expect(ae_object).to be_truthy
end

it "sends mail and rejects invalid inputs" do
method = "$evm.root['#{@ae_result_key}'] = $evm.execute(:send_email, #{options[:to].inspect}, #{options[:from].inspect}, #{options[:subject].inspect}, #{options[:body].inspect}, :bcc => #{options[:bcc].inspect}, :cc => #{options[:cc].inspect}, :joe => 'OMG', :content_type => #{options[:content_type].inspect})"
@ae_method.update(:data => method)
stub_const('MiqAeMethodService::MiqAeServiceMethods::SYNCHRONOUS', true)
expect(GenericMailer).to receive(:deliver).with(:automation_notification, options).once
ae_object = invoke_ae.root(@ae_result_key)
expect(ae_object).to be_truthy
end

it "sends mail synchronous with no options or kwargs" do
method = "$evm.root['#{@ae_result_key}'] = $evm.execute(:send_email, #{options[:to].inspect}, #{options[:from].inspect}, #{options[:subject].inspect}, #{options[:body].inspect}, nil)"
@ae_method.update(:data => method)
stub_const('MiqAeMethodService::MiqAeServiceMethods::SYNCHRONOUS', true)
lesser_options = options.except(:cc, :bcc, :content_type)
expect(GenericMailer).to receive(:deliver).with(:automation_notification, lesser_options).once
ae_object = invoke_ae.root(@ae_result_key)
expect(ae_object).to be_truthy
end

it "sends mail synchronous with options hash (for backwards compatibility with ruby 2.7) - drop when we drop ruby 2.7" do
method = "$evm.root['#{@ae_result_key}'] = $evm.execute(:send_email, #{options[:to].inspect}, #{options[:from].inspect}, #{options[:subject].inspect}, #{options[:body].inspect}, {:bcc => #{options[:bcc].inspect}, :cc => #{options[:cc].inspect}, :content_type => #{options[:content_type].inspect}})"
@ae_method.update(:data => method)
Expand Down

0 comments on commit 1a421c8

Please sign in to comment.