From ce7401dd837e1f1638bda04846951b39edb5b908 Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Thu, 25 May 2023 15:34:39 +0800 Subject: [PATCH] add MAILER_NAME (#2937) * add MAILER_NAME * add mailer test --------- Co-authored-by: Uku Taht --- CHANGELOG.md | 1 + config/runtime.exs | 8 ++++++++ test/plausible/config_test.exs | 22 ++++++++++++++++++++++ test/plausible/mailer_test.exs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 test/plausible/mailer_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index b293d0c13..1451c7ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. - 'Last updated X seconds ago' info to 'current visitors' tooltips - Add support for more Bamboo adapters, i.e. `Bamboo.MailgunAdapter`, `Bamboo.MandrillAdapter`, `Bamboo.SendGridAdapter` plausible/analytics#2649 - Ability to change domain for existing site (requires numeric IDs data migration, instructions will be provided separately) UI + API (`PUT /api/v1/sites`) +- Add `MAILER_NAME` environment variable support plausible/analytics#2937 - Add `MAILGUN_BASE_URI` support for `Bamboo.MailgunAdapter` plausible/analytics#2935 ### Fixed diff --git a/config/runtime.exs b/config/runtime.exs index 46ad8e0b4..315a9040a 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -72,6 +72,14 @@ super_admin_user_ids = env = get_var_from_path_or_env(config_dir, "ENVIRONMENT", "prod") mailer_adapter = get_var_from_path_or_env(config_dir, "MAILER_ADAPTER", "Bamboo.SMTPAdapter") mailer_email = get_var_from_path_or_env(config_dir, "MAILER_EMAIL", "hello@plausible.local") + +mailer_email = + if mailer_name = get_var_from_path_or_env(config_dir, "MAILER_NAME") do + {mailer_name, mailer_email} + else + mailer_email + end + app_version = get_var_from_path_or_env(config_dir, "APP_VERSION", "0.0.1") ch_db_url = diff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exs index 5071b7cb2..62ffe8702 100644 --- a/test/plausible/config_test.exs +++ b/test/plausible/config_test.exs @@ -2,6 +2,28 @@ defmodule Plausible.ConfigTest do use ExUnit.Case describe "mailer" do + test "mailer email default" do + env = [{"MAILER_EMAIL", nil}] + assert get_in(runtime_config(env), [:plausible, :mailer_email]) == "hello@plausible.local" + end + + test "mailer email custom" do + env = [{"MAILER_EMAIL", "custom@mailer.email"}] + assert get_in(runtime_config(env), [:plausible, :mailer_email]) == "custom@mailer.email" + end + + test "mailer name" do + env = [{"MAILER_EMAIL", nil}, {"MAILER_NAME", "John"}] + + assert get_in(runtime_config(env), [:plausible, :mailer_email]) == + {"John", "hello@plausible.local"} + + env = [{"MAILER_EMAIL", "custom@mailer.email"}, {"MAILER_NAME", "John"}] + + assert get_in(runtime_config(env), [:plausible, :mailer_email]) == + {"John", "custom@mailer.email"} + end + test "defaults to Bamboo.SMTPAdapter" do env = {"MAILER_ADAPTER", nil} diff --git a/test/plausible/mailer_test.exs b/test/plausible/mailer_test.exs new file mode 100644 index 000000000..48cd2f76d --- /dev/null +++ b/test/plausible/mailer_test.exs @@ -0,0 +1,33 @@ +defmodule Plausible.MailerTest do + use Plausible.DataCase + use Bamboo.Test + + describe "from" do + setup do + {:ok, user: insert(:user)} + end + + # see config tests as well + test "when MAILER_NAME and MAILER_EMAIL", %{user: user} do + mailer_email = {"John", "custom@mailer.email"} + patch_env(:mailer_email, mailer_email) + + email = PlausibleWeb.Email.welcome_email(user) + assert :ok = Plausible.Mailer.send(email) + + assert_delivered_email(email) + assert email.from == mailer_email + end + + test "when MAILER_EMAIL", %{user: user} do + mailer_email = "custom@mailer.email" + patch_env(:mailer_email, mailer_email) + + email = PlausibleWeb.Email.welcome_email(user) + assert :ok = Plausible.Mailer.send(email) + + assert_delivered_email(email) + assert email.from == mailer_email + end + end +end