Browse Source
* Add rake task to resend invitation emails * Update email to a job * remove redundant methodpull/1730/head
kosiakkatrina
2 years ago
committed by
GitHub
4 changed files with 147 additions and 0 deletions
@ -0,0 +1,5 @@
|
||||
class ResendInvitationMailer < NotifyMailer |
||||
def resend_invitation_email(user) |
||||
user.send_confirmation_instructions |
||||
end |
||||
end |
@ -0,0 +1,9 @@
|
||||
namespace :emails do |
||||
desc "Resend invitation emails" |
||||
task :resend_invitation_emails, %i[] => :environment do |_task, _args| |
||||
users = User.where(sign_in_count: 0, active: true) |
||||
users.each { |user| ResendInvitationMailer.resend_invitation_email(user).deliver_later } |
||||
|
||||
Rails.logger.info("Sent invitation emails to #{users.count} user.") |
||||
end |
||||
end |
@ -0,0 +1,68 @@
|
||||
require "rails_helper" |
||||
require "rake" |
||||
|
||||
RSpec.describe "emails" do |
||||
describe ":resend_invitation_emails", type: :task do |
||||
subject(:task) { Rake::Task["emails:resend_invitation_emails"] } |
||||
|
||||
let(:notify_client) { instance_double(Notifications::Client) } |
||||
let(:devise_notify_mailer) { DeviseNotifyMailer.new } |
||||
let(:resend_invitation_mailer) { ResendInvitationMailer.new } |
||||
let(:resend_invitation_email) { { deliver_later: nil } } |
||||
|
||||
before do |
||||
allow(ResendInvitationMailer).to receive(:new).and_return(resend_invitation_mailer) |
||||
allow(resend_invitation_mailer).to receive(:resend_invitation_email).and_return(resend_invitation_email) |
||||
organisation.users.destroy_all |
||||
Rake.application.rake_require("tasks/resend_invitation_emails") |
||||
Rake::Task.define_task(:environment) |
||||
task.reenable |
||||
end |
||||
|
||||
context "when the rake task is run" do |
||||
let(:organisation) { create(:organisation, name: "test organisation") } |
||||
let!(:active_user) { create(:user, name: "active user", email: "active_user@example.com", organisation:, confirmation_token: "ghi", initial_confirmation_sent: true, old_user_id: "234", sign_in_count: 0) } |
||||
let!(:inactive_user) { create(:user, name: "inactive user", email: "inactive_user@example.com", organisation:, confirmation_token: "jkl", initial_confirmation_sent: true, old_user_id: "345", active: false, sign_in_count: 0) } |
||||
let!(:logged_in_user) { create(:user, name: "logged in user", email: "logged_in_user@example.com", organisation:, confirmation_token: "mno", initial_confirmation_sent: true, old_user_id: "456", sign_in_count: 1) } |
||||
|
||||
context "with active user" do |
||||
it "sends an invitation" do |
||||
expect { task.invoke }.to enqueue_job(ActionMailer::MailDeliveryJob).with( |
||||
"ResendInvitationMailer", |
||||
"resend_invitation_email", |
||||
"deliver_now", |
||||
args: [active_user], |
||||
) |
||||
end |
||||
end |
||||
|
||||
context "with inactive user" do |
||||
it "does not send an invitation" do |
||||
expect { task.invoke }.not_to enqueue_job(ActionMailer::MailDeliveryJob).with( |
||||
"ResendInvitationMailer", |
||||
"resend_invitation_email", |
||||
"deliver_now", |
||||
args: [inactive_user], |
||||
) |
||||
end |
||||
end |
||||
|
||||
context "with logged in user" do |
||||
it "does not send an invitation" do |
||||
expect { task.invoke }.not_to enqueue_job(ActionMailer::MailDeliveryJob).with( |
||||
"ResendInvitationMailer", |
||||
"resend_invitation_email", |
||||
"deliver_now", |
||||
args: [logged_in_user], |
||||
) |
||||
end |
||||
end |
||||
|
||||
it "prints out the total number of invitations sent" do |
||||
expect(Rails.logger).to receive(:info).with(nil) |
||||
expect(Rails.logger).to receive(:info).with("Sent invitation emails to 1 user.") |
||||
task.invoke |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,65 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe ResendInvitationMailer do |
||||
describe "#resend_invitation_email" do |
||||
let(:notify_client) { instance_double(Notifications::Client) } |
||||
let(:organisation) { create(:organisation, name: "test organisation") } |
||||
let!(:active_user) { create(:user, name: "active user", email: "active_user@example.com", organisation:, confirmation_token: "ghi", initial_confirmation_sent: true, old_user_id: "234", sign_in_count: 0) } |
||||
let!(:new_active_user) { create(:user, name: "new active user", email: "new_active_user@example.com", organisation:, confirmation_token: "abc", initial_confirmation_sent: false, old_user_id: nil, sign_in_count: 0) } |
||||
let(:new_active_migrated_user) { create(:user, name: "new active migrated user", email: "new_active_migrated_user@example.com", organisation:, confirmation_token: "def", initial_confirmation_sent: false, old_user_id: "123", sign_in_count: 0) } |
||||
|
||||
before do |
||||
LegacyUser.destroy_all |
||||
allow(Notifications::Client).to receive(:new).and_return(notify_client) |
||||
allow(notify_client).to receive(:send_email).and_return(true) |
||||
end |
||||
|
||||
context "with a new active user" do |
||||
let(:personalisation) do |
||||
{ |
||||
name: "new active user", |
||||
email: "new_active_user@example.com", |
||||
organisation: "test organisation", |
||||
link: include("/account/confirmation?confirmation_token=abc"), |
||||
} |
||||
end |
||||
|
||||
it "sends invitation email to user" do |
||||
expect(notify_client).to receive(:send_email).with(email_address: "new_active_user@example.com", template_id: User::CONFIRMABLE_TEMPLATE_ID, personalisation:).once |
||||
described_class.new.resend_invitation_email(new_active_user) |
||||
end |
||||
end |
||||
|
||||
context "with active migrated user before the initial invitation has been sent" do |
||||
let(:personalisation) do |
||||
{ |
||||
name: "new active migrated user", |
||||
email: "new_active_migrated_user@example.com", |
||||
organisation: "test organisation", |
||||
link: include("/account/confirmation?confirmation_token=def"), |
||||
} |
||||
end |
||||
|
||||
it "sends an initial invitation" do |
||||
expect(notify_client).to receive(:send_email).with(email_address: "new_active_migrated_user@example.com", template_id: User::BETA_ONBOARDING_TEMPLATE_ID, personalisation:).once |
||||
described_class.new.resend_invitation_email(new_active_migrated_user) |
||||
end |
||||
end |
||||
|
||||
context "with active user after the initial invitation has been sent" do |
||||
let(:personalisation) do |
||||
{ |
||||
name: "active user", |
||||
email: "active_user@example.com", |
||||
organisation: "test organisation", |
||||
link: include("/account/confirmation?confirmation_token=ghi"), |
||||
} |
||||
end |
||||
|
||||
it "sends a reinvitation" do |
||||
expect(notify_client).to receive(:send_email).with(email_address: "active_user@example.com", template_id: User::RECONFIRMABLE_TEMPLATE_ID, personalisation:).once |
||||
described_class.new.resend_invitation_email(active_user) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue