2020-12-09 12:00:14 +03:00
|
|
|
defmodule Plausible.Workers.SpikeNotifierTest do
|
2022-05-06 10:30:38 +03:00
|
|
|
use Plausible.DataCase, async: true
|
2020-12-09 12:00:14 +03:00
|
|
|
use Bamboo.Test
|
|
|
|
import Double
|
|
|
|
alias Plausible.Workers.SpikeNotifier
|
|
|
|
|
|
|
|
test "does not notify anyone if current visitors does not exceed notification threshold" do
|
|
|
|
site = insert(:site)
|
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
insert(:spike_notification,
|
|
|
|
site: site,
|
|
|
|
threshold: 10,
|
|
|
|
recipients: ["jerod@example.com", "uku@example.com"]
|
|
|
|
)
|
|
|
|
|
|
|
|
clickhouse_stub =
|
|
|
|
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 5 end)
|
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
SpikeNotifier.perform(nil, clickhouse_stub)
|
2020-12-09 12:00:14 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "notifies all recipients when traffic is higher than configured threshold" do
|
|
|
|
site = insert(:site)
|
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
insert(:spike_notification,
|
|
|
|
site: site,
|
|
|
|
threshold: 10,
|
|
|
|
recipients: ["jerod@example.com", "uku@example.com"]
|
|
|
|
)
|
|
|
|
|
|
|
|
clickhouse_stub =
|
|
|
|
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)
|
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
SpikeNotifier.perform(nil, clickhouse_stub)
|
2020-12-09 12:00:14 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-06-17 10:20:54 +03:00
|
|
|
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, _query -> 10 end)
|
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
|
|
|
|
|
|
|
SpikeNotifier.perform(nil, clickhouse_stub)
|
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
2020-12-09 12:00:14 +03:00
|
|
|
test "does not notify anyone if a notification already went out in the last 12 hours" do
|
|
|
|
site = insert(:site)
|
2020-12-09 12:16:16 +03:00
|
|
|
insert(:spike_notification, site: site, threshold: 10, recipients: ["uku@example.com"])
|
2020-12-29 16:17:27 +03:00
|
|
|
|
|
|
|
clickhouse_stub =
|
|
|
|
stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)
|
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
2020-12-09 12:16:16 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
SpikeNotifier.perform(nil, clickhouse_stub)
|
2020-12-09 12:16:16 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
subject: "Traffic spike on #{site.domain}",
|
|
|
|
to: [nil: "uku@example.com"]
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
SpikeNotifier.perform(nil, clickhouse_stub)
|
2020-12-09 12:00:14 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
end
|