mirror of
https://github.com/plausible/analytics.git
synced 2024-12-28 20:12:44 +03:00
81c12884cd
* Add elixir action * Format the codebase * Add postgresql * Postgres config * Run postgres on localhost * Add clickhouse to CI
71 lines
2.1 KiB
Elixir
71 lines
2.1 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, _show_noref -> [] 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, _show_noref -> [] 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, _show_noref -> [] 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
|