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. 5
      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)
end
def host
ENV["APP_HOST"]
end
def send_beta_onboarding_email
return unless URI::MailTo::EMAIL_REGEXP.match?(email)
def send_beta_onboarding_email(host)
return unless URI::MailTo::EMAIL_REGEXP.match?(email) && host
template_id = BETA_ONBOARDING_TEMPLATE_ID
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
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]
host = args[:host]
raise "Organisation id must be provided" unless organisation_id
raise "host must be provided" unless host
organisation = Organisation.find(organisation_id)
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

5
spec/lib/tasks/onboarding_emails_spec.rb

@ -4,6 +4,7 @@ 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 }
@ -17,10 +18,8 @@ describe "rake onboarding_emails:send", type: task do
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(user).to receive(: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(
{
@ -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

Loading…
Cancel
Save