Skip to content

Commit

Permalink
Add mailer specs to CI build and fix broken specs
Browse files Browse the repository at this point in the history
All the moderation methods were converted to bang methods since they
can raise exceptions but since Welsh and Scottish petitions don't use
Action Mailer the UK specs were missed when backporting.
  • Loading branch information
pixeltrix committed Apr 20, 2021
1 parent b52f9f4 commit dee1727
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 48 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -229,6 +229,52 @@ jobs:
run: |
bundle exec rake spec:jobs
spec-mailers:
runs-on: ubuntu-20.04

services:
postgres:
image: postgres:10
ports: ["5432:5432"]
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
memcached:
image: memcached:1.5.16
ports: ["11211:11211"]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

- name: Setup gem cache
uses: actions/cache@v1
with:
path: vendor/bundle
key: ubuntu-20.04-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
ubuntu-20.04-gems-
- name: Setup
uses: ./.github/actions/setup

- name: Run specs
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/epets_test
RAILS_ENV: test
SPEC_OPTS: "-f doc -P spec/mailers/**/*_spec.rb"
run: |
bundle exec rake spec
spec-models:
runs-on: ubuntu-20.04

Expand Down
80 changes: 32 additions & 48 deletions spec/mailers/petition_mailer_spec.rb
@@ -1,33 +1,23 @@
require "rails_helper"

RSpec.describe PetitionMailer, type: :mailer do
let :creator do
FactoryBot.build(:validated_signature, name: "Barry Butler", email: "bazbutler@gmail.com", creator: true)
end

let :petition do
FactoryBot.create(:pending_petition,
creator: creator,
let :attributes do
{
action: "Allow organic vegetable vans to use red diesel",
background: "Add vans to permitted users of red diesel",
additional_details: "To promote organic vegetables"
)
additional_details: "To promote organic vegetables",
creator_name: "Barry Butler",
creator_email: "bazbutler@gmail.com"
}
end

let(:creator) { petition.creator }
let(:pending_signature) { FactoryBot.create(:pending_signature, name: "Alice Smith", email: "alice@example.com", petition: petition) }
let(:validated_signature) { FactoryBot.create(:validated_signature, name: "Bob Jones", email: "bob@example.com", petition: petition) }
let(:subject_prefix) { "HM Government & Parliament Petitions" }

describe "notifying creator that moderation is delayed" do
let! :petition do
FactoryBot.create(:sponsored_petition,
creator: creator,
action: "Allow organic vegetable vans to use red diesel",
background: "Add vans to permitted users of red diesel",
additional_details: "To promote organic vegetables"
)
end

let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:subject) { "Moderation of your petition is delayed" }
let(:body) { "Sorry, but moderation of your petition is delayed for reasons." }
let(:mail) { PetitionMailer.notify_creator_that_moderation_is_delayed(creator, subject, body) }
Expand All @@ -52,10 +42,11 @@
end

describe "notifying creator of publication" do
let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:mail) { PetitionMailer.notify_creator_that_petition_is_published(creator) }

before do
petition.publish
petition.publish!
end

it "is sent to the right address" do
Expand All @@ -78,6 +69,7 @@
end

describe "notifying sponsor of publication" do
let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:mail) { PetitionMailer.notify_sponsor_that_petition_is_published(sponsor) }
let(:sponsor) do
FactoryBot.create(:validated_signature,
Expand All @@ -88,7 +80,7 @@
end

before do
petition.publish
petition.publish!
end

it "is sent to the right address" do
Expand All @@ -111,11 +103,12 @@
end

describe "notifying creator of rejection" do
let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:mail) { PetitionMailer.notify_creator_that_petition_was_rejected(creator) }

context "when rejecting for normal reasons" do
before do
petition.reject(code: "duplicate")
petition.reject!(code: "duplicate")
end

it "is sent to the right address" do
Expand All @@ -139,7 +132,7 @@

context "when there are further details" do
before do
petition.reject(code: "irrelevant", details: "Please stop trolling us" )
petition.reject!(code: "irrelevant", details: "Please stop trolling us" )
end

it "includes those details in the email" do
Expand All @@ -149,7 +142,7 @@

context "when rejecting for reason that cause the petition to be hidden" do
before do
petition.reject(code: "offensive")
petition.reject!(code: "offensive")
end

it "doesn't include a link to the petition" do
Expand All @@ -159,6 +152,7 @@
end

