Skip to content

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

Add final usage check before locking sites (#5232)

RobertJoonas79f514910b7c
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index e694bbc35e..68df93b004 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -15,10 +15,11 @@ defmodule Plausible.Billing.SiteLocker do
{:locked, lock_reason()} | :unlocked
def update_sites_for(team, opts \\ []) do
send_email? = Keyword.get(opts, :send_email?, true)
+ usage_mod = Keyword.get(opts, :usage_mod, Teams.Billing)
team = Teams.with_subscription(team)
- case Plausible.Teams.Billing.check_needs_to_upgrade(team) do
+ case Plausible.Teams.Billing.check_needs_to_upgrade(team, usage_mod) do
{:needs_to_upgrade, :grace_period_ended} ->
set_lock_status_for(team, true)
diff --git a/lib/plausible/teams/billing.ex b/lib/plausible/teams/billing.ex
index 49f9e25190..4d1721c2e7 100644
--- a/lib/plausible/teams/billing.ex
+++ b/lib/plausible/teams/billing.ex
@@ -128,35 +128,43 @@ defmodule Plausible.Teams.Billing do
|> Repo.one()
end
- @spec check_needs_to_upgrade(Teams.Team.t() | nil) ::
- {:needs_to_upgrade, :no_trial | :no_active_subscription | :grace_period_ended}
+ @spec check_needs_to_upgrade(Teams.Team.t() | nil, atom()) ::
+ {:needs_to_upgrade, :no_active_trial_or_subscription | :grace_period_ended}
| :no_upgrade_needed
- def check_needs_to_upgrade(nil), do: {:needs_to_upgrade, :no_trial}
+ def check_needs_to_upgrade(team_or_nil, usage_mod \\ Teams.Billing)
- def check_needs_to_upgrade(team) do
- team = Teams.with_subscription(team)
-
- trial_over? =
- not is_nil(team.trial_expiry_date) and
- Date.before?(team.trial_expiry_date, Date.utc_today())
+ def check_needs_to_upgrade(nil, _usage_mod),
+ do: {:needs_to_upgrade, :no_active_trial_or_subscription}
- subscription_active? = Subscriptions.active?(team.subscription)
+ def check_needs_to_upgrade(team, usage_mod) do
+ team = Teams.with_subscription(team)
cond do
- is_nil(team.trial_expiry_date) and not subscription_active? ->
- {:needs_to_upgrade, :no_trial}
+ Plausible.Teams.on_trial?(team) ->
+ :no_upgrade_needed
- trial_over? and not subscription_active? ->
- {:needs_to_upgrade, :no_active_subscription}
+ not Subscriptions.active?(team.subscription) ->
+ {:needs_to_upgrade, :no_active_trial_or_subscription}
Teams.GracePeriod.expired?(team) ->
- {:needs_to_upgrade, :grace_period_ended}
+ revise_pageview_usage(team, usage_mod)
true ->
:no_upgrade_needed
end
end
+ defp revise_pageview_usage(team, usage_mod) do
+ case Plausible.Workers.CheckUsage.check_pageview_usage_two_cycles(team, usage_mod) do
+ {:over_limit, _} ->
+ {:needs_to_upgrade, :grace_period_ended}
+
+ {:below_limit, _} ->
+ Plausible.Teams.remove_grace_period(team)
+ :no_upgrade_needed
+ end
+ end
+
@doc """
Enterprise plans are always allowed to add more sites (even when
over limit) to avoid service disruption. Their usage is checked
diff --git a/lib/plausible_web/live/sites.ex b/lib/plausible_web/live/sites.ex
index dc5eaa4ba4..69faa41306 100644
--- a/lib/plausible_web/live/sites.ex
+++ b/lib/plausible_web/live/sites.ex
@@ -55,7 +55,9 @@ defmodule PlausibleWeb.Live.Sites do
class="container pt-6"
>
<PlausibleWeb.Live.Components.Visitors.gradient_defs />
- <.upgrade_nag_screen :if={@needs_to_upgrade == {:needs_to_upgrade, :no_active_subscription}} />
+ <.upgrade_nag_screen :if={
+ @needs_to_upgrade == {:needs_to_upgrade, :no_active_trial_or_subscription}
+ } />
<div class="group mt-6 pb-5 border-b border-gray-200 dark:border-gray-500 flex items-center justify-between">
<h2 class="text-2xl font-bold leading-7 text-gray-900 dark:text-gray-100 sm:text-3xl sm:leading-9 sm:truncate flex-shrink-0">
diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex
index f5633dfa01..5ca0d7b1a4 100644
--- a/lib/workers/check_usage.ex
+++ b/lib/workers/check_usage.ex
@@ -146,7 +146,7 @@ defmodule Plausible.Workers.CheckUsage do
end
end
- defp check_pageview_usage_two_cycles(subscriber, usage_mod) do
+ def check_pageview_usage_two_cycles(subscriber, usage_mod) do
usage = usage_mod.monthly_pageview_usage(subscriber)
limit = Teams.Billing.monthly_pageview_limit(subscriber.subscription)
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index 2c97085cd6..a5149d28ca 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -7,6 +7,8 @@ defmodule Plausible.BillingTest do
alias Plausible.Billing.Subscription
describe "check_needs_to_upgrade" do
+ @describetag :ee_only
+
test "is false for a trial user" do
team = new_user(trial_expiry_date: Date.utc_today()) |> team_of()
assert Plausible.Teams.Billing.check_needs_to_upgrade(team) == :no_upgrade_needed
@@ -16,12 +18,12 @@ defmodule Plausible.BillingTest do
team = new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1)) |> team_of()
assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
- {:needs_to_upgrade, :no_active_subscription}
+ {:needs_to_upgrade, :no_active_trial_or_subscription}
end
test "is true for a user with empty trial expiry date" do
assert Plausible.Teams.Billing.check_needs_to_upgrade(nil) ==
- {:needs_to_upgrade, :no_trial}
+ {:needs_to_upgrade, :no_active_trial_or_subscription}
end
test "is false for user with empty trial expiry date but with an active subscription" do
@@ -66,7 +68,7 @@ defmodule Plausible.BillingTest do
|> team_of()
assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
- {:needs_to_upgrade, :no_active_subscription}
+ {:needs_to_upgrade, :no_active_trial_or_subscription}
end
test "is true for a deleted subscription if no next_bill_date specified" do
@@ -79,10 +81,10 @@ defmodule Plausible.BillingTest do
|> team_of()
assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
- {:needs_to_upgrade, :no_active_subscription}
+ {:needs_to_upgrade, :no_active_trial_or_subscription}
end
- test "is true for a user past their grace period" do
+ test "needs to upgrade if subscription active and grace period ended when usage still over limits" do
user =
new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
|> subscribe_to_growth_plan(
@@ -92,9 +94,25 @@ defmodule Plausible.BillingTest do
team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.end_grace_period()
- assert Plausible.Teams.Billing.check_needs_to_upgrade(team) ==
+ over_limits_usage_stub = monthly_pageview_usage_stub(100_000_000, 100_000_000)
+
+ assert Plausible.Teams.Billing.check_needs_to_upgrade(team, over_limits_usage_stub) ==
{:needs_to_upgrade, :grace_period_ended}
end
+
+ test "no upgrade needed if subscription active and grace period ended but usage below limits" do
+ user =
+ new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
+ |> subscribe_to_growth_plan(
+ status: Subscription.Status.deleted(),
+ next_bill_date: Date.utc_today()
+ )
+
+ team = user |> team_of() |> Repo.reload!() |> Plausible.Teams.end_grace_period()
+
+ assert Plausible.Teams.Billing.check_needs_to_upgrade(team, Plausible.Teams.Billing) ==
+ :no_upgrade_needed
+ end
end
@subscription_id "subscription-123"
@@ -603,4 +621,30 @@ defmodule Plausible.BillingTest do
refute Plausible.Teams.Billing.has_active_subscription?(paused_team)
refute Plausible.Teams.Billing.has_active_subscription?(nil)
end
+
+ def monthly_pageview_usage_stub(penultimate_usage, last_usage) do
+ last_bill_date = Date.utc_today() |> Date.shift(day: -1)
+
+ Plausible.Teams.Billing
+ |> Double.stub(:monthly_pageview_usage, fn _user ->
+ %{
+ last_cycle: %{
+ date_range:
+ Date.range(
+ Date.shift(last_bill_date, month: -1),
+ Date.shift(last_bill_date, day: -1)
+ ),
+ total: last_usage
+ },
+ penultimate_cycle: %{
+ date_range:
+ Date.range(
+ Date.shift(last_bill_date, month: -2),
+ Date.shift(last_bill_date, day: -1, month: -1)
+ ),
+ total: penultimate_usage
+ }
+ }
+ end)
+ end
end
diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exs
index 28c53be63f..0d65f50f0b 100644
--- a/test/plausible/billing/site_locker_test.exs
+++ b/test/plausible/billing/site_locker_test.exs
@@ -5,6 +5,10 @@ defmodule Plausible.Billing.SiteLockerTest do
require Plausible.Billing.Subscription.Status
alias Plausible.Billing.{SiteLocker, Subscription}
+ @moduletag :ee_only
+
+ @v4_growth_plan_id "857097"
+
describe "update_sites_for/1" do
test "does not lock sites if user is on trial" do
user = new_user(trial_expiry_date: Date.utc_today())
@@ -61,6 +65,24 @@ defmodule Plausible.Billing.SiteLockerTest do
refute Repo.reload!(site).locked
end
+ test "does not lock enterprise customers with (manual lock) graceperiod ended" do
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Date.utc_today() |> Date.shift(day: -1),
+ manual_lock: true
+ }
+
+ user =
+ new_user(team: [grace_period: grace_period])
+ |> subscribe_to_enterprise_plan(monthly_pageview_limit: 10_000)
+
+ site = new_site(owner: user)
+ team = team_of(user)
+
+ assert SiteLocker.update_sites_for(team) == :unlocked
+
+ refute Repo.reload!(site).locked
+ end
+
test "locks user who cancelled subscription and the cancelled subscription has expired" do
user =
new_user(trial_expiry_date: Date.shift(Date.utc_today(), day: -1))
@@ -72,48 +94,54 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_subscription}
+ assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
assert Repo.reload!(site).locked
end
- test "locks all sites if team has active subscription but grace period has ended" do
+ test "locks all sites if team has an active subscription but grace period has ended (still over limits)" do
grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
user = new_user(team: [grace_period: grace_period])
- subscribe_to_plan(user, "123")
+ subscribe_to_plan(user, @v4_growth_plan_id)
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :grace_period_ended_now}
+ over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+
+ assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ {:locked, :grace_period_ended_now}
assert Repo.reload!(site).locked
end
- test "sends email if grace period has ended" do
+ test "does not lock sites (and removes grace period), when on active subscription and grace period ended, but usage now within limits" do
grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
user = new_user(team: [grace_period: grace_period])
- subscribe_to_plan(user, "123")
- new_site(owner: user)
+ subscribe_to_plan(user, @v4_growth_plan_id)
+ site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :grace_period_ended_now}
+ assert SiteLocker.update_sites_for(team) == :unlocked
- assert_email_delivered_with(
- to: [user],
- subject: "[Action required] Your Plausible dashboard is now locked"
- )
+ refute Repo.reload!(site).locked
+
+ assert_no_emails_delivered()
end
- test "sends email to billing members if grace period has ended" do
+ test "sends email to all billing members if grace period has ended and still over limits" do
grace_period = %Plausible.Teams.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
user = new_user(team: [grace_period: grace_period])
- subscribe_to_plan(user, "123")
+ subscribe_to_plan(user, @v4_growth_plan_id)
new_site(owner: user)
team = team_of(user)
+
billing_member = new_user()
add_member(team, user: billing_member, role: :billing)
- assert SiteLocker.update_sites_for(team) == {:locked, :grace_period_ended_now}
+ over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+
+ assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ {:locked, :grace_period_ended_now}
assert_email_delivered_with(
to: [user],
@@ -134,11 +162,14 @@ defmodule Plausible.Billing.SiteLockerTest do
user = new_user(team: [grace_period: grace_period])
- subscribe_to_plan(user, "123")
+ subscribe_to_plan(user, @v4_growth_plan_id)
new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :grace_period_ended_now}
+ over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+
+ assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ {:locked, :grace_period_ended_now}
assert_email_delivered_with(
to: [user],
@@ -146,17 +177,37 @@ defmodule Plausible.Billing.SiteLockerTest do
)
team = Repo.reload!(team)
- assert SiteLocker.update_sites_for(team) == {:locked, :grace_period_ended_already}
+
+ assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ {:locked, :grace_period_ended_already}
assert_no_emails_delivered()
end
+ test "unlocks already ended grace periods when they still have an active subscription and went within limits again" do
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Timex.shift(Timex.today(), days: -7),
+ is_over: true
+ }
+
+ user = new_user(team: [grace_period: grace_period])
+
+ subscribe_to_plan(user, @v4_growth_plan_id)
+ new_site(owner: user)
+ site = new_site(owner: user)
+ team = team_of(user)
+
+ assert SiteLocker.update_sites_for(team) == :unlocked
+
+ refute Repo.reload!(site).locked
+ end
+
test "locks all sites if user has no trial or active subscription" do
user = new_user(trial_expiry_date: Date.utc_today() |> Date.shift(day: -1))
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_subscription}
+ assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
assert Repo.reload!(site).locked
end
@@ -166,7 +217,7 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = user |> team_of() |> Ecto.Changeset.change(trial_expiry_date: nil) |> Repo.update!()
- assert SiteLocker.update_sites_for(team) == {:locked, :no_trial}
+ assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
assert Repo.reload!(site).locked
end
@@ -179,7 +230,7 @@ defmodule Plausible.Billing.SiteLockerTest do
add_guest(viewer_site, user: user, role: :viewer)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_subscription}
+ assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
owner_site = Repo.reload!(owner_site)
viewer_site = Repo.reload!(viewer_site)
diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs
index dd5d393827..fa44b4a7ba 100644
--- a/test/plausible_web/controllers/site_controller_test.exs
+++ b/test/plausible_web/controllers/site_controller_test.exs
@@ -141,6 +141,7 @@ defmodule PlausibleWeb.SiteControllerTest do
)
end
+ @tag :ee_only
test "shows upgrade nag message to expired trial user without subscription", %{
conn: initial_conn,
user: user
diff --git a/test/workers/lock_sites_test.exs b/test/workers/lock_sites_test.exs
index 84a9a7f7f3..5538070eb9 100644
--- a/test/workers/lock_sites_test.exs
+++ b/test/workers/lock_sites_test.exs
@@ -5,6 +5,8 @@ defmodule Plausible.Workers.LockSitesTest do
alias Plausible.Workers.LockSites
alias Plausible.Billing.Subscription
+ @moduletag :ee_only
+
test "does not lock enterprise site on grace period" do
user = new_user()
site = new_site(owner: user)