[deploy] Fix email sender and url issues (#10883)

* Fix email sender and url issues

* Add proper tests

* Add tests

* Change DeviseMailer approach

* ZFix devise test
This commit is contained in:
Ben Halpern 2020-10-19 16:55:40 -04:00 committed by GitHub
parent 292b4143ce
commit b0be814300
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 4 deletions

View file

@ -7,6 +7,8 @@ class ApplicationMailer < ActionMailer::Base
helper ApplicationHelper
helper AuthenticationHelper
before_action :use_custom_host
default(
from: -> { email_from("Community") },
template_path: ->(mailer) { "mailers/#{mailer.class.name.underscore}" },
@ -23,4 +25,8 @@ class ApplicationMailer < ActionMailer::Base
expires_at: 31.days.from_now,
)
end
def use_custom_host
ActionMailer::Base.default_url_options[:host] = SiteConfig.app_domain
end
end

View file

@ -0,0 +1,8 @@
class DeviseMailer < Devise::Mailer
before_action :use_site_config_values
def use_site_config_values
Devise.mailer_sender = "#{SiteConfig.community_name} Community <#{SiteConfig.email_addresses[:default]}>"
ActionMailer::Base.default_url_options[:host] = SiteConfig.app_domain
end
end

View file

@ -30,6 +30,7 @@ Devise.setup do |config|
# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
config.mailer = "DeviseMailer"
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and

View file

@ -0,0 +1,22 @@
require "rails_helper"
RSpec.describe DeviseMailer, type: :mailer do
let(:user) { create(:user) }
describe "#reset_password_instructions" do
let(:email) { described_class.reset_password_instructions(user, "test") }
before do
allow(SiteConfig).to receive(:app_domain).and_return("funky-one-of-a-kind-domain-#{rand(100)}.com")
end
it "renders sender" do
expected_from = "#{SiteConfig.community_name} Community <#{SiteConfig.email_addresses[:default]}>"
expect(email["from"].value).to eq(expected_from)
end
it "renders proper URL" do
expect(email.to_s).to include(SiteConfig.app_domain)
end
end
end

View file

@ -196,14 +196,14 @@ RSpec.describe NotifyMailer, type: :mailer do
it "includes the listings URL" do
expect(email.html_part.body).to include(
CGI.escape(
Rails.application.routes.url_helpers.listings_url,
Rails.application.routes.url_helpers.listings_url(host: SiteConfig.app_domain),
),
)
end
it "includes the about listings URL" do
expect(email.html_part.body).to include(
CGI.escape(Rails.application.routes.url_helpers.about_listings_url),
CGI.escape(Rails.application.routes.url_helpers.about_listings_url(host: SiteConfig.app_domain)),
)
end
@ -234,12 +234,14 @@ RSpec.describe NotifyMailer, type: :mailer do
it "includes the listings URL" do
expect(email.text_part.body).to include(
Rails.application.routes.url_helpers.listings_url,
Rails.application.routes.url_helpers.listings_url(host: SiteConfig.app_domain),
)
end
it "includes the about listings URL" do
expect(email.text_part.body).to include(Rails.application.routes.url_helpers.about_listings_url)
expect(email.text_part.body).to include(
Rails.application.routes.url_helpers.about_listings_url(host: SiteConfig.app_domain),
)
end
it "includes the rewarding_context_message in the email" do