Skip to content

Commit

Permalink
Merge pull request #580 from datacite/welcome-email
Browse files Browse the repository at this point in the history
Fix sending welcome email
  • Loading branch information
Martin Fenner authored Jul 13, 2020
2 parents 543176d + 322a252 commit ae8705f
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 254 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ gem "nokogiri", ">= 1.10.4"
gem "diffy", "~> 3.2", ">= 3.2.1"
gem "commonmarker", "~> 0.17.9"
gem "iso8601", "~> 0.9.0"
gem "maremma", "~> 4.6.1"
gem 'bolognese', '~> 1.5'
gem "maremma", "~> 4.7.1"
gem 'bolognese', '~> 1.6.12'
gem "dalli", "~> 2.7", ">= 2.7.6"
gem "lograge", "~> 0.11.2"
gem "logstash-event", "~> 1.2", ">= 1.2.02"
Expand Down Expand Up @@ -52,7 +52,7 @@ gem "mini_magick", "~> 4.8"
gem "elasticsearch", "~> 7.1.0"
gem "elasticsearch-model", "~> 7.0", require: "elasticsearch/model"
gem "elasticsearch-rails", "~> 7.0"
gem "faraday", "0.17.0"
gem "faraday", "~> 0.17.3"
gem "faraday_middleware-aws-sigv4", "~> 0.3.0"
gem "rack-utf8_sanitizer", "~> 1.6"
gem "turnout", "~> 2.5"
Expand Down
44 changes: 22 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ GEM
audited (4.9.0)
activerecord (>= 4.2, < 6.1)
aws-eventstream (1.1.0)
aws-partitions (1.337.0)
aws-partitions (1.341.0)
aws-sdk-core (3.103.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.239.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-kms (1.35.0)
aws-sdk-kms (1.36.0)
aws-sdk-core (~> 3, >= 3.99.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.72.0)
aws-sdk-s3 (1.74.0)
aws-sdk-core (~> 3, >= 3.102.1)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.1)
aws-sdk-sqs (1.29.0)
aws-sdk-sqs (1.30.0)
aws-sdk-core (~> 3, >= 3.99.0)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.2.1)
Expand Down Expand Up @@ -102,7 +102,7 @@ GEM
latex-decode (~> 0.0)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bolognese (1.6.11)
bolognese (1.6.12)
activesupport (>= 4.2.5)
benchmark_methods (~> 0.7)
bibtex-ruby (>= 5.1.0)
Expand All @@ -112,13 +112,13 @@ GEM
concurrent-ruby (~> 1.1, >= 1.1.5)
csl-styles (~> 1.0, >= 1.0.1.8)
edtf (~> 3.0, >= 3.0.4)
faraday (= 0.17.0)
faraday (~> 0.17.3)
gender_detector (~> 0.1.2)
iso8601 (~> 0.9.1)
json-ld (~> 3.1, >= 3.1.4)
jsonlint (~> 0.3.0)
loofah (~> 2.0, >= 2.0.3)
maremma (>= 4.3, < 5)
maremma (>= 4.7, < 5)
namae (~> 1.0)
nokogiri (~> 1.10.4)
oj (~> 3.10)
Expand Down Expand Up @@ -187,7 +187,7 @@ GEM
docopt (0.6.1)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.5)
dotenv (2.7.6)
ebnf (1.2.0)
rdf (~> 3.1)
sxp (~> 1.1)
Expand Down Expand Up @@ -221,11 +221,11 @@ GEM
railties (>= 3.0.0)
faker (1.9.6)
i18n (>= 0.7)
faraday (0.17.0)
faraday (0.17.3)
multipart-post (>= 1.2, < 3)
faraday-encoding (0.0.5)
faraday
faraday_middleware (0.13.1)
faraday_middleware (0.14.0)
faraday (>= 0.7.4, < 1.0)
faraday_middleware-aws-sigv4 (0.3.0)
aws-sigv4 (~> 1.0)
Expand Down Expand Up @@ -327,14 +327,14 @@ GEM
rest-client (~> 2.0.2)
marcel (0.3.3)
mimemagic (~> 0.3.2)
maremma (4.6.1)
maremma (4.7.1)
activesupport (>= 4.2.5)
addressable (>= 2.3.6)
builder (~> 3.2, >= 3.2.2)
excon (~> 0.71.0)
faraday (= 0.17.0)
faraday (~> 0.17.3)
faraday-encoding (~> 0.0.4)
faraday_middleware (~> 0.13.1)
faraday_middleware (~> 0.14.0)
nokogiri (~> 1.10.4)
oj (>= 2.8.3)
oj_mimic_json (~> 1.0, >= 1.0.1)
Expand All @@ -347,16 +347,16 @@ GEM
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.1)
money (6.13.7)
money (6.13.8)
i18n (>= 0.6.4, <= 2)
msgpack (1.3.3)
multi_json (1.14.1)
multi_json (1.15.0)
multipart-post (2.1.1)
mysql2 (0.4.10)
namae (1.0.1)
netrc (0.11.0)
nio4r (2.5.2)
nokogiri (1.10.9)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
oj (3.10.6)
oj_mimic_json (1.0.1)
Expand Down Expand Up @@ -423,7 +423,7 @@ GEM
link_header (~> 0.0, >= 0.0.8)
rdf-aggregate-repo (3.1.0)
rdf (~> 3.1)
rdf-rdfa (3.1.1)
rdf-rdfa (3.1.2)
haml (~> 5.1)
htmlentities (~> 4.3)
rdf (~> 3.1, >= 3.1.2)
Expand All @@ -438,7 +438,7 @@ GEM
rdf-turtle (3.1.1)
ebnf (~> 1.2)
rdf (~> 3.1, >= 3.1.2)
rdf-vocab (3.1.5)
rdf-vocab (3.1.7)
rdf (~> 3.1, >= 3.1.2)
rdf-xsd (3.1.0)
rdf (~> 3.1)
Expand Down Expand Up @@ -564,7 +564,7 @@ GEM
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.2)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
Expand All @@ -586,7 +586,7 @@ DEPENDENCIES
bergamasco (~> 0.3.10)
better_errors
binding_of_caller
bolognese (~> 1.5)
bolognese (~> 1.6.12)
bootsnap (~> 1.4, >= 1.4.4)
bullet (~> 6.1)
byebug
Expand All @@ -609,7 +609,7 @@ DEPENDENCIES
facets
factory_bot_rails (~> 4.8, >= 4.8.2)
faker (~> 1.9)
faraday (= 0.17.0)
faraday (~> 0.17.3)
faraday_middleware-aws-sigv4 (~> 0.3.0)
fast_jsonapi (~> 1.3)
flipper (~> 0.17.0)
Expand All @@ -634,7 +634,7 @@ DEPENDENCIES
logstash-event (~> 1.2, >= 1.2.02)
logstash-logger (~> 0.26.1)
mailgun-ruby (~> 1.1, >= 1.1.8)
maremma (~> 4.6.1)
maremma (~> 4.7.1)
mini_magick (~> 4.8)
mysql2 (~> 0.4.4)
nokogiri (>= 1.10.4)
Expand Down
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
Loading

0 comments on commit ae8705f

Please sign in to comment.