Files
sure/test/controllers/plans_controller_test.rb
T
e5750a6c09 feat(budgets): add per-user personal budgets with strict isolation (#2891)
* feat(budgets): add per-user personal budgets with strict isolation

Families can now opt into personal budgets (toggleable via family
settings): each family member gets their own budget for a given
period instead of sharing a single family-wide budget.

- Add families.personal_budgets flag and budgets.user_id, with
  partial unique indexes so shared budgets (user_id IS NULL) and
  personal budgets (user_id IS NOT NULL) can't collide.
- Budget.find_or_bootstrap scopes lookup/creation by user when the
  family has personal_budgets enabled.
- Scope most_recent_initialized_budget (used to seed a new budget
  from the prior period) by user_id so one user's copy-forward never
  bleeds into another user's budget.
- budgets.user_id cascades on user deletion so personal budgets don't
  outlive their owner.

* feat(budgets): enforce user-specific budget ownership and cascade deletion

* feat(budgets): display user name for personal budgets in budget card on the plan section

* feat(budgets): enhance personal budgets display for admins with preview feature indication

* feat(budgets): enforce user-specific budget and category visibility for personal budgets

* feat(budgets): create budget section titles and add translations notice in preferences

* feat(budgets): let household and personal budgets coexist with sharing

Previously enabling personal_budgets made the shared household budget
unreachable. Budget.find_or_bootstrap now takes an explicit household:
flag so both can be resolved independently for the same period, with a
new household_budget_enabled family setting to opt out of the household
side and keep personal budgets only.

Adds a BudgetShare model (read_only/read_write) so a member can grant
another family member access to their personal budget, enforced via
Budget#viewable_by?/editable_by? across BudgetsController,
BudgetCategoriesController, PlansController, and the read-only API.
Preferences gains a Budget sharing card (gated on preview access like
the rest of the personal budgets UI) and an owner switcher pill (
Household / mine / shared-with-me) appears on the budget page and the
Plan hub card.

Also fixes personal budgets showing the same "actual spending" as the
household budget: actual spending/income now scope to the budget
owner's own accounts instead of the viewer's full accessible set,
via a new accounts: override on IncomeStatement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(budgets): enhance budget switcher with icons and improved styling

* feat(budgets): remove user name display from budget card and header

* feat(budgets): remove unique index on taggable_type and taggable_id in taggings

* feat(budgets): enhance budget sharing functionality and improve UI elements

* Collapse personal budget migrations

---------

Signed-off-by: JulienGourmet <69808509+jubbakka@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-08-18 08:36:46 +02:00

99 lines
3.0 KiB
Ruby

require "test_helper"
class PlansControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:family_admin)
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
sign_in @user
ensure_tailwind_build
end
test "redirects users without preview access to budgets" do
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => false))
get plan_url
assert_redirected_to budgets_path
end
test "renders budget and goals summary cards with drill-in links" do
get plan_url
assert_response :success
assert_match I18n.t("plans.budget_card.title"), response.body
assert_match I18n.t("plans.goals_card.title"), response.body
assert_select "a[href=?]", budget_path(Budget.date_to_param(Date.current))
assert_select "a[href=?]", goals_path
end
test "lists active goals with links to their detail pages" do
get plan_url
assert_response :success
goal = goals(:vacation_italy)
assert_match goal.name, response.body
assert_select "a[href=?]", goal_path(goal)
end
test "shows the goals empty state when the family has no goals" do
@user.family.goals.destroy_all
get plan_url
assert_response :success
assert_match I18n.t("goals.empty_state.body"), response.body
assert_select "a[href=?]", goals_path, count: 0
end
test "keeps the all-goals link when only completed or archived goals remain" do
@user.family.goals.each { |goal| goal.update_columns(state: "archived") }
get plan_url
assert_response :success
assert_match I18n.t("goals.empty_state.body"), response.body
assert_select "a[href=?]", goals_path, minimum: 1
end
test "shows the budget setup CTA when the month is uninitialized" do
budgets(:one).update!(budgeted_spending: nil)
get plan_url
assert_response :success
assert_match I18n.t("plans.budget_card.empty_body"), response.body
assert_select "a[href=?]", edit_budget_path(Budget.date_to_param(Date.current))
end
end
class PlansControllerHouseholdSwitchingTest < ActionDispatch::IntegrationTest
setup do
@family = families(:empty)
@family.update!(personal_budgets: true)
@owner = users(:josh)
@owner.update!(preferences: (@owner.preferences || {}).merge("preview_features_enabled" => true))
sign_in @owner
ensure_tailwind_build
end
test "renders a household/mine switcher once personal_budgets is on, and switches to household" do
get plan_url
assert_response :success
assert_select "a[href=?]", plan_path(owner: "household")
assert_select "a[href=?]", plan_path(owner: @owner.id)
get plan_url, params: { owner: "household" }
assert_response :success
end
test "hides the household pill when household_budget_enabled is off" do
@family.update!(household_budget_enabled: false)
get plan_url
assert_response :success
assert_select "a[href=?]", plan_path(owner: "household"), count: 0
end
end