2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SendTrialNotificationsTest do
|
2019-09-02 14:29:19 +03:00
|
|
|
use Plausible.DataCase
|
|
|
|
use Bamboo.Test
|
2021-04-26 11:32:18 +03:00
|
|
|
use Oban.Testing, repo: Plausible.Repo
|
|
|
|
alias Plausible.Workers.SendTrialNotifications
|
2020-06-02 13:37:38 +03:00
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
test "does not send a notification if user didn't create a site" do
|
|
|
|
insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 7))
|
|
|
|
insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 1))
|
|
|
|
insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 0))
|
|
|
|
insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: -1))
|
|
|
|
|
|
|
|
perform_job(SendTrialNotifications, %{})
|
2022-04-18 11:05:08 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send a notification if user does not have a trial" do
|
|
|
|
user = insert(:user, trial_expiry_date: nil)
|
|
|
|
insert(:site, members: [user])
|
|
|
|
|
|
|
|
perform_job(SendTrialNotifications, %{})
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send a notification if user created a site but there are no pageviews" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 7))
|
2022-01-03 12:55:24 +03:00
|
|
|
insert(:site, members: [user])
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
perform_job(SendTrialNotifications, %{})
|
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send a notification if user is a collaborator on sites but not an owner" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.now())
|
|
|
|
|
2022-01-03 12:55:24 +03:00
|
|
|
site =
|
|
|
|
insert(:site,
|
|
|
|
memberships: [
|
|
|
|
build(:site_membership, user: user, role: :admin)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-03-27 14:52:42 +03:00
|
|
|
populate_stats(site, [build(:pageview)])
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with site and pageviews" do
|
2020-03-23 12:34:25 +03:00
|
|
|
test "sends a reminder 7 days before trial ends (16 days after user signed up)" do
|
2020-06-08 10:35:13 +03:00
|
|
|
user = insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 7))
|
2022-01-03 12:55:24 +03:00
|
|
|
site = insert(:site, members: [user])
|
2023-03-27 14:52:42 +03:00
|
|
|
populate_stats(site, [build(:pageview)])
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-03-23 12:34:25 +03:00
|
|
|
assert_delivered_email(PlausibleWeb.Email.trial_one_week_reminder(user))
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sends an upgrade email the day before the trial ends" do
|
2020-06-08 10:35:13 +03:00
|
|
|
user = insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 1))
|
2022-01-03 12:55:24 +03:00
|
|
|
site = insert(:site, members: [user])
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 3, custom_events: 0}
|
2022-01-03 12:55:24 +03:00
|
|
|
|
|
|
|
populate_stats(site, [
|
2023-03-27 14:52:42 +03:00
|
|
|
build(:pageview),
|
|
|
|
build(:pageview),
|
|
|
|
build(:pageview)
|
2022-01-03 12:55:24 +03:00
|
|
|
])
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage))
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2020-03-18 17:27:46 +03:00
|
|
|
test "sends an upgrade email the day the trial ends" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today())
|
2022-01-03 12:55:24 +03:00
|
|
|
site = insert(:site, members: [user])
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 3, custom_events: 0}
|
2022-01-03 12:55:24 +03:00
|
|
|
|
|
|
|
populate_stats(site, [
|
2023-03-27 14:52:42 +03:00
|
|
|
build(:pageview),
|
|
|
|
build(:pageview),
|
|
|
|
build(:pageview)
|
2022-01-03 12:55:24 +03:00
|
|
|
])
|
2020-03-18 17:27:46 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2020-03-18 17:27:46 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "today", usage))
|
2021-01-15 17:28:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not include custom event note if user has not used custom events" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today())
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 9_000, custom_events: 0}
|
2021-01-15 17:28:57 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2021-01-15 17:28:57 +03:00
|
|
|
|
|
|
|
assert email.html_body =~
|
|
|
|
"In the last month, your account has used 9,000 billable pageviews."
|
|
|
|
end
|
|
|
|
|
|
|
|
test "includes custom event note if user has used custom events" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today())
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 9_100, custom_events: 100}
|
2021-01-15 17:28:57 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2021-01-15 17:28:57 +03:00
|
|
|
|
|
|
|
assert email.html_body =~
|
|
|
|
"In the last month, your account has used 9,100 billable pageviews and custom events in total."
|
2020-03-18 17:27:46 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sends a trial over email the day after the trial ends" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
|
2022-01-03 12:55:24 +03:00
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
populate_stats(site, [
|
2023-03-27 14:52:42 +03:00
|
|
|
build(:pageview),
|
|
|
|
build(:pageview),
|
|
|
|
build(:pageview)
|
2022-01-03 12:55:24 +03:00
|
|
|
])
|
2020-03-18 17:27:46 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2020-03-18 17:27:46 +03:00
|
|
|
|
|
|
|
assert_delivered_email(PlausibleWeb.Email.trial_over_email(user))
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
test "does not send a notification if user has a subscription" do
|
2020-06-08 10:35:13 +03:00
|
|
|
user = insert(:user, trial_expiry_date: Timex.now() |> Timex.shift(days: 7))
|
2022-01-03 12:55:24 +03:00
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
populate_stats(site, [
|
2023-03-27 14:52:42 +03:00
|
|
|
build(:pageview),
|
|
|
|
build(:pageview),
|
|
|
|
build(:pageview)
|
2022-01-03 12:55:24 +03:00
|
|
|
])
|
|
|
|
|
2020-05-22 12:33:17 +03:00
|
|
|
insert(:subscription, user: user)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendTrialNotifications, %{})
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
end
|
2020-06-25 12:31:54 +03:00
|
|
|
|
|
|
|
describe "Suggested plans" do
|
|
|
|
test "suggests 10k/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 9_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 10k/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 100k/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 90_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 100k/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 200k/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 180_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 200k/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 500k/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 450_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 500k/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 1m/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 900_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 1M/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 2m/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 1_800_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 2M/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "suggests 5m/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 4_500_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 5M/mo plan."
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
|
|
|
|
2021-02-12 11:17:53 +03:00
|
|
|
test "suggests 10m/mo plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 9_000_000, custom_events: 0}
|
2021-02-12 11:17:53 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2023-11-02 17:46:14 +03:00
|
|
|
assert email.html_body =~ "we recommend you select a 10M/mo plan."
|
2021-02-12 11:17:53 +03:00
|
|
|
end
|
|
|
|
|
2020-06-25 12:31:54 +03:00
|
|
|
test "does not suggest a plan above that" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 20_000_000, custom_events: 0}
|
2020-06-25 12:31:54 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
2020-06-25 12:31:54 +03:00
|
|
|
assert email.html_body =~ "please reply back to this email to get a quote for your volume"
|
|
|
|
end
|
Avoid suggesting plan for trials switching to enterprise plans (#3282)
This commit fixes a bug where the SendTrialNotifications job tried to
suggest a plan for users switching to enterprise plans, resulting in the
exception below:
```
UndefinedFunctionError: function :enterprise.volume/0 is undefined (module :enterprise is not available)
Module "enterprise", in :enterprise.volume/0
File "lib/plausible_web/templates/email/trial_upgrade_email.html.eex", line 5, in PlausibleWeb.EmailView."trial_upgrade_email.html"/1
File "lib/phoenix_view.ex", line 381, in Phoenix.View.render_within/3
File "lib/phoenix_view.ex", line 557, in Phoenix.View.render_to_iodata/3
File "lib/phoenix_view.ex", line 564, in Phoenix.View.render_to_string/3
File "lib/bamboo_phoenix.ex", line 291, in Bamboo.Phoenix.render_text_or_html_email/1
File "lib/workers/send_trial_notifications.ex", line 67, in Plausible.Workers.SendTrialNotifications.send_today_reminder/1
File "lib/workers/send_trial_notifications.ex", line 36, in anonymous fn/2 in Plausible.Workers.SendTrialNotifications.perform/1
```
2023-08-21 15:01:50 +03:00
|
|
|
|
|
|
|
test "does not suggest a plan when user is switching to an enterprise plan" do
|
|
|
|
user = insert(:user)
|
2023-11-30 15:30:04 +03:00
|
|
|
usage = %{total: 10_000, custom_events: 0}
|
|
|
|
|
Avoid suggesting plan for trials switching to enterprise plans (#3282)
This commit fixes a bug where the SendTrialNotifications job tried to
suggest a plan for users switching to enterprise plans, resulting in the
exception below:
```
UndefinedFunctionError: function :enterprise.volume/0 is undefined (module :enterprise is not available)
Module "enterprise", in :enterprise.volume/0
File "lib/plausible_web/templates/email/trial_upgrade_email.html.eex", line 5, in PlausibleWeb.EmailView."trial_upgrade_email.html"/1
File "lib/phoenix_view.ex", line 381, in Phoenix.View.render_within/3
File "lib/phoenix_view.ex", line 557, in Phoenix.View.render_to_iodata/3
File "lib/phoenix_view.ex", line 564, in Phoenix.View.render_to_string/3
File "lib/bamboo_phoenix.ex", line 291, in Bamboo.Phoenix.render_text_or_html_email/1
File "lib/workers/send_trial_notifications.ex", line 67, in Plausible.Workers.SendTrialNotifications.send_today_reminder/1
File "lib/workers/send_trial_notifications.ex", line 36, in anonymous fn/2 in Plausible.Workers.SendTrialNotifications.perform/1
```
2023-08-21 15:01:50 +03:00
|
|
|
insert(:enterprise_plan, user: user, paddle_plan_id: "enterprise-plan-id")
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
email = PlausibleWeb.Email.trial_upgrade_email(user, "today", usage)
|
Avoid suggesting plan for trials switching to enterprise plans (#3282)
This commit fixes a bug where the SendTrialNotifications job tried to
suggest a plan for users switching to enterprise plans, resulting in the
exception below:
```
UndefinedFunctionError: function :enterprise.volume/0 is undefined (module :enterprise is not available)
Module "enterprise", in :enterprise.volume/0
File "lib/plausible_web/templates/email/trial_upgrade_email.html.eex", line 5, in PlausibleWeb.EmailView."trial_upgrade_email.html"/1
File "lib/phoenix_view.ex", line 381, in Phoenix.View.render_within/3
File "lib/phoenix_view.ex", line 557, in Phoenix.View.render_to_iodata/3
File "lib/phoenix_view.ex", line 564, in Phoenix.View.render_to_string/3
File "lib/bamboo_phoenix.ex", line 291, in Bamboo.Phoenix.render_text_or_html_email/1
File "lib/workers/send_trial_notifications.ex", line 67, in Plausible.Workers.SendTrialNotifications.send_today_reminder/1
File "lib/workers/send_trial_notifications.ex", line 36, in anonymous fn/2 in Plausible.Workers.SendTrialNotifications.perform/1
```
2023-08-21 15:01:50 +03:00
|
|
|
assert email.html_body =~ "please reply back to this email to get a quote for your volume"
|
|
|
|
end
|
2020-06-25 12:31:54 +03:00
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|