-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add specs for batch email notifications
This commit will add a few specs and also redo the logic for the batch email notification send frequency. We needed a migration to the users model to track when the last email was sent to check if it's time to send another one based on their batch_email_frequency setting.
- Loading branch information
Showing
12 changed files
with
214 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddLastEmailedAtToUsers < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :users, :last_emailed_at, :datetime unless column_exists?(:users, :last_emailed_at) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :mailboxer_conversation, class: 'Mailboxer::Conversation' do | ||
subject { 'Conversation subject' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :mailboxer_message, class: 'Mailboxer::Message' do | ||
type { 'Mailboxer::Message' } | ||
body { 'Message body' } | ||
subject { 'Message subject' } | ||
association :sender, factory: :user | ||
association :conversation, factory: :mailboxer_conversation | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :mailboxer_notification, class: 'Mailboxer::Notification' do | ||
type { 'Mailboxer::Notification' } | ||
body { 'Notification body' } | ||
subject { 'Notification subject' } | ||
association :sender, factory: :user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :mailboxer_receipt, class: 'Mailboxer::Receipt' do | ||
association :receiver, factory: :user | ||
association :notification, factory: :mailboxer_message | ||
is_read { false } | ||
trashed { false } | ||
deleted { false } | ||
mailbox_type { "inbox" } | ||
is_delivered { false } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe BatchEmailNotificationJob do | ||
let(:account) { FactoryBot.create(:account) } | ||
let(:receipt) { FactoryBot.create(:mailboxer_receipt, receiver: user) } | ||
let!(:message) { receipt.notification } | ||
let!(:user) { FactoryBot.create(:user, batch_email_frequency: frequency) } | ||
|
||
before do | ||
allow(Apartment::Tenant).to receive(:switch).and_yield | ||
ActionMailer::Base.deliveries.clear | ||
end | ||
|
||
after do | ||
clear_enqueued_jobs | ||
end | ||
|
||
describe '#perform' do | ||
let(:frequency) { 'daily' } | ||
|
||
it 'marks the message as delivered' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to change { message.receipts.first.is_delivered }.from(false).to(true) | ||
end | ||
|
||
it 're-enqueues the job' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to have_enqueued_job(BatchEmailNotificationJob).with(account) | ||
end | ||
|
||
context 'when the user has a daily frequency' do | ||
let(:frequency) { 'daily' } | ||
|
||
it 'sends email to users with batch_email_frequency set' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to change { ActionMailer::Base.deliveries.count }.by(1) | ||
end | ||
end | ||
|
||
context 'when the user has a weekly frequency' do | ||
let(:frequency) { 'weekly' } | ||
let(:user) { FactoryBot.create(:user, batch_email_frequency: frequency, last_emailed_at:) } | ||
|
||
context 'when the user was last emailed less than a week ago' do | ||
let(:last_emailed_at) { 5.days.ago } | ||
|
||
it 'does not send an email to users with batch_email_frequency set' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to_not change { ActionMailer::Base.deliveries.count } | ||
end | ||
end | ||
|
||
context 'when the user was last emailed more than a week ago' do | ||
let(:last_emailed_at) { 8.days.ago } | ||
|
||
it 'sends email to users with batch_email_frequency set' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to change { ActionMailer::Base.deliveries.count }.by(1) | ||
end | ||
end | ||
end | ||
|
||
context 'when the user has a monthly frequency' do | ||
let(:frequency) { 'monthly' } | ||
let(:user) { FactoryBot.create(:user, batch_email_frequency: frequency, last_emailed_at:) } | ||
|
||
context 'when the user was last emailed less than a month ago' do | ||
let(:last_emailed_at) { 20.days.ago } | ||
|
||
it 'does not send an email to users with batch_email_frequency set' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to_not change { ActionMailer::Base.deliveries.count } | ||
end | ||
end | ||
|
||
context 'when the user was last emailed more than a month ago' do | ||
let(:last_emailed_at) { 40.days.ago } | ||
|
||
it 'sends email to users with batch_email_frequency set' do | ||
expect { BatchEmailNotificationJob.perform_now(account) }.to change { ActionMailer::Base.deliveries.count }.by(1) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Mailboxer::Receipt, type: :decorator do | ||
describe '#mark_as_delivered' do | ||
context 'when the user has batch_email_frequency set to never' do | ||
let(:user) { create(:user, batch_email_frequency: 'never') } | ||
let(:receipt) { create(:mailboxer_receipt, receiver: user) } | ||
|
||
before do | ||
# ensure we have a undelivered receipt | ||
receipt.update(is_delivered: false) | ||
end | ||
|
||
it 'marks the receipt as delivered' do | ||
expect { receipt.mark_as_delivered }.to change { receipt.is_delivered }.from(false).to(true) | ||
end | ||
end | ||
|
||
context 'when the user does not have batch_email_frequency set to never' do | ||
let(:user) { create(:user, batch_email_frequency: 'daily') } | ||
let(:receipt) { create(:mailboxer_receipt, receiver: user) } | ||
|
||
before do | ||
# ensure we have a undelivered receipt | ||
receipt.update(is_delivered: false) | ||
end | ||
|
||
it 'does not mark the receipt as delivered' do | ||
expect { receipt.mark_as_delivered }.not_to change { receipt.is_delivered } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters