analytics/test/workers/send_email_report_test.exs
hq1 d2f2c69387
Conditionally support switching between v1 and v2 clickhouse schemas (#2780)
* Remove ClickhouseSetup module

This has been an implicit point of contact to many
tests. From now on the goal is for each test to maintain
its own, isolated setup so that no accidental clashes
and implicit assumptions are relied upon.

* Implement v2 schema check

An environment variable V2_MIGRATION_DONE acts like
a feature flag, switching plausible from using old events/sessions
schemas to v2 schemas introduced by NumericIDs migration.

* Run both test suites sequentially

While the code for v1 and v2 schemas must be kept still,
we will from now on run tests against both code paths.
Secondary test run will set V2_MIGRATION_DONE=1 variable,
thus making all `Plausible.v2?()` checks return `true'.

* Remove unused function

This is a remnant from the short period when
we would check for existing events before allowing
creating a new site.

* Update test setups/factories with v2 migration check

* Make GateKeeper return site id along with :allow

* Make Billing module check for v2 schema

* Make ingestion aware of v2 schema

* Disable site transfers for when v2 is live

In a separate changeset we will implement simplified
site transfer for when v2 migration is complete.
The new transfer will only rename the site domain in postgres
and keep track of the original site prior to the transfer
so we keep an ingestion grace period until the customers
redeploy their scripting.

* Make Stats base queries aware of v2 schema switch

* Update breakdown with v2 conditionals

* Update pageview local start with v2 check

* Update current visitoris with v2 check

* Update stats controller with v2 checks

* Update external controller with v2 checks

* Update remaining tests with proper fixtures

* Rewrite redundant assignment

* Remove unused alias

* Mute credo, this is not the right time

* Add test_helper prompt

* Fetch priv dir so it works with a release

* Fetch distinct partitions only

* Don't limit inspect output for partitions

* Ensure SQL is printed to IO

* Remove redundant domain fixture
2023-03-27 13:52:42 +02:00

138 lines
4.7 KiB
Elixir

defmodule Plausible.Workers.SendEmailReportTest do
use Plausible.DataCase
use Bamboo.Test
use Oban.Testing, repo: Plausible.Repo
alias Plausible.Workers.SendEmailReport
alias Timex.Timezone
describe "weekly reports" do
test "sends weekly report to all recipients" do
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["user@email.com", "user2@email.com"])
perform_job(SendEmailReport, %{"site_id" => site.id, "interval" => "weekly"})
assert_email_delivered_with(
subject: "Weekly report for #{site.domain}",
to: [nil: "user@email.com"]
)
assert_email_delivered_with(
subject: "Weekly report for #{site.domain}",
to: [nil: "user2@email.com"]
)
end
test "calculates timezone correctly" do
site =
insert(:site,
timezone: "US/Eastern"
)
insert(:weekly_report, site: site, recipients: ["user@email.com"])
now = Timex.now(site.timezone)
last_monday = Timex.shift(now, weeks: -1) |> Timex.beginning_of_week()
last_sunday = Timex.shift(now, weeks: -1) |> Timex.end_of_week()
sunday_before_last = Timex.shift(last_monday, minutes: -1)
this_monday = Timex.beginning_of_week(now)
create_pageviews([
# Sunday before last, not counted
%{site: site, timestamp: Timezone.convert(sunday_before_last, "UTC")},
# Sunday before last, not counted
%{site: site, timestamp: Timezone.convert(sunday_before_last, "UTC")},
# Last monday, counted
%{site: site, timestamp: Timezone.convert(last_monday, "UTC")},
# Last sunday, counted
%{site: site, timestamp: Timezone.convert(last_sunday, "UTC")},
# This monday, not counted
%{site: site, timestamp: Timezone.convert(this_monday, "UTC")},
# This monday, not counted
%{site: site, timestamp: Timezone.convert(this_monday, "UTC")}
])
perform_job(SendEmailReport, %{"site_id" => site.id, "interval" => "weekly"})
assert_delivered_email_matches(%{
to: [nil: "user@email.com"],
html_body: html_body
})
# Should find 2 visiors
assert html_body =~
~s(<span id="visitors" style="line-height: 24px; font-size: 20px;">2</span>)
end
test "includes the correct stats" do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
site = insert(:site, domain: "test-site.com", inserted_at: Timex.shift(now, days: -8))
insert(:weekly_report, site: site, recipients: ["user@email.com"])
populate_stats(site, [
build(:pageview,
referrer_source: "Google",
user_id: 123,
timestamp: Timex.shift(now, days: -7)
),
build(:pageview, user_id: 123, timestamp: Timex.shift(now, days: -7)),
build(:pageview, timestamp: Timex.shift(now, days: -7))
])
perform_job(SendEmailReport, %{"site_id" => site.id, "interval" => "weekly"})
assert_delivered_email_matches(%{
to: [nil: "user@email.com"],
html_body: html_body
})
{:ok, document} = Floki.parse_document(html_body)
visitors = Floki.find(document, "#visitors") |> Floki.text()
assert visitors == "2"
pageviews = Floki.find(document, "#pageviews") |> Floki.text()
assert pageviews == "3"
referrer = Floki.find(document, ".referrer") |> List.first()
referrer_name = referrer |> Floki.find("#referrer-name") |> Floki.text()
referrer_count = referrer |> Floki.find("#referrer-count") |> Floki.text()
assert referrer_name == "Google"
assert referrer_count == "1"
page = Floki.find(document, ".page") |> List.first()
page_name = page |> Floki.find("#page-name") |> Floki.text()
page_count = page |> Floki.find("#page-count") |> Floki.text()
assert page_name == "/"
assert page_count == "2"
end
end
describe "monthly_reports" do
test "sends monthly report to all recipients" do
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
insert(:monthly_report, site: site, recipients: ["user@email.com", "user2@email.com"])
last_month =
Timex.now(site.timezone)
|> Timex.shift(months: -1)
|> Timex.beginning_of_month()
|> Timex.format!("{Mfull}")
perform_job(SendEmailReport, %{"site_id" => site.id, "interval" => "monthly"})
assert_email_delivered_with(
subject: "#{last_month} report for #{site.domain}",
to: [nil: "user@email.com"]
)
assert_email_delivered_with(
subject: "#{last_month} report for #{site.domain}",
to: [nil: "user2@email.com"]
)
end
end
end