Skip to content

Commit

Permalink
fix sending welcome email. datacite/bracco#424
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 11, 2020
1 parent 543176d commit 001343c
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 229 deletions.
2 changes: 1 addition & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def create
authorize! :create, @client

if @client.save
@client.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test?
@client.send_welcome_email(responsible_id: current_user.uid)
options = {}
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def create
authorize! :create, @provider

if @provider.save
@provider.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test?
@provider.send_welcome_email(responsible_id: current_user.uid)
options = {}
options[:include] = @include
options[:is_collection] = false
Expand Down
1 change: 1 addition & 0 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def create
authorize! :create, @client

if @client.save
@client.send_welcome_email(responsible_id: current_user.uid)
options = {}
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down
37 changes: 28 additions & 9 deletions app/models/concerns/mailable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def send_welcome_email(responsible_id: nil)
title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica"
subject = "#{title}: New Account"
account_type = self.class.name == "Provider" ? member_type.humanize : client_type.humanize
responsible_id ||= "ADMIN"
responsible_id = (responsible_id || "admin").upcase
text = User.format_message_text(template: "users/welcome.text.erb", title: title, contact_name: name, name: symbol, url: url, reset_url: reset_url)
html = User.format_message_html(template: "users/welcome.html.erb", title: title, contact_name: name, name: symbol, url: url, reset_url: reset_url)

response = User.send_message(name: name, email: system_email, subject: subject, text: text, html: html)
response = User.send_email_message(name: name, email: system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: symbol, short: true },
{ title: "Account type", value: account_type, short: true },
Expand All @@ -54,7 +55,7 @@ def send_delete_email(responsible_id: nil)
text = User.format_message_text(template: "users/delete.text.erb", title: title, contact_name: name, name: symbol)
html = User.format_message_html(template: "users/delete.html.erb", title: title, contact_name: name, name: symbol)

response = User.send_message(name: name, email: system_email, subject: subject, text: text, html: html)
response = User.send_email_message(name: name, email: system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: symbol, short: true },
Expand All @@ -73,17 +74,24 @@ module ClassMethods
# icon for Slack messages
SLACK_ICON_URL = "https://github.com/datacite/segugio/blob/master/source/images/fabrica.png"

class NoOpHTTPClient
def self.post uri, params={}
Rails.logger.info JSON.parse(params[:payload])
OpenStruct.new(body: "ok", status: 200)
end
end

def format_message_text(template: nil, title: nil, contact_name: nil, name: nil, url: nil, reset_url: nil)
ActionController::Base.render(
assigns: { title: title, contact_name: name, name: name, url: url, reset_url: reset_url },
assigns: { title: title, contact_name: contact_name, name: name, url: url, reset_url: reset_url },
template: template,
layout: false
)
end

def format_message_html(template: nil, title: nil, contact_name: nil, name: nil, url: nil, reset_url: nil)
input = ActionController::Base.render(
assigns: { title: title, contact_name: name, name: name, url: url, reset_url: reset_url },
assigns: { title: title, contact_name: contact_name, name: name, url: url, reset_url: reset_url },
template: template,
layout: "application"
)
Expand All @@ -92,8 +100,9 @@ def format_message_html(template: nil, title: nil, contact_name: nil, name: nil,
premailer.to_inline_css
end

def send_message(name: nil, email: nil, subject: nil, text: nil, html: nil)
def send_email_message(name: nil, email: nil, subject: nil, text: nil, html: nil)
mg_client = Mailgun::Client.new ENV['MAILGUN_API_KEY']
mg_client.enable_test_mode! if Rails.env.test?
mb_obj = Mailgun::MessageBuilder.new

mb_obj.from(ENV['MG_FROM'], "last" => "DataCite Support")
Expand All @@ -118,9 +127,19 @@ def send_notification_to_slack(text, options={})
fields: options[:fields]
}.compact

notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'],
username: "Fabrica",
icon_url: SLACK_ICON_URL
# don't send message to Slack API in test and development environments
if Rails.env.test? || Rails.env.development?
notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'],
username: "Fabrica",
icon_url: SLACK_ICON_URL do
http_client NoOpHTTPClient
end
else
notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'],
username: "Fabrica",
icon_url: SLACK_ICON_URL
end

response = notifier.ping attachments: [attachment]
response.first.body
rescue Slack::Notifier::APIError => exception
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def self.reset(username)
account_type = user.class.name == "Provider" ? user.member_type.humanize : user.client_type.humanize
text = User.format_message_text(template: "users/reset.text.erb", title: title, contact_name: user.name, name: user.symbol, url: url, reset_url: reset_url)
html = User.format_message_html(template: "users/reset.html.erb", title: title, contact_name: user.name, name: user.symbol, url: url, reset_url: reset_url)
response = self.send_message(name: user.name, email: user.system_email, subject: subject, text: text, html: html)
response = self.send_email_message(name: user.name, email: user.system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: uid.upcase, short: true },
Expand Down
48 changes: 32 additions & 16 deletions spec/concerns/mailable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,43 @@
expect(response[:message]).to eq("Queued. Thank you.")
end

it "send_welcome_email provider" do
response = provider.send_welcome_email(responsible_id: "admin")
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end

it "send_delete_email" do
response = client.send_delete_email(responsible_id: provider.symbol)
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end

# it "format_message_text" do
# template = "users/reset.text.erb"
# text = User.format_message_text(template: template, title: title, system_email: client.name, name: client.symbol, url: token)
# line = text.split("\n").first
# expect(line).to eq("Dear #{client.name},")
# end

# it "format_message_html" do
# template = "users/reset.html.erb"
# html = User.format_message_html(template: template, title: title, system_email: client.name, name: client.symbol, url: token)
# line = html.split("\n")[41]
# expect(line.strip).to eq("<h1 style=\"font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; box-sizing: border-box; margin-top: 0; color: #2F3133; font-size: 19px; font-weight: bold;\" align=\"left\">Dear #{client.name},</h1>")
# end

it "send message" do
it "send_delete_email provider" do
response = provider.send_delete_email(responsible_id: "admin")
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end

it "format_message_text welcome" do
template = "users/welcome.text.erb"
url = ENV['BRACCO_URL'] + "?jwt=" + token
reset_url = ENV['BRACCO_URL'] + "/reset"
text = User.format_message_text(template: template, title: title, contact_name: client.name, name: client.symbol, url: url, reset_url: reset_url)
line = text.split("\n").first
expect(line).to eq("Dear #{client.name},")
end

it "format_message_html welcome" do
template = "users/welcome.html.erb"
url = ENV['BRACCO_URL'] + "?jwt=" + token
reset_url = ENV['BRACCO_URL'] + "/reset"
html = User.format_message_html(template: template, title: title, contact_name: client.name, name: client.symbol, url: url, reset_url: reset_url)
line = html.split("\n")[41]
expect(line.strip).to eq("<h1 style=\"font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; box-sizing: border-box; margin-top: 0; color: #2F3133; font-size: 19px; font-weight: bold;\" align=\"left\">Dear #{client.name},</h1>")
end

it "send email message" do
text = <<~BODY
Dear #{client.name},
Expand All @@ -49,7 +65,7 @@
DataCite Support
BODY
subj = title + ": Password Reset Request"
response = User.send_message(name: client.name, email: client.system_email, subject: subj, text: text)
response = User.send_email_message(name: client.name, email: client.system_email, subject: subj, text: text)
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end
Expand Down
50 changes: 0 additions & 50 deletions spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml

This file was deleted.

This file was deleted.

This file was deleted.

50 changes: 0 additions & 50 deletions spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml

This file was deleted.

Loading

0 comments on commit 001343c

Please sign in to comment.