mirror of
https://github.com/we-promise/sure.git
synced 2026-09-02 13:21:17 +00:00
* 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>
110 lines
3.8 KiB
Ruby
110 lines
3.8 KiB
Ruby
require "test_helper"
|
|
|
|
class BudgetsControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
@user = users(:family_admin)
|
|
sign_in @user
|
|
ensure_tailwind_build
|
|
end
|
|
|
|
test "index redirects to the current month budget" do
|
|
get budgets_url
|
|
|
|
assert_redirected_to budget_path(Budget.date_to_param(Date.current))
|
|
end
|
|
|
|
test "show renders the budget page" do
|
|
get budget_url(Budget.date_to_param(Date.current))
|
|
|
|
assert_response :success
|
|
end
|
|
|
|
test "breadcrumbs include the Plan hub for preview users" do
|
|
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
|
|
|
|
get budget_url(Budget.date_to_param(Date.current))
|
|
|
|
assert_response :success
|
|
assert_select "a[href=?]", plan_path, minimum: 1
|
|
end
|
|
|
|
test "renders no Plan links without preview features" do
|
|
get budget_url(Budget.date_to_param(Date.current))
|
|
|
|
assert_response :success
|
|
assert_select "a[href=?]", plan_path, count: 0
|
|
assert_select "a[href=?]", budgets_path, minimum: 1
|
|
end
|
|
end
|
|
|
|
class BudgetsControllerSharingTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
@family = families(:empty)
|
|
@family.update!(personal_budgets: true)
|
|
@owner = users(:josh)
|
|
@viewer = users(:ann)
|
|
@date = Date.current.beginning_of_month
|
|
end
|
|
|
|
test "household budget is viewable and editable by any family member" do
|
|
Budget.find_or_bootstrap(@family, start_date: @date, user: @owner, household: true)
|
|
sign_in @viewer
|
|
|
|
get budget_url(Budget.date_to_param(@date), params: { owner: "household" })
|
|
assert_response :success
|
|
|
|
patch budget_url(Budget.date_to_param(@date), params: { owner: "household" }),
|
|
params: { budget: { budgeted_spending: 1000, expected_income: 2000 } }
|
|
assert_response :redirect
|
|
end
|
|
|
|
test "household tab is unreachable once household_budget_enabled is off, falling back to the viewer's own budget" do
|
|
@family.update!(household_budget_enabled: false)
|
|
sign_in @viewer
|
|
|
|
get budget_url(Budget.date_to_param(@date), params: { owner: "household" })
|
|
|
|
assert_response :success
|
|
assert_equal @viewer.id, Budget.find_by(family: @family, start_date: @date).user_id
|
|
end
|
|
|
|
test "a member without a BudgetShare cannot view another member's personal budget" do
|
|
Budget.find_or_bootstrap(@family, start_date: @date, user: @owner)
|
|
sign_in @viewer
|
|
|
|
get budget_url(Budget.date_to_param(@date), params: { owner: @owner.id })
|
|
|
|
# Falls back to the viewer's own budget rather than the owner's.
|
|
assert_response :success
|
|
assert Budget.exists?(family: @family, start_date: @date, user_id: @viewer.id)
|
|
end
|
|
|
|
test "a read_only BudgetShare lets the viewer see but not edit the owner's budget" do
|
|
Budget.find_or_bootstrap(@family, start_date: @date, user: @owner)
|
|
BudgetShare.create!(owner: @owner, viewer: @viewer, permission: "read_only")
|
|
sign_in @viewer
|
|
|
|
get budget_url(Budget.date_to_param(@date), params: { owner: @owner.id })
|
|
assert_response :success
|
|
|
|
get edit_budget_url(Budget.date_to_param(@date), params: { owner: @owner.id })
|
|
assert_response :not_found
|
|
|
|
patch budget_url(Budget.date_to_param(@date), params: { owner: @owner.id }),
|
|
params: { budget: { budgeted_spending: 1000, expected_income: 2000 } }
|
|
assert_response :not_found
|
|
end
|
|
|
|
test "a read_write BudgetShare lets the viewer edit the owner's budget" do
|
|
Budget.find_or_bootstrap(@family, start_date: @date, user: @owner)
|
|
BudgetShare.create!(owner: @owner, viewer: @viewer, permission: "read_write")
|
|
sign_in @viewer
|
|
|
|
patch budget_url(Budget.date_to_param(@date), params: { owner: @owner.id }),
|
|
params: { budget: { budgeted_spending: 1000, expected_income: 2000 } }
|
|
|
|
assert_redirected_to budget_budget_categories_url(Budget.date_to_param(@date), owner: @owner.id)
|
|
assert_equal 1000, Budget.find_by(family: @family, user: @owner).budgeted_spending.to_i
|
|
end
|
|
end
|