Browse Source

Rake task can be called outside the rails environment so need to pass host in

pull/566/head
baarkerlounger 3 years ago
parent
commit
18567a5ef2
  1. 8
      app/models/user.rb
  2. 6
      lib/tasks/onboarding_emails.rake
  3. 31
      spec/lib/tasks/onboarding_emails_spec.rb

8
app/models/user.rb

@ -83,12 +83,8 @@ class User < ApplicationRecord
DeviseNotifyMailer.new.send_email(email, template_id, personalisation) DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end end
def host def send_beta_onboarding_email(host)
ENV["APP_HOST"] return unless URI::MailTo::EMAIL_REGEXP.match?(email) && host
end
def send_beta_onboarding_email
return unless URI::MailTo::EMAIL_REGEXP.match?(email)
template_id = BETA_ONBOARDING_TEMPLATE_ID template_id = BETA_ONBOARDING_TEMPLATE_ID
url = edit_user_password_url({ host: }) url = edit_user_password_url({ host: })

6
lib/tasks/onboarding_emails.rake

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

31
spec/lib/tasks/onboarding_emails_spec.rb

@ -4,23 +4,22 @@ require "rake"
describe "rake onboarding_emails:send", type: task do describe "rake onboarding_emails:send", type: task do
subject(:task) { Rake::Task["onboarding_emails:send"] } subject(:task) { Rake::Task["onboarding_emails:send"] }
let!(:user) { FactoryBot.create(:user) } context "when onboarding a new organisation to private beta" do
let(:notify_client) { instance_double(Notifications::Client) } let!(:user) { FactoryBot.create(:user) }
let(:devise_notify_mailer) { DeviseNotifyMailer.new } let(:notify_client) { instance_double(Notifications::Client) }
let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" } let(:devise_notify_mailer) { DeviseNotifyMailer.new }
let(:reset_password_token) { "MCDH5y6Km-U7CFPgAMVS" }
before do before do
Rake.application.rake_require("tasks/onboarding_emails") Rake.application.rake_require("tasks/onboarding_emails")
Rake::Task.define_task(:environment) Rake::Task.define_task(:environment)
task.reenable task.reenable
allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client) allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
allow(Devise.token_generator).to receive(:generate).and_return(reset_password_token) allow(Devise.token_generator).to receive(:generate).and_return(reset_password_token)
allow(user).to receive(:host).and_return("http://localhost:3000") end
end
context "when onboarding a new organisation to private beta" do
it "can send the onboarding emails" do it "can send the onboarding emails" do
expect(notify_client).to receive(:send_email).with( expect(notify_client).to receive(:send_email).with(
{ {
@ -33,7 +32,7 @@ describe "rake onboarding_emails:send", type: task do
}, },
) )
task.invoke(user.organisation.id) task.invoke(user.organisation.id, "http://localhost:3000")
end end
end end
end end

Loading…
Cancel
Save