2021-04-21 15:57:38 +03:00
|
|
|
defmodule Plausible.Workers.NotifyAnnualRenewalTest do
|
2022-05-06 10:30:38 +03:00
|
|
|
use Plausible.DataCase, async: true
|
2021-04-21 15:57:38 +03:00
|
|
|
use Bamboo.Test
|
2023-10-10 20:35:17 +03:00
|
|
|
require Plausible.Billing.Subscription.Status
|
2021-04-21 15:57:38 +03:00
|
|
|
alias Plausible.Workers.NotifyAnnualRenewal
|
2023-10-10 20:35:17 +03:00
|
|
|
alias Plausible.Billing.Subscription
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
setup [:create_user, :create_site]
|
2021-05-11 11:21:09 +03:00
|
|
|
@monthly_plan "558018"
|
|
|
|
@yearly_plan "572810"
|
|
|
|
@v2_pricing_yearly_plan "653232"
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
test "ignores user without subscription" do
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
assert_no_emails_delivered()
|
|
|
|
end
|
|
|
|
|
2021-07-21 16:50:26 +03:00
|
|
|
test "ignores user with old yearly subscription that's been superseded by a newer one", %{
|
|
|
|
user: user
|
|
|
|
} do
|
2021-07-19 15:03:15 +03:00
|
|
|
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
|
|
|
|
|
2021-04-21 15:57:38 +03:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{user.name, user.email}],
|
|
|
|
subject: "Your Plausible subscription is up for renewal"
|
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{user.name, user.email}],
|
|
|
|
subject: "Your Plausible subscription is up for renewal"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-05-03 17:00:13 +03:00
|
|
|
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
|
|
|
|
|
2021-05-11 11:21:09 +03:00
|
|
|
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
|
|
|
|
|
2021-04-21 15:57:38 +03:00
|
|
|
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),
|
2023-10-10 20:35:17 +03:00
|
|
|
status: Subscription.Status.deleted()
|
2021-04-21 15:57:38 +03:00
|
|
|
)
|
|
|
|
|
2021-04-26 11:32:18 +03:00
|
|
|
NotifyAnnualRenewal.perform(nil)
|
2021-04-21 15:57:38 +03:00
|
|
|
|
|
|
|
assert_email_delivered_with(
|
|
|
|
to: [{user.name, user.email}],
|
|
|
|
subject: "Your Plausible subscription is about to expire"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|