From 03614325d1fb4c55241bd9612d77037bbfed13eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 10 Nov 2022 00:19:02 +0100 Subject: [PATCH] Use current host as default email address domain We were using `consul.dev` as default, which might be fine for the development environment, but it's something that definitely needs to be changed on production. Now we're providing a better default setting: using the same domain which is used to generate URLs in emails. This also makes it easier to configure new tenants, since the setting will default to whatever host the new tenant is using. The `|| "consul.dev"` part might not be needed; I'm keeping it because I'm not sure production environents would not experience any new issues if we don't add it, but we might remove it in the future. --- app/models/setting.rb | 6 +++++- db/dev_seeds/settings.rb | 1 - spec/models/setting_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index 58cb8bb06..36df94788 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -167,7 +167,7 @@ class Setting < ApplicationRecord "proposal_notification_minimum_interval_in_days": 3, "direct_message_max_per_day": 3, "mailer_from_name": "CONSUL", - "mailer_from_address": "noreply@consul.dev", + "mailer_from_address": default_mailer_from_address, "min_age_to_participate": 16, "hot_score_period_in_days": 31, "related_content_score_threshold": -0.3, @@ -199,6 +199,10 @@ class Setting < ApplicationRecord } end + def default_mailer_from_address + "noreply@#{Tenant.current_host.presence || "consul.dev"}" + end + def reset_defaults defaults.each { |name, value| self[name] = value } end diff --git a/db/dev_seeds/settings.rb b/db/dev_seeds/settings.rb index 280e56c63..81b61b83b 100644 --- a/db/dev_seeds/settings.rb +++ b/db/dev_seeds/settings.rb @@ -6,7 +6,6 @@ section "Creating Settings" do "feature.featured_proposals": "true", "feature.map": "true", "instagram_handle": "CONSUL", - "mailer_from_address": "noreply@consul.dev", "mailer_from_name": "CONSUL", "meta_description": "Citizen participation tool for an open, "\ "transparent and democratic government", diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index 8a5dc95b2..0fa294635 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -187,6 +187,28 @@ describe Setting do end end + describe ".default_mailer_from_address" do + before { allow(Tenant).to receive(:default_host).and_return("consulproject.org") } + + it "uses the default host for the default tenant" do + expect(Setting.default_mailer_from_address).to eq "noreply@consulproject.org" + end + + it "uses the tenant host for other tenants" do + allow(Tenant).to receive(:current_schema).and_return("new") + + expect(Setting.default_mailer_from_address).to eq "noreply@new.consulproject.org" + end + + context "empty default host" do + before { allow(Tenant).to receive(:default_host).and_return("") } + + it "uses consul.dev as host" do + expect(Setting.default_mailer_from_address).to eq "noreply@consul.dev" + end + end + end + describe ".add_new_settings" do context "default settings with strings" do before do