From 0cd3214eb95c0d45a5488a89b0fcad6c05bedb7f Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 3 Jan 2020 16:20:17 +0100 Subject: [PATCH] support system_email. #382 --- app/models/client.rb | 5 ----- app/models/concerns/authenticable.rb | 2 +- app/models/concerns/mailable.rb | 8 ++++---- app/models/provider.rb | 1 - app/models/user.rb | 4 ++-- app/serializers/client_serializer.rb | 6 +++++- spec/concerns/authenticable_spec.rb | 6 +++--- spec/concerns/mailable_spec.rb | 10 +++++----- spec/controllers/providers_controller_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- 10 files changed, 22 insertions(+), 24 deletions(-) diff --git a/app/models/client.rb b/app/models/client.rb index c5bbed8fe..e77e91e4f 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -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 @@ -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 }, @@ -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, @@ -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 } diff --git a/app/models/concerns/authenticable.rb b/app/models/concerns/authenticable.rb index de75a3ee1..8ce471b22 100644 --- a/app/models/concerns/authenticable.rb +++ b/app/models/concerns/authenticable.rb @@ -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 diff --git a/app/models/concerns/mailable.rb b/app/models/concerns/mailable.rb index 77a61f32b..05b80f79c 100644 --- a/app/models/concerns/mailable.rb +++ b/app/models/concerns/mailable.rb @@ -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) @@ -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) diff --git a/app/models/provider.rb b/app/models/provider.rb index 73ca74639..979cd36a7 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index db16580b5..070cd7186 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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" diff --git a/app/serializers/client_serializer.rb b/app/serializers/client_serializer.rb index 5debe454b..211707a20 100644 --- a/app/serializers/client_serializer.rb +++ b/app/serializers/client_serializer.rb @@ -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 } @@ -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 diff --git a/spec/concerns/authenticable_spec.rb b/spec/concerns/authenticable_spec.rb index cb6674484..734cc4ab3 100644 --- a/spec/concerns/authenticable_spec.rb +++ b/spec/concerns/authenticable_spec.rb @@ -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 @@ -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 diff --git a/spec/concerns/mailable_spec.rb b/spec/concerns/mailable_spec.rb index f77617536..ff1e89280 100644 --- a/spec/concerns/mailable_spec.rb +++ b/spec/concerns/mailable_spec.rb @@ -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: "test@datacite.org") } + let(:client) { create(:client, name: "DATACITE.DATACITE", system_email: "test@datacite.org") } let(:title) { "DataCite Fabrica" } it "send_welcome_email" do @@ -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("

Dear #{client.name},

") # end @@ -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 diff --git a/spec/controllers/providers_controller_spec.rb b/spec/controllers/providers_controller_spec.rb index e12364b28..38b7c2d29 100644 --- a/spec/controllers/providers_controller_spec.rb +++ b/spec/controllers/providers_controller_spec.rb @@ -11,7 +11,7 @@ # "attributes" => { # "symbol" => "BL", # "name" => "British Library", -# "contact_email" => "bob@example.com", +# "system_email" => "bob@example.com", # "country_code" => "GB" } } } # end # diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index dd105b4a6..554ca5838 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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: "test@datacite.org") } + let(:client) { create(:client, provider: provider, symbol: "DATACITE.DATACITE", system_email: "test@datacite.org") } it "sends message" do response = User.reset(client.symbol)