diff --git a/test/towerops_web/live/org/settings_live_events_test.exs b/test/towerops_web/live/org/settings_live_events_test.exs index 1e0f5647..c062406b 100644 --- a/test/towerops_web/live/org/settings_live_events_test.exs +++ b/test/towerops_web/live/org/settings_live_events_test.exs @@ -83,4 +83,76 @@ defmodule ToweropsWeb.Org.SettingsLiveEventsTest do assert html =~ "Default Remote Agent" end end + + describe "billing tab" do + test "renders billing tab with free plan upgrade button", + %{conn: conn, user: user, organization: org} do + {:ok, _view, html} = + conn + |> log_in_user(user) + |> live(~p"/orgs/#{org.slug}/settings?tab=billing") + + # Free plan default + assert html =~ "Upgrade to" or html =~ "Free Plan" + end + + test "renders billing tab for paid plan with subscription details", + %{conn: conn, user: user, organization: org} do + now = DateTime.truncate(DateTime.utc_now(), :second) + + {:ok, _org} = + Towerops.Organizations.update_organization(org, %{ + subscription_plan: "paid", + subscription_status: "active", + subscription_current_period_start: now, + subscription_current_period_end: DateTime.add(now, 30 * 86_400, :second), + payment_method_status: "valid" + }) + + {:ok, _view, html} = + conn + |> log_in_user(user) + |> live(~p"/orgs/#{org.slug}/settings?tab=billing") + + # Should render billing details and Manage button + assert html =~ "Manage" or html =~ "Subscription" + assert html =~ "Subscription Active" or html =~ "active" or html =~ "Billing" + end + + test "manage_billing without stripe customer flashes error message", + %{conn: conn, user: user, organization: org} do + {:ok, _org} = + Towerops.Organizations.update_organization(org, %{ + subscription_plan: "paid", + subscription_status: "active" + }) + + {:ok, view, _html} = + conn + |> log_in_user(user) + |> live(~p"/orgs/#{org.slug}/settings?tab=billing") + + html = render_hook(view, "manage_billing", %{}) + # Either redirects to stripe portal (success) or returns error + # Without stripe_customer_id, it'll hit no_stripe_customer error + assert html =~ "billing" or html =~ "No billing" + end + + test "renders billing past_due banner", + %{conn: conn, user: user, organization: org} do + {:ok, _org} = + Towerops.Organizations.update_organization(org, %{ + subscription_plan: "paid", + subscription_status: "past_due", + payment_method_status: "expired" + }) + + {:ok, _view, html} = + conn + |> log_in_user(user) + |> live(~p"/orgs/#{org.slug}/settings?tab=billing") + + assert html =~ "Past Due" or html =~ "past_due" or html =~ "expired" + end + end end