Skip to content

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

Manually lock outgrown enterprise accounts (#2197)

Vinicius Brasil40182f6d6e2d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55f97d8333..e6077ebbfb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,8 @@ All notable changes to this project will be documented in this file.
- New width=manual mode for embedded dashboards plausible/analytics#2148
- Add more timezone options
- Add new strategy to recommend timezone when creating a new site
+- Alert outgrown enterprise users of their usage plausible/analytics#2197
+- Manually lock and unlock enterprise users plausible/analytics#2197
### Fixed
- Hash part of the URL can now be used when excluding pages with `script.exclusions.hash.js`.
diff --git a/guides/features/account-locking.md b/guides/features/account-locking.md
new file mode 100644
index 0000000000..c1fb5ca814
--- /dev/null
+++ b/guides/features/account-locking.md
@@ -0,0 +1,35 @@
+# 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:
+
+![](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)
diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex
index cb12997ca2..e0c38ce74b 100644
--- a/lib/plausible/auth/auth.ex
+++ b/lib/plausible/auth/auth.ex
@@ -98,4 +98,11 @@ defmodule Plausible.Auth do
def is_super_admin?(user_id) do
user_id in Application.get_env(:plausible, :super_admin_user_ids)
end
+
+ def enterprise?(nil), do: false
+
+ def enterprise?(%Plausible.Auth.User{} = user) do
+ user = Repo.preload(user, :enterprise_plan)
+ user.enterprise_plan != nil
+ end
end
diff --git a/lib/plausible/auth/grace_period.ex b/lib/plausible/auth/grace_period.ex
new file mode 100644
index 0000000000..9d3afdf875
--- /dev/null
+++ b/lib/plausible/auth/grace_period.ex
@@ -0,0 +1,106 @@
+defmodule Plausible.Auth.GracePeriod do
+ @moduledoc """
+ This embedded schema stores information about the account locking grace
+ period.
+
+ Users are given this 7-day grace period to upgrade their account after
+ outgrowing their subscriptions. The actual account locking happens in
+ 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
+ useful for enterprise subscriptions.
+ """
+
+ use Ecto.Schema
+ alias Plausible.Auth.User
+
+ @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()
+ @doc """
+ Starts a account locking grace period of 7 days by changing the User struct.
+ """
+ def start_changeset(%User{} = user, allowance_required) do
+ grace_period = %__MODULE__{
+ end_date: Timex.shift(Timex.today(), days: 7),
+ allowance_required: allowance_required,
+ is_over: false,
+ manual_lock: false
+ }
+
+ Ecto.Changeset.change(user, grace_period: grace_period)
+ end
+
+ @spec start_manual_lock_changeset(User.t(), non_neg_integer()) :: Ecto.Changeset.t()
+ @doc """
+ 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
+ grace_period = %__MODULE__{
+ end_date: nil,
+ allowance_required: allowance_required,
+ is_over: false,
+ manual_lock: true
+ }
+
+ Ecto.Changeset.change(user, grace_period: grace_period)
+ end
+
+ @spec end_changeset(User.t()) :: Ecto.Changeset.t()
+ @doc """
+ Ends an existing grace period by `setting users.grace_period.is_over` to true.
+ This means the grace period has expired.
+ """
+ def end_changeset(%User{} = user) do
+ Ecto.Changeset.change(user, grace_period: %{is_over: true})
+ end
+
+ @spec remove_changeset(User.t()) :: Ecto.Changeset.t()
+ @doc """
+ Removes the grace period from the User completely.
+ """
+ def remove_changeset(%User{} = user) do
+ Ecto.Changeset.change(user, grace_period: nil)
+ end
+
+ @spec active?(User.t()) :: boolean()
+ @doc """
+ Returns whether the grace period is still active for a User. Defaults to
+ false if the user is nil or there is no grace period.
+ """
+ def active?(user)
+
+ def active?(%User{grace_period: %__MODULE__{end_date: %Date{} = end_date}}) do
+ Timex.diff(end_date, Timex.today(), :days) >= 0
+ end
+
+ def active?(%User{grace_period: %__MODULE__{manual_lock: true}}) do
+ true
+ end
+
+ def active?(_user), do: false
+
+ @spec expired?(User.t()) :: boolean()
+ @doc """
+ Returns whether the grace period has already expired for a User. Defaults to
+ false if the user is nil or there is no grace period.
+ """
+ def expired?(user) do
+ if user && user.grace_period, do: !active?(user), else: false
+ end
+end
diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex
index b1c49b24b4..bdc6c6c2e5 100644
--- a/lib/plausible/auth/user.ex
+++ b/lib/plausible/auth/user.ex
@@ -10,20 +10,12 @@ defimpl FunWithFlags.Actor, for: Plausible.Auth.User do
end
end
-defmodule Plausible.Auth.GracePeriod do
- use Ecto.Schema
-
- embedded_schema do
- field :end_date, :date
- field :allowance_required, :integer
- field :is_over, :boolean
- end
-end
-
defmodule Plausible.Auth.User do
use Ecto.Schema
import Ecto.Changeset
+ @type t() :: %__MODULE__{}
+
@required [:email, :name, :password, :password_confirmation]
schema "users" do
field :email, :string
@@ -96,24 +88,6 @@ defmodule Plausible.Auth.User do
change(user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
end
- def start_grace_period(user, allowance_required) do
- grace_period = %Plausible.Auth.GracePeriod{
- end_date: Timex.today() |> Timex.shift(days: 7),
- allowance_required: allowance_required,
- is_over: false
- }
-
- change(user, grace_period: grace_period)
- end
-
- def end_grace_period(user) do
- change(user, grace_period: %{is_over: true})
- end
-
- def remove_grace_period(user) do
- change(user, grace_period: nil)
- end
-
defp trial_expiry() do
if Application.get_env(:plausible, :is_selfhost) do
Timex.today() |> Timex.shift(years: 100)
diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex
index ec0cfa93f9..38b57ee462 100644
--- a/lib/plausible/auth/user_admin.ex
+++ b/lib/plausible/auth/user_admin.ex
@@ -31,13 +31,26 @@ defmodule Plausible.Auth.UserAdmin do
unlock: %{
name: "Unlock",
action: fn _, user -> unlock(user) end
+ },
+ lock: %{
+ name: "Lock",
+ action: fn _, user -> lock(user) end
}
]
end
+ defp lock(user) do
+ if user.grace_period do
+ Plausible.Billing.SiteLocker.set_lock_status_for(user, true)
+ user |> Plausible.Auth.GracePeriod.end_changeset() |> Repo.update()
+ else
+ {:error, user, "No active grace period on this user"}
+ end
+ end
+
defp unlock(user) do
if user.grace_period do
- Plausible.Auth.User.remove_grace_period(user) |> Repo.update()
+ Plausible.Auth.GracePeriod.remove_changeset(user) |> Repo.update()
Plausible.Billing.SiteLocker.set_lock_status_for(user, false)
{:ok, user}
else
@@ -45,14 +58,23 @@ defmodule Plausible.Auth.UserAdmin do
end
end
- defp grace_period_status(%{grace_period: nil}), do: "--"
+ defp grace_period_status(%{grace_period: grace_period}) do
+ case grace_period do
+ nil ->
+ "--"
- defp grace_period_status(user) do
- if user.grace_period.is_over do
- "ended"
- else
- days_left = Timex.diff(user.grace_period.end_date, Timex.now(), :days)
- "#{days_left} days left"
+ %{manual_lock: true, is_over: true} ->
+ "Manually locked"
+
+ %{manual_lock: true, is_over: false} ->
+ "Waiting for manual lock"
+
+ %{is_over: true} ->
+ "ended"
+
+ %{end_date: %Date{} = end_date} ->
+ days_left = Timex.diff(end_date, Timex.now(), :days)
+ "#{days_left} days left"
end
end
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 885dc8731a..ef28e5e877 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -131,12 +131,9 @@ defmodule Plausible.Billing do
trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today())
subscription_active = subscription_is_active?(user.subscription)
- grace_period_ended =
- user.grace_period && Timex.before?(user.grace_period.end_date, Timex.today())
-
cond do
trial_is_over && !subscription_active -> {true, :no_active_subscription}
- grace_period_ended -> {true, :grace_period_ended}
+ Plausible.Auth.GracePeriod.expired?(user) -> {true, :grace_period_ended}
true -> false
end
end
@@ -274,7 +271,8 @@ defmodule Plausible.Billing do
new_allowance = Plausible.Billing.Plans.allowance(user.subscription)
if new_allowance > allowance_required do
- Plausible.Auth.User.remove_grace_period(user)
+ user
+ |> Plausible.Auth.GracePeriod.remove_changeset()
|> Repo.update()
else
{:ok, user}
diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex
index d18859662e..5454e86230 100644
--- a/lib/plausible/billing/plans.ex
+++ b/lib/plausible/billing/plans.ex
@@ -26,6 +26,16 @@ defmodule Plausible.Billing.Plans do
}
]
+ @type plan() ::
+ %{
+ limit: non_neg_integer(),
+ monthly_product_id: String.t(),
+ yearly_product_id: String.t(),
+ monthly_cost: String.t(),
+ yearly_cost: String.t()
+ }
+ | :enterprise
+
def plans_for(user) do
user = Repo.preload(user, :subscription)
sandbox_plans = plans_sandbox()
@@ -109,15 +119,21 @@ defmodule Plausible.Billing.Plans do
end
end
- def get_enterprise_plan(subscription) do
+ defp get_enterprise_plan(%Plausible.Billing.Subscription{} = subscription) do
Repo.get_by(Plausible.Billing.EnterprisePlan,
user_id: subscription.user_id,
paddle_plan_id: subscription.paddle_plan_id
)
end
- def suggested_plan(user, usage) do
- Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end)
+ @enterprise_level_usage 20_000_000
+ @spec suggested_plan(Plausible.Auth.User.t(), non_neg_integer()) :: plan()
+ def suggested_plan(user, usage_during_cycle) do
+ cond do
+ usage_during_cycle > @enterprise_level_usage -> :enterprise
+ Plausible.Auth.enterprise?(user) -> :enterprise
+ true -> Enum.find(plans_for(user), fn plan -> usage_during_cycle < plan[:limit] end)
+ end
end
defp contains?(_plans, nil), do: false
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index f12f78297b..ee5755f2b1 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -8,7 +8,7 @@ defmodule Plausible.Billing.SiteLocker do
if !user.grace_period.is_over do
send_grace_period_end_email(user)
- Plausible.Auth.User.end_grace_period(user) |> Repo.update()
+ Plausible.Auth.GracePeriod.end_changeset(user) |> Repo.update()
end
{true, _} ->
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 2508babdbb..08568f929b 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -128,12 +128,12 @@ defmodule PlausibleWeb.Email do
})
end
- def enterprise_over_limit_email(user, usage, last_cycle, site_usage, site_allowance) do
+ def enterprise_over_limit_internal_email(user, usage, last_cycle, site_usage, site_allowance) do
base_email()
|> tag("enterprise-over-limit")
|> subject("#{user.email} has outgrown their enterprise plan")
- |> render("enterprise_over_limit.html", %{
+ |> render("enterprise_over_limit_internal.html", %{
user: user,
usage: usage,
last_cycle: last_cycle,
diff --git a/lib/plausible_web/templates/email/dashboard_locked.html.eex b/lib/plausible_web/templates/email/dashboard_locked.html.eex
index 031070c535..67b9d867a9 100644
--- a/lib/plausible_web/templates/email/dashboard_locked.html.eex
+++ b/lib/plausible_web/templates/email/dashboard_locked.html.eex
@@ -5,12 +5,12 @@ Last week we sent a reminder that your traffic has exceeded your Plausible Analy
Your dashboard is now locked. We're still counting your stats, but you no longer have access to the stats. As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.
<br /><br />
In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
-<%= if @usage <= 20_000_000 do %>
+<%= if @suggested_plan == :enterprise do %>
+This is more than our standard plans, so please reply back to this email to get a quote for your volume.
+<% else %>
Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
<br /><br />
<a href="https://plausible.io/settings">Click here</a> to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
-<% else %>
-This is more than our standard plans, so please reply back to this email to get a quote for your volume.
<% end %>
<br /><br />
The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire. We will unlock your dashboard immediately after your account has been upgraded to the appropriate tier.
diff --git a/lib/plausible_web/templates/email/enterprise_over_limit.html.eex b/lib/plausible_web/templates/email/enterprise_over_limit_internal.html.eex
similarity index 100%
rename from lib/plausible_web/templates/email/enterprise_over_limit.html.eex
rename to lib/plausible_web/templates/email/enterprise_over_limit_internal.html.eex
diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex
index 2653449845..f0ebea30d4 100644
--- a/lib/plausible_web/templates/email/over_limit.html.eex
+++ b/lib/plausible_web/templates/email/over_limit.html.eex
@@ -7,14 +7,14 @@ This is a notice that your traffic has exceeded your subscription tier two month
In order to keep your stats running, we require you to upgrade your account to accommodate your new traffic levels. If you do not upgrade your account within the next 7 days, we will lock your stats and they won't be accessible.
<br /><br />
In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
-<%= if @usage <= 20_000_000 do %>
+<%= if @suggested_plan == :enterprise do %>
+This is more than our standard plans, so please reply back to this email to get a quote for your volume.
+<% else %>
Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
<br /><br />
You can upgrade your subscription using our self-serve platform. The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire.
<br /><br />
<a href="https://plausible.io/settings">Click here</a> to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
-<% else %>
-This is more than our standard plans, so please reply back to this email to get a quote for your volume.
<% end %>
<br /><br />
Have questions or need help with anything? Just reply to this email and we'll gladly help.
diff --git a/lib/plausible_web/templates/layout/_notice.html.eex b/lib/plausible_web/templates/layout/_notice.html.eex
index 33035d90b1..7d2f8094d4 100644
--- a/lib/plausible_web/templates/layout/_notice.html.eex
+++ b/lib/plausible_web/templates/layout/_notice.html.eex
@@ -3,7 +3,7 @@
<% end %>
-<%= if on_grace_period?(@conn.assigns[:current_user]) do %>
+<%= 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">
@@ -14,11 +14,26 @@
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800">
- Please upgrade your account
+ <%= if Plausible.Auth.enterprise?(@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>
- 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 stats and they won't be accessible. <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %>
+ <%= if Plausible.Auth.enterprise?(@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 stats and they won't be accessible.
+ <%= link("Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800") %>
+ <% end %>
</p>
</div>
</div>
@@ -27,7 +42,7 @@
</div>
<% end %>
-<%= if grace_period_over?(@conn.assigns[:current_user]) do %>
+<%= 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">
@@ -42,7 +57,9 @@
</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") %>
+ 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>
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex
index bba14ea7b4..626f9c06ea 100644
--- a/lib/plausible_web/views/layout_view.ex
+++ b/lib/plausible_web/views/layout_view.ex
@@ -55,30 +55,16 @@ defmodule PlausibleWeb.LayoutView do
end
end
- def on_grace_period?(nil), do: false
-
- def on_grace_period?(user) do
- user.grace_period &&
- Timex.diff(user.grace_period.end_date, Timex.today(), :days) >= 0
- end
-
- def grace_period_over?(nil), do: false
-
- def grace_period_over?(user) do
- user.grace_period &&
- Timex.diff(user.grace_period.end_date, Timex.today(), :days) < 0
- end
-
- def grace_period_end(user) do
- end_date = user.grace_period.end_date
-
- case Timex.diff(end_date, Timex.today(), :days) do
+ def grace_period_end(%{grace_period: %{end_date: %Date{} = date}}) do
+ case Timex.diff(date, Timex.today(), :days) do
0 -> "today"
1 -> "tomorrow"
n -> "within #{n} days"
end
end
+ def grace_period_end(_user), do: "in the following days"
+
@doc "http://blog.plataformatec.com.br/2018/05/nested-layouts-with-phoenix/"
def render_layout(layout, assigns, do: content) do
render(layout, Map.put(assigns, :inner_layout, content))
diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex
index 64c3bcb43f..fbc2e05a73 100644
--- a/lib/workers/check_usage.ex
+++ b/lib/workers/check_usage.ex
@@ -71,7 +71,7 @@ defmodule Plausible.Workers.CheckUsage do
{{_, {last_cycle, last_cycle_usage}}, {_, {site_usage, site_allowance}}} ->
template =
- PlausibleWeb.Email.enterprise_over_limit_email(
+ PlausibleWeb.Email.enterprise_over_limit_internal_email(
subscriber,
last_cycle_usage,
last_cycle,
@@ -80,6 +80,10 @@ defmodule Plausible.Workers.CheckUsage do
)
Plausible.Mailer.send_email_safe(template)
+
+ subscriber
+ |> Plausible.Auth.GracePeriod.start_manual_lock_changeset(last_cycle_usage)
+ |> Repo.update()
end
end
@@ -97,7 +101,10 @@ defmodule Plausible.Workers.CheckUsage do
)
Plausible.Mailer.send_email_safe(template)
- Plausible.Auth.User.start_grace_period(subscriber, last_cycle_usage) |> Repo.update()
+
+ subscriber
+ |> Plausible.Auth.GracePeriod.start_changeset(last_cycle_usage)
+ |> Repo.update()
_ ->
nil
diff --git a/mix.exs b/mix.exs
index 0f35c1c6aa..e540c7e05c 100644
--- a/mix.exs
+++ b/mix.exs
@@ -135,7 +135,8 @@ defmodule Plausible.MixProject do
extras:
Path.wildcard("guides/**/*.md") ++
[
- "README.md": [filename: "readme", title: "Introduction"]
+ "README.md": [filename: "readme", title: "Introduction"],
+ "CONTRIBUTING.md": [filename: "contributing", title: "Contributing"]
],
groups_for_extras: [
Features: Path.wildcard("guides/features/*.md")
diff --git a/test/plausible/auth/auth_test.exs b/test/plausible/auth/auth_test.exs
index f578554c7b..ab9058bf85 100644
--- a/test/plausible/auth/auth_test.exs
+++ b/test/plausible/auth/auth_test.exs
@@ -36,4 +36,13 @@ defmodule Plausible.AuthTest do
refute Auth.has_active_sites?(user, [:owner])
end
end
+
+ test "enterprise?/1 returns whether the user has an enterprise plan" do
+ user_without_plan = insert(:user)
+ user_with_plan = insert(:user, enterprise_plan: build(:enterprise_plan))
+
+ assert Auth.enterprise?(user_with_plan)
+ refute Auth.enterprise?(user_without_plan)
+ refute Auth.enterprise?(nil)
+ end
end
diff --git a/test/plausible/auth/grace_period_test.exs b/test/plausible/auth/grace_period_test.exs
new file mode 100644
index 0000000000..fd53eb66ce
--- /dev/null
+++ b/test/plausible/auth/grace_period_test.exs
@@ -0,0 +1,105 @@
+defmodule Plausible.Auth.GracePeriodTest do
+ use Plausible.DataCase, async: true
+
+ test "active?/1 returns false when grace period cannot be telled" do
+ without_grace_period = build(:user, grace_period: nil)
+ refute Plausible.Auth.GracePeriod.active?(without_grace_period)
+
+ without_user = nil
+ refute Plausible.Auth.GracePeriod.active?(without_user)
+ end
+
+ test "active?/1 returns false when grace period is expired" do
+ yesterday = Date.add(Date.utc_today(), -1)
+
+ grace_period = %Plausible.Auth.GracePeriod{
+ end_date: yesterday,
+ allowance_required: 100,
+ is_over: false
+ }
+
+ user = build(:user, grace_period: grace_period)
+
+ refute Plausible.Auth.GracePeriod.active?(user)
+ end
+
+ test "active?/1 returns true when grace period is still active" do
+ tomorrow = Date.add(Date.utc_today(), 1)
+
+ grace_period = %Plausible.Auth.GracePeriod{
+ end_date: tomorrow,
+ allowance_required: 100,
+ is_over: false
+ }
+
+ user = build(:user, grace_period: grace_period)
+
+ assert Plausible.Auth.GracePeriod.active?(user)
+ end
+
+ test "expired?/1 returns false when grace period cannot be telled" do
+ without_grace_period = build(:user, grace_period: nil)
+ refute Plausible.Auth.GracePeriod.expired?(without_grace_period)
+
+ without_user = nil
+ refute Plausible.Auth.GracePeriod.expired?(without_user)
+ end
+
+ test "expired?/1 returns true when grace period is expired" do
+ yesterday = Date.add(Date.utc_today(), -1)
+
+ grace_period = %Plausible.Auth.GracePeriod{
+ end_date: yesterday,
+ allowance_required: 100,
+ is_over: true
+ }
+
+ user = build(:user, grace_period: grace_period)
+
+ assert Plausible.Auth.GracePeriod.expired?(user)
+ end
+
+ test "expired?/1 returns false when grace period is still active" do
+ tomorrow = Date.add(Date.utc_today(), 1)
+
+ grace_period = %Plausible.Auth.GracePeriod{
+ end_date: tomorrow,
+ allowance_required: 100,
+ is_over: false
+ }
+
+ user = build(:user, grace_period: grace_period)
+
+ refute Plausible.Auth.GracePeriod.expired?(user)
+ end
+
+ 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)
+ user = Ecto.Changeset.apply_changes(changeset)
+
+ assert Plausible.Auth.GracePeriod.active?(user)
+ refute Plausible.Auth.GracePeriod.expired?(user)
+ end
+
+ test "start_changeset/1 creates an active grace period" do
+ user = build(:user)
+ changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
+ user = Ecto.Changeset.apply_changes(changeset)
+
+ assert Plausible.Auth.GracePeriod.active?(user)
+ refute Plausible.Auth.GracePeriod.expired?(user)
+ end
+
+ test "remove_changeset/1 removes the active grace period" do
+ user = build(:user)
+ start_changeset = Plausible.Auth.GracePeriod.start_changeset(user, 1)
+ user = Ecto.Changeset.apply_changes(start_changeset)
+
+ remove_changeset = Plausible.Auth.GracePeriod.remove_changeset(user)
+ user = Ecto.Changeset.apply_changes(remove_changeset)
+
+ refute Plausible.Auth.GracePeriod.active?(user)
+ refute Plausible.Auth.GracePeriod.expired?(user)
+ end
+end
diff --git a/test/plausible/billing/plans_test.exs b/test/plausible/billing/plans_test.exs
index 619e3a90d9..be6c27ca36 100644
--- a/test/plausible/billing/plans_test.exs
+++ b/test/plausible/billing/plans_test.exs
@@ -82,4 +82,39 @@ defmodule Plausible.Billing.PlansTest do
assert Plans.subscription_interval(subscription) == :yearly
end
end
+
+ describe "suggested_plan/2" do
+ test "returns suggested plan based on usage" do
+ user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
+
+ assert %{
+ limit: 100_000,
+ monthly_cost: "$12",
+ monthly_product_id: "558745",
+ volume: "100k",
+ yearly_cost: "$96",
+ yearly_product_id: "590752"
+ } == Plans.suggested_plan(user, 10_000)
+
+ assert %{
+ limit: 200_000,
+ monthly_cost: "$18",
+ monthly_product_id: "597485",
+ volume: "200k",
+ yearly_cost: "$144",
+ yearly_product_id: "597486"
+ } == Plans.suggested_plan(user, 100_000)
+ end
+
+ test "returns nil when user has enterprise-level usage" do
+ user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
+ assert :enterprise == Plans.suggested_plan(user, 100_000_000)
+ end
+
+ test "returns nil when user is on an enterprise plan" do
+ user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
+ _enterprise_plan = insert(:enterprise_plan, user_id: user.id, billing_interval: :yearly)
+ assert :enterprise == Plans.suggested_plan(user, 10_000)
+ end
+ end
end
diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs
index 890ca2b67f..9b79ae6f39 100644
--- a/test/workers/check_usage_test.exs
+++ b/test/workers/check_usage_test.exs
@@ -98,7 +98,7 @@ defmodule Plausible.Workers.CheckUsageTest do
end
test "skips checking users who already have a grace period", %{user: user} do
- Plausible.Auth.User.start_grace_period(user, 12_000) |> Repo.update()
+ user |> Plausible.Auth.GracePeriod.start_changeset(12_000) |> Repo.update()
billing_stub =
Plausible.Billing
@@ -203,6 +203,26 @@ defmodule Plausible.Workers.CheckUsageTest do
subject: "#{user.email} has outgrown their enterprise plan"
)
end
+
+ test "starts grace period when plan is outgrown", %{user: user} do
+ billing_stub =
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {1_100_000, 1_100_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
+
+ enterprise_plan = insert(:enterprise_plan, user: user, monthly_pageview_limit: 1_000_000)
+
+ insert(:subscription,
+ user: user,
+ paddle_plan_id: enterprise_plan.paddle_plan_id,
+ last_bill_date: Timex.shift(Timex.today(), days: -1)
+ )
+
+ CheckUsage.perform(nil, billing_stub)
+ assert user |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()
+ end
end
describe "timing" do
diff --git a/test/workers/lock_sites_test.exs b/test/workers/lock_sites_test.exs
index cf1a17a520..e47603425f 100644
--- a/test/workers/lock_sites_test.exs
+++ b/test/workers/lock_sites_test.exs
@@ -2,6 +2,20 @@ defmodule Plausible.Workers.LockSitesTest do
use Plausible.DataCase, async: true
alias Plausible.Workers.LockSites
+ test "does not lock enterprise site on grace period" do
+ user =
+ :user
+ |> build()
+ |> Plausible.Auth.GracePeriod.start_manual_lock_changeset(100)
+ |> Plausible.Repo.insert!()
+
+ site = insert(:site, members: [user])
+
+ LockSites.perform(nil)
+
+ refute Repo.reload!(site).locked
+ end
+
test "does not lock trial user's site" do
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: 1))
site = insert(:site, members: [user])