Skip to content

Commit

Permalink
support system_email. #382
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 3, 2020
1 parent 745a967 commit 0cd3214
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 24 deletions.
5 changes: 0 additions & 5 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Client < ActiveRecord::Base
alias_attribute :flipper_id, :symbol
alias_attribute :created_at, :created
alias_attribute :updated_at, :updated
alias_attribute :contact_email, :system_email
attr_readonly :symbol
delegate :symbol, to: :provider, prefix: true
delegate :consortium_id, to: :provider, allow_nil: true
Expand Down Expand Up @@ -96,7 +95,6 @@ class Client < ActiveRecord::Base
indexes :name, type: :text, fields: { keyword: { type: "keyword" }, raw: { type: "text", analyzer: "string_lowercase", "fielddata": true }}
indexes :alternate_name, type: :text, fields: { keyword: { type: "keyword" }, raw: { type: "text", analyzer: "string_lowercase", "fielddata": true }}
indexes :description, type: :text
indexes :contact_email, type: :text, fields: { keyword: { type: "keyword" }}
indexes :system_email, type: :text, fields: { keyword: { type: "keyword" }}
indexes :service_contact, type: :object, properties: {
email: { type: :text },
Expand Down Expand Up @@ -223,7 +221,6 @@ def as_indexed_json(options={})
"language" => Array.wrap(language),
"repository_type" => Array.wrap(repository_type),
"service_contact" => service_contact,
"contact_email" => contact_email,
"system_email" => system_email,
"domains" => domains,
"url" => url,
Expand Down Expand Up @@ -274,8 +271,6 @@ def csv
url: url,
software: software,
system_email: system_email,
# "service_contact" => service_contact,
# "contact_email" => contact_email,
}.values

CSV.generate { |csv| csv << client }
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/authenticable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_payload(uid: nil, user: nil, password: nil)
"uid" => uid,
"role_id" => roles.fetch(user.role_name, "user"),
"name" => user.name,
"email" => user.contact_email
"email" => user.system_email
}

# we only need password for clients registering DOIs in the handle system
Expand Down
8 changes: 4 additions & 4 deletions app/models/concerns/mailable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def send_welcome_email
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: contact_email, subject: subject, text: text, html: html)
response = User.send_message(name: name, email: system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: symbol},
{ title: "Contact name", value: name, short: true },
{ title: "Contact email", value: contact_email, short: true }
{ title: "Contact email", value: system_email, short: true }
]
User.send_notification_to_slack(nil, title: subject, level: "good", fields: fields)

Expand All @@ -49,12 +49,12 @@ def send_delete_email
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: contact_email, subject: subject, text: text, html: html)
response = User.send_message(name: name, email: system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: symbol},
{ title: "Contact name", value: name, short: true },
{ title: "Contact email", value: contact_email, short: true }
{ title: "Contact email", value: system_email, short: true }
]
User.send_notification_to_slack(nil, title: subject, level: "warning", fields: fields)

Expand Down
1 change: 0 additions & 1 deletion app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Provider < ActiveRecord::Base
alias_attribute :flipper_id, :symbol
alias_attribute :created_at, :created
alias_attribute :updated_at, :updated
alias_attribute :contact_email, :system_email
attr_readonly :symbol
attr_accessor :password_input

Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ def self.reset(username)
subject = "#{title}: Password Reset Request"
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.contact_email, subject: subject, text: text, html: html)
response = self.send_message(name: user.name, email: user.system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: uid.upcase},
{ title: "Name", value: user.name, short: true },
{ title: "Contact email", value: user.contact_email, short: true }
{ title: "Contact email", value: user.system_email, short: true }
]
slack_title = subject + (response[:status] == 200 ? " Sent" : " Failed")
level = response[:status] == 200 ? "good" : "danger"
Expand Down
6 changes: 5 additions & 1 deletion app/serializers/client_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ClientSerializer
set_type :clients
set_id :uid

attributes :name, :symbol, :year, :contact_email, :alternate_name, :description, :language, :client_type, :domains, :re3data, :opendoar, :issn, :url, :salesforce_id, :created, :updated
attributes :name, :symbol, :year, :contact_email, :system_email, :alternate_name, :description, :language, :client_type, :domains, :re3data, :opendoar, :issn, :url, :salesforce_id, :created, :updated

