@ -5,94 +5,6 @@ defmodule Plausible.BillingTest do
alias Plausible.Billing
alias Plausible.Billing.Subscription
describe " last_two_billing_cycles " do
test " billing on the 1st " do
last_bill_date = ~D[ 2021-01-01 ]
today = ~D[ 2021-01-02 ]
user = insert ( :user , subscription : build ( :subscription , last_bill_date : last_bill_date ) )
expected_cycles = {
Date . range ( ~D[ 2020-11-01 ] , ~D[ 2020-11-30 ] ) ,
Date . range ( ~D[ 2020-12-01 ] , ~D[ 2020-12-31 ] )
}
assert Billing . last_two_billing_cycles ( user , today ) == expected_cycles
end
test " in case of yearly billing, cycles are normalized as if they were paying monthly " do
last_bill_date = ~D[ 2020-09-01 ]
today = ~D[ 2021-02-02 ]
user = insert ( :user , subscription : build ( :subscription , last_bill_date : last_bill_date ) )
expected_cycles = {
Date . range ( ~D[ 2020-12-01 ] , ~D[ 2020-12-31 ] ) ,
Date . range ( ~D[ 2021-01-01 ] , ~D[ 2021-01-31 ] )
}
assert Billing . last_two_billing_cycles ( user , today ) == expected_cycles
end
end
describe " last_two_billing_months_usage " do
test " counts events from last two billing cycles " do
last_bill_date = ~D[ 2021-01-01 ]
today = ~D[ 2021-01-02 ]
user = insert ( :user , subscription : build ( :subscription , last_bill_date : last_bill_date ) )
site = insert ( :site , members : [ user ] )
create_pageviews ( [
%{ site : site , timestamp : ~N[ 2021-01-01 00:00:00 ] } ,
%{ site : site , timestamp : ~N[ 2020-12-31 00:00:00 ] } ,
%{ site : site , timestamp : ~N[ 2020-11-01 00:00:00 ] } ,
%{ site : site , timestamp : ~N[ 2020-10-31 00:00:00 ] }
] )
assert Billing . last_two_billing_months_usage ( user , today ) == { 1 , 1 }
end
test " only considers sites that the user owns " do
last_bill_date = ~D[ 2021-01-01 ]
today = ~D[ 2021-01-02 ]
user = insert ( :user , subscription : build ( :subscription , last_bill_date : last_bill_date ) )
owner_site =
insert ( :site ,
memberships : [
build ( :site_membership , user : user , role : :owner )
]
)
admin_site =
insert ( :site ,
memberships : [
build ( :site_membership , user : user , role : :admin )
]
)
create_pageviews ( [
%{ site : owner_site , timestamp : ~N[ 2020-12-31 00:00:00 ] } ,
%{ site : admin_site , timestamp : ~N[ 2020-12-31 00:00:00 ] } ,
%{ site : owner_site , timestamp : ~N[ 2020-11-01 00:00:00 ] } ,
%{ site : admin_site , timestamp : ~N[ 2020-11-01 00:00:00 ] }
] )
assert Billing . last_two_billing_months_usage ( user , today ) == { 1 , 1 }
end
test " gets event count from last month and this one " do
user =
insert ( :user ,
subscription :
build ( :subscription , last_bill_date : Timex . today ( ) |> Timex . shift ( days : - 1 ) )
)
assert Billing . last_two_billing_months_usage ( user ) == { 0 , 0 }
end
end
describe " trial_days_left " do
test " is 30 days for new signup " do
user = insert ( :user )
@ -203,22 +115,40 @@ defmodule Plausible.BillingTest do
@plan_id_10k " 654177 "
@plan_id_100k " 654178 "
@subscription_created_params %{
" alert_name " = > " subscription_created " ,
" passthrough " = > " " ,
" email " = > " " ,
" subscription_id " = > @subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" unit_price " = > " 6.00 " ,
" currency " = > " EUR "
}
@subscription_updated_params %{
" alert_name " = > " subscription_updated " ,
" passthrough " = > " " ,
" subscription_id " = > " " ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" old_status " = > " active " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
}
describe " subscription_created " do
test " creates a subscription " do
user = insert ( :user )
Billing . subscription_created ( %{
" alert_name " = > " subscription_created " ,
" subscription_id " = > @subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" unit_price " = > " 6.00 " ,
" currency " = > " EUR "
} )
%{ @subscription_created_params | " passthrough " = > user . id }
|> Billing . subscription_created ( )
subscription = Repo . get_by ( Plausible.Billing.Subscription , user_id : user . id )
assert subscription . paddle_subscription_id == @subscription_id
@ -230,19 +160,8 @@ defmodule Plausible.BillingTest do
test " create with email address " do
user = insert ( :user )
Billing . subscription_created ( %{
" passthrough " = > " " ,
" email " = > user . email ,
" alert_name " = > " subscription_created " ,
" subscription_id " = > @subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" unit_price " = > " 6.00 " ,
" currency " = > " EUR "
} )
%{ @subscription_created_params | " email " = > user . email }
|> Billing . subscription_created ( )
subscription = Repo . get_by ( Plausible.Billing.Subscription , user_id : user . id )
assert subscription . paddle_subscription_id == @subscription_id
@ -254,22 +173,21 @@ defmodule Plausible.BillingTest do
user = insert ( :user )
site = insert ( :site , locked : true , members : [ user ] )
Billing . subscription_created ( %{
" alert_name " = > " subscription_created " ,
" subscription_id " = > @subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" unit_price " = > " 6.00 " ,
" currency " = > " EUR "
} )
%{ @subscription_created_params | " passthrough " = > user . id }
|> Billing . subscription_created ( )
refute Repo . reload! ( site ) . locked
end
test " sets user.allow_next_upgrade_override field to false " do
user = insert ( :user , allow_next_upgrade_override : true )
%{ @subscription_created_params | " passthrough " = > user . id }
|> Billing . subscription_created ( )
refute Repo . reload! ( user ) . allow_next_upgrade_override
end
test " if user upgraded to an enterprise plan, their API key limits are automatically adjusted " do
user = insert ( :user )
@ -282,18 +200,8 @@ defmodule Plausible.BillingTest do
api_key = insert ( :api_key , user : user , hourly_request_limit : 1 )
Billing . subscription_created ( %{
" alert_name " = > " subscription_created " ,
" subscription_id " = > @subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" unit_price " = > " 6.00 " ,
" currency " = > " EUR "
} )
%{ @subscription_created_params | " passthrough " = > user . id }
|> Billing . subscription_created ( )
assert Repo . reload! ( api_key ) . hourly_request_limit == plan . hourly_api_request_limit
end
@ -304,21 +212,15 @@ defmodule Plausible.BillingTest do
user = insert ( :user )
subscription = insert ( :subscription , user : user )
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" subscription_plan_id " = > " new-plan-id " ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" passthrough " = > user . id
} )
|> Billing . subscription_updated ( )
subscription = Repo . get_by ( Plausible.Billing.Subscription , user_id : user . id )
assert subscription . paddle_plan_id == " new-plan-id "
assert subscription . paddle_plan_id == @plan_id_10k
assert subscription . next_bill_amount == " 12.00 "
end
@ -327,23 +229,31 @@ defmodule Plausible.BillingTest do
subscription = insert ( :subscription , user : user , status : Subscription.Status . past_due ( ) )
site = insert ( :site , locked : true , members : [ user ] )
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" subscription_plan_id " = > " new-plan-id " ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" old_status " = > " past_due " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" old_status " = > " past_due "
} )
|> Billing . subscription_updated ( )
refute Repo . reload! ( site ) . locked
end
test " sets user.allow_next_upgrade_override field to false " do
user = insert ( :user , allow_next_upgrade_override : true )
subscription = insert ( :subscription , user : user )
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" passthrough " = > user . id
} )
|> Billing . subscription_updated ( )
refute Repo . reload! ( user ) . allow_next_upgrade_override
end
test " if user upgraded to an enterprise plan, their API key limits are automatically adjusted " do
user = insert ( :user )
subscription = insert ( :subscription , user : user )
@ -357,19 +267,13 @@ defmodule Plausible.BillingTest do
api_key = insert ( :api_key , user : user , hourly_request_limit : 1 )
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" subscription_plan_id " = > " new-plan-id " ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" old_status " = > " past_due " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" subscription_plan_id " = > plan . paddle_plan_id
} )
|> Billing . subscription_updated ( )
assert Repo . reload! ( api_key ) . hourly_request_limit == plan . hourly_api_request_limit
end
@ -386,19 +290,13 @@ defmodule Plausible.BillingTest do
subscription = insert ( :subscription , user : user )
site = insert ( :site , locked : true , members : [ user ] )
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" subscription_plan_id " = > @plan_id_100k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" old_status " = > " past_due " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" subscription_plan_id " = > @plan_id_100k
} )
|> Billing . subscription_updated ( )
assert Repo . reload! ( site ) . locked == false
assert Repo . reload! ( user ) . grace_period == nil
@ -416,19 +314,12 @@ defmodule Plausible.BillingTest do
subscription = insert ( :subscription , user : user )
site = insert ( :site , locked : true , members : [ user ] )
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > subscription . paddle_subscription_id ,
" subscription_plan_id " = > @plan_id_10k ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" old_status " = > " past_due " ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" passthrough " = > user . id
} )
|> Billing . subscription_updated ( )
assert Repo . reload! ( site ) . locked == true
assert Repo . reload! ( user ) . grace_period . allowance_required == 11_000
@ -438,20 +329,14 @@ defmodule Plausible.BillingTest do
user = insert ( :user )
res =
Billing . subscription_updated ( %{
" alert_name " = > " subscription_updated " ,
@subscription_updated_params
|> Map . merge ( %{
" subscription_id " = > " 666 " ,
" subscription_plan_id " = > " new-plan-id " ,
" update_url " = > " update_url.com " ,
" cancel_url " = > " cancel_url.com " ,
" passthrough " = > user . id ,
" status " = > " active " ,
" next_bill_date " = > " 2019-06-01 " ,
" new_unit_price " = > " 12.00 " ,
" currency " = > " EUR "
" passthrough " = > user . id
} )
|> Billing . subscription_updated ( )
assert res == { :ok , nil }
assert { :ok , nil } = res
end
end