-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add campaigns test class to send campaign_email to committee only (#256)
* add campaigns test class to send campaign_email to committee only * move memberships to private methods
- Loading branch information
Showing
9 changed files
with
137 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class Campaigns::Test < Campaigns::Send | ||
private | ||
|
||
def memberships | ||
Membership.where(user: User.committee) | ||
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
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 @@ | ||
FactoryBot.define do | ||
factory :campaign_delivery do | ||
association :campaign | ||
association :membership | ||
delivered_at { Time.current } | ||
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Campaigns::Send do | ||
let(:user) { create(:user) } | ||
let(:campaign) { create(:campaign) } | ||
let(:membership) { user.memberships.first } | ||
let(:campaign_delivery) { create(:campaign_delivery, campaign: campaign, membership: membership) } | ||
|
||
before do | ||
membership | ||
allow(CampaignsMailer).to receive_message_chain(:campaign_email, :deliver_now) | ||
end | ||
|
||
describe '.call' do | ||
subject { described_class.call(campaign) } | ||
|
||
context 'when campaign has already been delivered' do | ||
before do | ||
campaign_delivery | ||
campaign.update!(delivered_at: Time.current) | ||
end | ||
|
||
it 'does not send any emails' do | ||
expect(CampaignsMailer).not_to receive(:campaign_email) | ||
subject | ||
end | ||
end | ||
|
||
context 'when campaign has not been delivered' do | ||
before { campaign.update(delivered_at: nil) } | ||
|
||
context 'and memberships exist' do | ||
it 'sends emails to all memberships' do | ||
expect(CampaignsMailer).to receive(:campaign_email).with(campaign, membership, anything).and_call_original | ||
subject | ||
end | ||
|
||
it 'generate the delivery' do | ||
expect { subject }.to change { CampaignDelivery.count }.by(1) | ||
end | ||
|
||
it 'updates the campaign delivery time' do | ||
subject | ||
expect(CampaignDelivery.first.delivered_at).to be_present | ||
end | ||
end | ||
|
||
context 'and no memberships exist' do | ||
before do | ||
membership.update!(left_at: Time.current) | ||
end | ||
|
||
it 'does not send any emails' do | ||
expect(CampaignsMailer).not_to receive(:campaign_email) | ||
subject | ||
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,30 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Campaigns::Test do | ||
subject { described_class } | ||
|
||
let(:committee_users) { create_list(:user, 3, :committee) } | ||
let(:non_committee_user) { create(:user) } | ||
let(:committee_memberships) { Membership.where(user: committee_users) } | ||
let(:non_committee_membership) { Membership.where(user: non_committee_user) } | ||
let(:campaign) { create(:campaign) } | ||
|
||
before do | ||
committee_memberships | ||
non_committee_membership | ||
allow(CampaignsMailer).to receive_message_chain(:campaign_email, :deliver_now) | ||
end | ||
describe '.call' do | ||
it 'delivers campaign delivery for committee users' do | ||
expect do | ||
subject.call(campaign) | ||
end.to change { CampaignDelivery.where(membership: committee_memberships).count }.by(3) | ||
end | ||
|
||
it 'does not deliver campaign delivery for non-committee users' do | ||
expect do | ||
subject.call(campaign) | ||
end.to change { CampaignDelivery.where(membership: non_committee_membership).count }.by(0) | ||
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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User do | ||
RSpec.describe User, type: :model do | ||
let(:unconfirmed_user) { create(:user, confirmed_at: nil) } | ||
let(:committee_user) { create(:user, committee: true) } | ||
|
||
describe '.committee' do | ||
before do | ||
committee_user | ||
unconfirmed_user | ||
end | ||
it 'returns users who are part of the committee' do | ||
expect(User.committee).to include(committee_user) | ||
expect(User.committee).not_to include(unconfirmed_user) | ||
end | ||
end | ||
end |