diff --git a/app/models/user.rb b/app/models/user.rb index 27a884fa4..83f09b605 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -67,6 +67,7 @@ class User < ApplicationRecord MFA_TEMPLATE_ID = "6bdf5ee1-8e01-4be1-b1f9-747061d8a24c".freeze RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".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 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) 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 return {} unless data_coordinator? || support? return ROLES if support? diff --git a/lib/tasks/onboarding_emails.rake b/lib/tasks/onboarding_emails.rake new file mode 100644 index 000000000..4624055ef --- /dev/null +++ b/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 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..dc58a7944 --- /dev/null +++ b/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