2021-06-16 15:00:07 +03:00
|
|
|
defmodule Plausible.Workers.LockSitesTest do
|
2022-05-06 10:30:38 +03:00
|
|
|
use Plausible.DataCase, async: true
|
2023-10-10 20:35:17 +03:00
|
|
|
require Plausible.Billing.Subscription.Status
|
2021-06-16 15:00:07 +03:00
|
|
|
alias Plausible.Workers.LockSites
|
2023-10-10 20:35:17 +03:00
|
|
|
alias Plausible.Billing.Subscription
|
2021-06-16 15:00:07 +03:00
|
|
|
|
2022-09-20 11:46:28 +03:00
|
|
|
test "does not lock enterprise site on grace period" do
|
|
|
|
user =
|
|
|
|
:user
|
|
|
|
|> build()
|
2024-01-15 17:59:56 +03:00
|
|
|
|> Plausible.Auth.GracePeriod.start_manual_lock_changeset()
|
2022-09-20 11:46:28 +03:00
|
|
|
|> Plausible.Repo.insert!()
|
|
|
|
|
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
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])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "locks site for user whose trial has expired" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
|
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
assert Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not lock active subsriber's sites" do
|
|
|
|
user = insert(:user)
|
2023-10-10 20:35:17 +03:00
|
|
|
insert(:subscription, status: Subscription.Status.active(), user: user)
|
2021-06-16 15:00:07 +03:00
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not lock user who is past due" do
|
|
|
|
user = insert(:user)
|
2023-10-10 20:35:17 +03:00
|
|
|
insert(:subscription, status: Subscription.Status.past_due(), user: user)
|
2021-06-16 15:00:07 +03:00
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not lock user who cancelled subscription but it hasn't expired yet" do
|
|
|
|
user = insert(:user)
|
2023-10-10 20:35:17 +03:00
|
|
|
insert(:subscription, status: Subscription.Status.deleted(), user: user)
|
2021-06-16 15:00:07 +03:00
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "locks user who cancelled subscription and the cancelled subscription has expired" do
|
2021-06-18 11:13:54 +03:00
|
|
|
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
insert(:subscription,
|
2023-10-10 20:35:17 +03:00
|
|
|
status: Subscription.Status.deleted(),
|
2021-06-16 15:00:07 +03:00
|
|
|
next_bill_date: Timex.today() |> Timex.shift(days: -1),
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
|
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
2021-06-18 11:13:54 +03:00
|
|
|
assert Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not lock if user has an old cancelled subscription and a new active subscription" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
|
|
|
|
|
|
|
|
insert(:subscription,
|
2023-10-10 20:35:17 +03:00
|
|
|
status: Subscription.Status.deleted(),
|
2021-06-18 11:13:54 +03:00
|
|
|
next_bill_date: Timex.today() |> Timex.shift(days: -1),
|
|
|
|
user: user,
|
|
|
|
inserted_at: Timex.now() |> Timex.shift(days: -1)
|
|
|
|
)
|
|
|
|
|
2023-10-10 20:35:17 +03:00
|
|
|
insert(:subscription, status: Subscription.Status.active(), user: user)
|
2021-06-18 11:13:54 +03:00
|
|
|
|
|
|
|
site = insert(:site, members: [user])
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
refute Repo.reload!(site).locked
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "locking" do
|
|
|
|
test "only locks sites that the user owns" do
|
|
|
|
user = insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
|
|
|
|
|
|
|
|
owner_site =
|
|
|
|
insert(:site,
|
|
|
|
memberships: [
|
|
|
|
build(:site_membership, user: user, role: :owner)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
viewer_site =
|
|
|
|
insert(:site,
|
|
|
|
memberships: [
|
|
|
|
build(:site_membership, user: user, role: :viewer)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
LockSites.perform(nil)
|
|
|
|
|
|
|
|
owner_site = Repo.reload!(owner_site)
|
|
|
|
viewer_site = Repo.reload!(viewer_site)
|
|
|
|
|
|
|
|
assert owner_site.locked
|
|
|
|
refute viewer_site.locked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|