analytics/test/mix/tasks/send_feedback_emails_test.exs
Uku Taht 7dbbc8ba22
Configurable site id (#30)
* Use site id instead of hostname for events

* Use site id in domain status check

* Revert change to tracking module

* Catch more places where link generation needed updating

* Rename site_id to domain

* Drop hostname index from events
2020-02-04 15:44:13 +02:00

66 lines
1.9 KiB
Elixir

defmodule Mix.Tasks.SendFeedbackEmailsTest do
use Plausible.DataCase
use Bamboo.Test
alias Mix.Tasks.SendFeedbackEmails
describe "when user does not have an active site" do
test "does not send an email ever" do
insert(:user, inserted_at: days_ago(15))
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
end
describe "when user has an active site" do
test "sends an email if the user is more than 30 days old and logged on in the last week" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_email_delivered_with(subject: "Plausible feedback")
end
test "sends the email only once" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_email_delivered_with(subject: "Plausible feedback")
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
test "does not send if user has not logged in recently" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(15))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
test "does not send if user is less than a month old" do
user = insert(:user, inserted_at: days_ago(15), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
end
defp days_ago(days) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(days: -days)
end
end