From 55551dad37584be2500613512f44ca3ba1856e5e Mon Sep 17 00:00:00 2001 From: colinsoleim Date: Sun, 31 May 2020 10:31:15 -0400 Subject: [PATCH 1/2] Finishes email configuration for password reset - Updates copy on existing Devise reset password email - Adds new mailer prompting user to add password when new volunteer is created --- app/controllers/volunteers_controller.rb | 5 +- app/mailers/volunteer_mailer.rb | 9 +- app/models/user.rb | 12 + .../reset_password_instructions.html.erb | 215 ++++++++++++++---- .../volunteer_mailer/account_setup.html.erb | 18 ++ .../previews/volunteer_mailer_preview.rb | 4 + 6 files changed, 214 insertions(+), 49 deletions(-) create mode 100644 app/views/volunteer_mailer/account_setup.html.erb diff --git a/app/controllers/volunteers_controller.rb b/app/controllers/volunteers_controller.rb index 05f8b3f8fc..0b77e3acff 100644 --- a/app/controllers/volunteers_controller.rb +++ b/app/controllers/volunteers_controller.rb @@ -9,9 +9,10 @@ def new end def create - volunteer = User.new(create_volunteer_params) + @volunteer = User.new(create_volunteer_params) - if volunteer.save + if @volunteer.save + VolunteerMailer.account_setup(@volunteer).deliver redirect_to root_path else render :new diff --git a/app/mailers/volunteer_mailer.rb b/app/mailers/volunteer_mailer.rb index 068780b284..0016fa8f1b 100644 --- a/app/mailers/volunteer_mailer.rb +++ b/app/mailers/volunteer_mailer.rb @@ -1,9 +1,16 @@ class VolunteerMailer < ApplicationMailer default from: "CASA Admin " - # send a signup email to the user, pass in the user object that contains the user's email address + # send a signup email to the user, pass in the user object that contains the user's email address def deactivation(user) @user = user mail(to: @user.email, subject: "Your CASA volunteer account has been deactivated") end + + # send a signup email to the user, pass in the user object that contains the user's email address + def account_setup(user) + @user = user + @token = @user.generate_password_reset_token + mail(to: @user.email, subject: "Create a password & set up your account") + end end diff --git a/app/models/user.rb b/app/models/user.rb index 6ab3918dd1..44a38d1f45 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -32,6 +32,18 @@ def past_names # get past_names from paper_trail gem, version_limit is 10 so no performance concerns versions.map { |version| version&.reify&.display_name } end + + # Generate a Devise reset_token, used for the account_setup mailer. This happens automatically + # when a user clicks "Reset My Password", so do not use this method in that flow. + def generate_password_reset_token + raw_token, hashed_token = Devise.token_generator.generate(self.class, :reset_password_token) + + self.reset_password_token = hashed_token + self.reset_password_sent_at = Time.now.utc + save(validate: false) + + raw_token + end end # == Schema Information diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb index e1780d9fb4..c74e82c7cf 100644 --- a/app/views/devise/mailer/reset_password_instructions.html.erb +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -1,53 +1,176 @@ - - + + + + + Actionable emails e.g. reset password - - - - - - - +
-

CASA Password Reset

-
- - - - -
- - - - - - -
- - - - -
-

If you've lost your password or wish to reset it, click the button below. If you didn’t request this, you can safely ignore this email.

-
-
- - - + +
- - - + + + + + +
" target="_blank" style="font-size: 20px;font-family: Helvetica, Arial, sans-serif;color: #ffffff;text-decoration: none; text-decoration: none;padding:15px 20px;display: inline-block;">Reset your password
+ + + - + +
+ + + + - -
+ + + + + + +
+
Court Appointed Special Advocate (CASA) / Prince George's County +
+
+
+ + + + + + +
+ GrapesJS. +
+ + + + + + +
+ + + + + + +
+ + + + +
+ + + + + + + +
+ + + + +
+ If you've lost your password or wish to reset it, click the button below. If you didn’t request this, you can safely ignore this email. +
+
+ + + + +
+ + + + +
+ " target="_blank" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">Reset your password +
+
+
+
+
+
+ + + + + + + +
+ + + + + + -
+ About CASA + + Update Profile +
-
-
+ +
-
- - +
+ diff --git a/app/views/volunteer_mailer/account_setup.html.erb b/app/views/volunteer_mailer/account_setup.html.erb new file mode 100644 index 0000000000..8524e8dba3 --- /dev/null +++ b/app/views/volunteer_mailer/account_setup.html.erb @@ -0,0 +1,18 @@ + + + + + + + + + + + +
+ A CASA/Prince George’s County volunteer console account has been created for you. This console is for logging the time you spend and actions you take on your CASA case. You can log activity with your CASA youth, their family members, their foster family or placement, the DSS worker, your Case Supervisor and others associated with your CASA case (such as teachers and therapists). +
+ Your console account is associated with this email. If this is not the correct email to use, please stop here and contact your Case Supervisor to change the email address. If you are ready to get started, please set your password. This is the first step to accessing your new volunteer account. +
+ " target="_blank" class="btn-primary" itemprop="url" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">Set Your Password +
diff --git a/lib/mailers/previews/volunteer_mailer_preview.rb b/lib/mailers/previews/volunteer_mailer_preview.rb index 18bc116f09..0177ac69e6 100644 --- a/lib/mailers/previews/volunteer_mailer_preview.rb +++ b/lib/mailers/previews/volunteer_mailer_preview.rb @@ -3,4 +3,8 @@ class VolunteerMailerPreview < ActionMailer::Preview def deactivation VolunteerMailer.deactivation(User.last) end + + def account_setup + VolunteerMailer.account_setup(User.last) + end end From fcfda0e8e0d403449f8e150781c41a34afb19359 Mon Sep 17 00:00:00 2001 From: colinsoleim Date: Sun, 31 May 2020 10:53:18 -0400 Subject: [PATCH 2/2] Add test for email delivery on volunteers#create --- config/environments/development.rb | 2 +- config/environments/test.rb | 2 +- spec/requests/volunteers_spec.rb | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index f5fe3c0a25..ef32050a45 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,7 +1,7 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. - config.action_mailer.default_url_options = {host: "localhost", port: 3000} # for devise authentication + config.action_mailer.default_url_options = { host: "localhost", port: 3000 } # for devise authentication # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development diff --git a/config/environments/test.rb b/config/environments/test.rb index 2e3941338c..18eb4f0d83 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -6,7 +6,7 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. - # config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # for devise authentication + config.action_mailer.default_url_options = { host: "localhost", port: 3000 } # for devise authentication config.cache_classes = false diff --git a/spec/requests/volunteers_spec.rb b/spec/requests/volunteers_spec.rb index 42a433db49..0b8b3beb8b 100644 --- a/spec/requests/volunteers_spec.rb +++ b/spec/requests/volunteers_spec.rb @@ -29,13 +29,25 @@ expected_email = "volunteer1@example.com" sign_in admin - post volunteers_url, - params: {user: {email: expected_email, casa_org_id: admin.casa_org_id}} + post volunteers_url, params: { + user: { email: expected_email, casa_org_id: admin.casa_org_id } + } expect(User.last.email).to eq(expected_email) expect(response).to redirect_to root_path end + + it "sends an account_setup email" do + expected_email = "volunteer1@example.com" + sign_in admin + + expect do + post volunteers_url, params: { + user: { email: expected_email, casa_org_id: admin.casa_org_id } + } + end.to change { ActionMailer::Base.deliveries.count }.by(1) + end end describe "PATCH /update" do