2020-12-09 12:00:14 +03:00
|
|
|
defmodule Plausible.Workers.SpikeNotifierTest do
|
|
|
|
use Plausible.DataCase
|
|
|
|
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)
|
|
|
|
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)
|
2020-12-11 18:01:09 +03:00
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
2020-12-09 12:00:14 +03:00
|
|
|
SpikeNotifier.perform(nil, 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, _query -> 10 end)
|
2020-12-11 18:01:09 +03:00
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
2020-12-09 12:00:14 +03:00
|
|
|
SpikeNotifier.perform(nil, 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 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-09 12:00:14 +03:00
|
|
|
clickhouse_stub = stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)
|
2020-12-11 18:01:09 +03:00
|
|
|
|> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end)
|
2020-12-09 12:16:16 +03:00
|
|
|
|
|
|
|
SpikeNotifier.perform(nil, nil, clickhouse_stub)
|
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
subject: "Traffic spike on #{site.domain}",
|
|
|
|
to: [nil: "uku@example.com"]
|
|
|
|
)
|
|
|
|
|
2020-12-09 12:00:14 +03:00
|
|
|
SpikeNotifier.perform(nil, nil, clickhouse_stub)
|
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
end
|