Skip to content

Commit

Permalink
add campaigns test class to send campaign_email to committee only (#256)
Browse files Browse the repository at this point in the history
* add campaigns test class to send campaign_email to committee only
* move memberships to private methods
  • Loading branch information
gamesover authored Nov 2, 2024
1 parent d8bf4a4 commit f04440a
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/lib/campaigns/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(campaign)
def call
return if campaign.delivered_at.present?

Membership.current.each do |membership|
memberships.each do |membership|
delivery = delivery_for membership
next if delivery.delivered_at.present?

Expand All @@ -38,6 +38,10 @@ def delivery_for(membership)
)
end

def memberships
Membership.current
end

def ics
return nil if rsvp_event.nil?

Expand Down
9 changes: 9 additions & 0 deletions app/lib/campaigns/test.rb
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
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < ApplicationRecord
end.join(" OR ")
)
}
scope :committee, -> { where(committee: true) }

attr_accessor :skip_subscriptions

Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/campaigns.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ namespace :campaigns do
task send: :environment do
Campaigns::Send.call Campaign.find(ENV["CAMPAIGN_ID"])
end

desc "Only send emails to committee users for a given campaign"
task test: :environment do
Campaigns::Test.call Campaign.find(ENV["CAMPAIGN_ID"])
end
end
7 changes: 7 additions & 0 deletions spec/factories/campaign_deliveries.rb
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
4 changes: 4 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
visible { false }
end

trait :committee do
committee { true }
end

after(:create) do |user, _evaluator|
if user.confirmed_at
user.memberships.create(
Expand Down
62 changes: 62 additions & 0 deletions spec/lib/campaigns/send_spec.rb
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
30 changes: 30 additions & 0 deletions spec/lib/campaigns/test_spec.rb
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
15 changes: 14 additions & 1 deletion spec/models/user_spec.rb
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

0 comments on commit f04440a

Please sign in to comment.