Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

add tests for base_email template

Robertd6125ffeb1d0
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 22fdc66952..382eb47bc4 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -320,9 +320,9 @@ defmodule PlausibleWeb.Email do
})
end
- defp base_email(), do: base_email(%{layout: "base_email.html"})
+ def base_email(), do: base_email(%{layout: "base_email.html"})
- defp base_email(%{layout: layout}) do
+ def base_email(%{layout: layout}) do
mailer_from = Application.get_env(:plausible, :mailer_email)
new_email()
diff --git a/test/plausible_web/email_test.exs b/test/plausible_web/email_test.exs
new file mode 100644
index 0000000000..578d8c3865
--- /dev/null
+++ b/test/plausible_web/email_test.exs
@@ -0,0 +1,68 @@
+defmodule PlausibleWeb.EmailTest do
+ alias PlausibleWeb.Email
+ use ExUnit.Case, async: true
+ import Plausible.Factory
+
+ describe "base_email layout" do
+ test "greets user by first name if user in template assigns" do
+ email =
+ Email.base_email()
+ |> Email.render("activation_email.html", %{
+ user: build(:user, name: "John Doe"),
+ code: "123"
+ })
+
+ assert email.html_body =~ "Hey John,"
+ end
+
+ test "greets impersonally when user not in template assigns" do
+ email =
+ Email.base_email()
+ |> Email.render("password_reset_email.html", %{
+ reset_link: "imaginary"
+ })
+
+ assert email.html_body =~ "Hey,"
+ end
+
+ test "renders plausible link" do
+ email =
+ Email.base_email()
+ |> Email.render("cancellation_email.html")
+
+ assert email.html_body =~ plausible_link()
+ end
+
+ test "does not render unsubscribe placeholder by default" do
+ email =
+ Email.base_email()
+ |> Email.render("welcome_email.html")
+
+ refute email.html_body =~ "{{{ pm:unsubscribe }}}"
+ end
+
+ test "renders unsubscribe placeholder if told" do
+ email =
+ Email.base_email()
+ |> Email.render("welcome_email.html", %{unsubscribe: true})
+
+ assert email.html_body =~ "{{{ pm:unsubscribe }}}"
+ end
+
+ test "can be disabled with a nil layout" do
+ email =
+ Email.base_email(%{layout: nil})
+ |> Email.render("welcome_email.html", %{
+ user: build(:user, name: "John Doe")
+ })
+
+ refute email.html_body =~ "Hey John,"
+ refute email.html_body =~ plausible_link()
+ end
+ end
+
+ def plausible_link() do
+ plausible_url = PlausibleWeb.EmailView.plausible_url()
+ "<a href=\"#{plausible_url}\">#{plausible_url}</a>"
+ end
+end