Skip to content

Commit

Permalink
Add jobs for queuing and delivering petition emails
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed Aug 7, 2015
1 parent 0afe2cb commit a76d28c
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 40 deletions.
14 changes: 14 additions & 0 deletions app/jobs/deliver_petition_email_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DeliverPetitionEmailJob < ActiveJob::Base
include EmailDelivery

attr_reader :email

def perform(**args)
@email = args[:email]
super
end

def create_email
mailer.email_signer petition, signature, email
end
end
19 changes: 19 additions & 0 deletions app/jobs/email_petitioners_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class EmailPetitionersJob < ActiveJob::Base
include EmailAllPetitionSignatories

self.email_delivery_job_class = DeliverPetitionEmailJob
self.timestamp_name = 'petition_email'

attr_reader :email

def perform(**args)
@email = args[:email]
super
end

private

def mailer_arguments(signature)
super.merge(email: email)
end
end
22 changes: 14 additions & 8 deletions spec/jobs/deliver_debate_outcome_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
require_relative 'shared_examples'

RSpec.describe DeliverDebateOutcomeEmailJob, type: :job do
let(:email_requested_at) { Time.current }
let(:requested_at) { Time.current.change(usec: 0) }
let(:requested_at_as_string) { requested_at.getutc.iso8601(6) }

let(:petition) { FactoryGirl.create(:debated_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, petition: petition) }
let(:timestamp_name) { 'debate_outcome' }

let :arguments do
{
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: requested_at_as_string
}
end

before do
petition.set_email_requested_at_for(timestamp_name, to: email_requested_at)
petition.set_email_requested_at_for(timestamp_name, to: requested_at)
end

it_behaves_like "a job to send an signatory email"

it "uses the correct mailer method to generate the email" do
expect(subject).to receive_message_chain(:mailer, :notify_signer_of_debate_outcome).with(petition, signature).and_return double.as_null_object
subject.perform(
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: email_requested_at.getutc.iso8601(6)
)
subject.perform(**arguments)
end
end
22 changes: 14 additions & 8 deletions spec/jobs/deliver_debate_scheduled_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
require_relative 'shared_examples'

RSpec.describe DeliverDebateScheduledEmailJob, type: :job do
let(:email_requested_at) { Time.current }
let(:requested_at) { Time.current.change(usec: 0) }
let(:requested_at_as_string) { requested_at.getutc.iso8601(6) }

let(:petition) { FactoryGirl.create(:awaiting_debate_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, petition: petition) }
let(:timestamp_name) { 'debate_scheduled' }

let :arguments do
{
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: requested_at_as_string
}
end

before do
petition.set_email_requested_at_for(timestamp_name, to: email_requested_at)
petition.set_email_requested_at_for(timestamp_name, to: requested_at)
end

it_behaves_like "a job to send an signatory email"

it "uses the correct mailer method to generate the email" do
expect(subject).to receive_message_chain(:mailer, :notify_signer_of_debate_scheduled).with(petition, signature).and_return double.as_null_object
subject.perform(
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: email_requested_at.getutc.iso8601(6)
)
subject.perform(**arguments)
end
end
33 changes: 33 additions & 0 deletions spec/jobs/deliver_petition_email_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rails_helper'
require_relative 'shared_examples'

RSpec.describe DeliverPetitionEmailJob, type: :job do
let(:requested_at) { Time.current.change(usec: 0) }
let(:requested_at_as_string) { requested_at.getutc.iso8601(6) }

let(:petition) { FactoryGirl.create(:debated_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, petition: petition) }
let(:email) { FactoryGirl.create(:petition_email, petition: petition) }
let(:timestamp_name) { 'petition_email' }

let :arguments do
{
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: requested_at_as_string,
email: email
}
end

before do
petition.set_email_requested_at_for(timestamp_name, to: requested_at)
end

it_behaves_like "a job to send an signatory email"

