File size: 13,802 Bytes
6778ee0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | defmodule PlausibleWeb.BillingControllerTest do
use PlausibleWeb.ConnCase, async: true
require Plausible.Billing.Subscription.Status
alias Plausible.Billing.Subscription
@v4_growth_plan "857097"
@v4_business_plan "857105"
describe "GET /choose-plan" do
setup [:create_user, :log_in]
test "redirects to enterprise upgrade page if user has an enterprise plan configured",
%{conn: conn, user: user} do
subscribe_to_enterprise_plan(user, paddle_plan_id: "123")
conn = get(conn, Routes.billing_path(conn, :choose_plan))
assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan)
end
end
describe "POST /change-plan" do
setup [:create_user, :log_in]
test "errors if usage exceeds team member limit on the new plan", %{conn: conn, user: user} do
subscribe_to_plan(user, "123123")
site = new_site(owner: user)
for _ <- 1..4 do
add_guest(site, role: :viewer)
end
conn = post(conn, Routes.billing_path(conn, :change_plan, @v4_growth_plan))
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
"Unable to subscribe to this plan because the following limits are exceeded: team member limit"
end
test "errors if usage exceeds site limit even when user.next_upgrade_override is true", %{
conn: conn,
user: user
} do
subscribe_to_plan(user, "123123")
team =
user
|> team_of()
|> Ecto.Changeset.change(allow_next_upgrade_override: true)
|> Plausible.Repo.update!()
for _ <- 1..11, do: new_site(owner: user)
conn = post(conn, Routes.billing_path(conn, :change_plan, @v4_growth_plan))
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, team_id: team.id)
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "are exceeded: site limit"
assert subscription.paddle_plan_id == "123123"
end
test "can override allowing to upgrade when pageview limit is exceeded", %{
conn: conn,
user: user
} do
subscribe_to_plan(user, "123123")
team = team_of(user)
site = new_site(owner: user)
now = NaiveDateTime.utc_now()
generate_usage_for(site, 11_000, NaiveDateTime.shift(now, day: -5))
generate_usage_for(site, 11_000, NaiveDateTime.shift(now, day: -35))
conn1 = post(conn, Routes.billing_path(conn, :change_plan, @v4_growth_plan))
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, team_id: team.id)
assert Phoenix.Flash.get(conn1.assigns.flash, :error) =~
"are exceeded: monthly pageview limit"
assert subscription.paddle_plan_id == "123123"
user
|> team_of()
|> Ecto.Changeset.change(allow_next_upgrade_override: true)
|> Plausible.Repo.update!()
conn2 = post(conn, Routes.billing_path(conn, :change_plan, @v4_growth_plan))
subscription = Plausible.Repo.reload!(subscription)
assert Phoenix.Flash.get(conn2.assigns.flash, :success) =~ "Plan changed successfully"
assert subscription.paddle_plan_id == @v4_growth_plan
end
test "calls Paddle API to update subscription", %{conn: conn, user: user} do
subscribe_to_plan(user, "321321")
team = team_of(user)
post(conn, Routes.billing_path(conn, :change_plan, "123123"))
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, team_id: team.id)
assert subscription.paddle_plan_id == "123123"
assert subscription.next_bill_date == ~D[2019-07-10]
assert subscription.next_bill_amount == "6.00"
end
end
describe "GET /billing/upgrade-success" do
setup [:create_user, :create_team, :log_in]
test "shows success page after user subscribes", %{conn: conn} do
conn = get(conn, Routes.billing_path(conn, :upgrade_success))
assert html_response(conn, 200) =~ "Your account is being upgraded"
end
end
@configured_enterprise_plan_paddle_plan_id "123"
describe "GET /upgrade-to-enterprise-plan (no existing subscription)" do
setup [:create_user, :log_in, :configure_enterprise_plan]
test "displays basic page content", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ "Upgrade to Enterprise"
assert doc =~ "prepared a custom enterprise plan for your account with the following limits"
assert doc =~ "Questions?"
assert doc =~ "Contact us"
assert doc =~ "+ VAT if applicable"
assert doc =~ "Click the button below to upgrade"
assert doc =~ "Pay securely via Paddle"
end
test "displays info about the enterprise plan to upgrade to", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
assert doc =~ ~r/The plan is priced at\s*<b>\s*€123\s*<\/b>\s*/
assert doc =~ "per year"
end
test "data-product attribute on the checkout link is the paddle_plan_id of the enterprise plan",
%{conn: conn, user: user} do
{:ok, team} = Plausible.Teams.get_by_owner(user)
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert %{
"disableLogout" => true,
"email" => user.email,
"passthrough" => "ee:#{ee?()};user:#{user.id};team:#{team.id}",
"product" => @configured_enterprise_plan_paddle_plan_id,
"success" => Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success),
"theme" => "none"
} == get_paddle_checkout_params(find(doc, "#paddle-button"))
end
end
describe "GET /upgrade-to-enterprise-plan (active subscription, new enterprise plan configured)" do
setup [:create_user, :log_in, :subscribe_enterprise, :configure_enterprise_plan]
test "displays basic page content", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ "Change subscription plan"
assert doc =~ "prepared your account for an upgrade to custom limits"
assert doc =~ "+ VAT if applicable"
assert doc =~ "calculate the prorated amount that your card will be charged"
assert doc =~ "Preview changes"
assert doc =~ "Questions?"
assert doc =~ "Contact us"
end
test "displays info about the enterprise plan to upgrade to", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
assert doc =~ ~r/The plan is priced at\s*<b>\s*€123\s*<\/b>\s*/
assert doc =~ "per year"
end
test "preview changes links to :change_plan_preview action", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
preview_changes_link = find(doc, "#preview-changes")
assert text(preview_changes_link) == "Preview changes"
assert text_of_attr(preview_changes_link, "href") ==
Routes.billing_path(
PlausibleWeb.Endpoint,
:change_plan_preview,
@configured_enterprise_plan_paddle_plan_id
)
end
end
describe "GET /billing/change-plan/preview/:plan_id" do
setup [:create_user, :log_in]
test "renders preview information about the plan change", %{conn: conn, user: user} do
subscribe_to_plan(user, @v4_growth_plan)
html_response =
conn
|> get(Routes.billing_path(conn, :change_plan_preview, @v4_business_plan))
|> html_response(200)
assert html_response =~
"Your card will be charged a pro-rated amount for the current billing period"
assert html_response =~ "€-72.6"
assert html_response =~ "Back"
assert html_response =~ ~s[<a href="/billing/choose-plan"]
end
test "flashes error and redirects to choose-plan page", %{conn: conn, user: user} do
# choose-plan enforces at least 1 site, which implies team created, before allowing the user to upgrade
new_site(owner: user)
conn = get(conn, Routes.billing_path(conn, :change_plan_preview, @v4_business_plan))
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
"Something went wrong with loading your plan change information. Please try again, or contact us at support@plausible.io if the issue persists."
assert redirected_to(conn) == Routes.billing_path(conn, :choose_plan)
end
end
describe "GET /upgrade-to-enterprise-plan (already subscribed to latest enterprise plan)" do
setup [:create_user, :log_in, :configure_enterprise_plan]
setup context do
subscribe_enterprise(context, paddle_plan_id: @configured_enterprise_plan_paddle_plan_id)
end
test "renders contact note", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ "Looking to adjust your plan?"
assert doc =~ "You're currently on a custom plan."
assert LazyHTML.text(LazyHTML.from_document(doc)) =~
"please contact us at hello@plausible.io"
end
end
describe "GET /upgrade-to-enterprise-plan (subscription past_due or paused)" do
setup [:create_user, :log_in, :configure_enterprise_plan]
test "redirects to /settings when past_due", %{conn: conn} = context do
subscribe_enterprise(context, status: Subscription.Status.past_due())
conn = get(conn, Routes.billing_path(conn, :upgrade_to_enterprise_plan))
assert redirected_to(conn) == Routes.settings_path(conn, :subscription)
end
test "redirects to /settings when paused", %{conn: conn} = context do
subscribe_enterprise(context, status: Subscription.Status.paused())
conn = get(conn, Routes.billing_path(conn, :upgrade_to_enterprise_plan))
assert redirected_to(conn) == Routes.settings_path(conn, :subscription)
end
end
describe "GET /upgrade-to-enterprise-plan (deleted enterprise subscription)" do
setup [:create_user, :log_in, :configure_enterprise_plan]
setup context do
subscribe_enterprise(context,
paddle_plan_id: @configured_enterprise_plan_paddle_plan_id,
status: Subscription.Status.deleted()
)
context
end
test "displays the same content as for a user without a subscription", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ "Upgrade to Enterprise"
assert doc =~ "prepared a custom enterprise plan for your account with the following limits"
assert doc =~ "Questions?"
assert doc =~ "Contact us"
assert doc =~ "+ VAT if applicable"
assert doc =~ "Click the button below to upgrade"
assert doc =~ "Pay securely via Paddle"
end
test "still allows to subscribe back to the same plan", %{conn: conn} do
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert doc =~ ~r/Up to\s*<b>\s*50M\s*<\/b>\s*monthly pageviews/
assert doc =~ ~r/Up to\s*<b>\s*20k\s*<\/b>\s*sites/
assert doc =~ ~r/Up to\s*<b>\s*5k\s*<\/b>\s*hourly api requests/
assert doc =~ ~r/The plan is priced at\s*<b>\s*€123\s*<\/b>\s*/
assert doc =~ "per year"
end
test "renders paddle button with the correct checkout params",
%{conn: conn, user: user} do
{:ok, team} = Plausible.Teams.get_by_owner(user)
doc =
conn
|> get(Routes.billing_path(conn, :upgrade_to_enterprise_plan))
|> html_response(200)
assert %{
"disableLogout" => true,
"email" => user.email,
"passthrough" => "ee:#{ee?()};user:#{user.id};team:#{team.id}",
"product" => @configured_enterprise_plan_paddle_plan_id,
"success" => Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success),
"theme" => "none"
} == get_paddle_checkout_params(find(doc, "#paddle-button"))
end
end
defp configure_enterprise_plan(%{user: user}) do
subscribe_to_enterprise_plan(user,
paddle_plan_id: "123",
billing_interval: :yearly,
monthly_pageview_limit: 50_000_000,
site_limit: 20_000,
hourly_api_request_limit: 5000,
inserted_at: NaiveDateTime.utc_now() |> NaiveDateTime.shift(hour: 1),
subscription?: false
)
:ok
end
defp subscribe_enterprise(%{user: user}, opts \\ []) do
{paddle_plan_id, opts} = Keyword.pop(opts, :paddle_plan_id, "321")
opts = Keyword.put_new(opts, :status, Subscription.Status.active())
user = subscribe_to_plan(user, paddle_plan_id, opts)
{:ok, user: user}
end
defp get_paddle_checkout_params(element) do
with onclick <- text_of_attr(element, "onclick"),
[[_, checkout_params_str]] <- Regex.scan(~r/Paddle\.Checkout\.open\((.*?)\)/, onclick),
{:ok, checkout_params} <- Jason.decode(checkout_params_str) do
checkout_params
end
end
end
|