2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.BillingControllerTest do
|
|
|
|
use PlausibleWeb.ConnCase
|
|
|
|
import Plausible.TestUtils
|
|
|
|
|
2021-10-20 17:49:11 +03:00
|
|
|
describe "GET /upgrade" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
|
|
|
test "shows upgrade page when user does not have a subcription already", %{conn: conn} do
|
|
|
|
conn = get(conn, "/billing/upgrade")
|
|
|
|
|
|
|
|
assert html_response(conn, 200) =~ "Upgrade your free trial"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "redirects user to change plan if they already have a plan", %{conn: conn, user: user} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
conn = get(conn, "/billing/upgrade")
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/billing/change-plan"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "redirects user to enteprise plan page if they are configured with one", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
plan = insert(:enterprise_plan, user: user)
|
|
|
|
conn = get(conn, "/billing/upgrade")
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/billing/upgrade/enterprise/#{plan.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /upgrade/enterprise/:plan_id" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
|
|
|
test "renders enteprise plan upgrade page", %{conn: conn, user: user} do
|
|
|
|
plan = insert(:enterprise_plan, user: user)
|
|
|
|
|
|
|
|
conn = get(conn, "/billing/upgrade/enterprise/#{plan.id}")
|
|
|
|
|
|
|
|
assert html_response(conn, 200) =~ "Upgrade your free trial"
|
|
|
|
assert html_response(conn, 200) =~ "enterprise plan"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
describe "GET /change-plan" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
2019-10-25 09:51:54 +03:00
|
|
|
test "shows change plan page if user has subsription", %{conn: conn, user: user} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
conn = get(conn, "/billing/change-plan")
|
|
|
|
|
|
|
|
assert html_response(conn, 200) =~ "Change subscription plan"
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
test "redirects to /upgrade if user does not have a subscription", %{conn: conn} do
|
|
|
|
conn = get(conn, "/billing/change-plan")
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/billing/upgrade"
|
|
|
|
end
|
2021-10-20 17:49:11 +03:00
|
|
|
|
|
|
|
test "redirects to enterprise change plan page if user has enterprise plan and existing subscription",
|
|
|
|
%{conn: conn, user: user} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
plan = insert(:enterprise_plan, user: user)
|
|
|
|
conn = get(conn, "/billing/change-plan")
|
|
|
|
|
|
|
|
assert redirected_to(conn) == "/billing/change-plan/enterprise/#{plan.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /change-plan/enterprise/:plan_id" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
|
|
|
test "shows change plan page if user has subsription and enterprise plan", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
plan = insert(:enterprise_plan, user: user)
|
|
|
|
conn = get(conn, "/billing/change-plan/enterprise/#{plan.id}")
|
|
|
|
|
|
|
|
assert html_response(conn, 200) =~ "Change subscription plan"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "renders 404 is user does not have enterprise plan", %{conn: conn, user: user} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
conn = get(conn, "/billing/change-plan/enterprise/123")
|
|
|
|
|
|
|
|
assert conn.status == 404
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /change-plan" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
2020-04-21 14:11:38 +03:00
|
|
|
test "calls Paddle API to update subscription", %{conn: conn, user: user} do
|
|
|
|
insert(:subscription, user: user)
|
|
|
|
|
|
|
|
post(conn, "/billing/change-plan/123123")
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-04-21 14:11:38 +03:00
|
|
|
subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, user_id: user.id)
|
|
|
|
assert subscription.paddle_plan_id == "123123"
|
|
|
|
assert subscription.next_bill_date == ~D[2019-07-10]
|
|
|
|
assert subscription.next_bill_amount == "6.00"
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
2020-08-18 12:48:01 +03:00
|
|
|
|
|
|
|
describe "GET /billing/upgrade-success" do
|
|
|
|
setup [:create_user, :log_in]
|
|
|
|
|
|
|
|
test "shows success page after user subscribes", %{conn: conn} do
|
|
|
|
conn = get(conn, "/billing/upgrade-success")
|
|
|
|
|
2021-03-15 12:40:53 +03:00
|
|
|
assert html_response(conn, 200) =~ "Subscription created successfully"
|
2020-08-18 12:48:01 +03:00
|
|
|
end
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|