Update subscription mailer specs to check that customer facing emails are white labelled, while shop facing emails are not

Also make use of the newly separated shared_examples

Made the check for link to order page more general, because on my system a double quote was expected but a single quote was generated
This commit is contained in:
Konrad
2025-05-28 10:36:37 +02:00
parent 0de4f2f596
commit 89453ec758

View File

@@ -5,8 +5,8 @@ require 'spec_helper'
RSpec.describe SubscriptionMailer do
include ActionView::Helpers::SanitizeHelper
describe '#placement_email' do
subject(:email) { SubscriptionMailer.placement_email(order, changes) }
describe '#placement_email (customer)' do
subject(:mail) { described_class.placement_email(order, changes) }
let(:changes) { {} }
let(:shop) { create(:enterprise) }
@@ -15,11 +15,16 @@ RSpec.describe SubscriptionMailer do
let(:proxy_order) { create(:proxy_order, subscription:) }
let!(:order) { proxy_order.initialise_order! }
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'customer facing email with active white labelling', :mail
end
context "when changes have been made to the order" do
before { changes[order.line_items.first.id] = 2 }
it "sends the email, which notifies the customer of changes made" do
expect { email.deliver_now }.to change { SubscriptionMailer.deliveries.count }.by(1)
expect { mail.deliver_now }.to change { SubscriptionMailer.deliveries.count }.by(1)
body = SubscriptionMailer.deliveries.last.body.encoded
@@ -30,7 +35,7 @@ RSpec.describe SubscriptionMailer do
context "and changes have not been made to the order" do
it "sends the email" do
expect { email.deliver_now }.to change { SubscriptionMailer.deliveries.count }.by(1)
expect { mail.deliver_now }.to change { SubscriptionMailer.deliveries.count }.by(1)
body = SubscriptionMailer.deliveries.last.body.encoded
@@ -46,7 +51,7 @@ RSpec.describe SubscriptionMailer do
let(:content) { Capybara::Node::Simple.new(body) }
let(:body) { SubscriptionMailer.deliveries.last.body.encoded }
before { email.deliver_now }
before { mail.deliver_now }
context "when the customer has a user account" do
let(:customer) { create(:customer, enterprise: shop) }
@@ -87,7 +92,7 @@ RSpec.describe SubscriptionMailer do
before { allow(order).to receive(:new_outstanding_balance) { 123 } }
it 'renders the amount as money' do
expect(email.body).to include('$123')
expect(mail.body).to include('$123')
end
end
@@ -95,13 +100,13 @@ RSpec.describe SubscriptionMailer do
before { allow(order).to receive(:new_outstanding_balance) { 0 } }
it 'displays the payment status' do
expect(email.body).to include('NOT PAID')
expect(mail.body).to include('NOT PAID')
end
end
end
describe '#confirmation_email' do
subject(:email) { SubscriptionMailer.confirmation_email(order) }
describe '#confirmation_email (customer)' do
subject(:mail) { described_class.confirmation_email(order) }
let(:customer) { create(:customer) }
let(:subscription) { create(:subscription, customer:, with_items: true) }
@@ -110,14 +115,15 @@ RSpec.describe SubscriptionMailer do
let(:user) { order.user }
it "sends the email" do
expect { email.deliver_now }.to change{ SubscriptionMailer.deliveries.count }.by(1)
expect { mail.deliver_now }.to change{ SubscriptionMailer.deliveries.count }.by(1)
body = SubscriptionMailer.deliveries.last.body.encoded
expect(body).to include "This order was automatically placed for you"
end
it "display the OFN header by default" do
expect(email.body).to include(ContentConfig.url_for(:logo))
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'customer facing email with active white labelling', :mail
end
describe "linking to order page" do
@@ -127,7 +133,7 @@ RSpec.describe SubscriptionMailer do
let(:customer) { create(:customer) }
it "provides link to view details" do
expect(email.body.encoded).to include(order_url(order))
expect(mail.body.encoded).to include(order_url(order))
end
end
@@ -135,7 +141,7 @@ RSpec.describe SubscriptionMailer do
let(:customer) { create(:customer, user: nil) }
it "does not provide link" do
expect(email.body).not_to match /#{order_link_href}/
expect(mail.body).not_to match /#{order_link_href}/
end
end
end
@@ -144,7 +150,7 @@ RSpec.describe SubscriptionMailer do
before { allow(order).to receive(:new_outstanding_balance) { 123 } }
it 'renders the amount as money' do
expect(email.body).to include('$123')
expect(mail.body).to include('$123')
end
end
@@ -152,40 +158,36 @@ RSpec.describe SubscriptionMailer do
before { allow(order).to receive(:new_outstanding_balance) { 0 } }
it 'displays the payment status' do
expect(email.body).to include('NOT PAID')
end
end
context 'when hide OFN navigation is enabled for the distributor of the order' do
before do
allow(order.distributor).to receive(:hide_ofn_navigation).and_return(true)
end
it 'does not display the OFN navigation' do
expect(email.body).not_to include(ContentConfig.url_for(:logo))
expect(mail.body).to include('NOT PAID')
end
end
end
describe "empty order notification" do
describe "#empty_order_email (customer)" do
subject(:mail) { described_class.empty_email(order, {}) }
let(:subscription) { create(:subscription, with_items: true) }
let(:proxy_order) { create(:proxy_order, subscription:) }
let(:distributor) { create(:distributor_enterprise) }
let!(:order) { proxy_order.initialise_order! }
before do
expect do
SubscriptionMailer.empty_email(order, {}).deliver_now
end.to change{ SubscriptionMailer.deliveries.count }.by(1)
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'customer facing email with active white labelling', :mail
end
it "sends the email" do
expect { mail.deliver_now }.to change{ SubscriptionMailer.deliveries.count }.by(1)
body = SubscriptionMailer.deliveries.last.body.encoded
expect(body).to include "We tried to place a new order with"
expect(body).to include "Unfortunately, none of products that you ordered were available"
end
end
describe "failed payment notification" do
describe "#failed_payment_email (customer)" do
subject(:mail) { described_class.failed_payment_email(order) }
let(:customer) { create(:customer) }
let(:subscription) { create(:subscription, customer:, with_items: true) }
let(:proxy_order) { create(:proxy_order, subscription:) }
@@ -193,13 +195,16 @@ RSpec.describe SubscriptionMailer do
before do
order.errors.add(:base, "This is a payment failure error")
end
expect do
SubscriptionMailer.failed_payment_email(order).deliver_now
end.to change{ SubscriptionMailer.deliveries.count }.by(1)
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'customer facing email with active white labelling', :mail
end
it "sends the email" do
expect { mail.deliver_now }.to change{ SubscriptionMailer.deliveries.count }.by(1)
body = strip_tags(SubscriptionMailer.deliveries.last.body.encoded)
expect(body).to include 'We tried to process a payment, but had some problems...'
email_so_failed_payment_explainer_html = "The payment for your subscription with <strong>%s" \
@@ -214,10 +219,8 @@ RSpec.describe SubscriptionMailer do
end
describe "linking to order page" do
let(:order_link_href) { "href=\"#{order_url(order)}\"" }
let(:email) { SubscriptionMailer.deliveries.last }
let(:body) { email.body.encoded }
let(:order_link_href) { "href=['\"]#{order_url(order)}['\"]" }
let(:body) { mail.body.encoded }
context "when the customer has a user account" do
let(:customer) { create(:customer) }
@@ -237,28 +240,37 @@ RSpec.describe SubscriptionMailer do
end
end
describe "order placement summary" do
describe "#order_placement_summary_email (hub)" do
subject(:mail) { described_class.placement_summary_email(summary) }
let!(:shop) { create(:enterprise, :with_logo_image) }
let!(:summary) { double(:summary, shop_id: shop.id) }
let(:body) { strip_tags(SubscriptionMailer.deliveries.last.body.encoded) }
let(:scope) { "subscription_mailer" }
let(:order) { build(:order_with_distributor) }
before do
allow(summary).to receive(:unrecorded_ids) { [] }
allow(summary).to receive(:subscription_issues) { [] }
allow(summary).to receive(:order_count) { 37 }
allow(summary).to receive(:issue_count) { 0 }
allow(summary).to receive(:issues) { {} }
end
it "renders the shop's logo" do
mail.deliver_now
expect(SubscriptionMailer.deliveries.last.body).to include "logo.png"
end
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'non-customer facing email with active white labelling', :mail
end
context "when no issues were encountered while processing subscriptions" do
before do
allow(summary).to receive(:order_count) { 37 }
allow(summary).to receive(:issue_count) { 0 }
allow(summary).to receive(:issues) { {} }
end
it "sends the email, which notifies the enterprise that all orders " \
"were successfully processed" do
SubscriptionMailer.placement_summary_email(summary).deliver_now
mail.deliver_now
expect(body).to include("Below is a summary of the subscription orders " \
"that have just been placed for %s." % shop.name)
expect(body).to include("A total of %d subscriptions were marked " \
@@ -266,11 +278,6 @@ RSpec.describe SubscriptionMailer do
expect(body).to include 'All were processed successfully.'
expect(body).not_to include 'Details of the issues encountered are provided below.'
end
it "renders the shop's logo" do
SubscriptionMailer.placement_summary_email(summary).deliver_now
expect(SubscriptionMailer.deliveries.last.body).to include "logo.png"
end
end
context "when some issues were encountered while processing subscriptions" do
@@ -289,7 +296,7 @@ RSpec.describe SubscriptionMailer do
context "when no unrecorded issues are present" do
it "sends the email, which notifies the enterprise that some issues were encountered" do
SubscriptionMailer.placement_summary_email(summary).deliver_now
mail.deliver_now
expect(body).to include("Below is a summary of the subscription orders " \
"that have just been placed for %s." % shop.name)
expect(body).to include("A total of %d subscriptions were marked " \
@@ -320,7 +327,7 @@ RSpec.describe SubscriptionMailer do
it "sends the email, which notifies the enterprise that some issues were encountered" do
expect(summary).to receive(:orders_affected_by).with(:other) { [order3, order4] }
SubscriptionMailer.placement_summary_email(summary).deliver_now
mail.deliver_now
expect(body).to include("Error Encountered (%d orders)" % 2)
expect(body).to include 'Automatic processing of these orders failed due to an error. ' \
'The error has been listed where possible.'
@@ -346,7 +353,7 @@ RSpec.describe SubscriptionMailer do
allow(summary).to receive(:issue_count) { 2 }
allow(summary).to receive(:issues) { { changes: { 1 => nil, 2 => nil } } }
allow(summary).to receive(:orders_affected_by) { [order1, order2] }
SubscriptionMailer.placement_summary_email(summary).deliver_now
mail.deliver_now
end
it "sends the email, which notifies the enterprise that some issues were encountered" do
@@ -370,24 +377,29 @@ RSpec.describe SubscriptionMailer do
end
end
describe "order confirmation summary" do
describe "#order_confirmation_summary_email (hub)" do
subject(:mail) { SubscriptionMailer.confirmation_summary_email(summary) }
let!(:shop) { create(:enterprise) }
let!(:summary) { double(:summary, shop_id: shop.id) }
let(:body) { strip_tags(SubscriptionMailer.deliveries.last.body.encoded) }
let(:scope) { "subscription_mailer" }
let(:order) { build(:order_with_distributor) }
before do
allow(summary).to receive(:unrecorded_ids) { [] }
allow(summary).to receive(:subscription_issues) { [] }
allow(summary).to receive(:order_count) { 37 }
allow(summary).to receive(:issue_count) { 0 }
allow(summary).to receive(:issues) { {} }
end
context "white labelling" do
it_behaves_like 'email with inactive white labelling', :mail
it_behaves_like 'non-customer facing email with active white labelling', :mail
end
context "when no issues were encountered while processing subscriptions" do
before do
allow(summary).to receive(:order_count) { 37 }
allow(summary).to receive(:issue_count) { 0 }
allow(summary).to receive(:issues) { {} }
SubscriptionMailer.confirmation_summary_email(summary).deliver_now
end
before { mail.deliver_now }
it "sends the email, which notifies the enterprise " \
"that all orders were successfully processed" do
@@ -416,7 +428,7 @@ RSpec.describe SubscriptionMailer do
context "when no unrecorded issues are present" do
it "sends the email, which notifies the enterprise that some issues were encountered" do
SubscriptionMailer.confirmation_summary_email(summary).deliver_now
mail.deliver_now
expect(body).to include("Below is a summary of the subscription orders " \
"that have just been finalised for %s." % shop.name)
expect(body).to include("A total of %d subscriptions were marked " \
@@ -447,7 +459,7 @@ RSpec.describe SubscriptionMailer do
it "sends the email, which notifies the enterprise that some issues were encountered" do
expect(summary).to receive(:orders_affected_by).with(:other) { [order3, order4] }
SubscriptionMailer.confirmation_summary_email(summary).deliver_now
mail.deliver_now
expect(body).to include("Failed Payment (%d orders)" % 2)
expect(body).to include 'Automatic processing of payment for these orders failed ' \
'due to an error. The error has been listed where possible.'
@@ -473,7 +485,7 @@ RSpec.describe SubscriptionMailer do
allow(summary).to receive(:issue_count) { 2 }
allow(summary).to receive(:issues) { { changes: { 1 => nil, 2 => nil } } }
allow(summary).to receive(:orders_affected_by) { [order1, order2] }
SubscriptionMailer.confirmation_summary_email(summary).deliver_now
mail.deliver_now
end
it "sends the email, which notifies the enterprise that some issues were encountered" do