2020-06-02 13:37:38 +03:00
|
|
|
defmodule Plausible.Workers.SendCheckStatsEmailsTest do
|
2022-05-06 10:30:38 +03:00
|
|
|
use Plausible.DataCase, async: true
|
2021-04-26 11:32:18 +03:00
|
|
|
use Oban.Testing, repo: Plausible.Repo
|
2020-03-23 12:34:25 +03:00
|
|
|
use Bamboo.Test
|
2021-04-26 11:32:18 +03:00
|
|
|
alias Plausible.Workers.SendCheckStatsEmails
|
2020-06-02 13:37:38 +03:00
|
|
|
|
2020-03-23 12:34:25 +03:00
|
|
|
test "does not send an email before a week has passed" do
|
|
|
|
user = insert(:user, inserted_at: days_ago(6), last_seen: days_ago(6))
|
2020-05-22 12:33:17 +03:00
|
|
|
insert(:site, domain: "test-site.com", members: [user])
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendCheckStatsEmails, %{})
|
2020-03-23 12:34:25 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not send an email if the user has logged in recently" do
|
|
|
|
user = insert(:user, inserted_at: days_ago(9), last_seen: days_ago(6))
|
2020-05-22 12:33:17 +03:00
|
|
|
insert(:site, domain: "test-site.com", members: [user])
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendCheckStatsEmails, %{})
|
2020-03-23 12:34:25 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
2020-03-23 12:45:04 +03:00
|
|
|
test "does not send an email if the user has configured a weekly report" do
|
|
|
|
user = insert(:user, inserted_at: days_ago(9), last_seen: days_ago(7))
|
2020-05-22 12:33:17 +03:00
|
|
|
site = insert(:site, domain: "test-site.com", members: [user])
|
2020-03-23 12:45:04 +03:00
|
|
|
insert(:weekly_report, site: site, recipients: ["user@email.com"])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendCheckStatsEmails, %{})
|
2020-03-23 12:45:04 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
2020-03-23 12:34:25 +03:00
|
|
|
test "sends an email after a week of signup if the user hasn't logged in" do
|
|
|
|
user = insert(:user, inserted_at: days_ago(8), last_seen: days_ago(8))
|
2020-05-22 12:33:17 +03:00
|
|
|
insert(:site, domain: "test-site.com", members: [user])
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
perform_job(SendCheckStatsEmails, %{})
|
2020-03-23 12:34:25 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{user.name, user.email}],
|
|
|
|
subject: "Check your Plausible website stats"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp days_ago(days) do
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> NaiveDateTime.truncate(:second)
|
|
|
|
|> Timex.shift(days: -days)
|
|
|
|
end
|
|
|
|
end
|