Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Prevent upgrades that would leave the customer locked (#3683)
RobertJoonasd3094ffdb7e2
diff --git a/guides/features/account-locking.md b/guides/features/account-locking.mddeleted file mode 100644index c1fb5ca814..0000000000--- 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 `[email protected]`.--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.exindex 9d3afdf875..79847fb241 100644--- a/lib/plausible/auth/grace_period.ex+++ b/lib/plausible/auth/grace_period.ex@@ -8,7 +8,7 @@ defmodule Plausible.Auth.GracePeriod dobackground 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 isuseful 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 dofield :end_date, :date- field :allowance_required, :integerfield :is_over, :booleanfield :manual_lock, :booleanend- @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) dograce_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 doEcto.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 theCRM."""- def start_manual_lock_changeset(%User{} = user, allowance_required) do+ def start_manual_lock_changeset(%User{} = user) dograce_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.exindex 5a958f51c9..c9d69d158a 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -228,25 +228,10 @@ defmodule Plausible.Billing dodefp present?(nil), do: falsedefp 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 douser|> 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.exindex 6fa5cba816..42d3b8f3c6 100644--- a/lib/plausible/billing/quota.ex+++ b/lib/plausible/billing/quota.ex@@ -25,6 +25,10 @@ defmodule Plausible.Billing.Quota dototal: non_neg_integer()}+ @pageview_allowance_margin 0.1++ def pageview_allowance_margin(), do: @pageview_allowance_margin+def usage(user, opts \\ []) dobasic_usage = %{monthly_pageviews: monthly_pageview_usage(user),@@ -410,7 +414,7 @@ defmodule Plausible.Billing.Quota doelsecase 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 doendend- 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.exindex d60b1322f0..650b2193d9 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 doalias Plausible.Auth.Useralias 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:[email protected]" class="whitespace-nowrap font-semibold">+ Contact us <span aria-hidden="true"> →</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"> →</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"> →</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())diff --git a/lib/plausible_web/components/billing/plan_box.ex b/lib/plausible_web/components/billing/plan_box.exindex 94615d80e7..9775291f85 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 dopaddle_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.subscriptionbilling_details_expired =@@ -184,7 +173,7 @@ defmodule PlausibleWeb.Components.Billing.PlanBox dochange_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_iddefp get_paddle_product_id(%Plan{yearly_product_id: plan_id}, :yearly), do: plan_iddiff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heexindex 190acacdf5..cb55e60f9e 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 %>- <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:[email protected]",- 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_duesubscription={@conn.assigns.current_user.subscription}class="container"/>- <PlausibleWeb.Components.Billing.Notice.subscription_paused+ <Notice.subscription_pausedsubscription={@conn.assigns.current_user.subscription}class="container"/>diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.exindex ee940b5766..6cbcacda2d 100644--- a/lib/plausible_web/views/layout_view.ex+++ b/lib/plausible_web/views/layout_view.ex@@ -1,6 +1,7 @@defmodule PlausibleWeb.LayoutView douse PlausibleWeb, :viewuse Plausible+ alias PlausibleWeb.Components.Billing.Noticedef plausible_url doPlausibleWeb.Endpoint.url()diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.exindex 6c3d62f3d3..ccb5bc56e3 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()endend@@ -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 doboolean()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.exsindex fd53eb66ce..42a7845e7d 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 dograce_period = %Plausible.Auth.GracePeriod{end_date: yesterday,- allowance_required: 100,is_over: false}@@ -28,7 +27,6 @@ defmodule Plausible.Auth.GracePeriodTest dograce_period = %Plausible.Auth.GracePeriod{end_date: tomorrow,- allowance_required: 100,is_over: false}@@ -50,7 +48,6 @@ defmodule Plausible.Auth.GracePeriodTest dograce_period = %Plausible.Auth.GracePeriod{end_date: yesterday,- allowance_required: 100,is_over: true}@@ -64,7 +61,6 @@ defmodule Plausible.Auth.GracePeriodTest dograce_period = %Plausible.Auth.GracePeriod{end_date: tomorrow,- allowance_required: 100,is_over: false}@@ -75,7 +71,7 @@ defmodule Plausible.Auth.GracePeriodTest dotest "start_manual_lock_changeset/1 creates an active grace period" douser = 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 dotest "start_changeset/1 creates an active grace period" douser = 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 dotest "remove_changeset/1 removes the active grace period" douser = 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.exsindex d4295572cf..a45cd18de6 100644--- a/test/plausible/billing/billing_test.exs+++ b/test/plausible/billing/billing_test.exs@@ -319,14 +319,9 @@ defmodule Plausible.BillingTest doassert Repo.reload!(api_key).hourly_request_limit == plan.hourly_api_request_limitend- 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 doassert Repo.reload!(user).grace_period == nilend- 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" douser = insert(:user)diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exsindex 938ca71e68..ae7cdb2fda 100644--- a/test/plausible/billing/quota_test.exs+++ b/test/plausible/billing/quota_test.exs@@ -142,36 +142,15 @@ defmodule Plausible.Billing.QuotaTest doassert :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" dousage_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.exsindex 58a497e8c7..1e3bdbae47 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 doendtest "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 doendtest "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 doendtest "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 doinsert(: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.exsindex 00058491ad..e8a034cee7 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 douse PlausibleWeb.ConnCase, async: true+ @moduletag :full_build_only+import Phoenix.LiveViewTestimport Plausible.Test.Support.HTMLrequire Plausible.Billing.Subscription.Status@@ -222,7 +224,6 @@ defmodule PlausibleWeb.Live.ChoosePlanTest dorefute text_of_element(doc, @business_plan_box) =~ "Recommended"end- @tag :full_build_onlytest "recommends Business when Revenue Goals used during trial", %{conn: conn, site: site} doinsert(:goal, site: site, currency: :USD, event_name: "Purchase")@@ -231,6 +232,75 @@ defmodule PlausibleWeb.Live.ChoosePlanTest doassert 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"+ endenddescribe "for a user with a v4 growth subscription plan" do@@ -454,13 +524,14 @@ defmodule PlausibleWeb.Live.ChoosePlanTest doPlausible.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_onlytest "warns about losing access to a feature", %{conn: conn, user: user, site: site} doPlausible.Props.allow(site, ["author"])insert(:goal, currency: :USD, site: site, event_name: "Purchase")@@ -694,7 +765,6 @@ defmodule PlausibleWeb.Live.ChoosePlanTest dorefute element_exists?(doc, @business_highlight_pill)end- @tag :full_build_onlytest "recommends Business tier when premium features used", %{conn: conn, site: site} doinsert(:goal, currency: :USD, site: site, event_name: "Purchase")diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exsindex d486a408da..ba8246ef1a 100644--- a/test/workers/check_usage_test.exs+++ b/test/workers/check_usage_test.exs@@ -131,7 +131,10 @@ defmodule Plausible.Workers.CheckUsageTest doendtest "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 doCheckUsage.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.idendtest "recommends a plan to upgrade to", %{diff --git a/test/workers/lock_sites_test.exs b/test/workers/lock_sites_test.exsindex 22166715c5..b8c27b4c97 100644--- a/test/workers/lock_sites_test.exs+++ b/test/workers/lock_sites_test.exs@@ -8,7 +8,7 @@ defmodule Plausible.Workers.LockSitesTest douser =: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])