analytics/test/workers/notify_annual_renewal_test.exs
RobertJoonas 3d2f356ba7
Refactor enterprise plan upgrade and change-plan actions (#3397)
* rename enterprise?/1 function

* change link text to Upgrade when subscription deleted

* extract paddle_button and paddle_script components

* create a new upgrade-to-enterprise-plan page

* extract upgrade_link component

* rename function

* link to enterprise plan upgrade page from settings

...if the user has an enterprise plan configured

* fetch enterprise plan price on the new page

* add change_enterprise_plan functionality on the new page

* render existing change_enterprise_plan_contact_us.html

...when subscribed to latest configured enterprise plan

* rename vars and extract resumable? fn

* remove dead billing route

* small test refactor: extract convenience fn

* add tests for...

...restricting paused and past_due subscription access to the new
enterprise plan page.

1. redirect to /settings from the controller action
2. hiding the change-plan link from the user settings

* implement redirect to /settings

* hide the enterprise upgrade/change-plan link

* add tests for a deleted enterprise subscription

* plug in the new controller action and delete dead code

* optimize for dark mode

* fix compile warning

* credo fix

* display N/A instead of crash when price nil

* change subscription.status type to Ecto.Enum

Also, create a new `Subscription.Status` module that exposes macros to
return the used atom values (prevent typos at compiletime).

* fix bug (@conn not available anymore)

* use Routes.billing_path where applicable

* add a status() type

* silence credo

* refactor suggestion from review

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Remove the __using__ macro from Subscription.Status

... instead be explicit about requires and aliases and also order
the use, import, require, and alias clauses according to
https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering

* drop the virtual Enteprise 'price_per_interval' field

* apply review suggestion to make the code more DRY

* use dot syntax to fetch current user in new controller actions

* fix formatting

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
2023-10-10 20:35:17 +03:00

201 lines
5.2 KiB
Elixir

defmodule Plausible.Workers.NotifyAnnualRenewalTest do
use Plausible.DataCase, async: true
use Bamboo.Test
require Plausible.Billing.Subscription.Status
alias Plausible.Workers.NotifyAnnualRenewal
alias Plausible.Billing.Subscription
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: Subscription.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