fix: make sure smtp can be set either via env or ui
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Simon 2026-05-07 10:01:19 +02:00
parent 2bb01bd201
commit 01acea6838
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
9 changed files with 238 additions and 42 deletions

View file

@ -23,7 +23,8 @@ defmodule Mv.ConfigSmtpTest do
end
describe "smtp_port/0" do
test "returns parsed integer when SMTP_PORT ENV is set" do
test "returns parsed integer when SMTP_PORT ENV is set in ENV-only mode" do
set_smtp_env("SMTP_HOST", "smtp.example.com")
set_smtp_env("SMTP_PORT", "587")
assert Mv.Config.smtp_port() == 587
after
@ -52,13 +53,21 @@ defmodule Mv.ConfigSmtpTest do
end
describe "smtp_env_configured?/0" do
test "returns true when any SMTP ENV variable is set" do
test "returns true when SMTP_HOST is set" do
set_smtp_env("SMTP_HOST", "smtp.example.com")
assert Mv.Config.smtp_env_configured?() == true
after
clear_smtp_env()
end
test "returns false when SMTP_HOST is not set even if other SMTP ENV variables are set" do
set_smtp_env("SMTP_USERNAME", "user@example.com")
set_smtp_env("SMTP_PASSWORD", "secret")
refute Mv.Config.smtp_env_configured?()
after
clear_smtp_env()
end
test "returns false when no SMTP ENV variables are set" do
clear_smtp_env()
refute Mv.Config.smtp_env_configured?()
@ -66,15 +75,17 @@ defmodule Mv.ConfigSmtpTest do
end
describe "smtp_password/0 and SMTP_PASSWORD_FILE" do
test "returns value from SMTP_PASSWORD when set" do
test "returns value from SMTP_PASSWORD when set in ENV-only mode" do
set_smtp_env("SMTP_HOST", "smtp.example.com")
set_smtp_env("SMTP_PASSWORD", "env-secret")
assert Mv.Config.smtp_password() == "env-secret"
after
clear_smtp_env()
end
test "returns content of file when SMTP_PASSWORD_FILE is set and SMTP_PASSWORD is not" do
test "returns content of file when SMTP_PASSWORD_FILE is set in ENV-only mode and SMTP_PASSWORD is not" do
clear_smtp_env()
set_smtp_env("SMTP_HOST", "smtp.example.com")
path = Path.join(System.tmp_dir!(), "mv_smtp_test_#{System.unique_integer([:positive])}")
File.write!(path, "file-secret\n")
Process.put(:smtp_password_file_path, path)
@ -85,7 +96,8 @@ defmodule Mv.ConfigSmtpTest do
if path = Process.get(:smtp_password_file_path), do: File.rm(path)
end
test "SMTP_PASSWORD overrides SMTP_PASSWORD_FILE when both are set" do
test "SMTP_PASSWORD overrides SMTP_PASSWORD_FILE in ENV-only mode when both are set" do
set_smtp_env("SMTP_HOST", "smtp.example.com")
path = Path.join(System.tmp_dir!(), "mv_smtp_test_#{System.unique_integer([:positive])}")
File.write!(path, "file-secret")
Process.put(:smtp_password_file_path, path)

View file

@ -124,6 +124,71 @@ defmodule MvWeb.GlobalSettingsLiveTest do
{:ok, _view, html} = live(conn, ~p"/settings")
assert html =~ "SMTP" or html =~ "E-Mail" or html =~ "Settings"
end
@tag :ui
test "disables all SMTP inputs when SMTP_HOST is set", %{conn: conn} do
clear_smtp_env()
System.put_env("SMTP_HOST", "smtp.env-only.example")
on_exit(fn -> clear_smtp_env() end)
{:ok, view, _html} = live(conn, ~p"/settings")
assert has_element?(view, "#setting_smtp_host[disabled]")
assert has_element?(view, "#setting_smtp_port[disabled]")
assert has_element?(view, "#setting_smtp_ssl[disabled]")
assert has_element?(view, "#setting_smtp_username[disabled]")
assert has_element?(view, "#setting_smtp_password[disabled]")
assert has_element?(view, "#setting_smtp_from_email[disabled]")
assert has_element?(view, "#setting_smtp_from_name[disabled]")
end
@tag :ui
test "does not render SMTP save action when SMTP_HOST is set", %{conn: conn} do
clear_smtp_env()
System.put_env("SMTP_HOST", "smtp.env-only.example")
on_exit(fn -> clear_smtp_env() end)
{:ok, view, _html} = live(conn, ~p"/settings")
refute has_element?(view, "#smtp-form button", "Save SMTP Settings")
end
@tag :ui
test "shows explicit ENV-only mode hint when SMTP_HOST is set", %{conn: conn} do
clear_smtp_env()
System.put_env("SMTP_HOST", "smtp.env-only.example")
on_exit(fn -> clear_smtp_env() end)
{:ok, _view, html} = live(conn, ~p"/settings")
assert html =~ "SMTP is fully managed via environment variables"
end
@tag :ui
test "shows warning block for missing required SMTP ENV values in ENV-only mode", %{
conn: conn
} do
clear_smtp_env()
System.put_env("SMTP_HOST", "smtp.env-only.example")
on_exit(fn -> clear_smtp_env() end)
{:ok, _view, html} = live(conn, ~p"/settings")
assert html =~ "SMTP environment configuration appears incomplete"
assert html =~ "SMTP_USERNAME"
assert html =~ "SMTP_PASSWORD/SMTP_PASSWORD_FILE"
end
@tag :ui
test "does not enter ENV-only mode when SMTP_HOST is not set", %{conn: conn} do
clear_smtp_env()
System.put_env("SMTP_USERNAME", "leftover@example.com")
on_exit(fn -> clear_smtp_env() end)
{:ok, view, html} = live(conn, ~p"/settings")
refute html =~ "SMTP is fully managed via environment variables"
refute html =~ "SMTP environment configuration appears incomplete"
refute has_element?(view, "#setting_smtp_host[disabled]")
refute has_element?(view, "#setting_smtp_username[disabled]")
end
end
describe "Authentication section when OIDC-only is enabled" do