Merge pull request #13230 from filipefurtad0/specs_mailer_reply_to_changes

Adds tests to recent reply_to email changes
This commit is contained in:
Gaetan Craig-Riou
2025-03-31 10:38:59 +11:00
committed by GitHub
7 changed files with 122 additions and 29 deletions

View File

@@ -265,6 +265,7 @@ RSpec.configure do |config|
config.include OpenFoodNetwork::PerformanceHelper
config.include ActiveJob::TestHelper
config.include ReportsHelper
config.include MailersHelper, type: :mailer
config.include TomSelectHelper, type: :system
config.include ViewComponent::TestHelpers, type: :component

View File

@@ -3,25 +3,31 @@
require 'spec_helper'
RSpec.describe EnterpriseMailer do
let!(:enterprise) { create(:enterprise) }
let!(:user) { create(:user) }
let(:enterprise) { build(:enterprise, name: "Fred's Farm") }
describe "#welcome" do
it "sends a welcome email when given an enterprise" do
EnterpriseMailer.welcome(enterprise).deliver_now
subject(:mail) { EnterpriseMailer.welcome(enterprise) }
mail = ActionMailer::Base.deliveries.first
it "sends a welcome email when given an enterprise" do
expect(mail.subject)
.to eq "#{enterprise.name} is now on #{Spree::Config[:site_name]}"
.to eq "Fred's Farm is now on OFN Demo Site"
end
it "does not set a reply-to email" do
expect(mail.reply_to).to eq nil
end
end
describe "#manager_invitation" do
subject(:mail) { EnterpriseMailer.manager_invitation(enterprise, user) }
let(:user) { build(:user) }
it "should send a manager invitation email when given an enterprise and user" do
EnterpriseMailer.manager_invitation(enterprise, user).deliver_now
expect(ActionMailer::Base.deliveries.count).to eq 1
mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to eq "#{enterprise.name} has invited you to be a manager"
expect(mail.subject).to eq "Fred's Farm has invited you to be a manager"
end
it "sets a reply-to of the enterprise email" do
expect(mail.reply_to).to eq([enterprise.contact.email])
end
end
end

View File

@@ -69,6 +69,10 @@ RSpec.describe Spree::OrderMailer do
expect(email.body).to include('Payment summary')
end
it "sets a reply-to of the customer email" do
expect(email.reply_to).to eq([order.email])
end
context 'when the order has outstanding balance' do
before { allow(order).to receive(:new_outstanding_balance) { 123 } }
@@ -150,6 +154,28 @@ RSpec.describe Spree::OrderMailer do
it "includes a link to the cancelled order in admin" do
expect(mail.body).to match /#{admin_order_link_href}/
end
it "sets a reply-to of the customer email" do
expect(mail.reply_to).to eq([order.email])
end
end
describe "#cancel_email (for_customer)" do
let(:distributor) { create(:distributor_enterprise) }
let(:order) { create(:order, distributor:, state: "canceled") }
let(:mail) { Spree::OrderMailer.cancel_email(order) }
it "sends an email to the customer" do
expect(mail.to).to eq([order.email])
end
it "displays the order number" do
expect(mail.body).to include(order.number.to_s)
end
it "sets a reply-to of the customer email" do
expect(mail.reply_to).to eq([order.distributor.contact.email])
end
end
describe "order confimation" do
@@ -245,6 +271,7 @@ RSpec.describe Spree::OrderMailer do
expect(deliveries.count).to eq(1)
expect(deliveries.first.attachments.count).to eq(1)
expect(deliveries.first.attachments.first.filename).to eq(attachment_filename)
expect(email.reply_to).to eq([order.distributor.contact.email])
end
end

View File

@@ -0,0 +1,46 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PaymentMailer do
describe '#payment_mailer' do
let(:enterprise) { create(:enterprise) }
let(:payment_method) {
create(:payment_method, distributors: [order.distributor])
}
let(:payment) {
create(:payment, order:, payment_method:)
}
let(:order) { create(:completed_order_with_totals) }
context "authorize payment email" do
subject(:email) { described_class.authorize_payment(payment) }
it "includes the distributor's name in the subject" do
expect(email.subject).to include("authorize your payment to #{order.distributor.name}")
end
it "sets a reply-to of the customer email" do
expect(email.reply_to).to eq([order.distributor.contact.email])
end
it "includes a link to authorize the payment" do
link = "http://test.host/payments/#{payment.id}/authorize"
expect(email.text_part.body).to match link
expect(html_body(email)).to have_link link, href: link
end
end
context "authorization required email" do
subject(:email) { described_class.authorization_required(payment) }
it "includes the distributor's name in the subject" do
expect(email.subject).to include("A payment requires authorization from the customer")
end
it "sets a reply-to of the customer email" do
expect(email.reply_to).to eq([order.email])
end
end
end
end

View File

