From 90bfbff1033dacd512981f1b1fc2989d632a8332 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 25 Apr 2020 13:52:03 +0200 Subject: [PATCH] include responsible account is slack messages. datacite/datacite#918 --- app/controllers/clients_controller.rb | 3 +- app/controllers/providers_controller.rb | 3 +- app/models/client.rb | 2 - app/models/concerns/mailable.rb | 12 +++-- app/models/provider.rb | 2 - app/models/user.rb | 6 ++- lib/tasks/client.rake | 2 +- spec/concerns/mailable_spec.rb | 8 ++- .../Mailable/send_delete_email.yml | 50 +++++++++++++++++++ .../send_notification_to_slack/succeeds.yml | 6 +-- .../succeeds.yml | 6 +-- .../Mailable/send_welcome_email.yml | 10 ++-- .../reset_client_password/sends_message.yml | 26 +++------- 13 files changed, 93 insertions(+), 43 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index edd4f8941..f3192f52c 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -110,6 +110,7 @@ def create authorize! :create, @client if @client.save + @client.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test? options = {} options[:is_collection] = false options[:params] = { current_ability: current_ability } @@ -143,7 +144,7 @@ def destroy Rails.logger.warn message render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status elsif @client.update(is_active: nil, deleted_at: Time.zone.now) - @client.send_delete_email unless Rails.env.test? + @client.send_delete_email(responsible_id: current_user.uid) unless Rails.env.test? head :no_content else Rails.logger.error @client.errors.inspect diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index f32cff4c1..5a0109547 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -171,6 +171,7 @@ def create authorize! :create, @provider if @provider.save + @provider.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test? options = {} options[:include] = @include options[:is_collection] = false @@ -206,7 +207,7 @@ def destroy Rails.logger.warn message render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status elsif @provider.update_attributes(is_active: nil, deleted_at: Time.zone.now) - @provider.send_delete_email unless Rails.env.test? + @provider.send_delete_email(responsible_id: current_user.uid) unless Rails.env.test? head :no_content else Rails.logger.error @provider.errors.inspect diff --git a/app/models/client.rb b/app/models/client.rb index f48e5c290..64a653669 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -63,8 +63,6 @@ class Client < ActiveRecord::Base before_create { self.created = Time.zone.now.utc.iso8601 } before_save { self.updated = Time.zone.now.utc.iso8601 } - after_create :send_welcome_email, unless: Proc.new { Rails.env.test? } - attr_accessor :target_id # use different index for testing diff --git a/app/models/concerns/mailable.rb b/app/models/concerns/mailable.rb index 0769c58bb..63b295360 100644 --- a/app/models/concerns/mailable.rb +++ b/app/models/concerns/mailable.rb @@ -6,7 +6,7 @@ module Mailable require 'slack-notifier' included do - def send_welcome_email + def send_welcome_email(responsible_id: nil) if self.class.name == "Provider" client_id = nil provider_id = symbol.downcase @@ -29,6 +29,7 @@ def send_welcome_email 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" 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) @@ -38,17 +39,19 @@ def send_welcome_email { title: "Account ID", value: symbol, short: true }, { title: "Account type", value: account_type, short: true }, { title: "Account name", value: name, short: true }, - { title: "System email", value: system_email, short: true } + { title: "System email", value: system_email, short: true }, + { title: "Responsible Account ID", value: responsible_id } ] User.send_notification_to_slack(nil, title: subject, level: "good", fields: fields) response end - def send_delete_email + def send_delete_email(responsible_id: nil) title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica" subject = "#{title}: Account Deleted" account_type = self.class.name == "Provider" ? member_type.humanize : client_type.humanize + responsible_id ||= "ADMIN" 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) @@ -58,7 +61,8 @@ def send_delete_email { title: "Account ID", value: symbol, short: true }, { title: "Account type", value: account_type, short: true }, { title: "Account name", value: name, short: true }, - { title: "System email", value: system_email, short: true } + { title: "System email", value: system_email, short: true }, + { title: "Responsible Account ID", value: responsible_id } ] 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 2c0e2e91b..b7fc42850 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -79,8 +79,6 @@ class Provider < ActiveRecord::Base before_create { self.created = Time.zone.now.utc.iso8601 } before_save { self.updated = Time.zone.now.utc.iso8601 } - after_create :send_welcome_email, unless: Proc.new { Rails.env.test? } - accepts_nested_attributes_for :prefixes # use different index for testing diff --git a/app/models/user.rb b/app/models/user.rb index 6c7015be7..4238c59b3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -115,13 +115,15 @@ def self.reset(username) reset_url = ENV['BRACCO_URL'] + "/reset" title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica" subject = "#{title}: Password Reset Request" + 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) fields = [ - { title: "Account ID", value: uid.upcase}, - { title: "Name", value: user.name, short: true }, + { title: "Account ID", value: uid.upcase, short: true }, + { title: "Account type", value: account_type, short: true }, + { title: "Account name", value: user.name, short: true }, { title: "System email", value: user.system_email, short: true } ] slack_title = subject + (response[:status] == 200 ? " Sent" : " Failed") diff --git a/lib/tasks/client.rake b/lib/tasks/client.rake index aea73c84a..8532c9a81 100644 --- a/lib/tasks/client.rake +++ b/lib/tasks/client.rake @@ -130,7 +130,7 @@ namespace :client do end if client.update_attributes(is_active: nil, deleted_at: Time.zone.now) - client.send_delete_email unless Rails.env.test? + client.send_delete_email(responsible_id: "admin") unless Rails.env.test? puts "Client with client ID #{ENV['CLIENT_ID']} deleted." else puts client.errors.inspect diff --git a/spec/concerns/mailable_spec.rb b/spec/concerns/mailable_spec.rb index 75790be8c..c645131a4 100644 --- a/spec/concerns/mailable_spec.rb +++ b/spec/concerns/mailable_spec.rb @@ -7,7 +7,13 @@ let(:title) { "DataCite Fabrica" } it "send_welcome_email" do - response = client.send_welcome_email + response = client.send_welcome_email(responsible_id: provider.symbol) + 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 diff --git a/spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml b/spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml new file mode 100644 index 000000000..c629a4012 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml @@ -0,0 +1,50 @@ +--- +http_interactions: +- request: + method: post + uri: "" + body: + encoding: US-ASCII + string: payload=%7B%22username%22%3A%22Fabrica%22%2C%22icon_url%22%3A%22https%3A%2F%2Fgithub.com%2Fdatacite%2Fsegugio%2Fblob%2Fmaster%2Fsource%2Fimages%2Ffabrica.png%22%2C%22attachments%22%3A%5B%7B%22title%22%3A%22DataCite+Fabrica%3A+Account+Deleted%22%2C%22color%22%3A%22warning%22%2C%22fields%22%3A%5B%7B%22title%22%3A%22Account+ID%22%2C%22value%22%3A%22DATACITE.DATACITE%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+type%22%2C%22value%22%3A%22Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+name%22%2C%22value%22%3A%22DataCite+Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22System+email%22%2C%22value%22%3A%22test%40datacite.org%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Responsible+Account+ID%22%2C%22value%22%3A%22DATACITE%22%7D%5D%7D%5D%7D + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + Date: + - Sat, 25 Apr 2020 11:49:46 GMT + Server: + - Apache + Vary: + - Accept-Encoding + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Referrer-Policy: + - no-referrer + X-Slack-Backend: + - r + X-Frame-Options: + - SAMEORIGIN + Access-Control-Allow-Origin: + - "*" + Content-Length: + - '22' + Content-Type: + - text/html + X-Via: + - haproxy-www-5jn9 + body: + encoding: ASCII-8BIT + string: ok + http_version: null + recorded_at: Sat, 25 Apr 2020 11:49:47 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack/succeeds.yml b/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack/succeeds.yml index 2d448694a..47ad8bd58 100644 --- a/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack/succeeds.yml +++ b/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack/succeeds.yml @@ -21,7 +21,7 @@ http_interactions: message: OK headers: Date: - - Mon, 20 Apr 2020 14:48:47 GMT + - Sat, 25 Apr 2020 11:47:34 GMT Server: - Apache Vary: @@ -41,10 +41,10 @@ http_interactions: Content-Type: - text/html X-Via: - - haproxy-www-8gaq + - haproxy-www-5jx7 body: encoding: ASCII-8BIT string: ok http_version: null - recorded_at: Mon, 20 Apr 2020 14:48:48 GMT + recorded_at: Sat, 25 Apr 2020 11:47:34 GMT recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack_provider/succeeds.yml b/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack_provider/succeeds.yml index 363740b7b..386a62023 100644 --- a/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack_provider/succeeds.yml +++ b/spec/fixtures/vcr_cassettes/Mailable/send_notification_to_slack_provider/succeeds.yml @@ -21,7 +21,7 @@ http_interactions: message: OK headers: Date: - - Mon, 20 Apr 2020 14:48:48 GMT + - Sat, 25 Apr 2020 11:47:33 GMT Server: - Apache Vary: @@ -41,10 +41,10 @@ http_interactions: Content-Type: - text/html X-Via: - - haproxy-www-8k7d + - haproxy-www-0a4v body: encoding: ASCII-8BIT string: ok http_version: null - recorded_at: Mon, 20 Apr 2020 14:48:48 GMT + recorded_at: Sat, 25 Apr 2020 11:47:34 GMT recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml b/spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml index 9bbcc6da2..0847817e9 100644 --- a/spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml +++ b/spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml @@ -5,7 +5,7 @@ http_interactions: uri: "" body: encoding: US-ASCII - string: payload=%7B%22username%22%3A%22Fabrica%22%2C%22icon_url%22%3A%22https%3A%2F%2Fgithub.com%2Fdatacite%2Fsegugio%2Fblob%2Fmaster%2Fsource%2Fimages%2Ffabrica.png%22%2C%22attachments%22%3A%5B%7B%22title%22%3A%22DataCite+Fabrica%3A+New+Account%22%2C%22color%22%3A%22good%22%2C%22fields%22%3A%5B%7B%22title%22%3A%22Account+ID%22%2C%22value%22%3A%22DATACITE.DATACITE%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+type%22%2C%22value%22%3A%22Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+name%22%2C%22value%22%3A%22DataCite+Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22System+email%22%2C%22value%22%3A%22test%40datacite.org%22%2C%22short%22%3Atrue%7D%5D%7D%5D%7D + string: payload=%7B%22username%22%3A%22Fabrica%22%2C%22icon_url%22%3A%22https%3A%2F%2Fgithub.com%2Fdatacite%2Fsegugio%2Fblob%2Fmaster%2Fsource%2Fimages%2Ffabrica.png%22%2C%22attachments%22%3A%5B%7B%22title%22%3A%22DataCite+Fabrica%3A+New+Account%22%2C%22color%22%3A%22good%22%2C%22fields%22%3A%5B%7B%22title%22%3A%22Account+ID%22%2C%22value%22%3A%22DATACITE.DATACITE%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+type%22%2C%22value%22%3A%22Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+name%22%2C%22value%22%3A%22DataCite+Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22System+email%22%2C%22value%22%3A%22test%40datacite.org%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Responsible+Account+ID%22%2C%22value%22%3A%22DATACITE%22%7D%5D%7D%5D%7D headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 @@ -21,7 +21,7 @@ http_interactions: message: OK headers: Date: - - Mon, 20 Apr 2020 14:48:46 GMT + - Sat, 25 Apr 2020 11:47:32 GMT Server: - Apache Vary: @@ -31,7 +31,7 @@ http_interactions: Referrer-Policy: - no-referrer X-Slack-Backend: - - h + - r X-Frame-Options: - SAMEORIGIN Access-Control-Allow-Origin: @@ -41,10 +41,10 @@ http_interactions: Content-Type: - text/html X-Via: - - haproxy-www-s8m4 + - haproxy-www-or0r body: encoding: ASCII-8BIT string: ok http_version: null - recorded_at: Mon, 20 Apr 2020 14:48:47 GMT + recorded_at: Sat, 25 Apr 2020 11:47:32 GMT recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/User/reset_client_password/sends_message.yml b/spec/fixtures/vcr_cassettes/User/reset_client_password/sends_message.yml index 433f95b22..4811cc700 100644 --- a/spec/fixtures/vcr_cassettes/User/reset_client_password/sends_message.yml +++ b/spec/fixtures/vcr_cassettes/User/reset_client_password/sends_message.yml @@ -5,7 +5,7 @@ http_interactions: uri: "" body: encoding: US-ASCII - string: payload=%7B%22username%22%3A%22Fabrica%22%2C%22icon_url%22%3A%22https%3A%2F%2Fgithub.com%2Fdatacite%2Fsegugio%2Fblob%2Fmaster%2Fsource%2Fimages%2Ffabrica.png%22%2C%22attachments%22%3A%5B%7B%22title%22%3A%22DataCite+Fabrica%3A+Password+Reset+Request+Sent%22%2C%22color%22%3A%22good%22%2C%22fields%22%3A%5B%7B%22title%22%3A%22Account+ID%22%2C%22value%22%3A%22DATACITE.DATACITE%22%7D%2C%7B%22title%22%3A%22Name%22%2C%22value%22%3A%22My+data+center%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22System+email%22%2C%22value%22%3A%22test%40datacite.org%22%2C%22short%22%3Atrue%7D%5D%7D%5D%7D + string: payload=%7B%22username%22%3A%22Fabrica%22%2C%22icon_url%22%3A%22https%3A%2F%2Fgithub.com%2Fdatacite%2Fsegugio%2Fblob%2Fmaster%2Fsource%2Fimages%2Ffabrica.png%22%2C%22attachments%22%3A%5B%7B%22title%22%3A%22DataCite+Fabrica%3A+Password+Reset+Request+Sent%22%2C%22color%22%3A%22good%22%2C%22fields%22%3A%5B%7B%22title%22%3A%22Account+ID%22%2C%22value%22%3A%22DATACITE.DATACITE%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+type%22%2C%22value%22%3A%22Repository%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22Account+name%22%2C%22value%22%3A%22My+data+center%22%2C%22short%22%3Atrue%7D%2C%7B%22title%22%3A%22System+email%22%2C%22value%22%3A%22test%40datacite.org%22%2C%22short%22%3Atrue%7D%5D%7D%5D%7D headers: Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 @@ -20,14 +20,8 @@ http_interactions: code: 200 message: OK headers: - Content-Type: - - text/html - Content-Length: - - '22' - Connection: - - keep-alive Date: - - Sat, 28 Mar 2020 04:59:23 GMT + - Sat, 25 Apr 2020 11:45:54 GMT Server: - Apache Vary: @@ -42,19 +36,15 @@ http_interactions: - SAMEORIGIN Access-Control-Allow-Origin: - "*" + Content-Length: + - '22' + Content-Type: + - text/html X-Via: - - haproxy-www-4tm2 - X-Cache: - - Miss from cloudfront - Via: - - 1.1 cddd3e95f67291463f7a95d065c7fcff.cloudfront.net (CloudFront) - X-Amz-Cf-Pop: - - DUS51-C1 - X-Amz-Cf-Id: - - gbFYaawbJ2Yx7OmirQWS9b7g1YSFP3FLOaq1-mnX7NKVqK4ZmV5z4Q== + - haproxy-www-scvj body: encoding: ASCII-8BIT string: ok http_version: null - recorded_at: Sat, 28 Mar 2020 04:59:23 GMT + recorded_at: Sat, 25 Apr 2020 11:45:55 GMT recorded_with: VCR 5.1.0