analytics/test/workers/accept_traffic_until_test.exs
Adrian Gruntkowski 729a32e610
Teams writes switch (#4883)
* Comment out legacy fields and relationships

* WIP

* WIP 2

* WIP 3

* wip

* Remove teams backfill and consistency check scripts

* WIP 3

* Fix CheckUsage tests

* Update billing/subscription tests

* WIP 4

* Make site transfer fail if some invitation already exists

* Fixup: do symmetric invitation/site transfer check

* Update UI bugs: make listing sites/inviting admins work like before

* Fix Sites test

* Fix external sites controller test

* Fix live sites tests

* Fix props availability lookup

* Fix site controller tests

* Fix billing controller tests

* WIP - accept invitation tests

* Another round of test fixes + invitations logic bugs

* users_test -> teams_test

* Update registration via invitation

Here, we still rely on "polymorphic" invitation structures,
hence the "unified by id" helper.

For now, it'll remain local unless we discover it's
needed in the broader `Teams.Invitations` context.

cc @zoldar

* Yet another round of test and bugfixes along the way

* Include team in site setup success e-mail

* Fix send_site_setup_emails worker

* Fixed almost all tests except CRM ones

* Update enterprise plan admin test

* Fix CRM + remaining tests

* Address credo warnings (modulo one FIXME)

* Remove last FIXME and rephrase the invitation test case description

* Set Team fields via User CRM transparently

* Map user reference in Enterprise Plan CRM via team owner

* Fix resource actions in user CRM

* Get rid of warning when opening create form in API keys CRM

* Stop emitting warnings when editing Enterprise Plans via CRM

* Tests: Bump await_clickhouse_count interval

* Remove XXX marker

* Fix register from invitation link in email sent for ownership transfer

* Simplify fetching all pending site ownership site IDs

* Remove commented out schema fields

* Remove unused functions

* Address flakiness in ingest counter tests

* Remove unused `Teams.Sites.create`

* Don't restart trial on team with subscription when creating site

* Account for cases of legacy teams with empty trial expiry date

* Revert "Address flakiness in ingest counter tests"

This reverts commit 60dc1e4115.

* Fix flaky ingest counters tests under load

* Attempt 2

* Pre-emptively hardcode site ids in sampling cache test

to avoid supplying the same IDs alongside with counters test,
that inserts through another repo (async).
what we're observing is, clickhouse not summing mergetree columns fast
enough, even though we wait quite a bit.

* Fix ingest counter tests by accounting for delayed summation

---------

Co-authored-by: Adam Rutkowski <hq@mtod.org>
2024-12-16 11:11:14 +00:00

168 lines
5.4 KiB
Elixir

defmodule Plausible.Workers.AcceptTrafficUntilTest do
use Plausible.DataCase, async: true
use Plausible.Teams.Test
use Bamboo.Test
alias Plausible.Workers.AcceptTrafficUntil
@moduletag :ee_only
test "does not send any notifications when sites have no stats" do
today = Date.utc_today()
next_week = today |> Date.add(+7)
tomorrow = today |> Date.add(+1)
user1 = new_user(team: [accept_traffic_until: next_week])
user2 = new_user(team: [accept_traffic_until: tomorrow])
_site1 = new_site(owner: user1)
_site2 = new_site(owner: user2)
{:ok, 2} = AcceptTrafficUntil.perform(nil)
refute_notifications(user1.email)
refute_notifications(user2.email)
end
test "does not send any notifications when site has stats older than 2d" do
today = Date.utc_today()
next_week = today |> Date.add(+7)
user = new_user(team: [accept_traffic_until: next_week])
new_site(owner: user)
|> populate_stats([build(:pageview, timestamp: Date.add(today, -3))])
{:ok, 1} = AcceptTrafficUntil.perform(nil)
refute_notifications(user.email)
end
test "does send notification when last stat is 2d old" do
today = Date.utc_today()
next_week = today |> Date.add(+7)
user = new_user(team: [accept_traffic_until: next_week])
new_site(owner: user)
|> populate_stats([build(:pageview, timestamp: Date.add(today, -2))])
{:ok, 1} = AcceptTrafficUntil.perform(nil)
assert_weekly_notification(user.email)
end
test "tomorrow: sends one e-mail" do
tomorrow = Date.utc_today() |> Date.add(+1)
user = new_user(team: [accept_traffic_until: tomorrow])
new_site(owner: user) |> populate_stats([build(:pageview)])
{:ok, 1} = AcceptTrafficUntil.perform(nil)
assert_final_notification(user.email)
end
test "next week: sends one e-mail" do
next_week = Date.utc_today() |> Date.add(+7)
user = new_user(team: [accept_traffic_until: next_week])
new_site(owner: user) |> populate_stats([build(:pageview)])
{:ok, 1} = AcceptTrafficUntil.perform(nil)
assert_weekly_notification(user.email)
end
test "sends combined warnings in one shot" do
today = Date.utc_today()
next_week = today |> Date.add(+7)
tomorrow = today |> Date.add(+1)
in_8_days = today |> Date.add(+8)
user1 = new_user(team: [accept_traffic_until: next_week])
user2 = new_user(team: [accept_traffic_until: tomorrow])
user3 = new_user(team: [accept_traffic_until: in_8_days], email: "nope@example.com")
user4 = new_user(team: [accept_traffic_until: today], email: "nope2@example.com")
new_site(owner: user1) |> populate_stats([build(:pageview)])
new_site(owner: user2) |> populate_stats([build(:pageview)])
new_site(owner: user3) |> populate_stats([build(:pageview)])
new_site(owner: user4) |> populate_stats([build(:pageview)])
{:ok, 2} = AcceptTrafficUntil.perform(nil)
assert_weekly_notification(user1.email)
assert_final_notification(user2.email)
refute_notifications("nope@example.com")
refute_notifications("nope2@example.com")
end
test "sends multiple notifications per recipient as the time passes" do
today = Date.utc_today()
email = "cycle@example.com"
user =
new_user(
email: email,
trial_expiry_date: Plausible.Teams.Team.trial_expiry()
)
team = team_of(user)
site = new_site(owner: user)
populate_stats(site, [
build(:pageview, timestamp: today),
build(:pageview, timestamp: Date.add(team.trial_expiry_date, 7)),
build(:pageview, timestamp: Date.add(team.trial_expiry_date, 8)),
build(:pageview, timestamp: Date.add(team.trial_expiry_date, 13))
])
# today's worker is no-op
{:ok, 0} = AcceptTrafficUntil.perform(nil, today)
refute_notifications(email)
# trial_expiry + 7 days worker => we'll stop counting next week
{:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 7))
assert_weekly_notification(email)
# another call on the same day == no-op, e-mail already sent
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 7))
refute_notifications(email)
# another call on another day == no-op, no notification type applies
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 8))
refute_notifications(email)
# day before accept_stats_until (trial_expiry_date + 14 - 1)
{:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 13))
assert_final_notification(email)
# another one on the day before accept_stats_until (trial_expiry_date + 14 - 1) == no-op
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 13))
refute_notifications(email)
end
defp assert_weekly_notification(email) when is_binary(email) do
assert_email_delivered_with(
html_body: ~r/Hey Jane,/,
to: [nil: email],
subject:
PlausibleWeb.Email.approaching_accept_traffic_until(%{name: "", email: email}).subject
)
end
defp assert_final_notification(email) when is_binary(email) do
assert_email_delivered_with(
html_body: ~r/Hey Jane,/,
to: [nil: email],
subject:
PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{name: "", email: email}).subject
)
end
defp refute_notifications(email) when is_binary(email) do
refute_email_delivered_with(to: [nil: email])
end
end