belongs_to :provider, record_type: :providers
belongs_to :consortium, record_type: :providers, serializer: ProviderSerializer, if: Proc.new { |client| client.consortium_id }
Expand All @@ -18,6 +18,10 @@ class ClientSerializer
object.password.present?
end

attribute :contact_email do |object|
object.system_email
end

attribute :salesforce_id, if: Proc.new { |object, params| params[:current_ability] && params[:current_ability].can?(:read_salesforce_id, object) == true } do |object|
object.salesforce_id
end
Expand Down
6 changes: 3 additions & 3 deletions spec/concerns/authenticable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@

describe 'decode_auth_param' do
it "provider" do
expect(subject.decode_auth_param(username: subject.symbol, password: "12345")).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.contact_email, "role_id"=>"provider_admin", "provider_id"=>subject.symbol.downcase)
expect(subject.decode_auth_param(username: subject.symbol, password: "12345")).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.system_email, "role_id"=>"provider_admin", "provider_id"=>subject.symbol.downcase)
end

it "admin" do
subject = create(:provider, symbol: "ADMIN", role_name: "ROLE_ADMIN", password_input: "12345")
expect(subject.decode_auth_param(username: subject.symbol, password: "12345")).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.contact_email, "role_id"=>"staff_admin")
expect(subject.decode_auth_param(username: subject.symbol, password: "12345")).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.system_email, "role_id"=>"staff_admin")
end
end
end
Expand All @@ -112,7 +112,7 @@

describe 'decode_auth_param' do
it "works" do
expect(subject.decode_auth_param(username: subject.symbol, password: 12345)).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.contact_email, "password" => "12345", "role_id"=>"client_admin", "provider_id"=>subject.symbol.downcase.split(".").first, "client_id"=>subject.symbol.downcase)
expect(subject.decode_auth_param(username: subject.symbol, password: 12345)).to eq("uid"=>subject.symbol.downcase, "name"=>subject.name, "email"=>subject.system_email, "password" => "12345", "role_id"=>"client_admin", "provider_id"=>subject.symbol.downcase.split(".").first, "client_id"=>subject.symbol.downcase)
end
end
end
10 changes: 5 additions & 5 deletions spec/concerns/mailable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "Mailable", type: :model, vcr: true do
let(:token) { User.generate_token }
let(:client) { create(:client, name: "DATACITE.DATACITE", contact_email: "[email protected]") }
let(:client) { create(:client, name: "DATACITE.DATACITE", system_email: "[email protected]") }
let(:title) { "DataCite Fabrica" }

it "send_welcome_email" do
Expand All @@ -13,14 +13,14 @@

# it "format_message_text" do
# template = "users/reset.text.erb"
# text = User.format_message_text(template: template, title: title, contact_name: client.name, name: client.symbol, url: token)
# 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, contact_name: client.name, name: client.symbol, url: token)
# 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
Expand All @@ -42,14 +42,14 @@
DataCite Support
BODY
subj = title + ": Password Reset Request"
response = User.send_message(name: client.name, email: client.contact_email, subject: subj, text: text)
response = User.send_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

context "send_notification_to_slack" do
it "succeeds" do
text = "Using contact email #{client.contact_email}."
text = "Using contact email #{client.system_email}."
options = { title: "TEST: new client account #{client.symbol} created." }
expect(Client.send_notification_to_slack(text, options)).to eq("ok")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/providers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# "attributes" => {
# "symbol" => "BL",
# "name" => "British Library",
# "contact_email" => "[email protected]",
# "system_email" => "[email protected]",
# "country_code" => "GB" } } }
# end
#
Expand Down
2 changes: 1 addition & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

describe "reset client password", vcr: true do
let(:provider) { create(:provider, symbol: "DATACITE", password_input: "12345") }
let(:client) { create(:client, provider: provider, symbol: "DATACITE.DATACITE", contact_email: "[email protected]") }
let(:client) { create(:client, provider: provider, symbol: "DATACITE.DATACITE", system_email: "[email protected]") }

it "sends message" do
response = User.reset(client.symbol)
Expand Down

0 comments on commit 0cd3214

Please sign in to comment.