Skip to content

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

Switch from `Site.locked` to `Team.locked` (#5304)

Adrian Gruntkowskiec4c6c453a53
diff --git a/extra/lib/plausible/help_scout.ex b/extra/lib/plausible/help_scout.ex
index f0e7b7db5a..c6a1405e55 100644
--- a/extra/lib/plausible/help_scout.ex
+++ b/extra/lib/plausible/help_scout.ex
@@ -222,7 +222,7 @@ defmodule Plausible.HelpScout do
subscription.status == Subscription.Status.paused() ->
"Paused"
- Teams.owned_sites_locked?(team) ->
+ Teams.locked?(team) ->
"Dashboard locked"
subscription_active? ->
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index a8d4ebfc3c..c064087ead 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -233,7 +233,7 @@ defmodule Plausible.Billing do
|> Plausible.Teams.update_accept_traffic_until()
|> Plausible.Teams.remove_grace_period()
|> Plausible.Teams.maybe_reset_next_upgrade_override()
- |> tap(&Plausible.Billing.SiteLocker.update_sites_for/1)
+ |> tap(&Plausible.Billing.SiteLocker.update_for/1)
|> maybe_adjust_api_key_limits()
end
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index cb4c770b10..611665d585 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -11,9 +11,9 @@ defmodule Plausible.Billing.SiteLocker do
| :no_trial
| :no_active_subscription
- @spec update_sites_for(Teams.Team.t(), [update_opt()]) ::
+ @spec update_for(Teams.Team.t(), [update_opt()]) ::
{:locked, lock_reason()} | :unlocked
- def update_sites_for(team, opts \\ []) do
+ def update_for(team, opts \\ []) do
send_email? = Keyword.get(opts, :send_email?, true)
usage_mod = Keyword.get(opts, :usage_mod, Teams.Billing)
@@ -26,10 +26,7 @@ defmodule Plausible.Billing.SiteLocker do
if team.grace_period.is_over != true do
Plausible.Teams.end_grace_period(team)
- if send_email? do
- team = Repo.preload(team, [:owners, :billing_members])
- send_grace_period_end_email(team)
- end
+ send_grace_period_end_email(team, send_email?)
{:locked, :grace_period_ended_now}
else
@@ -48,16 +45,6 @@ defmodule Plausible.Billing.SiteLocker do
@spec set_lock_status_for(Teams.Team.t(), boolean()) :: :ok
def set_lock_status_for(team, status) do
- site_ids = Teams.owned_sites_ids(team)
-
- site_q =
- from(
- s in Plausible.Site,
- where: s.id in ^site_ids
- )
-
- {_, _} = Repo.update_all(site_q, set: [locked: status])
-
query = from(t in Teams.Team, where: t.id == ^team.id)
{_, _} = Repo.update_all(query, set: [locked: status])
@@ -65,7 +52,8 @@ defmodule Plausible.Billing.SiteLocker do
:ok
end
- defp send_grace_period_end_email(team) do
+ defp send_grace_period_end_email(team, true) do
+ team = Repo.preload(team, [:owners, :billing_members])
usage = Teams.Billing.monthly_pageview_usage(team)
suggested_plan = Plausible.Billing.Plans.suggest(team, usage.last_cycle.total)
@@ -75,4 +63,6 @@ defmodule Plausible.Billing.SiteLocker do
|> Plausible.Mailer.send()
end
end
+
+ defp send_grace_period_end_email(_team, false), do: :ok
end
diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex
index 375a9d773a..ce812e2007 100644
--- a/lib/plausible/site.ex
+++ b/lib/plausible/site.ex
@@ -14,7 +14,6 @@ defmodule Plausible.Site do
field :domain, :string
field :timezone, :string, default: "Etc/UTC"
field :public, :boolean
- field :locked, :boolean
field :stats_start_date, :date
field :native_stats_start_at, :naive_datetime
field :allowed_event_props, {:array, :string}
diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex
index a2349bcb92..9565bbc313 100644
--- a/lib/plausible/sites.ex
+++ b/lib/plausible/sites.ex
@@ -367,10 +367,6 @@ defmodule Plausible.Sites do
)
end
- def locked?(%Site{locked: locked}) do
- locked
- end
-
def get_for_user!(user, domain, roles \\ [:owner, :admin, :editor, :viewer]) do
site =
if :super_admin in roles and Plausible.Auth.is_super_admin?(user.id) do
diff --git a/lib/plausible/teams.ex b/lib/plausible/teams.ex
index 8b7dd1dcd2..e1a57b3be5 100644
--- a/lib/plausible/teams.ex
+++ b/lib/plausible/teams.ex
@@ -102,19 +102,6 @@ defmodule Plausible.Teams do
)
end
- def owned_sites_locked?(nil) do
- false
- end
-
- def owned_sites_locked?(team) do
- Repo.exists?(
- from(s in Plausible.Site,
- where: s.team_id == ^team.id,
- where: s.locked == true
- )
- )
- end
-
def owned_sites_count(nil), do: 0
def owned_sites_count(team) do
diff --git a/lib/plausible/teams/invitations.ex b/lib/plausible/teams/invitations.ex
index f382fec00f..85538b901d 100644
--- a/lib/plausible/teams/invitations.ex
+++ b/lib/plausible/teams/invitations.ex
@@ -410,7 +410,8 @@ defmodule Plausible.Teams.Invitations do
end
on_ee do
- :unlocked = Billing.SiteLocker.update_sites_for(team, send_email?: false)
+ Billing.SiteLocker.update_for(prior_team, send_email?: false)
+ :unlocked = Billing.SiteLocker.update_for(team, send_email?: false)
end
:ok
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 121e2587a0..140d867ecb 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -46,6 +46,7 @@ defmodule PlausibleWeb.StatsController do
alias Plausible.Sites
alias Plausible.Stats.{Filters, Query}
+ alias Plausible.Teams
alias PlausibleWeb.Api
plug(PlausibleWeb.Plugs.AuthorizeSiteAccess when action in [:stats, :csv_export])
@@ -55,7 +56,7 @@ defmodule PlausibleWeb.StatsController do
site_role = conn.assigns[:site_role]
current_user = conn.assigns[:current_user]
stats_start_date = Plausible.Sites.stats_start_date(site)
- can_see_stats? = not Sites.locked?(site) or site_role == :super_admin
+ can_see_stats? = not Teams.locked?(site.team) or site_role == :super_admin
demo = site.domain == PlausibleWeb.Endpoint.host()
dogfood_page_path = if demo, do: "/#{site.domain}", else: "/:dashboard"
skip_to_dashboard? = conn.params["skip_to_dashboard"] == "true"
@@ -90,7 +91,7 @@ defmodule PlausibleWeb.StatsController do
!stats_start_date && can_see_stats? ->
redirect(conn, external: Routes.site_path(conn, :verification, site.domain))
- Sites.locked?(site) ->
+ Teams.locked?(site.team) ->
site = Plausible.Repo.preload(site, :owners)
render(conn, "site_locked.html", site: site, dogfood_page_path: dogfood_page_path)
end
@@ -295,10 +296,11 @@ defmodule PlausibleWeb.StatsController do
link_query =
from(link in Plausible.Site.SharedLink,
inner_join: site in assoc(link, :site),
+ inner_join: team in assoc(site, :team),
where: link.slug == ^auth,
where: site.domain == ^domain,
limit: 1,
- preload: [site: site]
+ preload: [site: {site, team: team}]
)
case Repo.one(link_query) do
@@ -340,7 +342,7 @@ defmodule PlausibleWeb.StatsController do
defp render_shared_link(conn, shared_link) do
cond do
- !shared_link.site.locked ->
+ not Teams.locked?(shared_link.site.team) ->
current_user = conn.assigns[:current_user]
site_role = get_fallback_site_role(conn)
shared_link = Plausible.Repo.preload(shared_link, site: :owners)
@@ -378,7 +380,7 @@ defmodule PlausibleWeb.StatsController do
hide_footer?: if(ce?(), do: embedded?, else: embedded? || site_role != :public)
)
- Sites.locked?(shared_link.site) ->
+ Teams.locked?(shared_link.site.team) ->
owners = Plausible.Repo.preload(shared_link.site, :owners)
render(conn, "site_locked.html",
diff --git a/lib/plausible_web/plugs/authorize_public_api.ex b/lib/plausible_web/plugs/authorize_public_api.ex
index 1d5b4f9e15..5bbdfb2874 100644
--- a/lib/plausible_web/plugs/authorize_public_api.ex
+++ b/lib/plausible_web/plugs/authorize_public_api.ex
@@ -31,7 +31,7 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPI do
alias Plausible.Auth
alias Plausible.RateLimit
- alias Plausible.Sites
+ alias Plausible.Teams
alias PlausibleWeb.Api.Helpers, as: H
require Logger
@@ -201,7 +201,7 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPI do
api_key.team_id && api_key.team_id != site.team_id ->
{:error, :invalid_api_key}
- Sites.locked?(site) ->
+ Teams.locked?(team) ->
{:error, :site_locked}
Plausible.Billing.Feature.StatsAPI.check_availability(team) !== :ok ->
diff --git a/lib/plausible_web/templates/stats/stats.html.heex b/lib/plausible_web/templates/stats/stats.html.heex
index b1537e19f3..2c81f41c63 100644
--- a/lib/plausible_web/templates/stats/stats.html.heex
+++ b/lib/plausible_web/templates/stats/stats.html.heex
@@ -1,7 +1,7 @@
<div class={stats_container_class(@conn)} data-site-domain={@site.domain}>
<PlausibleWeb.Components.FirstDashboardLaunchBanner.render site={@site} />
- <%= if @site.locked do %>
+ <%= if Plausible.Teams.locked?(@site.team) do %>
<div
class="w-full px-4 py-4 text-sm font-bold text-center text-yellow-800 bg-yellow-100 rounded transition"
style="top: 91px"
diff --git a/lib/workers/lock_sites.ex b/lib/workers/lock_sites.ex
index 2b4babab9e..6221019923 100644
--- a/lib/workers/lock_sites.ex
+++ b/lib/workers/lock_sites.ex
@@ -16,7 +16,7 @@ defmodule Plausible.Workers.LockSites do
)
for team <- teams do
- Plausible.Billing.SiteLocker.update_sites_for(team)
+ Plausible.Billing.SiteLocker.update_for(team)
end
:ok
diff --git a/lib/workers/schedule_email_reports.ex b/lib/workers/schedule_email_reports.ex
index 8fb450cde0..1551618741 100644
--- a/lib/workers/schedule_email_reports.ex
+++ b/lib/workers/schedule_email_reports.ex
@@ -27,6 +27,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
sites =
Repo.all(
from s in Plausible.Site,
+ inner_join: t in assoc(s, :team),
join: wr in Plausible.Site.WeeklyReport,
on: wr.site_id == s.id,
left_join: job in subquery(weekly_jobs),
@@ -34,7 +35,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
fragment("(? -> 'site_id')::int", job.args) == s.id and
job.state not in ["completed", "discarded"],
where: is_nil(job),
- where: not s.locked,
+ where: not t.locked,
preload: [weekly_report: wr]
)
@@ -67,6 +68,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
sites =
Repo.all(
from s in Plausible.Site,
+ inner_join: t in assoc(s, :team),
join: mr in Plausible.Site.MonthlyReport,
on: mr.site_id == s.id,
left_join: job in subquery(monthly_jobs),
@@ -74,7 +76,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
fragment("(? -> 'site_id')::int", job.args) == s.id and
job.state not in ["completed", "discarded"],
where: is_nil(job),
- where: not s.locked,
+ where: not t.locked,
preload: [monthly_report: mr]
)
diff --git a/lib/workers/traffic_change_notifier.ex b/lib/workers/traffic_change_notifier.ex
index b3c2458663..02f93d18a1 100644
--- a/lib/workers/traffic_change_notifier.ex
+++ b/lib/workers/traffic_change_notifier.ex
@@ -23,7 +23,7 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
sn.last_sent < ^NaiveDateTime.add(now, -@min_interval_hours, :hour),
inner_join: s in assoc(sn, :site),
inner_join: t in assoc(s, :team),
- where: not s.locked,
+ where: not t.locked,
where: is_nil(t.accept_traffic_until) or t.accept_traffic_until > ^today,
preload: [site: {s, team: t}]
)
diff --git a/mix.exs b/mix.exs
index a33c4a1375..fe53ba9045 100644
--- a/mix.exs
+++ b/mix.exs
@@ -76,7 +76,7 @@ defmodule Plausible.MixProject do
{:cors_plug, "~> 3.0"},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
- {:double, "~> 0.8.0", only: [:test, :ce_test]},
+ {:double, "~> 0.8.0", only: [:dev, :test, :ce_test]},
{:ecto, "~> 3.12.0"},
{:ecto_sql, "~> 3.12.0"},
{:envy, "~> 1.1.1"},
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index 90061a7bd0..fd8377d343 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -5,6 +5,7 @@ defmodule Plausible.BillingTest do
require Plausible.Billing.Subscription.Status
alias Plausible.Billing
alias Plausible.Billing.Subscription
+ alias Plausible.Repo
describe "check_needs_to_upgrade" do
@describetag :ee_only
@@ -245,13 +246,14 @@ defmodule Plausible.BillingTest do
test "unlocks sites if user has any locked sites" do
user = new_user()
- site = new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
team = team_of(user)
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id};team:#{team.id}"}
|> Billing.subscription_created()
- refute Repo.reload!(site).locked
+ refute Repo.reload!(team).locked
end
@tag :ee_only
@@ -357,7 +359,8 @@ defmodule Plausible.BillingTest do
test "unlocks sites if subscription is changed from past_due to active" do
user = new_user()
subscribe_to_growth_plan(user, status: Subscription.Status.past_due())
- site = new_site(locked: true, owner: user)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
team = team_of(user)
@subscription_updated_params
@@ -368,7 +371,7 @@ defmodule Plausible.BillingTest do
})
|> Billing.subscription_updated()
- refute Repo.reload!(site).locked
+ refute Repo.reload!(team).locked
end
@tag :ee_only
@@ -441,7 +444,8 @@ defmodule Plausible.BillingTest do
team = team_of(user)
- site = new_site(locked: true, owner: user)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
subscription_id = subscription_of(user).paddle_subscription_id
@@ -453,8 +457,8 @@ defmodule Plausible.BillingTest do
})
|> Billing.subscription_updated()
- assert Repo.reload!(site).locked == false
- assert Repo.reload!(team_of(user)).grace_period == nil
+ assert Repo.reload!(team).locked == false
+ assert Repo.reload!(team).grace_period == nil
end
test "ignores if subscription cannot be found" do
@@ -618,30 +622,4 @@ defmodule Plausible.BillingTest do
refute Plausible.Teams.Billing.has_active_subscription?(paused_team)
refute Plausible.Teams.Billing.has_active_subscription?(nil)
end
-
- def monthly_pageview_usage_stub(penultimate_usage, last_usage) do
- last_bill_date = Date.utc_today() |> Date.shift(day: -1)
-
- Plausible.Teams.Billing
- |> Double.stub(:monthly_pageview_usage, fn _user ->
- %{
- last_cycle: %{
- date_range:
- Date.range(
- Date.shift(last_bill_date, month: -1),
- Date.shift(last_bill_date, day: -1)
- ),
- total: last_usage
- },
- penultimate_cycle: %{
- date_range:
- Date.range(
- Date.shift(last_bill_date, month: -2),
- Date.shift(last_bill_date, day: -1, month: -1)
- ),
- total: penultimate_usage
- }
- }
- end)
- end
end
diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exs
index 0d65f50f0b..4a80422d49 100644
--- a/test/plausible/billing/site_locker_test.exs
+++ b/test/plausible/billing/site_locker_test.exs
@@ -9,15 +9,16 @@ defmodule Plausible.Billing.SiteLockerTest do
@v4_growth_plan_id "857097"
- describe "update_sites_for/1" do
+ describe "update_for/1" do
test "does not lock sites if user is on trial" do
user = new_user(trial_expiry_date: Date.utc_today())
- site = new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock if user has an active subscription" do
@@ -25,9 +26,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock user who is past due" do
@@ -35,9 +36,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock user who cancelled subscription but it hasn't expired yet" do
@@ -45,9 +46,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock team which has an active subscription and is on grace period" do
@@ -60,9 +61,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock enterprise customers with (manual lock) graceperiod ended" do
@@ -78,9 +79,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "locks user who cancelled subscription and the cancelled subscription has expired" do
@@ -94,9 +95,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
+ assert SiteLocker.update_for(team) == {:locked, :no_active_trial_or_subscription}
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "locks all sites if team has an active subscription but grace period has ended (still over limits)" do
@@ -106,12 +107,12 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+ over_limits_usage_stub = monthly_pageview_usage_stub(15_000, 15_000)
- assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ assert SiteLocker.update_for(team, usage_mod: over_limits_usage_stub) ==
{:locked, :grace_period_ended_now}
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "does not lock sites (and removes grace period), when on active subscription and grace period ended, but usage now within limits" do
@@ -121,9 +122,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
assert_no_emails_delivered()
end
@@ -138,9 +139,9 @@ defmodule Plausible.Billing.SiteLockerTest do
billing_member = new_user()
add_member(team, user: billing_member, role: :billing)
- over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+ over_limits_usage_stub = monthly_pageview_usage_stub(15_000, 15_000)
- assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ assert SiteLocker.update_for(team, usage_mod: over_limits_usage_stub) ==
{:locked, :grace_period_ended_now}
assert_email_delivered_with(
@@ -152,6 +153,8 @@ defmodule Plausible.Billing.SiteLockerTest do
to: [billing_member],
subject: "[Action required] Your Plausible dashboard is now locked"
)
+
+ assert Repo.reload!(team).locked
end
test "does not send grace period email if site is already locked" do
@@ -166,9 +169,9 @@ defmodule Plausible.Billing.SiteLockerTest do
new_site(owner: user)
team = team_of(user)
- over_limits_usage_stub = Plausible.BillingTest.monthly_pageview_usage_stub(15_000, 15_000)
+ over_limits_usage_stub = monthly_pageview_usage_stub(15_000, 15_000)
- assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ assert SiteLocker.update_for(team, usage_mod: over_limits_usage_stub) ==
{:locked, :grace_period_ended_now}
assert_email_delivered_with(
@@ -178,10 +181,14 @@ defmodule Plausible.Billing.SiteLockerTest do
team = Repo.reload!(team)
- assert SiteLocker.update_sites_for(team, usage_mod: over_limits_usage_stub) ==
+ assert team.locked
+
+ assert SiteLocker.update_for(team, usage_mod: over_limits_usage_stub) ==
{:locked, :grace_period_ended_already}
assert_no_emails_delivered()
+
+ assert Repo.reload!(team).locked
end
test "unlocks already ended grace periods when they still have an active subscription and went within limits again" do
@@ -197,9 +204,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == :unlocked
+ assert SiteLocker.update_for(team) == :unlocked
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "locks all sites if user has no trial or active subscription" do
@@ -207,9 +214,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
+ assert SiteLocker.update_for(team) == {:locked, :no_active_trial_or_subscription}
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "locks sites for user with empty trial - shouldn't happen under normal circumstances" do
@@ -217,9 +224,9 @@ defmodule Plausible.Billing.SiteLockerTest do
site = new_site(owner: user)
team = user |> team_of() |> Ecto.Changeset.change(trial_expiry_date: nil) |> Repo.update!()
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
+ assert SiteLocker.update_for(team) == {:locked, :no_active_trial_or_subscription}
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "only locks sites that the user owns" do
@@ -230,13 +237,10 @@ defmodule Plausible.Billing.SiteLockerTest do
add_guest(viewer_site, user: user, role: :viewer)
team = team_of(user)
- assert SiteLocker.update_sites_for(team) == {:locked, :no_active_trial_or_subscription}
-
- owner_site = Repo.reload!(owner_site)
- viewer_site = Repo.reload!(viewer_site)
+ assert SiteLocker.update_for(team) == {:locked, :no_active_trial_or_subscription}
- assert owner_site.locked
- refute viewer_site.locked
+ assert Repo.reload!(owner_site.team).locked
+ refute Repo.reload!(viewer_site.team).locked
end
end
end
diff --git a/test/plausible/help_scout_test.exs b/test/plausible/help_scout_test.exs
index b252b05d67..2c5ecba1c0 100644
--- a/test/plausible/help_scout_test.exs
+++ b/test/plausible/help_scout_test.exs
@@ -264,7 +264,8 @@ defmodule Plausible.HelpScoutTest do
test "returns for user with locked site" do
user = %{email: email} = new_user(trial_expiry_date: Date.add(Date.utc_today(), -1))
- new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
subscribe_to_plan(user, @v4_business_monthly_plan_id)
stub_help_scout_requests(email)
diff --git a/test/plausible/site/memberships/accept_invitation_test.exs b/test/plausible/site/memberships/accept_invitation_test.exs
index 76dafdb3d3..90c47084b4 100644
--- a/test/plausible/site/memberships/accept_invitation_test.exs
+++ b/test/plausible/site/memberships/accept_invitation_test.exs
@@ -492,8 +492,12 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
@tag :ee_only
test "unlocks a previously locked site after transfer" do
existing_owner = new_user()
- site = new_site(owner: existing_owner, locked: true)
+ site = new_site(owner: existing_owner)
+ old_team = site.team
+ old_team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
new_owner = new_user() |> subscribe_to_growth_plan()
+ new_team = team_of(new_owner)
+ new_team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
transfer = invite_transfer(site, new_owner, inviter: existing_owner)
@@ -504,7 +508,8 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
)
refute Repo.reload(transfer)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(old_team).locked
+ refute Repo.reload!(new_team).locked
end
for role <- [:viewer, :editor] do
diff --git a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
index 2783fee452..e02567990f 100644
--- a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
+++ b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs
@@ -2,6 +2,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
use PlausibleWeb.ConnCase
use Plausible.Teams.Test
+ alias Plausible.Repo
+
setup [:create_user, :create_api_key]
test "unauthenticated request - returns 401", %{conn: conn} do
@@ -47,7 +49,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
end
test "locked site - returns 402", %{conn: conn, api_key: api_key, user: user} do
- site = new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
conn
|> with_api_key(api_key)
@@ -88,7 +91,8 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
api_key: api_key,
user: user
} do
- site = new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
conn
|> with_api_key(api_key)
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index 35fecd487b..d956f4699d 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -169,7 +169,8 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "shows locked page if site is locked", %{conn: conn, user: user} do
- locked_site = new_site(locked: true, owner: user)
+ locked_site = new_site(owner: user)
+ locked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
conn = get(conn, "/" <> locked_site.domain)
resp = html_response(conn, 200)
assert resp =~ "Dashboard locked"
@@ -178,7 +179,8 @@ defmodule PlausibleWeb.StatsControllerTest do
test "shows locked page if site is locked for billing role", %{conn: conn, user: user} do
other_user = new_user()
- locked_site = new_site(locked: true, owner: other_user)
+ locked_site = new_site(owner: other_user)
+ locked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
add_member(team_of(other_user), user: user, role: :billing)
conn = get(conn, "/" <> locked_site.domain)
@@ -189,7 +191,8 @@ defmodule PlausibleWeb.StatsControllerTest do
test "shows locked page if site is locked for viewer role", %{conn: conn, user: user} do
other_user = new_user()
- locked_site = new_site(locked: true, owner: other_user)
+ locked_site = new_site(owner: other_user)
+ locked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
add_member(team_of(other_user), user: user, role: :viewer)
conn = get(conn, "/" <> locked_site.domain)
@@ -200,7 +203,8 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "shows locked page for anonymous" do
- locked_site = new_site(locked: true, public: true)
+ locked_site = new_site(public: true)
+ locked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
conn = get(build_conn(), "/" <> locked_site.domain)
resp = html_response(conn, 200)
assert resp =~ "Dashboard locked"
@@ -274,7 +278,8 @@ defmodule PlausibleWeb.StatsControllerTest do
test "can view a private locked dashboard with stats", %{conn: conn} do
user = new_user()
- site = new_site(locked: true, owner: user)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
populate_stats(site, [build(:pageview)])
conn = get(conn, "/" <> site.domain)
@@ -287,14 +292,16 @@ defmodule PlausibleWeb.StatsControllerTest do
test "can view private locked verification without stats", %{conn: conn} do
user = new_user()
- site = new_site(locked: true, owner: user)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
conn = get(conn, conn |> get("/#{site.domain}") |> redirected_to())
assert html_response(conn, 200) =~ "Verifying your installation"
end
test "can view a locked public dashboard", %{conn: conn} do
- site = new_site(locked: true, public: true)
+ site = new_site(public: true)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
populate_stats(site, [build(:pageview)])
conn = get(conn, "/" <> site.domain)
@@ -1237,7 +1244,7 @@ defmodule PlausibleWeb.StatsControllerTest do
describe "GET /share/:domain?auth=:auth" do
test "prompts a password for a password-protected link", %{conn: conn} do
- site = insert(:site)
+ site = new_site()
link =
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
@@ -1318,7 +1325,8 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "shows locked page if page is locked", %{conn: conn} do
- site = insert(:site, domain: "test-site.com", locked: true)
+ site = new_site(domain: "test-site.com")
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
link = insert(:shared_link, site: site)
conn = get(conn, "/share/test-site.com/?auth=#{link.slug}")
@@ -1423,8 +1431,8 @@ defmodule PlausibleWeb.StatsControllerTest do
end
test "only gives access to the correct dashboard", %{conn: conn} do
- site = insert(:site, domain: "test-site.com")
- site2 = insert(:site, domain: "test-site2.com")
+ site = new_site(domain: "test-site.com")
+ site2 = new_site(domain: "test-site2.com")
link =
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
diff --git a/test/plausible_web/plugs/authorize_public_api_test.exs b/test/plausible_web/plugs/authorize_public_api_test.exs
index aaf41853fb..4847668e1a 100644
--- a/test/plausible_web/plugs/authorize_public_api_test.exs
+++ b/test/plausible_web/plugs/authorize_public_api_test.exs
@@ -5,6 +5,7 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest do
import ExUnit.CaptureLog
alias PlausibleWeb.Plugs.AuthorizePublicAPI
+ alias Plausible.Repo
setup %{conn: conn} do
conn =
@@ -109,7 +110,8 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest do
test "halts with error when site is locked", %{conn: conn} do
user = new_user()
- site = new_site(owner: user, locked: true)
+ site = new_site(owner: user)
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
api_key = insert(:api_key, user: user)
conn =
@@ -332,7 +334,8 @@ defmodule PlausibleWeb.Plugs.AuthorizePublicAPITest do
test "passes for super admin user even if not a member of the requested site", %{conn: conn} do
user = new_user()
patch_env(:super_admin_user_ids, [user.id])
- site = new_site(locked: true)
+ site = new_site()
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
api_key = insert(:api_key, user: user)
conn =
diff --git a/test/support/test_utils.ex b/test/support/test_utils.ex
index 04104242e0..cc004235d4 100644
--- a/test/support/test_utils.ex
+++ b/test/support/test_utils.ex
@@ -302,6 +302,32 @@ defmodule Plausible.TestUtils do
end
end
+ def monthly_pageview_usage_stub(penultimate_usage, last_usage) do
+ last_bill_date = Date.utc_today() |> Date.shift(day: -1)
+
+ Plausible.Teams.Billing
+ |> Double.stub(:monthly_pageview_usage, fn _user ->
+ %{
+ last_cycle: %{
+ date_range:
+ Date.range(
+ Date.shift(last_bill_date, month: -1),
+ Date.shift(last_bill_date, day: -1)
+ ),
+ total: last_usage
+ },
+ penultimate_cycle: %{
+ date_range:
+ Date.range(
+ Date.shift(last_bill_date, month: -2),
+ Date.shift(last_bill_date, day: -1, month: -1)
+ ),
+ total: penultimate_usage
+ }
+ }
+ end)
+ end
+
defp secret_key_base() do
:plausible
|> Application.fetch_env!(PlausibleWeb.Endpoint)
diff --git a/test/workers/lock_sites_test.exs b/test/workers/lock_sites_test.exs
index 5538070eb9..e9e809dac9 100644
--- a/test/workers/lock_sites_test.exs
+++ b/test/workers/lock_sites_test.exs
@@ -17,7 +17,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock trial user's site" do
@@ -26,7 +26,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "locks site for user whose trial has expired" do
@@ -35,7 +35,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "does not lock active subsriber's sites" do
@@ -44,7 +44,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock user who is past due" do
@@ -53,7 +53,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "does not lock user who cancelled subscription but it hasn't expired yet" do
@@ -62,7 +62,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
test "locks user who cancelled subscription and the cancelled subscription has expired" do
@@ -77,7 +77,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- assert Repo.reload!(site).locked
+ assert Repo.reload!(site.team).locked
end
test "does not lock if user has an old cancelled subscription and a new active subscription" do
@@ -94,7 +94,7 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- refute Repo.reload!(site).locked
+ refute Repo.reload!(site.team).locked
end
describe "locking" do
@@ -107,11 +107,8 @@ defmodule Plausible.Workers.LockSitesTest do
LockSites.perform(nil)
- owner_site = Repo.reload!(owner_site)
- viewer_site = Repo.reload!(viewer_site)
-
- assert owner_site.locked
- refute viewer_site.locked
+ assert Repo.reload!(owner_site.team).locked
+ refute Repo.reload!(viewer_site.team).locked
end
end
end
diff --git a/test/workers/schedule_email_reports_test.exs b/test/workers/schedule_email_reports_test.exs
index 489b29f700..211611b860 100644
--- a/test/workers/schedule_email_reports_test.exs
+++ b/test/workers/schedule_email_reports_test.exs
@@ -1,11 +1,12 @@
defmodule Plausible.Workers.ScheduleEmailReportsTest do
use Plausible.DataCase
use Oban.Testing, repo: Plausible.Repo
+ import Plausible.Teams.Test
alias Plausible.Workers.{ScheduleEmailReports, SendEmailReport}
describe "weekly reports" do
test "schedules weekly report on Monday 9am local timezone" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -18,7 +19,7 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "does not schedule more than one weekly report at a time" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -28,7 +29,8 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "does not schedule a weekly report for locked site" do
- site = insert(:site, locked: true, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
insert(:weekly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -37,7 +39,7 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "schedules a new report as soon as a previous one is completed" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -50,7 +52,7 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
describe "monthly_reports" do
test "schedules monthly report on first of the next month at 9am local timezone" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:monthly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -63,7 +65,7 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "does not schedule more than one monthly report at a time" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:monthly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -73,7 +75,8 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "does not schedule a monthly report for locked site" do
- site = insert(:site, locked: true, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
insert(:monthly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
@@ -82,7 +85,7 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
end
test "schedules a new report as soon as a previous one is completed" do
- site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
+ site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:monthly_report, site: site, recipients: ["[email protected]"])
perform_job(ScheduleEmailReports, %{})
diff --git a/test/workers/traffic_change_notifier_test.exs b/test/workers/traffic_change_notifier_test.exs
index 1a66f26c40..e602bfa00b 100644
--- a/test/workers/traffic_change_notifier_test.exs
+++ b/test/workers/traffic_change_notifier_test.exs
@@ -269,7 +269,8 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
end
test "does not check site if it is locked" do
- site = insert(:site, locked: true)
+ site = new_site()
+ site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()
insert(:spike_notification,
site: site,