Skip to content

Commit e86f959

Browse files
Backport 'Backport smtp settings correction' to v0.22 (#6878)
1 parent 1f72f33 commit e86f959

12 files changed

Lines changed: 162 additions & 41 deletions

File tree

decidim-admin/config/locales/en.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ en:
3737
organization_url: Organization URL
3838
redirect_uri: Redirect URI
3939
organization:
40-
address: SMTP hostname
4140
alert_color: Alert
4241
badges_enabled: Enable badges
4342
cta_button_path: Call To Action button path
@@ -69,8 +68,6 @@ en:
6968
omnipresent_banner_short_description: Short description
7069
omnipresent_banner_title: Title
7170
omnipresent_banner_url: URL
72-
password: Password
73-
port: Port
7471
primary_color: Primary
7572
reference_prefix: Reference prefix
7673
rich_text_editor_in_public_views: Enable rich text editor for participants
@@ -82,7 +79,6 @@ en:
8279
tos_version: Terms of service version
8380
twitter_handler: Twitter handler
8481
user_groups_enabled: Enable groups
85-
user_name: Username
8682
warning_color: Warning
8783
welcome_notification_body: Welcome notification body
8884
welcome_notification_subject: Welcome notification subject

decidim-core/app/mailers/decidim/application_mailer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ def set_smtp
1717
return if @organization.nil? || @organization.smtp_settings.blank?
1818

1919
mail.from = @organization.smtp_settings["from"].presence || mail.from
20+
mail.reply_to = mail.from || Decidim.config.mailer_reply
2021
mail.delivery_method.settings.merge!(
21-
address: @organization.smtp_settings["address"],
22-
port: @organization.smtp_settings["port"],
23-
user_name: @organization.smtp_settings["user_name"],
24-
password: Decidim::AttributeEncryptor.decrypt(@organization.smtp_settings["encrypted_password"])
22+
address: @organization.smtp_settings[:address],
23+
port: @organization.smtp_settings[:port],
24+
user_name: @organization.smtp_settings[:user_name],
25+
password: Decidim::AttributeEncryptor.decrypt(@organization.smtp_settings[:encrypted_password])
2526
) { |_k, o, v| v.presence || o }.reject! { |_k, v| v.blank? }
2627
end
2728
end

decidim-core/lib/decidim/core/test/factories.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def generate_localized_title
9393
force_users_to_authenticate_before_access_organization { false }
9494
smtp_settings do
9595
{
96-
"from" => "test@example.org",
97-
"user_name" => "test",
98-
"encrypted_password" => Decidim::AttributeEncryptor.encrypt("demo"),
99-
"port" => "25",
100-
"address" => "smtp.example.org"
96+
from: "test@example.org",
97+
user_name: "test",
98+
encrypted_password: Decidim::AttributeEncryptor.encrypt("demo"),
99+
port: "25",
100+
address: "smtp.example.org"
101101
}
102102
end
103103

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "zip"
5+
6+
module Decidim
7+
describe Decidim::DummyResources::DummyResourceMailer, type: :mailer do
8+
describe "smtp_settings" do
9+
let(:user) { create(:user, organization: organization) }
10+
let(:organization) { create(:organization, smtp_settings: smtp_settings) }
11+
let(:smtp_settings) do
12+
{
13+
address: "mail.gotham.gov",
14+
port: "25",
15+
user_name: "f.laguardia",
16+
password: Decidim::AttributeEncryptor.encrypt("password"),
17+
from_email: "",
18+
from_label: "",
19+
from: from
20+
}
21+
end
22+
let(:mail) { described_class.fake_mail(user, organization) }
23+
let(:from) { "" }
24+
25+
context "when there is no organization at all" do
26+
let(:mail) { described_class.fake_mail(user, nil) }
27+
28+
it "returns values defined in Decidim.config" do
29+
expect(mail.from).to eq(["change-me@domain.org"])
30+
expect(mail.reply_to).to eq(nil)
31+
end
32+
end
33+
34+
context "when smtp_settings are not setted" do
35+
let(:smtp_settings) { nil }
36+
37+
it "returns values defined in Decidim.config" do
38+
expect(mail.from).to eq(["change-me@domain.org"])
39+
expect(mail.reply_to).to eq(nil)
40+
end
41+
end
42+
43+
context "when from label is not setted" do
44+
let(:from) { nil }
45+
46+
it "set default values for mail.from and mail.reply_to" do
47+
expect(mail.from).to eq(["change-me@domain.org"])
48+
expect(mail.reply_to).to eq(["change-me@domain.org"])
49+
end
50+
end
51+
52+
context "when from label is setted" do
53+
let(:from) { "Bruce Wayne <decide@gotham.org>" }
54+
55+
it "set default values for mail.from and mail.reply_to" do
56+
expect(mail.from).to eq(["decide@gotham.org"])
57+
expect(mail.reply_to).to eq(["decide@gotham.org"])
58+
end
59+
end
60+
end
61+
end
62+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module DummyResources
5+
class DummyResourceMailer < ApplicationMailer
6+
def fake_mail(user, organization)
7+
@user = user
8+
@organization = organization
9+
10+
mail(to: "#{user.name} <#{user.email}>") do |format|
11+
format.text { "This is the test" }
12+
format.html { "<p>This is a mail </p>" }
13+
end
14+
end
15+
end
16+
end
17+
end

decidim-system/app/forms/decidim/system/update_organization_form.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ def password
7272
end
7373

7474
def encrypted_smtp_settings
75-
smtp_settings["from"] = set_from
75+
smtp_settings[:from] = set_from
7676

7777
smtp_settings.merge(encrypted_password: Decidim::AttributeEncryptor.encrypt(@password))
7878
end
7979

8080
def set_from
81-
return smtp_settings["from_email"] if smtp_settings["from_label"].blank?
81+
return from_email if from_label.blank?
8282

83-
"#{smtp_settings["from_label"]} <#{smtp_settings["from_email"]}>"
83+
"#{from_label} <#{from_email}>"
8484
end
8585

8686
def encrypted_omniauth_settings

decidim-system/app/views/decidim/system/organizations/_smtp_settings.html.erb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<div class="fieldset">
22
<h4><%= t("decidim.system.models.organization.fields.smtp_settings") %></h4>
33
<%= f.fields_for :smtp_settings do %>
4-
<div class="field">
4+
<fieldset class="fieldset">
5+
<legend><%= t(".fieldsets.sender") %></legend>
56
<%= f.email_field :from_email, placeholder: t(".placeholder.from_email") %>
6-
</div>
7-
<div class="field">
8-
<%= f.email_field :from_label, placeholder: t(".placeholder.from_label") %>
9-
<p><%= t(".instructions.from_label") %></p>
10-
</div>
7+
<%= f.text_field :from_label, placeholder: t(".placeholder.from_label") %>
8+
<p class="help-text"><%= t(".instructions.from_label") %></p>
9+
</fieldset>
1110
<div class="field">
1211
<%= f.text_field :user_name %>
1312
</div>

decidim-system/config/locales/en.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
---
22
en:
3+
activemodel:
4+
attributes:
5+
organization:
6+
address: SMTP hostname
7+
from_email: Email address
8+
from_label: Label
9+
password: Password
10+
port: Port
11+
user_name: Username
312
decidim:
413
system:
514
actions:
@@ -82,8 +91,10 @@ en:
8291
api_key: API key
8392
api_secret: API secret
8493
smtp_settings:
94+
fieldsets:
95+
sender: Sender
8596
instructions:
86-
from_label: 'Email sender will be: "your-organization-name <your-organization@your-provider.org>". Leave blank to use the ''from_email'' as label'
97+
from_label: 'Email sender will be: "your-organization-name <your-organization@your-provider.org>". Leave blank to use the ''Email address'' as label'
8798
placeholder:
8899
from_email: your-organization@your-provider.org
89100
from_label: your-organization-name
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class UpdateSmtpFromLabel < ActiveRecord::Migration[5.2]
4+
def change
5+
Decidim::Organization.all.each do |org|
6+
if org.smtp_settings[:from_email].blank?
7+
org.smtp_settings[:from_email] = org.smtp_settings[:from]
8+
org.save!
9+
end
10+
end
11+
end
12+
end

decidim-system/spec/commands/decidim/system/register_organization_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ module System
2727
users_registration_mode: "enabled",
2828
force_users_to_authenticate_before_access_organization: "false",
2929
smtp_settings: {
30-
"address" => "mail.gotham.gov",
31-
"port" => "25",
32-
"user_name" => "f.laguardia",
33-
"password" => Decidim::AttributeEncryptor.encrypt("password"),
34-
"from_email" => "decide@gotham.gov",
35-
"from_label" => from_label
30+
address: "mail.gotham.gov",
31+
port: "25",
32+
user_name: "f.laguardia",
33+
password: Decidim::AttributeEncryptor.encrypt("password"),
34+
from_email: "decide@gotham.gov",
35+
from_label: from_label
3636
},
3737
omniauth_settings_facebook_enabled: true,
3838
omniauth_settings_facebook_app_id: "facebook-app-id",
@@ -52,6 +52,7 @@ module System
5252
expect(organization.host).to eq("decide.gotham.gov")
5353
expect(organization.secondary_hosts).to match_array(["foo.gotham.gov", "bar.gotham.gov"])
5454
expect(organization.smtp_settings["from"]).to eq("Decide Gotham <decide@gotham.gov>")
55+
expect(organization.smtp_settings["from_email"]).to eq("decide@gotham.gov")
5556
expect(organization.omniauth_settings["omniauth_settings_facebook_enabled"]).to eq(true)
5657
expect(
5758
Decidim::AttributeEncryptor.decrypt(organization.omniauth_settings["omniauth_settings_facebook_app_id"])
@@ -109,6 +110,7 @@ module System
109110
organization = Organization.last
110111

111112
expect(organization.smtp_settings["from"]).to eq("decide@gotham.gov")
113+
expect(organization.smtp_settings["from_email"]).to eq("decide@gotham.gov")
112114
end
113115
end
114116
end

0 commit comments

Comments
 (0)