analytics/test/workers/traffic_change_notifier_test.exs
hq1 e3af1a317d
Onboarding improvements (#4459)
* Migration: add installation meta

* Update site schema with installation meta

* Remove VERIFICATION_ENABLED env var

* Add context API to create/remove special goals

* Add context api to update installation meta

* Remove verification enabled check

* Update new progress flow definitions

* Update generic components

* Remove internal /status API

* Implement installation live view

* Update traffic change notifier link

* Update verification, no more modal

* Update routes

* Remove focus.html - will unify everything under app layout

* Fix broken link

* Update templates with focus_box mostly

* Update controller tests

* Update controllers and stop using the focus layout

* copy changes

* Update verification.ex

* Remove dead template

* Update settings_general.html.heex

* Update copy in tests

* Update installation.ex

* Remove dangling dot

* Fix link

* Update installation.ex

* Update installation.ex

* Better tooltips?

* Simpler labels

* Revert "Simpler labels"

This reverts commit 797560ef82f2067458b03b884be5aecc8fdc72bc.

* Add copy to clipboard link and fix snippet's dark mode

* Offer installation detection skip only if ws connected

* Put COPY link at the bottom with background

* Make tooltips link to docs

* Fix cherry-pick gone wrong

* Hide tooltips on mobile screens

* WIP: 404 tracking wizard

* Revert "WIP: 404 tracking wizard"

This reverts commit a9c9c79bbd.

* Update lib/plausible_web/live/components/verification.ex

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Update lib/plausible_web/live/installation.ex

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Use current_user from socket.assigns

* Update lib/plausible_web/live/installation.ex

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Use current_user from socket.assigns

* Use conn.private to steer verification tests

* Drop non-sticky tooltip in favour of component parametrization

Co-authored-by: Artur Pata <artur.pata@gmail.com>

* Reapply "WIP: 404 tracking wizard"

This reverts commit 3ba81671d7.

* Fix installation tests including 404 tracking

* Fixup the tooltip component

* Format

* Update installation.ex

* Put flash whenever installation option changes

* Use last known installation type on domain change

* Extract user flow definition to provide compile-time checks

* See if this helps running CE migrations successfully

* Use `styled_link` on registration/login views

* Don't crash when there's no conn.private carried over

* Format

* Push "Determining installation type" message a bit lower

* Use links and footer lists uniformly

This commit introduces a `<.focus_list/>` component
for rendering focus box footer links with colored
discs. It also equips generic link components
with the ability of sending non-GET requests
along with CSRF token, so we can apply uniform
styling and stop using legacy Phoenix link tags.

cc @zoldar @apata

* ws 👾

* Render more descriptive flashes on script config change

---------

Co-authored-by: Marko Saric <34340819+metmarkosaric@users.noreply.github.com>
Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
Co-authored-by: Artur Pata <artur.pata@gmail.com>
2024-09-02 12:49:54 +02:00

246 lines
7.2 KiB
Elixir

defmodule Plausible.Workers.TrafficChangeNotifierTest do
use Plausible.DataCase, async: true
use Bamboo.Test
import Double
alias Plausible.Workers.TrafficChangeNotifier
describe "drops" do
test "does not notify anyone if we've stopped accepting traffic for the owner" do
site =
insert(:site,
memberships: [
build(:site_membership,
user: build(:user, accept_traffic_until: Date.utc_today()),
role: :owner
)
]
)
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com", "uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "does notify if threshold reached and we're accepting traffic" do
site =
insert(:site,
memberships: [
build(:site_membership,
user: build(:user, accept_traffic_until: Date.utc_today() |> Date.add(+1)),
role: :owner
)
]
)
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic Drop on #{site.domain}",
to: [nil: "jerod@example.com"]
)
end
test "does not notify anyone if current visitors does not drop below notification threshold" do
site = insert(:site)
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com", "uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 11 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "notifies all recipients when traffic drops under configured threshold" do
site = insert(:site)
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com", "uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 7 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic Drop on #{site.domain}",
to: [nil: "jerod@example.com"]
)
assert_email_delivered_with(
subject: "Traffic Drop on #{site.domain}",
to: [nil: "uku@example.com"]
)
end
test "does not notify anyone if a notification already went out in the last 12 hours" do
site = insert(:site)
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 4 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic Drop on #{site.domain}",
to: [nil: "uku@example.com"]
)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "adds settings link if recipient has access to the site" do
user = insert(:user, email: "robert@example.com")
site = insert(:site, domain: "example.com", members: [user])
insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["robert@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 6 end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
html_body: ~r|http://localhost:8000/example.com/installation\?flow=review|
)
end
end
describe "spikes" do
test "does not notify anyone if current visitors does not exceed notification threshold" do
site = insert(:site)
insert(:spike_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com", "uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site -> 5 end)
|> stub(:top_sources_for_spike, fn _site, _query, _limit, _page -> [] end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "notifies all recipients when traffic is higher than configured threshold" do
site = insert(:site)
insert(:spike_notification,
site: site,
threshold: 10,
recipients: ["jerod@example.com", "uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site -> 10 end)
|> stub(:top_sources_for_spike, fn _site, _query, _limit, _page -> [] end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic Spike on #{site.domain}",
to: [nil: "jerod@example.com"]
)
assert_email_delivered_with(
subject: "Traffic Spike on #{site.domain}",
to: [nil: "uku@example.com"]
)
end
test "does not check site if it is locked" do
site = insert(:site, locked: true)
insert(:spike_notification,
site: site,
threshold: 10,
recipients: ["uku@example.com"]
)
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site -> 10 end)
|> stub(:top_sources_for_spike, fn _site, _query, _limit, _page -> [] end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "does not notify anyone if a notification already went out in the last 12 hours" do
site = insert(:site)
insert(:spike_notification, site: site, threshold: 10, recipients: ["uku@example.com"])
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site -> 10 end)
|> stub(:top_sources_for_spike, fn _site, _query, _limit, _page -> [] end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic Spike on #{site.domain}",
to: [nil: "uku@example.com"]
)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_no_emails_delivered()
end
test "adds a dashboard link if recipient has access to the site" do
user = insert(:user, email: "robert@example.com")
site = insert(:site, domain: "example.com", members: [user])
insert(:spike_notification, site: site, threshold: 10, recipients: ["robert@example.com"])
clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site -> 10 end)
|> stub(:top_sources_for_spike, fn _site, _query, _limit, _page -> [] end)
TrafficChangeNotifier.perform(nil, clickhouse_stub)
assert_email_delivered_with(html_body: ~r/View dashboard: <a href=\"http.+\/example.com/)
end
end
end