@@ -83,13 +83,13 @@ RSpec.describe ProducerMailer, type: :mailer do
expect(line).to include 'QTY: 3'
expect(line).to include '@ $10.00 = $30.00'
end
expect(body_as_html(mail).find("table.order-summary tr", text: p1.name))
expect(html_body(mail).find("table.order-summary tr", text: p1.name))
.to have_selector("td", text: "$30.00")
end
it "displays tax totals for each product" do
# Tax for p1 line items
expect(body_as_html(mail).find("table.order-summary tr", text: p1.name))
expect(html_body(mail).find("table.order-summary tr", text: p1.name))
.to have_selector("td.tax", text: "$2.73")
expect(
product_line_from_order_summary_text(mail, p1.name)
@@ -122,7 +122,7 @@ RSpec.describe ProducerMailer, type: :mailer do
it "includes the total" do
expect(mail.body.encoded).to include 'Total: $50.00'
expect(body_as_html(mail).find("tr.total-row"))
expect(html_body(mail).find("tr.total-row"))
.to have_selector("td", text: "$50.00")
end
@@ -151,15 +151,15 @@ RSpec.describe ProducerMailer, type: :mailer do
end
it "adds customer names table" do
expect(body_as_html(mail).find(".order-summary.customer-order")).not_to be_nil
expect(html_body(mail).find(".order-summary.customer-order")).not_to be_nil
expect(customer_details_summary_text(mail)).to be_present
end
it "displays last name for each order" do
product_name = order.line_items.first.product.name
last_name = order.billing_address.lastname
expect(body_as_html(mail).find("table.order-summary.customer-order tr",
text: product_name)).to have_selector("td", text: last_name)
expect(html_body(mail).find("table.order-summary.customer-order tr",
text: product_name)).to have_selector("td", text: last_name)
expect(
product_line_from_details_summary_text(mail, product_name)
).to include(last_name)
@@ -168,8 +168,8 @@ RSpec.describe ProducerMailer, type: :mailer do
it "displays first name for each order" do
product_name = order.line_items.first.product.name
first_name = order.billing_address.firstname
expect(body_as_html(mail).find("table.order-summary.customer-order tr",
text: product_name)).to have_selector("td", text: first_name)
expect(html_body(mail).find("table.order-summary.customer-order tr",
text: product_name)).to have_selector("td", text: first_name)
expect(
product_line_from_details_summary_text(mail, product_name)
).to include(first_name)
@@ -186,7 +186,7 @@ RSpec.describe ProducerMailer, type: :mailer do
context "validate business name" do
let(:table_header) do
body_as_html(mail).find("table.order-summary.customer-order thead")
html_body(mail).find("table.order-summary.customer-order thead")
end
context "when no customer has customer code" do
@@ -202,7 +202,7 @@ RSpec.describe ProducerMailer, type: :mailer do
it 'displays business name for the customer' do
expect(table_header).to have_selector("th", text: 'Business Name')
expect(
body_as_html(mail).find("table.order-summary.customer-order tbody tr")
html_body(mail).find("table.order-summary.customer-order tbody tr")
).to have_selector("td", text: 'Test Business Name')
expect(customer_details_summary_text(mail)).to include('Test Business Name')
end
@@ -217,7 +217,7 @@ RSpec.describe ProducerMailer, type: :mailer do
it "does not add customer names table" do
expect {
body_as_html(mail).find(".order-summary.customer-order")
html_body(mail).find(".order-summary.customer-order")
}.to raise_error(Capybara::ElementNotFound)
expect(customer_details_summary_text(mail)).to be_nil
end
@@ -236,7 +236,7 @@ RSpec.describe ProducerMailer, type: :mailer do
end
it "displays a supplier column" do
expect(body_as_html(mail).find(".order-summary"))
expect(html_body(mail).find(".order-summary"))
.to have_selector("th", text: "Supplier")
end
@@ -244,7 +244,7 @@ RSpec.describe ProducerMailer, type: :mailer do
before { order_cycle.coordinator.update!(show_customer_names_to_suppliers: true) }
it "displays a supplier column in the summary of orders grouped by customer" do
expect(body_as_html(mail).find(".customer-order"))
expect(html_body(mail).find(".customer-order"))
.to have_selector("th", text: "Supplier")
end
end
@@ -252,7 +252,7 @@ RSpec.describe ProducerMailer, type: :mailer do
context "products from only one supplier" do
it "doesn't display a supplier column" do
expect(body_as_html(mail).find(".order-summary"))
expect(html_body(mail).find(".order-summary"))
.not_to have_selector("th", text: "Supplier")
end
@@ -260,7 +260,7 @@ RSpec.describe ProducerMailer, type: :mailer do
before { order_cycle.coordinator.update!(show_customer_names_to_suppliers: true) }
it "doesn't display a supplier column in the summary of orders grouped by customer" do
expect(body_as_html(mail).find(".customer-order"))
expect(html_body(mail).find(".customer-order"))
.not_to have_selector("th", text: "Supplier")
end
end
@@ -272,10 +272,6 @@ RSpec.describe ProducerMailer, type: :mailer do
mail.body.to_s.lines.select { |line| line.include? str }
end
def body_as_html(mail)
Capybara.string(mail.html_part.body.encoded)
end
def body_as_text(mail)
mail.text_part.body.decoded
end

View File

@@ -55,4 +55,14 @@ RSpec.describe Spree::ShipmentMailer do
shipment_email = Spree::ShipmentMailer.shipped_email(shipment, delivery: false)
expect(shipment_email.subject).to include("#{distributor.name} Pick up Notification")
end
it "picked_up email has as the reply to email as the distributor" do
shipment_email = Spree::ShipmentMailer.shipped_email(shipment, delivery: false)
expect(shipment_email.reply_to).to eq([distributor.contact.email])
end
it "shipment_email email has as the reply to email as the distributor" do
shipment_email = Spree::ShipmentMailer.shipped_email(shipment, delivery: true)
expect(shipment_email.reply_to).to eq([distributor.contact.email])
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
module MailersHelper
def html_body(mail)
Capybara.string(mail.html_part.body.to_s)
end
end