2021-02-12 11:17:53 +03:00
|
|
|
defmodule Plausible.Workers.CheckUsageTest do
|
2022-05-06 10:30:38 +03:00
|
|
|
use Plausible.DataCase, async: true
|
2021-02-12 11:17:53 +03:00
|
|
|
use Bamboo.Test
|
|
|
|
import Double
|
2022-10-24 10:34:02 +03:00
|
|
|
|
2021-02-12 11:17:53 +03:00
|
|
|
alias Plausible.Workers.CheckUsage
|
|
|
|
|
|
|
|
setup [:create_user, :create_site]
|
2021-05-11 11:21:09 +03:00
|
|
|
@paddle_id_10k "558018"
|
2023-11-30 15:30:04 +03:00
|
|
|
@date_range Date.range(Timex.today(), Timex.today())
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
test "ignores user without subscription" do
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "ignores user with subscription but no usage", %{user: user} do
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
2021-11-04 12:05:33 +03:00
|
|
|
assert Repo.reload(user).grace_period == nil
|
2021-02-12 11:17:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send an email if account has been over the limit for one billing month", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 9_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-10-22 12:26:07 +03:00
|
|
|
end)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
2021-11-04 12:46:41 +03:00
|
|
|
assert Repo.reload(user).grace_period == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send an email if account is over the limit by less than 10%", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 10_999},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-11-04 12:46:41 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-11-04 12:46:41 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
2021-11-04 12:05:33 +03:00
|
|
|
assert Repo.reload(user).grace_period == nil
|
2021-02-12 11:17:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sends an email when an account is over their limit for two consecutive billing months", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-02-18 16:46:58 +03:00
|
|
|
end)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-11-04 11:13:08 +03:00
|
|
|
subject: "[Action required] You have outgrown your Plausible subscription tier"
|
2021-10-20 17:49:11 +03:00
|
|
|
)
|
2021-10-29 11:18:29 +03:00
|
|
|
|
2021-11-04 12:05:33 +03:00
|
|
|
assert Repo.reload(user).grace_period.end_date == Timex.shift(Timex.today(), days: 7)
|
2021-10-20 17:49:11 +03:00
|
|
|
end
|
|
|
|
|
2022-10-25 14:16:44 +03:00
|
|
|
test "sends an email suggesting enterprise plan when usage is greater than 10M ", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000_000}
|
|
|
|
}
|
2022-10-25 14:16:44 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2022-10-25 14:16:44 +03:00
|
|
|
|
|
|
|
assert_delivered_email_matches(%{html_body: html_body})
|
|
|
|
|
|
|
|
assert html_body =~
|
2023-12-06 15:02:22 +03:00
|
|
|
"Your usage exceeds our standard plans, so please reply back to this email for a tailored quote"
|
2022-10-25 14:16:44 +03:00
|
|
|
end
|
|
|
|
|
2021-12-02 12:42:34 +03:00
|
|
|
test "skips checking users who already have a grace period", %{user: user} do
|
2024-01-15 17:59:56 +03:00
|
|
|
%{grace_period: existing_grace_period} =
|
|
|
|
user
|
|
|
|
|> Plausible.Auth.GracePeriod.start_changeset()
|
|
|
|
|> Repo.update!()
|
2021-12-02 12:42:34 +03:00
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-12-02 12:42:34 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-12-02 12:42:34 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
2024-01-15 17:59:56 +03:00
|
|
|
assert Repo.reload(user).grace_period.id == existing_grace_period.id
|
2021-12-02 12:42:34 +03:00
|
|
|
end
|
|
|
|
|
2022-04-11 20:42:40 +03:00
|
|
|
test "recommends a plan to upgrade to", %{
|
2021-11-23 12:21:22 +03:00
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-11-23 12:21:22 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-11-23 12:21:22 +03:00
|
|
|
|
|
|
|
assert_delivered_email_matches(%{
|
|
|
|
html_body: html_body
|
|
|
|
})
|
|
|
|
|
2023-12-06 15:02:22 +03:00
|
|
|
assert html_body =~ "We recommend you upgrade to the 100k/mo plan"
|
2021-11-23 12:21:22 +03:00
|
|
|
end
|
|
|
|
|
2021-10-22 12:26:07 +03:00
|
|
|
describe "enterprise customers" do
|
|
|
|
test "checks billable pageview usage for enterprise customer, sends usage information to enterprise@plausible.io",
|
|
|
|
%{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 1_100_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 1_100_000}
|
|
|
|
}
|
2021-10-22 12:26:07 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
enterprise_plan = insert(:enterprise_plan, user: user, monthly_pageview_limit: 1_000_000)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: enterprise_plan.paddle_plan_id,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-10-22 12:26:07 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{nil, "enterprise@plausible.io"}],
|
|
|
|
subject: "#{user.email} has outgrown their enterprise plan"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "checks site limit for enterprise customer, sends usage information to enterprise@plausible.io",
|
|
|
|
%{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 1},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 1}
|
|
|
|
}
|
2021-10-22 12:26:07 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
enterprise_plan = insert(:enterprise_plan, user: user, site_limit: 2)
|
|
|
|
|
|
|
|
insert(:site, members: [user])
|
|
|
|
insert(:site, members: [user])
|
|
|
|
insert(:site, members: [user])
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: enterprise_plan.paddle_plan_id,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-10-22 12:26:07 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{nil, "enterprise@plausible.io"}],
|
|
|
|
subject: "#{user.email} has outgrown their enterprise plan"
|
|
|
|
)
|
|
|
|
end
|
2022-09-20 11:46:28 +03:00
|
|
|
|
|
|
|
test "starts grace period when plan is outgrown", %{user: user} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 1_100_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 1_100_000}
|
|
|
|
}
|
2022-09-20 11:46:28 +03:00
|
|
|
end)
|
|
|
|
|
|
|
|
enterprise_plan = insert(:enterprise_plan, user: user, monthly_pageview_limit: 1_000_000)
|
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: enterprise_plan.paddle_plan_id,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2022-09-20 11:46:28 +03:00
|
|
|
assert user |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()
|
|
|
|
end
|
2021-02-12 11:17:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "timing" do
|
|
|
|
test "checks usage one day after the last_bill_date", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-02-18 16:46:58 +03:00
|
|
|
end)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
|
|
|
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-11-04 11:13:08 +03:00
|
|
|
subject: "[Action required] You have outgrown your Plausible subscription tier"
|
2021-02-12 11:17:53 +03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-03-29 14:53:34 +03:00
|
|
|
test "does not check exactly one month after last_bill_date", %{
|
2021-02-12 11:17:53 +03:00
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-02-18 16:46:58 +03:00
|
|
|
end)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
2021-03-29 14:53:34 +03:00
|
|
|
last_bill_date: ~D[2021-03-28]
|
2021-02-12 11:17:53 +03:00
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub, ~D[2021-03-28])
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "for yearly subscriptions, checks usage multiple months + one day after the last_bill_date",
|
|
|
|
%{
|
|
|
|
user: user
|
|
|
|
} do
|
2023-11-30 15:30:04 +03:00
|
|
|
quota_stub =
|
|
|
|
Plausible.Billing.Quota
|
|
|
|
|> stub(:monthly_pageview_usage, fn _user ->
|
|
|
|
%{
|
|
|
|
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
|
|
|
last_cycle: %{date_range: @date_range, total: 11_000}
|
|
|
|
}
|
2021-02-18 16:46:58 +03:00
|
|
|
end)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
|
|
|
user: user,
|
|
|
|
paddle_plan_id: @paddle_id_10k,
|
2021-08-31 11:13:09 +03:00
|
|
|
last_bill_date: ~D[2021-06-29]
|
2021-02-12 11:17:53 +03:00
|
|
|
)
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
CheckUsage.perform(nil, quota_stub, ~D[2021-08-30])
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-11-04 11:13:08 +03:00
|
|
|
subject: "[Action required] You have outgrown your Plausible subscription tier"
|
2021-02-12 11:17:53 +03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|