analytics/test/workers/send_site_setup_emails_test.exs
Adam Rutkowski 0fa6b688af
Google APIs integration improvements (#2358)
* Make TestUtils module available in all tests

* Add macros patching the application env in tests

Unfortunately a lot of existing functionality relies on
certain application env setup. This isn't ideal because
the app config is a shared state that prevents us from
running the tests in parallel.

Those macros encapsulate setting up new env for test purposes
and make sure the changes are reverted when the test finishes.

* Allow passing request opts to HTTPClient.post/4

We need this to swap custom request building in
Google Analytics import.

* Unify errors when listing sites

* React: propagate backend error messages if available

* React: catch API errors in Search Terms component

* Propagate google API errors on referrer drilldown

* Handle verified properties errors in SC settings

* Add missing tests for SC settings controller

* Unify errors for fetching search analytics queries (list stats)

* Unify errors refreshing Google Auth Token

* Test fetch_stats/3 errors and replace Double with Mox

* Fixup makrup

* s/class/className

* Simplify Search Terms display in case of errors

* Fix warnings
2022-10-24 09:34:02 +02:00

101 lines
2.8 KiB
Elixir

defmodule Plausible.Workers.SendSiteSetupEmailsTest do
use Plausible.DataCase, async: true
use Bamboo.Test
use Oban.Testing, repo: Plausible.Repo
alias Plausible.Workers.SendSiteSetupEmails
describe "when user has not managed to set up the site" do
test "does not send an email 47 hours after site creation" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(47))
perform_job(SendSiteSetupEmails, %{})
assert_no_emails_delivered()
end
test "sends a setup help email 48 hours after site has been created" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(49))
perform_job(SendSiteSetupEmails, %{})
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Waiting for the first page views"
)
end
test "does not send an email more than 72 hours after signup" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(73))
perform_job(SendSiteSetupEmails, %{})
assert_no_emails_delivered()
end
end
describe "when user has managed to set up their site" do
test "sends the setup completed email as soon as possible" do
user = insert(:user)
insert(:site, members: [user], domain: "test-site.com")
perform_job(SendSiteSetupEmails, %{})
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Plausible is now tracking your website stats"
)
end
test "sends the setup completed email after the help email has been sent" do
user = insert(:user)
site = insert(:site, members: [user], inserted_at: hours_ago(49))
perform_job(SendSiteSetupEmails, %{})
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Waiting for the first page views"
)
create_pageviews([%{domain: site.domain}])
perform_job(SendSiteSetupEmails, %{})
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Plausible is now tracking your website stats"
)
end
end
describe "trial user who has not set up a website" do
test "does not send an email before 48h have passed" do
insert(:user, inserted_at: hours_ago(47))
perform_job(SendSiteSetupEmails, %{})
assert_no_emails_delivered()
end
test "sends the create site email after 48h" do
user = insert(:user, inserted_at: hours_ago(49))
perform_job(SendSiteSetupEmails, %{})
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Add your website details"
)
end
end
defp hours_ago(hours) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(hours: -hours)
end
end