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
pull/3690/head r2024.01.15-316
RobertJoonas 3 years ago committed by GitHub
parent 7536f0285c
commit d3094ffdb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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:
![](https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN4GLWMwCrTuTcf31kYE9%2Fuploads%2FmsLk4CdSHKzU8TbfvaPq%2FPasted%20image%2020220909120933.png?alt=media&token=76f247a1-28cf-4d88-a0fa-06547268aee9)
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.
![](https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN4GLWMwCrTuTcf31kYE9%2Fuploads%2FAplurtG7UsGXMskZOlUO%2FPasted%20image%2020220909122622.png?alt=media&token=5c8156d7-d4a7-4c99-8bac-2f1e9b7d4cae)

@ -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
}

@ -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()

@ -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 """

@ -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"""
<aside class="container">
<.notice
title="You have outgrown your Plausible subscription tier"
class="shadow-md dark:shadow-none"
>
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 plan.
<.link href="mailto:enterprise@plausible.io" class="whitespace-nowrap font-semibold">
Contact us <span aria-hidden="true"> &rarr;</span>
</.link>
</.notice>
</aside>
"""
else
~H"""
<aside class="container">
<.notice title="Please upgrade your account" class="shadow-md dark:shadow-none">
In order to keep your stats running, we require you to upgrade your account. If you do not upgrade your account <%= @grace_period_end %>, we will lock your dashboard and it won't be accessible.
<.link
href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
class="whitespace-nowrap font-semibold"
>
Upgrade now <span aria-hidden="true"> &rarr;</span>
</.link>
</.notice>
</aside>
"""
end
end
def dashboard_locked(assigns) do
~H"""
<aside class="container">
<.notice title="Dashboard locked" class="shadow-md dark:shadow-none">
As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.
<.link
href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)}
class="whitespace-nowrap font-semibold"
>
Upgrade now <span aria-hidden="true"> &rarr;</span>
</.link>
</.notice>
</aside>
"""
end
attr(:billable_user, User, required: true)
attr(:current_user, User, required: true)
attr(:feature_mod, :atom, required: true, values: Feature.list())

@ -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

@ -2,111 +2,24 @@
<%= render("_flash.html", assigns) %>
<% end %>
<%= if Plausible.Auth.GracePeriod.active?(@conn.assigns[:current_user]) do %>
<div class="container">
<div class="rounded-md bg-yellow-100 p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg
class="h-5 w-5 text-yellow-400"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
clip-rule="evenodd"
/>
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800">
<%= if Plausible.Auth.enterprise_configured?(@conn.assigns[:current_user]) do %>
You have outgrown your Plausible subscription tier
<% else %>
Please upgrade your account
<% end %>
</h3>
<div class="mt-2 text-sm text-yellow-700">
<p>
<%= 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 %>
</p>
</div>
</div>
</div>
</div>
</div>
<% end %>
<%= if Plausible.Auth.GracePeriod.expired?(@conn.assigns[:current_user]) do %>
<div class="container">
<div class="rounded-md bg-yellow-100 p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg
class="h-5 w-5 text-yellow-400"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
clip-rule="evenodd"
/>
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800">
Dashboard locked
</h3>
<div class="mt-2 text-sm text-yellow-700">
<p>
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"
) %>
</p>
</div>
</div>
</div>
</div>
</div>
<% end %>
<%= if @conn.assigns[:current_user] do %>
<div class="flex flex-col gap-y-2">
<PlausibleWeb.Components.Billing.Notice.subscription_cancelled user={
@conn.assigns.current_user
} />
<Notice.active_grace_period
:if={Plausible.Auth.GracePeriod.active?(@conn.assigns.current_user)}
enterprise?={Plausible.Auth.enterprise_configured?(@conn.assigns.current_user)}
grace_period_end={grace_period_end(@conn.assigns.current_user)}
/>
<Notice.dashboard_locked :if={Plausible.Auth.GracePeriod.expired?(@conn.assigns.current_user)} />
<Notice.subscription_cancelled user={@conn.assigns.current_user} />
<PlausibleWeb.Components.Billing.Notice.subscription_past_due
<Notice.subscription_past_due
subscription={@conn.assigns.current_user.subscription}
class="container"
/>
<PlausibleWeb.Components.Billing.Notice.subscription_paused
<Notice.subscription_paused
subscription={@conn.assigns.current_user.subscription}
class="container"
/>

@ -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()

@ -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)

@ -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)

@ -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)

@ -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
}

@ -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
}
)

@ -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")

@ -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", %{

@ -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])

Loading…
Cancel
Save