63 lines
2.2 KiB
Elixir
63 lines
2.2 KiB
Elixir
defmodule Mv.Membership.SettingSmtpTest do
|
|
@moduledoc """
|
|
Unit tests for Setting resource SMTP attributes.
|
|
|
|
TDD: tests expect smtp_host, smtp_port, smtp_username, smtp_password, smtp_ssl
|
|
to be accepted on update and persisted. Password must not be exposed in plaintext
|
|
when reading settings (sensitive). Tests will fail until Setting has these attributes.
|
|
"""
|
|
use Mv.DataCase, async: false
|
|
|
|
alias Mv.Helpers.SystemActor
|
|
alias Mv.Membership
|
|
|
|
setup do
|
|
{:ok, settings} = Membership.get_settings()
|
|
# Save current SMTP values to restore in on_exit (when attributes exist)
|
|
saved = %{
|
|
smtp_host: Map.get(settings, :smtp_host),
|
|
smtp_port: Map.get(settings, :smtp_port),
|
|
smtp_username: Map.get(settings, :smtp_username),
|
|
smtp_ssl: Map.get(settings, :smtp_ssl)
|
|
}
|
|
|
|
on_exit(fn ->
|
|
{:ok, s} = Membership.get_settings()
|
|
attrs = Enum.reject(saved, fn {_k, v} -> is_nil(v) end) |> Map.new()
|
|
if attrs != %{}, do: Membership.update_settings(s, attrs)
|
|
end)
|
|
|
|
{:ok, settings: settings, saved: saved}
|
|
end
|
|
|
|
describe "SMTP attributes update and persistence" do
|
|
test "update_settings accepts smtp_host, smtp_port, smtp_username, smtp_ssl and persists", %{
|
|
settings: settings
|
|
} do
|
|
attrs = %{
|
|
smtp_host: "smtp.example.com",
|
|
smtp_port: 587,
|
|
smtp_username: "user",
|
|
smtp_ssl: "tls"
|
|
}
|
|
|
|
assert {:ok, updated} = Membership.update_settings(settings, attrs)
|
|
assert updated.smtp_host == "smtp.example.com"
|
|
assert updated.smtp_port == 587
|
|
assert updated.smtp_username == "user"
|
|
assert updated.smtp_ssl == "tls"
|
|
end
|
|
|
|
test "smtp_password can be set and is not exposed in plaintext when reading settings", %{
|
|
settings: settings
|
|
} do
|
|
secret = "sensitive-password-#{System.unique_integer([:positive])}"
|
|
assert {:ok, _} = Membership.update_settings(settings, %{smtp_password: secret})
|
|
|
|
{:ok, read_back} = Membership.get_settings()
|
|
# Sensitive: raw password must not be returned (e.g. nil or redacted)
|
|
refute read_back.smtp_password == secret,
|
|
"smtp_password must not be returned in plaintext when reading settings"
|
|
end
|
|
end
|
|
end
|