analytics/test/workers/spike_notifier_test.exs
2020-12-11 17:03:25 +02:00

55 lines
2.0 KiB
Elixir

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)
|> stub(:top_sources, fn _site, _query, _limit, _page -> [] end)
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)
|> stub(:top_sources, fn _site, _query, _limit, _page -> [] end)
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)
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 -> [] end)
SpikeNotifier.perform(nil, nil, clickhouse_stub)
assert_email_delivered_with(
subject: "Traffic spike on #{site.domain}",
to: [nil: "uku@example.com"]
)
SpikeNotifier.perform(nil, nil, clickhouse_stub)
assert_no_emails_delivered()
end
end