From 1b80d0f88fd2d40262b2675fdcfd36279074683e Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Wed, 11 May 2022 09:47:00 +0100 Subject: [PATCH] Use rake task to send onboarding emails (#566) * Use rake task to send onboarding emails * Wrap host lookup so it's easily stubbable * Rake task can be called outside the rails environment so need to pass host in * Not part of the usual app flow so contain to rake task * Including routes helper in a rake task is a rabbit hole * Use ENV var rather than param for host --- .env.example | 2 ++ lib/tasks/onboarding_emails.rake | 22 +++++++++++++ spec/features/reset_password.html.erb | 31 ------------------ spec/lib/tasks/onboarding_emails_spec.rb | 41 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 lib/tasks/onboarding_emails.rake delete mode 100644 spec/features/reset_password.html.erb create mode 100644 spec/lib/tasks/onboarding_emails_spec.rb diff --git a/.env.example b/.env.example index 2163f2802..b6a2bf14f 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,5 @@ DB_PASSWORD=postgres-password GOVUK_NOTIFY_API_KEY= OTP_SECRET_ENCRYPTION_KEY="" + +APP_HOST="http://localhost:3000" diff --git a/lib/tasks/onboarding_emails.rake b/lib/tasks/onboarding_emails.rake new file mode 100644 index 000000000..6610358db --- /dev/null +++ b/lib/tasks/onboarding_emails.rake @@ -0,0 +1,22 @@ +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] + host = ENV["APP_HOST"] + raise "Organisation id must be provided" unless organisation_id + raise "Host is not set" unless host + + organisation = Organisation.find(organisation_id) + raise "Organisation #{organisation_id} does not exist" unless organisation + + organisation.users.each do |user| + next unless URI::MailTo::EMAIL_REGEXP.match?(user.email) + + onboarding_template_id = "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd".freeze + token = user.send(:set_reset_password_token) + url = "#{host}/account/password/edit?reset_password_token=#{token}" + personalisation = { name: user.name || user.email, link: url } + DeviseNotifyMailer.new.send_email(user.email, onboarding_template_id, personalisation) + end + end +end diff --git a/spec/features/reset_password.html.erb b/spec/features/reset_password.html.erb deleted file mode 100644 index 1bf271dab..000000000 --- a/spec/features/reset_password.html.erb +++ /dev/null @@ -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 %> -
-
- <%= f.govuk_error_summary %> - -

- <%= content_for(:title) %> -

- - <%= 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" %> -
-
-<% end %> diff --git a/spec/lib/tasks/onboarding_emails_spec.rb b/spec/lib/tasks/onboarding_emails_spec.rb new file mode 100644 index 000000000..a1f2809b1 --- /dev/null +++ b/spec/lib/tasks/onboarding_emails_spec.rb @@ -0,0 +1,41 @@ +require "rails_helper" +require "rake" + +describe "rake onboarding_emails:send", type: task do + subject(:task) { Rake::Task["onboarding_emails:send"] } + + context "when onboarding a new organisation to private beta" do + let!(:user) { FactoryBot.create(:user) } + let(:notify_client) { instance_double(Notifications::Client) } + let(:devise_notify_mailer) { DeviseNotifyMailer.new } + let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" } + let(:host) { "http://localhost:3000" } + + 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(:[]) + allow(ENV).to receive(:[]).with("APP_HOST").and_return(host) + end + + it "can send the onboarding emails" do + expect(notify_client).to receive(:send_email).with( + { + email_address: user.email, + template_id: "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd", + personalisation: { + name: user.name, + link: "#{host}/account/password/edit?reset_password_token=#{reset_password_token}", + }, + }, + ) + + task.invoke(user.organisation.id) + end + end +end