Browse Source

Use rake task to send onboarding emails

pull/566/head
baarkerlounger 3 years ago
parent
commit
cb0030c87a
  1. 11
      app/models/user.rb
  2. 14
      lib/tasks/onboarding_emails.rake
  3. 31
      spec/features/reset_password.html.erb
  4. 39
      spec/lib/tasks/onboarding_emails_spec.rb

11
app/models/user.rb

@ -67,6 +67,7 @@ class User < ApplicationRecord
MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze
RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".freeze RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".freeze
SET_PASSWORD_TEMPLATE_ID = "257460a6-6616-4640-a3f9-17c3d73d9e91".freeze SET_PASSWORD_TEMPLATE_ID = "257460a6-6616-4640-a3f9-17c3d73d9e91".freeze
BETA_ONBOARDING_TEMPLATE_ID = "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd".freeze
def reset_password_notify_template def reset_password_notify_template
last_sign_in_at ? RESET_PASSWORD_TEMPLATE_ID : SET_PASSWORD_TEMPLATE_ID last_sign_in_at ? RESET_PASSWORD_TEMPLATE_ID : SET_PASSWORD_TEMPLATE_ID
@ -82,6 +83,16 @@ class User < ApplicationRecord
DeviseNotifyMailer.new.send_email(email, template_id, personalisation) DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end end
def send_beta_onboarding_email
return unless URI::MailTo::EMAIL_REGEXP.match?(email)
template_id = BETA_ONBOARDING_TEMPLATE_ID
url = edit_user_password_url({ host: ENV["APP_HOST"] })
token = Devise.token_generator.generate(User, :reset_password_token)
personalisation = { name: name || email, link: "#{url}?reset_password_token=#{token}" }
DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end
def assignable_roles def assignable_roles
return {} unless data_coordinator? || support? return {} unless data_coordinator? || support?
return ROLES if support? return ROLES if support?

14
lib/tasks/onboarding_emails.rake

@ -0,0 +1,14 @@
include Rails.application.routes.url_helpers
namespace :onboarding_emails do
desc "Send onboarding emails to private beta users"
task :send, %i[organisation_id] => :environment do |_task, args|
organisation_id = args[:organisation_id]
raise "Organisation id must be provided" unless organisation_id
organisation = Organisation.find(organisation_id)
raise "Organisation #{organisation_id} does not exist" unless organisation
organisation.users.each(&:send_beta_onboarding_email)
end
end

31
spec/features/reset_password.html.erb

@ -1,31 +0,0 @@
<% content_for :title, "Reset your password" %>
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: :back,
) %>
<% end %>
<%= form_for(@user, as: :user, url: password_path(User), html: { method: :put }) do |f| %>
<%= f.hidden_field :reset_password_token %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
<h1 class="govuk-heading-l">
<%= content_for(:title) %>
</h1>
<%= f.govuk_password_field :password,
label: { text: "New password" },
hint: @minimum_password_length ? { text: "Your password must be at least #{@minimum_password_length} characters and hard to guess." } : nil,
autocomplete: "new-password" %>
<%= f.govuk_password_field :password_confirmation,
label: { text: "Confirm new password" } %>
<%= f.govuk_submit "Update" %>
</div>
</div>
<% end %>

39
spec/lib/tasks/onboarding_emails_spec.rb

@ -0,0 +1,39 @@
require "rails_helper"
require "rake"
describe "rake onboarding_emails:send", type: task do
subject(:task) { Rake::Task["onboarding_emails:send"] }
let!(:user) { FactoryBot.create(:user) }
let(:notify_client) { instance_double(Notifications::Client) }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" }
before do
Rake.application.rake_require("tasks/onboarding_emails")
Rake::Task.define_task(:environment)
task.reenable
allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true)
allow(Devise.token_generator).to receive(:generate).and_return(reset_password_token)
allow(ENV).to receive(:[]).with("APP_HOST").and_return("http://localhost:3000")
end
context "when onboarding a new organisation to private beta" do
it "can send the onboarding emails" do
expect(notify_client).to receive(:send_email).with(
{
email_address: user.email,
template_id: User::BETA_ONBOARDING_TEMPLATE_ID,
personalisation: {
name: user.name,
link: "http://localhost:3000/account/password/edit?reset_password_token=#{reset_password_token}",
},
},
)
task.invoke(user.organisation.id)
end
end
end
Loading…
Cancel
Save