it "uses the correct mailer method to generate the email" do
expect(subject).to receive_message_chain(:mailer, :email_signer).with(petition, signature, email).and_return double.as_null_object
subject.perform(**arguments)
end
end
22 changes: 14 additions & 8 deletions spec/jobs/deliver_threshold_response_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
require_relative 'shared_examples'

RSpec.describe DeliverThresholdResponseEmailJob, type: :job do
let(:email_requested_at) { Time.current }
let(:requested_at) { Time.current.change(usec: 0) }
let(:requested_at_as_string) { requested_at.getutc.iso8601(6) }

let(:petition) { FactoryGirl.create(:responded_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, petition: petition) }
let(:timestamp_name) { 'government_response' }

let :arguments do
{
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: requested_at_as_string
}
end

before do
petition.set_email_requested_at_for(timestamp_name, to: email_requested_at)
petition.set_email_requested_at_for(timestamp_name, to: requested_at)
end

it_behaves_like "a job to send an signatory email"

it "uses the correct mailer method to generate the email" do
expect(subject).to receive_message_chain(:mailer, :notify_signer_of_threshold_response).with(petition, signature).and_return double.as_null_object
subject.perform(
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: email_requested_at.getutc.iso8601
)
subject.perform(**arguments)
end
end
1 change: 1 addition & 0 deletions spec/jobs/email_debate_outcomes_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:email_requested_at) { Time.current }
let(:petition) { FactoryGirl.create(:open_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, :petition => petition) }
let(:arguments) { { petition: petition } }

before do
petition.set_email_requested_at_for('debate_outcome', to: email_requested_at)
Expand Down
1 change: 1 addition & 0 deletions spec/jobs/email_debate_scheduled_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:email_requested_at) { Time.current }
let(:petition) { FactoryGirl.create(:open_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, :petition => petition) }
let(:arguments) { { petition: petition } }

before do
petition.set_email_requested_at_for('debate_scheduled', to: email_requested_at)
Expand Down
18 changes: 18 additions & 0 deletions spec/jobs/email_petitioners_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'
require_relative 'shared_examples'

RSpec.describe EmailPetitionersJob, type: :job do
let(:email_requested_at) { Time.current }
let(:petition) { FactoryGirl.create(:open_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, :petition => petition) }
let(:email) { FactoryGirl.create(:petition_email, petition: petition) }
let(:arguments) { { petition: petition, email: email } }

before do
petition.set_email_requested_at_for('petition_email', to: email_requested_at)
allow(petition).to receive_message_chain(:signatures_to_email_for, :find_each).and_yield(signature)
end

it_behaves_like "job to enqueue signatory mailing jobs"

end
1 change: 1 addition & 0 deletions spec/jobs/email_threshold_response_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:email_requested_at) { Time.current }
let(:petition) { FactoryGirl.create(:open_petition) }
let(:signature) { FactoryGirl.create(:validated_signature, :petition => petition) }
let(:arguments) { { petition: petition } }

before do
petition.set_email_requested_at_for('government_response', to: email_requested_at)
Expand Down
18 changes: 2 additions & 16 deletions spec/jobs/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def do_work
end

before do
described_class.run_later_tonight(petition: petition)
described_class.run_later_tonight(**arguments)
end

it 'queues up a job' do
Expand Down Expand Up @@ -82,21 +82,7 @@ def do_work
end

RSpec.shared_examples_for "a job to send an signatory email" do
let(:requested_at) { Time.current.change(usec: 0) }
let(:requested_at_as_string) { requested_at.getutc.iso8601(6) }

let :arguments do
{
signature: signature,
timestamp_name: timestamp_name,
petition: petition,
requested_at: requested_at_as_string
}
end

let :job do
described_class.new(arguments)
end
let(:job) { described_class.new(arguments) }

context "when the petition has not been updated" do
let(:mail_object) { double(:mail_object) }
Expand Down

0 comments on commit a76d28c

Please sign in to comment.