2020-06-28 13:24:54 +03:00
|
|
|
defmodule Plausible.Workers.ScheduleEmailReportsTest do
|
|
|
|
use Plausible.DataCase
|
|
|
|
use Oban.Testing, repo: Plausible.Repo
|
|
|
|
alias Plausible.Workers.{ScheduleEmailReports, SendEmailReport}
|
|
|
|
|
|
|
|
describe "weekly reports" do
|
|
|
|
test "schedules weekly report on Monday 9am local timezone" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:weekly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
assert_enqueued(
|
|
|
|
worker: SendEmailReport,
|
|
|
|
args: %{site_id: site.id, interval: "weekly"},
|
|
|
|
scheduled_at: ScheduleEmailReports.monday_9am(site.timezone)
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not schedule more than one weekly report at a time" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:weekly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
assert Enum.count(all_enqueued(worker: SendEmailReport)) == 1
|
|
|
|
end
|
|
|
|
|
2021-06-17 10:20:54 +03:00
|
|
|
test "does not schedule a weekly report for locked site" do
|
|
|
|
site = insert(:site, locked: true, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:weekly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
|
|
|
perform_job(ScheduleEmailReports, %{})
|
|
|
|
|
|
|
|
assert Enum.empty?(all_enqueued(worker: SendEmailReport))
|
|
|
|
end
|
|
|
|
|
2020-06-28 13:24:54 +03:00
|
|
|
test "schedules a new report as soon as a previous one is completed" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:weekly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-07-21 09:58:00 +03:00
|
|
|
Repo.update_all("oban_jobs", set: [state: "completed"])
|
2021-01-07 16:16:04 +03:00
|
|
|
assert Enum.empty?(all_enqueued(worker: SendEmailReport))
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
assert Enum.count(all_enqueued(worker: SendEmailReport)) == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "monthly_reports" do
|
|
|
|
test "schedules monthly report on first of the next month at 9am local timezone" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:monthly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
assert_enqueued(
|
|
|
|
worker: SendEmailReport,
|
|
|
|
args: %{site_id: site.id, interval: "monthly"},
|
|
|
|
scheduled_at: ScheduleEmailReports.first_of_month_9am(site.timezone)
|
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not schedule more than one monthly report at a time" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:monthly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-06-17 10:20:54 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
assert Enum.count(all_enqueued(worker: SendEmailReport)) == 1
|
|
|
|
end
|
|
|
|
|
2021-06-17 10:20:54 +03:00
|
|
|
test "does not schedule a monthly report for locked site" do
|
|
|
|
site = insert(:site, locked: true, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:monthly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
|
|
|
perform_job(ScheduleEmailReports, %{})
|
|
|
|
|
|
|
|
assert Enum.empty?(all_enqueued(worker: SendEmailReport))
|
|
|
|
end
|
|
|
|
|
2020-06-28 13:24:54 +03:00
|
|
|
test "schedules a new report as soon as a previous one is completed" do
|
|
|
|
site = insert(:site, domain: "test-site.com", timezone: "US/Eastern")
|
|
|
|
insert(:monthly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-07-21 09:58:00 +03:00
|
|
|
Repo.update_all("oban_jobs", set: [state: "completed"])
|
2021-01-07 16:16:04 +03:00
|
|
|
assert Enum.empty?(all_enqueued(worker: SendEmailReport))
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(ScheduleEmailReports, %{})
|
2020-06-28 13:24:54 +03:00
|
|
|
assert Enum.count(all_enqueued(worker: SendEmailReport)) == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|