analytics/test/workers/notify_annual_renewal_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

200 lines
5.1 KiB
Elixir

defmodule Plausible.Workers.NotifyAnnualRenewalTest do
use Plausible.DataCase, async: true
use Bamboo.Test
alias Plausible.Workers.NotifyAnnualRenewal
setup [:create_user, :create_site]
@monthly_plan "558018"
@yearly_plan "572810"
@v2_pricing_yearly_plan "653232"
test "ignores user without subscription" do
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "ignores user with monthly subscription", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @monthly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "ignores user with yearly subscription that's not due for renewal in 7 days", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 10)
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "ignores user with old yearly subscription that's been superseded by a newer one", %{
user: user
} do
insert(:subscription,
inserted_at: Timex.shift(Timex.now(), days: -1),
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 5)
)
insert(:subscription,
inserted_at: Timex.now(),
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 30)
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "sends renewal notification to user whose subscription is due for renewal in 7 days", %{
user: user
} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
end
test "sends renewal notification to user whose subscription is due for renewal in 2 days", %{
user: user
} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 2)
)
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
end
test "does not send renewal notification multiple times", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "sends a renewal notification again a year after the previous one", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
Repo.insert_all("sent_renewal_notifications", [
%{
user_id: user.id,
timestamp: Timex.shift(Timex.today(), years: -1) |> Timex.to_naive_datetime()
}
])
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
end
test "does not send multiple notifications on second year", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
Repo.insert_all("sent_renewal_notifications", [
%{
user_id: user.id,
timestamp: Timex.shift(Timex.today(), years: -1) |> Timex.to_naive_datetime()
}
])
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
NotifyAnnualRenewal.perform(nil)
assert_no_emails_delivered()
end
test "sends renewal notification to user on v2 yearly pricing plans", %{
user: user
} do
insert(:subscription,
user: user,
paddle_plan_id: @v2_pricing_yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7)
)
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is up for renewal"
)
end
describe "expiration" do
test "if user subscription is 'deleted', notify them about expiration instead", %{user: user} do
insert(:subscription,
user: user,
paddle_plan_id: @yearly_plan,
next_bill_date: Timex.shift(Timex.today(), days: 7),
status: "deleted"
)
NotifyAnnualRenewal.perform(nil)
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible subscription is about to expire"
)
end
end
end