From d3094ffdb7e2fdddf14ecab63a156a7e3d14cab3 Mon Sep 17 00:00:00 2001
From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com>
Date: Mon, 15 Jan 2024 14:59:56 +0000
Subject: [PATCH] Prevent upgrades that would leave the customer locked (#3683)
* Remove allowance_required field from grace_period
Since we are now preventing customers from subscribing to a plan that
does not accommodate their pageview usage, there is no need for an extra
check on removing the grace period after a successful upgrade.
This extra check is the reason why the automatic unlocks have recently
failed in several cases.
* refactor outgrown subscription notices
* make a test actually test the described functionality
* Apply greater pageview allowance margin only for trial upgrades
...in order to prevent cancelled or paused subscriptions from subscribing
to plans that would still leave their account locked.
* Mark the entire ChoosePlanTest module full build only
* remove account locking guide
This is irrelevant for self-hosters, and the internal knowledge base is
a better place for this document. Moved it there.
* refactor Keyword get clause
* add a pattern matching assertion in code
---
guides/features/account-locking.md | 35 ------
lib/plausible/auth/grace_period.ex | 16 +--
lib/plausible/billing/billing.ex | 25 +---
lib/plausible/billing/quota.ex | 12 +-
.../components/billing/notice.ex | 48 ++++++++
.../components/billing/plan_box.ex | 40 +++++--
.../templates/layout/_notice.html.heex | 109 ++----------------
lib/plausible_web/views/layout_view.ex | 1 +
lib/workers/check_usage.ex | 6 +-
test/plausible/auth/grace_period_test.exs | 10 +-
test/plausible/billing/billing_test.exs | 34 +-----
test/plausible/billing/quota_test.exs | 27 +----
test/plausible/billing/site_locker_test.exs | 28 +----
test/plausible_web/live/choose_plan_test.exs | 78 ++++++++++++-
test/workers/check_usage_test.exs | 7 +-
test/workers/lock_sites_test.exs | 2 +-
16 files changed, 205 insertions(+), 273 deletions(-)
delete mode 100644 guides/features/account-locking.md
diff --git a/guides/features/account-locking.md b/guides/features/account-locking.md
deleted file mode 100644
index c1fb5ca81..000000000
--- a/guides/features/account-locking.md
+++ /dev/null
@@ -1,35 +0,0 @@
-# Account locking
-This document explains the account locking feature from a technical perspective. Account locking happens when users have outgrown their accounts, and despite e-mail and dashboard alerts, don't upgrade after a grace period.
-
-## Usage alert and grace period
-The `Plausible.Workers.CheckUsage` daily background job alerts users they have reached their subscription limits. This runs for outgrown users one day after their last billing date.
-
-When users reach the number of sites limit, or use >110% of their pageview limit for 2 consecutive billing cycles, the background job sends them an e-mail alert. The e-mail suggests a suitable subscription plan based on usage. For enterprise users, only an internal e-mail is sent to `enterprise@plausible.io`.
-
-The user is given 7 days to upgrade their account after the alert, and this is called grace period. The background starts this grace period by adding a `users.grace_period` JSON to the user record:
-
-```json
-// SELECT grace_period FROM users LIMIT 1
-
-{
- "id": "1aa855bd-022d-4dfc-b572-6853442c3f19",
- "is_over": true,
- "end_date": "2022-03-09",
- "allowance_required": 100,
- "manual_lock": false
-}
-```
-
-
-During this period, the following alert pops up on the dashboard for both standard and enterprise users:
-
-
-
-If the user upgrades to a suitable plan, the grace period is removed (check `Plausible.Billing.subscription_updated/1`), otherwise [Account locking](#Account\ locking) follows. For enterprise users, the grace period has a manual lock flag set to true, meaning enterprise accounts can only be locked/unlocked through the internal CRM.
-
-## Account locking
-The grace period is checked daily by the `Plausible.Workers.LockSites` background job.
-
-For users that expired their grace period, `sites.locked` is is set to `true`, restricting access to dashboards. This does not stop event ingestion, so users can have their stats up to date when they finally upgrade.
-
-
diff --git a/lib/plausible/auth/grace_period.ex b/lib/plausible/auth/grace_period.ex
index 9d3afdf87..79847fb24 100644
--- a/lib/plausible/auth/grace_period.ex
+++ b/lib/plausible/auth/grace_period.ex
@@ -8,7 +8,7 @@ defmodule Plausible.Auth.GracePeriod do
background with `Plausible.Workers.LockSites`.
The grace period can also be manual, without an end date, being controlled
- manually from the CRM, and not by the background site locker job. This is
+ manually from the CRM, and not by the background site locker job. This is
useful for enterprise subscriptions.
"""
@@ -17,26 +17,23 @@ defmodule Plausible.Auth.GracePeriod do
@type t() :: %__MODULE__{
end_date: Date.t() | nil,
- allowance_required: non_neg_integer(),
is_over: boolean(),
manual_lock: boolean()
}
embedded_schema do
field :end_date, :date
- field :allowance_required, :integer
field :is_over, :boolean
field :manual_lock, :boolean
end
- @spec start_changeset(User.t(), non_neg_integer()) :: Ecto.Changeset.t()
+ @spec start_changeset(User.t()) :: Ecto.Changeset.t()
@doc """
Starts a account locking grace period of 7 days by changing the User struct.
"""
- def start_changeset(%User{} = user, allowance_required) do
+ def start_changeset(%User{} = user) do
grace_period = %__MODULE__{
end_date: Timex.shift(Timex.today(), days: 7),
- allowance_required: allowance_required,
is_over: false,
manual_lock: false
}
@@ -44,16 +41,15 @@ defmodule Plausible.Auth.GracePeriod do
Ecto.Changeset.change(user, grace_period: grace_period)
end
- @spec start_manual_lock_changeset(User.t(), non_neg_integer()) :: Ecto.Changeset.t()
+ @spec start_manual_lock_changeset(User.t()) :: Ecto.Changeset.t()
@doc """
- Starts a manual account locking grace period by changing the User struct.
+ Starts a manual account locking grace period by changing the User struct.
Manual locking means the grace period can only be removed manually from the
CRM.
"""
- def start_manual_lock_changeset(%User{} = user, allowance_required) do
+ def start_manual_lock_changeset(%User{} = user) do
grace_period = %__MODULE__{
end_date: nil,
- allowance_required: allowance_required,
is_over: false,
manual_lock: true
}
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 5a958f51c..c9d69d158 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -228,25 +228,10 @@ defmodule Plausible.Billing do
defp present?(nil), do: false
defp present?(_), do: true
- defp maybe_remove_grace_period(%User{} = user) do
- alias Plausible.Auth.GracePeriod
-
- case user.grace_period do
- %GracePeriod{allowance_required: allowance_required} ->
- new_monthly_pageview_limit =
- Plausible.Billing.Quota.monthly_pageview_limit(user)
-
- if new_monthly_pageview_limit > allowance_required do
- user
- |> Plausible.Auth.GracePeriod.remove_changeset()
- |> Repo.update!()
- else
- user
- end
-
- _ ->
- user
- end
+ defp remove_grace_period(%User{} = user) do
+ user
+ |> Plausible.Auth.GracePeriod.remove_changeset()
+ |> Repo.update!()
end
@spec format_price(Money.t()) :: String.t()
@@ -276,7 +261,7 @@ defmodule Plausible.Billing do
user
|> Plausible.Users.update_accept_traffic_until()
- |> maybe_remove_grace_period()
+ |> remove_grace_period()
|> Plausible.Users.maybe_reset_next_upgrade_override()
|> tap(&Plausible.Billing.SiteLocker.update_sites_for/1)
|> maybe_adjust_api_key_limits()
diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex
index 6fa5cba81..42d3b8f3c 100644
--- a/lib/plausible/billing/quota.ex
+++ b/lib/plausible/billing/quota.ex
@@ -25,6 +25,10 @@ defmodule Plausible.Billing.Quota do
total: non_neg_integer()
}
+ @pageview_allowance_margin 0.1
+
+ def pageview_allowance_margin(), do: @pageview_allowance_margin
+
def usage(user, opts \\ []) do
basic_usage = %{
monthly_pageviews: monthly_pageview_usage(user),
@@ -410,7 +414,7 @@ defmodule Plausible.Billing.Quota do
else
case usage do
%{last_30_days: %{total: total}} ->
- !within_limit?(total, pageview_limit_with_margin(plan))
+ !within_limit?(total, pageview_limit_with_margin(plan, opts))
billing_cycles_usage ->
Plausible.Workers.CheckUsage.exceeds_last_two_usage_cycles?(
@@ -421,9 +425,9 @@ defmodule Plausible.Billing.Quota do
end
end
- defp pageview_limit_with_margin(%{monthly_pageview_limit: limit}) do
- allowance_margin = if limit == 10_000, do: 0.3, else: 0.15
- ceil(limit * (1 + allowance_margin))
+ defp pageview_limit_with_margin(%{monthly_pageview_limit: limit}, opts) do
+ margin = Keyword.get(opts, :pageview_allowance_margin, @pageview_allowance_margin)
+ ceil(limit * (1 + margin))
end
@doc """
diff --git a/lib/plausible_web/components/billing/notice.ex b/lib/plausible_web/components/billing/notice.ex
index d60b1322f..650b2193d 100644
--- a/lib/plausible_web/components/billing/notice.ex
+++ b/lib/plausible_web/components/billing/notice.ex
@@ -8,6 +8,54 @@ defmodule PlausibleWeb.Components.Billing.Notice do
alias Plausible.Auth.User
alias Plausible.Billing.{Subscription, Plans, Subscriptions, Feature}
+ def active_grace_period(assigns) do
+ if assigns.enterprise? do
+ ~H"""
+
+ """
+ else
+ ~H"""
+
+ """
+ end
+ end
+
+ def dashboard_locked(assigns) do
+ ~H"""
+
+ """
+ end
+
attr(:billable_user, User, required: true)
attr(:current_user, User, required: true)
attr(:feature_mod, :atom, required: true, values: Feature.list())
diff --git a/lib/plausible_web/components/billing/plan_box.ex b/lib/plausible_web/components/billing/plan_box.ex
index 94615d80e..9775291f8 100644
--- a/lib/plausible_web/components/billing/plan_box.ex
+++ b/lib/plausible_web/components/billing/plan_box.ex
@@ -155,17 +155,6 @@ defmodule PlausibleWeb.Components.Billing.PlanBox do
paddle_product_id = get_paddle_product_id(assigns.plan_to_render, assigns.selected_interval)
change_plan_link_text = change_plan_link_text(assigns)
- limit_checking_opts =
- if assigns.user.allow_next_upgrade_override do
- [ignore_pageview_limit: true]
- else
- []
- end
-
- usage_within_limits =
- Quota.ensure_within_plan_limits(assigns.usage, assigns.plan_to_render, limit_checking_opts) ==
- :ok
-
subscription = assigns.user.subscription
billing_details_expired =
@@ -184,7 +173,7 @@ defmodule PlausibleWeb.Components.Billing.PlanBox do
change_plan_link_text == "Currently on this plan" && not subscription_deleted ->
{true, nil}
- assigns.available && !usage_within_limits ->
+ assigns.available && !usage_within_plan_limits?(assigns) ->
{true, "Your usage exceeds this plan"}
billing_details_expired ->
@@ -218,6 +207,33 @@ defmodule PlausibleWeb.Components.Billing.PlanBox do
"""
end
+ defp usage_within_plan_limits?(%{usage: usage, user: user, plan_to_render: plan}) do
+ # At this point, the user is guaranteed to have a `trial_expiry_date`.
+ # Otherwise, they'd be ineligible for an upgrade and this function
+ # would never be called.
+ %Date{} = user.trial_expiry_date
+
+ trial_active_or_ended_recently? =
+ Timex.diff(Timex.today(), user.trial_expiry_date, :days) <= 10
+
+ limit_checking_opts =
+ cond do
+ user.allow_next_upgrade_override ->
+ [ignore_pageview_limit: true]
+
+ trial_active_or_ended_recently? && plan.volume == "10k" ->
+ [pageview_allowance_margin: 0.3]
+
+ trial_active_or_ended_recently? ->
+ [pageview_allowance_margin: 0.15]
+
+ true ->
+ []
+ end
+
+ Quota.ensure_within_plan_limits(usage, plan, limit_checking_opts) == :ok
+ end
+
defp get_paddle_product_id(%Plan{monthly_product_id: plan_id}, :monthly), do: plan_id
defp get_paddle_product_id(%Plan{yearly_product_id: plan_id}, :yearly), do: plan_id
diff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heex
index 190acacdf..cb55e60f9 100644
--- a/lib/plausible_web/templates/layout/_notice.html.heex
+++ b/lib/plausible_web/templates/layout/_notice.html.heex
@@ -2,111 +2,24 @@
<%= render("_flash.html", assigns) %>
<% end %>
-<%= if Plausible.Auth.GracePeriod.active?(@conn.assigns[:current_user]) do %>
-
-
-
-
-
-
-
-
- <%= if Plausible.Auth.enterprise_configured?(@conn.assigns[:current_user]) do %>
- You have outgrown your Plausible subscription tier
- <% else %>
- Please upgrade your account
- <% end %>
-
-
-
- <%= if Plausible.Auth.enterprise_configured?(@conn.assigns[:current_user]) do %>
- In order to keep your stats running, we require you to upgrade
- your account to accommodate your new usage levels. Please contact
- us to discuss a new custom enterprise plan. <%= link("Contact us →",
- to: "mailto:enterprise@plausible.io",
- class: "text-sm font-medium text-yellow-800"
- ) %>
- <% else %>
- In order to keep your stats running, we require you to upgrade
- your account. If you do not upgrade your account <%= grace_period_end(
- @conn.assigns[:current_user]
- ) %>, we will
- lock your dashboard and it won't be accessible. <%= link("Upgrade now →",
- to: "/settings",
- class: "text-sm font-medium text-yellow-800"
- ) %>
- <% end %>
-
-
-
-
-
-
-<% end %>
-
-<%= if Plausible.Auth.GracePeriod.expired?(@conn.assigns[:current_user]) do %>
-
-
-
-
-
-
-
-
- Dashboard locked
-
-
-
- As you have outgrown your subscription tier, we kindly ask you to
- upgrade your subscription to accommodate your new traffic levels. <%= link(
- "Upgrade now →",
- to: "/settings",
- class: "text-sm font-medium text-yellow-800"
- ) %>
-
-
-
-
-
-
-<% end %>
-
<%= if @conn.assigns[:current_user] do %>
-
+
+
+
+
+
-
-
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex
index ee940b576..6cbcacda2 100644
--- a/lib/plausible_web/views/layout_view.ex
+++ b/lib/plausible_web/views/layout_view.ex
@@ -1,6 +1,7 @@
defmodule PlausibleWeb.LayoutView do
use PlausibleWeb, :view
use Plausible
+ alias PlausibleWeb.Components.Billing.Notice
def plausible_url do
PlausibleWeb.Endpoint.url()
diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex
index 6c3d62f3d..ccb5bc56e 100644
--- a/lib/workers/check_usage.ex
+++ b/lib/workers/check_usage.ex
@@ -83,7 +83,7 @@ defmodule Plausible.Workers.CheckUsage do
|> Plausible.Mailer.send()
subscriber
- |> Plausible.Auth.GracePeriod.start_manual_lock_changeset(pageview_usage.last_cycle.total)
+ |> Plausible.Auth.GracePeriod.start_manual_lock_changeset()
|> Repo.update()
end
end
@@ -98,7 +98,7 @@ defmodule Plausible.Workers.CheckUsage do
|> Plausible.Mailer.send()
subscriber
- |> Plausible.Auth.GracePeriod.start_changeset(pageview_usage.last_cycle.total)
+ |> Plausible.Auth.GracePeriod.start_changeset()
|> Repo.update()
_ ->
@@ -121,7 +121,7 @@ defmodule Plausible.Workers.CheckUsage do
boolean()
def exceeds_last_two_usage_cycles?(usage, limit) when is_integer(limit) do
- limit = ceil(limit * 1.1)
+ limit = ceil(limit * (1 + Quota.pageview_allowance_margin()))
Enum.all?([usage.last_cycle, usage.penultimate_cycle], fn usage ->
not Quota.below_limit?(usage.total, limit)
diff --git a/test/plausible/auth/grace_period_test.exs b/test/plausible/auth/grace_period_test.exs
index fd53eb66c..42a7845e7 100644
--- a/test/plausible/auth/grace_period_test.exs
+++ b/test/plausible/auth/grace_period_test.exs
@@ -14,7 +14,6 @@ defmodule Plausible.Auth.GracePeriodTest do
grace_period = %Plausible.Auth.GracePeriod{
end_date: yesterday,
- allowance_required: 100,
is_over: false
}
@@ -28,7 +27,6 @@ defmodule Plausible.Auth.GracePeriodTest do
grace_period = %Plausible.Auth.GracePeriod{
end_date: tomorrow,
- allowance_required: 100,
is_over: false
}
@@ -50,7 +48,6 @@ defmodule Plausible.Auth.GracePeriodTest do
grace_period = %Plausible.Auth.GracePeriod{
end_date: yesterday,
- allowance_required: 100,
is_over: true
}
@@ -64,7 +61,6 @@ defmodule Plausible.Auth.GracePeriodTest do
grace_period = %Plausible.Auth.GracePeriod{
end_date: tomorrow,
- allowance_required: 100,
is_over: false
}
@@ -75,7 +71,7 @@ defmodule Plausible.Auth.GracePeriodTest do
test "start_manual_lock_changeset/1 creates an active grace period" do
user = build(:user)
- changeset = Plausible.Auth.GracePeriod.start_manual_lock_changeset(user, 1)
+ changeset = Plausible.Auth.GracePeriod.start_manual_lock_changeset(user)
user = Ecto.Changeset.apply_changes(changeset)
assert Plausible.Auth.GracePeriod.active?(user)
@@ -84,7 +80,7 @@ defmodule Plausible.Auth.GracePeriodTest do
test "start_changeset/1 creates an active grace period" do
user = build(:user)
- changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
+ changeset = Plausible.Auth.GracePeriod.start_changeset(user)
user = Ecto.Changeset.apply_changes(changeset)
assert Plausible.Auth.GracePeriod.active?(user)
@@ -93,7 +89,7 @@ defmodule Plausible.Auth.GracePeriodTest do
test "remove_changeset/1 removes the active grace period" do
user = build(:user)
- start_changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
+ start_changeset = Plausible.Auth.GracePeriod.start_changeset(user)
user = Ecto.Changeset.apply_changes(start_changeset)
remove_changeset = Plausible.Auth.GracePeriod.remove_changeset(user)
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index d4295572c..a45cd18de 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -319,14 +319,9 @@ defmodule Plausible.BillingTest do
assert Repo.reload!(api_key).hourly_request_limit == plan.hourly_api_request_limit
end
- test "if user's grace period has ended, upgrading to the proper plan will unlock sites and remove grace period" do
- user =
- insert(:user,
- grace_period: %Plausible.Auth.GracePeriod{
- end_date: Timex.shift(Timex.today(), days: -1),
- allowance_required: 11_000
- }
- )
+ test "if user's grace period has ended, upgrading will unlock sites and remove grace period" do
+ grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
+ user = insert(:user, grace_period: grace_period)
subscription = insert(:subscription, user: user)
site = insert(:site, locked: true, members: [user])
@@ -343,29 +338,6 @@ defmodule Plausible.BillingTest do
assert Repo.reload!(user).grace_period == nil
end
- test "does not remove grace period if upgraded plan allowance is too low" do
- user =
- insert(:user,
- grace_period: %Plausible.Auth.GracePeriod{
- end_date: Timex.shift(Timex.today(), days: -1),
- allowance_required: 11_000
- }
- )
-
- subscription = insert(:subscription, user: user)
- site = insert(:site, locked: true, members: [user])
-
- @subscription_updated_params
- |> Map.merge(%{
- "subscription_id" => subscription.paddle_subscription_id,
- "passthrough" => user.id
- })
- |> Billing.subscription_updated()
-
- assert Repo.reload!(site).locked == true
- assert Repo.reload!(user).grace_period.allowance_required == 11_000
- end
-
test "ignores if subscription cannot be found" do
user = insert(:user)
diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exs
index 938ca71e6..ae7cdb2fd 100644
--- a/test/plausible/billing/quota_test.exs
+++ b/test/plausible/billing/quota_test.exs
@@ -142,36 +142,15 @@ defmodule Plausible.Billing.QuotaTest do
assert :ok = Quota.ensure_within_plan_limits(usage, plan, ignore_pageview_limit: true)
end
- test "by the last 30 days usage, pageview limit for 10k plan is only exceeded when 30% over the limit" do
+ test "by the last 30 days usage, pageview limit is exceeded when more than 10% over the limit" do
usage_within_pageview_limit = %{
- monthly_pageviews: %{last_30_days: %{total: 13_000}},
+ monthly_pageviews: %{last_30_days: %{total: 1_100_000}},
team_members: 1,
sites: 1
}
usage_over_pageview_limit = %{
- monthly_pageviews: %{last_30_days: %{total: 13_001}},
- team_members: 1,
- sites: 1
- }
-
- plan = Plans.find(@v3_plan_id)
-
- assert Quota.ensure_within_plan_limits(usage_within_pageview_limit, plan) == :ok
-
- assert Quota.ensure_within_plan_limits(usage_over_pageview_limit, plan) ==
- {:error, {:over_plan_limits, [:monthly_pageview_limit]}}
- end
-
- test "by the last 30 days usage, pageview limit for all plans above 10k is exceeded when 15% over the limit" do
- usage_within_pageview_limit = %{
- monthly_pageviews: %{last_30_days: %{total: 1_150_000}},
- team_members: 1,
- sites: 1
- }
-
- usage_over_pageview_limit = %{
- monthly_pageviews: %{last_30_days: %{total: 1_150_001}},
+ monthly_pageviews: %{last_30_days: %{total: 1_100_001}},
team_members: 1,
sites: 1
}
diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exs
index 58a497e8c..1e3bdbae4 100644
--- a/test/plausible/billing/site_locker_test.exs
+++ b/test/plausible/billing/site_locker_test.exs
@@ -71,13 +71,8 @@ defmodule Plausible.Billing.SiteLockerTest do
end
test "does not lock user who has an active subscription and is on grace period" do
- user =
- insert(:user,
- grace_period: %Plausible.Auth.GracePeriod{
- end_date: Timex.shift(Timex.today(), days: 1),
- allowance_required: 10_000
- }
- )
+ grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: 1)}
+ user = insert(:user, grace_period: grace_period)
insert(:subscription, status: Subscription.Status.active(), user: user)
@@ -115,13 +110,8 @@ defmodule Plausible.Billing.SiteLockerTest do
end
test "locks all sites if user has active subscription but grace period has ended" do
- user =
- insert(:user,
- grace_period: %Plausible.Auth.GracePeriod{
- end_date: Timex.shift(Timex.today(), days: -1),
- allowance_required: 10_000
- }
- )
+ grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
+ user = insert(:user, grace_period: grace_period)
insert(:subscription, status: Subscription.Status.active(), user: user)
@@ -138,13 +128,8 @@ defmodule Plausible.Billing.SiteLockerTest do
end
test "sends email if grace period has ended" do
- user =
- insert(:user,
- grace_period: %Plausible.Auth.GracePeriod{
- end_date: Timex.shift(Timex.today(), days: -1),
- allowance_required: 10_000
- }
- )
+ grace_period = %Plausible.Auth.GracePeriod{end_date: Timex.shift(Timex.today(), days: -1)}
+ user = insert(:user, grace_period: grace_period)
insert(:subscription, status: Subscription.Status.active(), user: user)
@@ -167,7 +152,6 @@ defmodule Plausible.Billing.SiteLockerTest do
insert(:user,
grace_period: %Plausible.Auth.GracePeriod{
end_date: Timex.shift(Timex.today(), days: -1),
- allowance_required: 10_000,
is_over: false
}
)
diff --git a/test/plausible_web/live/choose_plan_test.exs b/test/plausible_web/live/choose_plan_test.exs
index 00058491a..e8a034cee 100644
--- a/test/plausible_web/live/choose_plan_test.exs
+++ b/test/plausible_web/live/choose_plan_test.exs
@@ -1,5 +1,7 @@
defmodule PlausibleWeb.Live.ChoosePlanTest do
use PlausibleWeb.ConnCase, async: true
+ @moduletag :full_build_only
+
import Phoenix.LiveViewTest
import Plausible.Test.Support.HTML
require Plausible.Billing.Subscription.Status
@@ -222,7 +224,6 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
refute text_of_element(doc, @business_plan_box) =~ "Recommended"
end
- @tag :full_build_only
test "recommends Business when Revenue Goals used during trial", %{conn: conn, site: site} do
insert(:goal, site: site, currency: :USD, event_name: "Purchase")
@@ -231,6 +232,75 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
assert text_of_element(doc, @business_plan_box) =~ "Recommended"
refute text_of_element(doc, @growth_plan_box) =~ "Recommended"
end
+
+ @tag :slow
+ test "allows upgrade to a 100k plan with a pageview allowance margin of 0.15 when trial is active",
+ %{conn: conn, site: site} do
+ generate_usage_for(site, 115_000)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "100k")
+
+ refute class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ refute class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+
+ generate_usage_for(site, 1)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "100k")
+
+ assert class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ assert class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+ end
+
+ test "allows upgrade to a 10k plan with a pageview allowance margin of 0.3 when trial ended 10 days ago",
+ %{conn: conn, site: site, user: user} do
+ user
+ |> Plausible.Auth.User.changeset(%{trial_expiry_date: Timex.shift(Timex.today(), days: -10)})
+ |> Repo.update!()
+
+ generate_usage_for(site, 13_000)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "10k")
+
+ refute class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ refute class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+
+ generate_usage_for(site, 1)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "10k")
+
+ assert class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ assert class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+ end
+
+ test "pageview allowance margin on upgrade is 0.1 when trial ended more than 10 days ago", %{
+ conn: conn,
+ site: site,
+ user: user
+ } do
+ user
+ |> Plausible.Auth.User.changeset(%{trial_expiry_date: Timex.shift(Timex.today(), days: -11)})
+ |> Repo.update!()
+
+ generate_usage_for(site, 11_000)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "10k")
+
+ refute class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ refute class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+
+ generate_usage_for(site, 1)
+
+ {:ok, lv, _doc} = get_liveview(conn)
+ doc = set_slider(lv, "10k")
+
+ assert class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
+ assert class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"
+ end
end
describe "for a user with a v4 growth subscription plan" do
@@ -454,13 +524,14 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
Plausible.Users.allow_next_upgrade_override(user)
- {:ok, _lv, doc} = get_liveview(conn)
+ {:ok, lv, _doc} = get_liveview(conn)
+
+ doc = set_slider(lv, "10k")
refute text_of_element(doc, @growth_plan_box) =~ "Your usage exceeds this plan"
refute class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"
end
- @tag :full_build_only
test "warns about losing access to a feature", %{conn: conn, user: user, site: site} do
Plausible.Props.allow(site, ["author"])
insert(:goal, currency: :USD, site: site, event_name: "Purchase")
@@ -694,7 +765,6 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
refute element_exists?(doc, @business_highlight_pill)
end
- @tag :full_build_only
test "recommends Business tier when premium features used", %{conn: conn, site: site} do
insert(:goal, currency: :USD, site: site, event_name: "Purchase")
diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs
index d486a408d..ba8246ef1 100644
--- a/test/workers/check_usage_test.exs
+++ b/test/workers/check_usage_test.exs
@@ -131,7 +131,10 @@ defmodule Plausible.Workers.CheckUsageTest do
end
test "skips checking users who already have a grace period", %{user: user} do
- user |> Plausible.Auth.GracePeriod.start_changeset(12_000) |> Repo.update()
+ %{grace_period: existing_grace_period} =
+ user
+ |> Plausible.Auth.GracePeriod.start_changeset()
+ |> Repo.update!()
quota_stub =
Plausible.Billing.Quota
@@ -151,7 +154,7 @@ defmodule Plausible.Workers.CheckUsageTest do
CheckUsage.perform(nil, quota_stub)
assert_no_emails_delivered()
- assert Repo.reload(user).grace_period.allowance_required == 12_000
+ assert Repo.reload(user).grace_period.id == existing_grace_period.id
end
test "recommends a plan to upgrade to", %{
diff --git a/test/workers/lock_sites_test.exs b/test/workers/lock_sites_test.exs
index 22166715c..b8c27b4c9 100644
--- a/test/workers/lock_sites_test.exs
+++ b/test/workers/lock_sites_test.exs
@@ -8,7 +8,7 @@ defmodule Plausible.Workers.LockSitesTest do
user =
:user
|> build()
- |> Plausible.Auth.GracePeriod.start_manual_lock_changeset(100)
+ |> Plausible.Auth.GracePeriod.start_manual_lock_changeset()
|> Plausible.Repo.insert!()
site = insert(:site, members: [user])