2021-02-12 11:17:53 +03:00
|
|
|
defmodule Plausible.Workers.CheckUsageTest do
|
|
|
|
use Plausible.DataCase
|
|
|
|
use Bamboo.Test
|
|
|
|
import Double
|
|
|
|
import Plausible.TestUtils
|
|
|
|
alias Plausible.Workers.CheckUsage
|
|
|
|
|
|
|
|
setup [:create_user, :create_site]
|
2021-05-11 11:21:09 +03:00
|
|
|
@paddle_id_10k "558018"
|
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()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send an email if account has been over the limit for one billing month", %{
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
billing_stub =
|
2021-02-18 16:46:58 +03:00
|
|
|
Plausible.Billing
|
|
|
|
|> stub(:last_two_billing_months_usage, fn _user -> {9_000, 11_000} 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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil, billing_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "sends an email when an account is over their limit for two consecutive billing months", %{
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
billing_stub =
|
2021-02-18 16:46:58 +03:00
|
|
|
Plausible.Billing
|
|
|
|
|> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
|
|
|
|> stub(:last_two_billing_cycles, fn _user ->
|
|
|
|
{Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil, billing_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-02-12 11:17:53 +03:00
|
|
|
subject: "You have outgrown your Plausible subscription tier "
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "timing" do
|
|
|
|
test "checks usage one day after the last_bill_date", %{
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
billing_stub =
|
2021-02-18 16:46:58 +03:00
|
|
|
Plausible.Billing
|
|
|
|
|> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
|
|
|
|> stub(:last_two_billing_cycles, fn _user ->
|
|
|
|
{Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil, billing_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-02-12 11:17:53 +03:00
|
|
|
subject: "You have outgrown your Plausible subscription tier "
|
|
|
|
)
|
|
|
|
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
|
|
|
|
billing_stub =
|
2021-02-18 16:46:58 +03:00
|
|
|
Plausible.Billing
|
|
|
|
|> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
|
|
|
|> stub(:last_two_billing_cycles, fn _user ->
|
|
|
|
{Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil, billing_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
|
|
|
|
billing_stub =
|
2021-02-18 16:46:58 +03:00
|
|
|
Plausible.Billing
|
|
|
|
|> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
|
|
|
|> stub(:last_two_billing_cycles, fn _user ->
|
|
|
|
{Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
|
|
|
|
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(), months: -2, days: -1)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
CheckUsage.perform(nil, billing_stub)
|
2021-02-12 11:17:53 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
2021-03-01 11:11:49 +03:00
|
|
|
to: [user],
|
2021-02-12 11:17:53 +03:00
|
|
|
subject: "You have outgrown your Plausible subscription tier "
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|