describe "notifying sponsor of rejection" do
let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:mail) { PetitionMailer.notify_sponsor_that_petition_was_rejected(sponsor) }
let(:sponsor) do
FactoryBot.create(:validated_signature,
Expand All @@ -170,7 +164,7 @@

context "when rejecting for normal reasons" do
before do
petition.reject(code: "duplicate")
petition.reject!(code: "duplicate")
end

it "is sent to the right address" do
Expand All @@ -194,7 +188,7 @@

context "when there are further details" do
before do
petition.reject(code: "irrelevant", details: "Please stop trolling us" )
petition.reject!(code: "irrelevant", details: "Please stop trolling us" )
end

it "includes those details in the email" do
Expand All @@ -204,7 +198,7 @@

context "when rejecting for reason that cause the petition to be hidden" do
before do
petition.reject(code: "offensive")
petition.reject!(code: "offensive")
end

it "doesn't include a link to the petition" do
Expand All @@ -214,10 +208,10 @@
end

describe "notifying creator of closing date change" do
let(:petition) { FactoryBot.create(:open_petition, attributes) }
let(:mail) { PetitionMailer.notify_creator_of_closing_date_change(creator, [petition], 3) }

before do
petition.publish
allow(Parliament).to receive(:dissolution_at).and_return(2.weeks.from_now)
allow(Parliament).to receive(:registration_closed_at).and_return(4.weeks.from_now)
allow(Parliament).to receive(:election_date).and_return(6.weeks.from_now.to_date)
Expand Down Expand Up @@ -247,10 +241,11 @@
end

describe "notifying signer of closing date change" do
let(:petition) { FactoryBot.create(:open_petition, attributes) }
let(:mail) { PetitionMailer.notify_signer_of_closing_date_change(validated_signature, [petition], 15) }

before do
petition.publish
petition.publish!
allow(Parliament).to receive(:dissolution_at).and_return(2.weeks.from_now)
allow(Parliament).to receive(:registration_closed_at).and_return(4.weeks.from_now)
allow(Parliament).to receive(:election_date).and_return(6.weeks.from_now.to_date)
Expand Down Expand Up @@ -280,15 +275,7 @@
end

describe "notifying creator of their sponsored petition being stopped" do
let! :petition do
FactoryBot.create(:sponsored_petition,
creator: creator,
action: "Allow organic vegetable vans to use red diesel",
background: "Add vans to permitted users of red diesel",
additional_details: "To promote organic vegetables"
)
end

let(:petition) { FactoryBot.create(:sponsored_petition, attributes) }
let(:mail) { PetitionMailer.notify_creator_of_sponsored_petition_being_stopped(creator) }

it "is sent to the right address" do
Expand All @@ -311,15 +298,7 @@
end

describe "notifying creator of their validated petition being stopped" do
let! :petition do
FactoryBot.create(:validated_petition,
creator: creator,
action: "Allow organic vegetable vans to use red diesel",
background: "Add vans to permitted users of red diesel",
additional_details: "To promote organic vegetables"
)
end

let(:petition) { FactoryBot.create(:validated_petition, attributes) }
let(:mail) { PetitionMailer.notify_creator_of_validated_petition_being_stopped(creator) }

it "is sent to the right address" do
Expand All @@ -342,6 +321,8 @@
end

describe "gathering sponsors for petition" do
let(:petition) { FactoryBot.create(:pending_petition, attributes) }

let(:petition_scope) { double(Petition) }
let(:moderation_queue) { 499 }

Expand Down Expand Up @@ -452,6 +433,8 @@
end

describe "notifying signature of debate outcome" do
let(:petition) { FactoryBot.create(:open_petition, attributes) }

context "when the signature is the creator" do
let(:signature) { petition.creator }
subject(:mail) { described_class.notify_creator_of_debate_outcome(petition, signature) }
Expand Down Expand Up @@ -694,7 +677,7 @@
end

describe "notifying signature of debate scheduled" do
let(:petition) { FactoryBot.create(:open_petition, :scheduled_for_debate, creator_attributes: { name: "Bob Jones", email: "bob@jones.com" }, action: "Allow organic vegetable vans to use red diesel") }
let(:petition) { FactoryBot.create(:open_petition, :scheduled_for_debate, attributes) }

shared_examples_for "a debate scheduled email" do
it "addresses the signatory by name" do
Expand Down Expand Up @@ -752,7 +735,7 @@
end

describe "emailing a signature" do
let(:petition) { FactoryBot.create(:open_petition, :scheduled_for_debate, creator_attributes: { name: "Bob Jones", email: "bob@jones.com" }, action: "Allow organic vegetable vans to use red diesel") }
let(:petition) { FactoryBot.create(:open_petition, :scheduled_for_debate, attributes) }
let(:email) { FactoryBot.create(:petition_email, petition: petition, subject: "This is a message from the committee", body: "Message body from the petition committee") }

shared_examples_for "a petition email" do
Expand Down Expand Up @@ -811,6 +794,7 @@
end

describe "skipping anonymized signatures" do
let(:petition) { FactoryBot.create(:closed_petition, attributes) }
let(:email) { FactoryBot.create(:petition_email, petition: petition, subject: "This is a message from the committee", body: "Message body from the petition committee") }
subject(:mail) { described_class.email_signer(petition, signature, email) }

Expand Down

0 comments on commit dee1727

Please sign in to comment.