File size: 14,877 Bytes
6778ee0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | defmodule PlausibleWeb.EmailTest do
use Plausible.DataCase, async: true
import Plausible.Factory
alias PlausibleWeb.Email
describe "base_email layout" do
test "greets user by first name if user in template assigns" do
email =
Email.base_email()
|> Email.render("welcome_email.html", %{
user: build(:user, name: "John Doe"),
code: "123"
})
assert email.html_body =~ "Hey John,"
assert email.text_body =~ "Hey John,"
end
test "greets impersonally when user not in template assigns" do
email =
Email.base_email()
|> Email.render("welcome_email.html")
assert email.html_body =~ "Hey,"
assert email.text_body =~ "Hey,"
end
test "renders plausible link" do
email =
Email.base_email()
|> Email.render("welcome_email.html")
assert email.html_body =~ plausible_link()
assert email.text_body =~ plausible_url()
end
@tag :ee_only
test "renders unsubscribe placeholder" do
email =
Email.base_email()
|> Email.render("welcome_email.html")
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.text_body =~ "Hey John,"
end
end
describe "priority email layout" do
@tag :ee_only
test "uses the `priority` message stream in Postmark in EE" do
email =
Email.priority_email()
|> Email.render("activation_email.html", %{
user: build(:user, name: "John Doe"),
code: "123"
})
assert %{"MessageStream" => "priority"} = email.private[:message_params]
end
@tag :ce_build_only
test "doesn't use the `priority` message stream in Postmark in CE" do
email =
Email.priority_email()
|> Email.render("activation_email.html", %{
user: build(:user, name: "John Doe"),
code: "123"
})
refute email.private[:message_params]["MessageStream"]
end
test "greets user by first name if user in template assigns" do
email =
Email.priority_email()
|> Email.render("activation_email.html", %{
user: build(:user, name: "John Doe"),
code: "123"
})
assert email.html_body =~ "Hey John,"
assert email.text_body =~ "Hey John,"
end
test "greets impersonally when user not in template assigns" do
email =
Email.priority_email()
|> Email.render("password_reset_email.html", %{
reset_link: "imaginary"
})
assert email.html_body =~ "Hey,"
assert email.text_body =~ "Hey,"
end
test "renders plausible link" do
email =
Email.priority_email()
|> Email.render("password_reset_email.html", %{
reset_link: "imaginary"
})
assert email.html_body =~ plausible_link()
assert email.text_body =~ plausible_url()
end
test "does not render unsubscribe placeholder" do
email =
Email.priority_email()
|> Email.render("password_reset_email.html", %{
reset_link: "imaginary"
})
refute email.html_body =~ "{{{ pm:unsubscribe }}}"
end
test "can be disabled with a nil layout" do
email =
Email.priority_email(%{layout: nil})
|> Email.render("password_reset_email.html", %{
reset_link: "imaginary"
})
refute email.html_body =~ "Hey John,"
refute email.html_body =~ plausible_link()
refute email.text_body =~ "Hey John,"
refute email.text_body =~ plausible_url()
end
end
describe "over_limit_email/3" do
test "renders usage, suggested plan, and links to upgrade and account settings" do
user = build(:user)
team = build(:team, identifier: Ecto.UUID.generate())
penultimate_cycle = Date.range(~D[2023-03-01], ~D[2023-03-31])
last_cycle = Date.range(~D[2023-04-01], ~D[2023-04-30])
usage = %{
penultimate_cycle: %{date_range: penultimate_cycle, total: 12_300},
last_cycle: %{date_range: last_cycle, total: 32_100}
}
%{html_body: html_body, subject: subject} =
PlausibleWeb.Email.over_limit_email(user, team, usage, "100k")
assert subject == "[Action required] You have outgrown your Plausible subscription tier"
assert html_body =~ PlausibleWeb.TextHelpers.format_date_range(last_cycle)
assert html_body =~ "We recommend you upgrade to the 100k pageviews/month plan"
assert html_body =~ "your account recorded 32,100 billable pageviews"
assert html_body =~
"cycle before that (#{PlausibleWeb.TextHelpers.format_date_range(penultimate_cycle)}), your account used 12,300 billable pageviews"
assert text_of_element(
html_body,
~s|a[href$="/billing/choose-plan?__team=#{team.identifier}"]|
) ==
"Click here to upgrade your subscription"
assert text_of_element(
html_body,
~s|a[href$="/settings/billing/subscription?__team=#{team.identifier}"]|
) ==
"account settings"
assert html_body =~
PlausibleWeb.Router.Helpers.billing_url(PlausibleWeb.Endpoint, :choose_plan) <>
"?__team=#{team.identifier}"
end
test "asks enterprise level usage to contact us" do
user = build(:user)
team = build(:team, identifier: Ecto.UUID.generate())
penultimate_cycle = Date.range(~D[2023-03-01], ~D[2023-03-31])
last_cycle = Date.range(~D[2023-04-01], ~D[2023-04-30])
suggested_plan = :enterprise
usage = %{
penultimate_cycle: %{date_range: penultimate_cycle, total: 12_300},
last_cycle: %{date_range: last_cycle, total: 32_100}
}
%{html_body: html_body} =
PlausibleWeb.Email.over_limit_email(user, team, usage, suggested_plan)
refute html_body =~ "Click here to upgrade your subscription"
assert html_body =~ "Your usage exceeds our standard plans, so please reply back"
end
end
describe "dashboard_locked/3" do
test "renders usage, suggested plan, and links to upgrade and account settings" do
user = build(:user)
team = build(:team, identifier: Ecto.UUID.generate())
penultimate_cycle = Date.range(~D[2023-03-01], ~D[2023-03-31])
last_cycle = Date.range(~D[2023-04-01], ~D[2023-04-30])
usage = %{
penultimate_cycle: %{date_range: penultimate_cycle, total: 12_300},
last_cycle: %{date_range: last_cycle, total: 32_100}
}
%{html_body: html_body, subject: subject} =
PlausibleWeb.Email.dashboard_locked(user, team, usage, "100k")
assert subject == "[Action required] Your Plausible dashboard is now locked"
assert html_body =~ PlausibleWeb.TextHelpers.format_date_range(last_cycle)
assert html_body =~ "We recommend you upgrade to the 100k pageviews/month plan"
assert html_body =~ "your account recorded 32,100 billable pageviews"
assert html_body =~
"cycle before that (#{PlausibleWeb.TextHelpers.format_date_range(penultimate_cycle)}), the usage was 12,300 billable pageviews"
assert text_of_element(
html_body,
~s|a[href$="/billing/choose-plan?__team=#{team.identifier}"]|
) ==
"Click here to upgrade your subscription"
assert text_of_element(
html_body,
~s|a[href$="/settings/billing/subscription?__team=#{team.identifier}"]|
) ==
"account settings"
assert html_body =~
PlausibleWeb.Router.Helpers.billing_url(PlausibleWeb.Endpoint, :choose_plan) <>
"?__team=#{team.identifier}"
end
test "asks enterprise level usage to contact us" do
user = build(:user)
team = build(:team, identifier: Ecto.UUID.generate())
penultimate_cycle = Date.range(~D[2023-03-01], ~D[2023-03-31])
last_cycle = Date.range(~D[2023-04-01], ~D[2023-04-30])
suggested_plan = :enterprise
usage = %{
penultimate_cycle: %{date_range: penultimate_cycle, total: 12_300},
last_cycle: %{date_range: last_cycle, total: 32_100}
}
%{html_body: html_body} =
PlausibleWeb.Email.dashboard_locked(user, team, usage, suggested_plan)
refute html_body =~ "Click here to upgrade your subscription"
assert html_body =~ "Your usage exceeds our standard plans, so please reply back"
end
end
describe "enterprise_over_limit_internal_email/4" do
test "renders pageview usage by billing cycles + sites usage/limit" do
user = build(:user)
penultimate_cycle = Date.range(~D[2023-03-01], ~D[2023-03-31])
last_cycle = Date.range(~D[2023-04-01], ~D[2023-04-30])
pageview_usage = %{
penultimate_cycle: %{date_range: penultimate_cycle, total: 100_141_888},
last_cycle: %{date_range: last_cycle, total: 100_222_999}
}
%{html_body: html_body, subject: subject} =
PlausibleWeb.Email.enterprise_over_limit_internal_email(user, pageview_usage, 80, 50)
assert subject == "#{user.email} has outgrown their enterprise plan"
assert html_body =~
"Last billing cycle: #{PlausibleWeb.TextHelpers.format_date_range(last_cycle)}"
assert html_body =~ "Last cycle pageview usage: 100,222,999 billable pageviews"
assert html_body =~
"Penultimate billing cycle: #{PlausibleWeb.TextHelpers.format_date_range(penultimate_cycle)}"
assert html_body =~ "Penultimate cycle pageview usage: 100,141,888 billable pageviews"
assert html_body =~ "Site usage: 80 / 50 allowed sites"
end
end
describe "approaching accept_traffic_until" do
test "renders first warning" do
user = build(:user, id: 123, name: "John Doe")
team = build(:team, identifier: Ecto.UUID.generate())
notification = %{
id: user.id,
email: user.email,
deadline: Date.add(Date.utc_today(), 7),
site_ids: [1, 2, 3],
name: user.name,
team: team
}
%{html_body: body, subject: subject} =
PlausibleWeb.Email.approaching_accept_traffic_until(notification)
assert subject == "Your stats stop collecting soon"
assert body =~ plausible_link(team: team, label: "start a Plausible subscription")
assert body =~ "Hey John,"
assert body =~
"Your sites are still sending us data, but your account is no longer active. We'll stop counting your stats next week."
end
test "renders final warning" do
user = build(:user, id: 123, name: "John Doe")
team = build(:team, identifier: Ecto.UUID.generate())
notification = %{
id: user.id,
email: user.email,
deadline: Date.add(Date.utc_today(), 1),
site_ids: [1, 2, 3],
name: user.name,
team: team
}
%{html_body: body, subject: subject} =
PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(notification)
assert subject == "Your stats stop tomorrow"
assert body =~ plausible_link(team: team, label: "start a Plausible subscription")
assert body =~
"Your sites are still sending us data, but your account is no longer active. We'll stop counting your stats tomorrow."
end
end
describe "site setup emails" do
setup do
trial_user = new_user(trial_expiry_date: Date.add(Date.utc_today(), 100))
site = new_site(owner: trial_user)
emails = [
PlausibleWeb.Email.create_site_email(trial_user),
PlausibleWeb.Email.site_setup_help(trial_user, site.team, site),
PlausibleWeb.Email.site_setup_success(trial_user, site)
]
{:ok, emails: emails, user: trial_user, site: site}
end
test "create site email", %{user: user} do
email = PlausibleWeb.Email.create_site_email(user)
assert email.html_body =~
"You’ve created your Plausible account but haven’t added a site yet."
end
test "site setup help email", %{user: user, site: site} do
email = PlausibleWeb.Email.site_setup_help(user, site.team, site)
assert email.html_body =~
"We haven't recorded any traffic for #{site.domain} yet."
end
test "site setup success email", %{user: user, site: site} do
email = PlausibleWeb.Email.site_setup_success(user, site)
assert email.html_body =~ "Your first visitor is now visible in Plausible."
end
end
describe "import emails" do
setup do
user = new_user()
site = new_site(owner: user)
{:ok, user: user, site: site}
end
for {source, success_phrase, failure_phrase} <- [
{:google_analytics_4, "Your Google Analytics import has completed successfully",
"Unfortunately, your Google Analytics import for"},
{:universal_analytics, "Your Google Analytics import has completed successfully",
"Unfortunately, your Google Analytics import for"},
{:csv, "Your CSV import has completed successfully",
"Unfortunately, your CSV import for"}
] do
test "success email for #{source}", %{user: user, site: site} do
site_import = insert(:site_import, site: site, source: unquote(source))
PlausibleWeb.Email.import_success(site_import, user)
email = PlausibleWeb.Email.import_success(site_import, user)
assert email.text_body =~ unquote(success_phrase)
end
test "failure email for #{source}", %{user: user, site: site} do
site_import = insert(:site_import, site: site, source: unquote(source))
email = PlausibleWeb.Email.import_failure(site_import, user)
assert email.text_body =~ unquote(failure_phrase)
end
end
end
describe "text_body" do
test "welcome email" do
email =
Email.base_email()
|> Email.render("welcome_email.html", %{
user: build(:user, name: "John Doe"),
code: "123"
})
assert email.text_body =~ "Welcome to Plausible."
end
end
def plausible_url do
PlausibleWeb.EmailView.plausible_url()
end
def plausible_link(opts \\ []) do
suffix =
if team = Keyword.get(opts, :team) do
"?__team=#{team.identifier}"
else
""
end
plausible_url = plausible_url()
label =
if label = Keyword.get(opts, :label) do
label
else
plausible_url
end
"<a href=\"#{plausible_url <> suffix}\">#{label}</a>"
end
end
|